Compare commits

..

No commits in common. "1c600e8d4d7078a890acba9289cb075df3903a8d" and "76999b805dbcdc407f9eedb585990f7908679c7a" have entirely different histories.

7 changed files with 40 additions and 145 deletions

3
.gitignore vendored
View File

@ -4,4 +4,5 @@ obj
/.vs/FIT5032-Assignment /.vs/FIT5032-Assignment
/.vs /.vs
packages/ packages/
FIT5032-Assignment/App_Data/ FIT5032-Assignment/App_Data/FIT5032_Assignment.mdf
FIT5032-Assignment/App_Data/FIT5032_Assignment_log.ldf

View File

@ -20,9 +20,6 @@ using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
using Newtonsoft.Json; using Newtonsoft.Json;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Dynamic;
using RestSharp;
using RestSharp.Authenticators;
namespace FIT5032_Assignment.Controllers { namespace FIT5032_Assignment.Controllers {
@ -35,15 +32,6 @@ namespace FIT5032_Assignment.Controllers {
public string Email { get; set; } public string Email { get; set; }
} }
public class PassageUserFindReply {
public int Total_Users { get; set; }
public List<PassageUserFindReplyUser> Users { get; set; }
}
public class PassageUserFindReplyUser {
public string Id { get; set; }
}
// Database // Database
public class Database1Entities : DbContext { public class Database1Entities : DbContext {
public DbSet<Users> Users { get; set; } public DbSet<Users> Users { get; set; }
@ -77,7 +65,7 @@ namespace FIT5032_Assignment.Controllers {
return new RsaSecurityKey(rsa); return new RsaSecurityKey(rsa);
} }
private String psgCredentialVerify(string token) { private String loginVerify(string token) {
var jwtHandler = new JwtSecurityTokenHandler(); var jwtHandler = new JwtSecurityTokenHandler();
var jwtToken = jwtHandler.ReadJwtToken(token); var jwtToken = jwtHandler.ReadJwtToken(token);
@ -119,21 +107,7 @@ namespace FIT5032_Assignment.Controllers {
return sub; return sub;
} }
private Users loginInfo(string user) {
var db = new Database1Entities();
var credential = db.Credentials.Where(res => (res.uniqueIdCode == user) && (res.provider == 0));
if (credential.Count() == 0) {
return null;
} else {
var userUuid = credential.First().user;
var dbUser = db.Users.Where(res => res.uuid == userUuid);
if (dbUser.Count() == 0) {
return null;
} else {
return dbUser.First();
}
}
}
private Database1Entities db = new Database1Entities(); private Database1Entities db = new Database1Entities();
private string GenerateRandomString(int length) { private string GenerateRandomString(int length) {
@ -154,7 +128,7 @@ namespace FIT5032_Assignment.Controllers {
public ActionResult Index() { public ActionResult Index() {
// If user logged in, show user name // If user logged in, show user name
if (Request.Cookies["psg_auth_token"] != null) { if (Request.Cookies["psg_auth_token"] != null) {
var user = psgCredentialVerify(Request.Cookies["psg_auth_token"].Value); var user = loginVerify(Request.Cookies["psg_auth_token"].Value);
if (user != null) { if (user != null) {
var db = new Database1Entities(); var db = new Database1Entities();
var credential = db.Credentials.Where(res => (res.uniqueIdCode == user) && (res.provider == 0)); var credential = db.Credentials.Where(res => (res.uniqueIdCode == user) && (res.provider == 0));
@ -181,7 +155,7 @@ namespace FIT5032_Assignment.Controllers {
var psg_auth_token = Request.Cookies["psg_auth_token"]; var psg_auth_token = Request.Cookies["psg_auth_token"];
Trace.WriteLine(psg_auth_token.Value); Trace.WriteLine(psg_auth_token.Value);
// JWT Verify // JWT Verify
string sub = psgCredentialVerify(psg_auth_token.Value); string sub = loginVerify(psg_auth_token.Value);
if (sub == null) { if (sub == null) {
return RedirectToAction("Login"); return RedirectToAction("Login");
} else { } else {
@ -207,7 +181,7 @@ namespace FIT5032_Assignment.Controllers {
// Verify user is logged in // Verify user is logged in
var psg_auth_token = Request.Cookies["psg_auth_token"]; var psg_auth_token = Request.Cookies["psg_auth_token"];
var user = psgCredentialVerify(psg_auth_token.Value); var user = loginVerify(psg_auth_token.Value);
if (user == null) { if (user == null) {
return RedirectToAction("Login"); return RedirectToAction("Login");
} }
@ -280,7 +254,7 @@ namespace FIT5032_Assignment.Controllers {
// Redirect to home page // Redirect to home page
return RedirectToAction("Index"); return RedirectToAction("Index");
} }
var user = psgCredentialVerify(Request.Cookies["psg_auth_token"].Value); var user = loginVerify(Request.Cookies["psg_auth_token"].Value);
if (user == null) { if (user == null) {
// Redirect to home page // Redirect to home page
return RedirectToAction("Index"); return RedirectToAction("Index");
@ -303,107 +277,32 @@ namespace FIT5032_Assignment.Controllers {
} }
[HttpPost] [HttpPost]
public async Task<ActionResult> ImageUpload(Models.ImageUploadForm model) { public ActionResult ImageUpload(Models.ImageUploadForm model) {
try { try {
if (Request.Cookies["psg_auth_token"] == null) { if (Request.Cookies["psg_auth_token"] == null) {
// Return 401 error // Redirect to home page
return new HttpStatusCodeResult(HttpStatusCode.Unauthorized); return new HttpStatusCodeResult(HttpStatusCode.Forbidden);
} }
var userCre = psgCredentialVerify(Request.Cookies["psg_auth_token"].Value); var user = loginVerify(Request.Cookies["psg_auth_token"].Value);
var user = loginInfo(userCre); if (user == null) {
if (user.role != 2) { // Redirect to home page
// Return 403 error if user is not doctor return new HttpStatusCodeResult(HttpStatusCode.Forbidden);
return new HttpStatusCodeResult(HttpStatusCode.Forbidden) ; } else {
// Detect if user is doctor or patient
var db = new Database1Entities();
var credential = db.Credentials.Where(res => (res.uniqueIdCode == user) && (res.provider == 0));
if (credential.Count() == 0) {
// return error 403
return new HttpStatusCodeResult(HttpStatusCode.Forbidden);
}
var dbUser = db.Users.Where(res => res.uuid == credential.First().user);
// print dbUser
Trace.WriteLine(dbUser.First());
return View();
} }
// check recived items
if (!ModelState.IsValid) {
ModelState.AddModelError("patientEmail", "Form not valid");
return View(model);
}
// check uploaded file
if (model.imageFile == null) {
ModelState.AddModelError("imageFile", "Please upload a file");
return View(model);
}
// check format: png, jpg
if (model.imageFile.ContentType != "image/png" && model.imageFile.ContentType != "image/jpeg") {
ModelState.AddModelError("imageFile", "Please upload a png or jpg file");
return View(model);
}
// Check if the email have a patient profile
var db = new Database1Entities();
// Find the account associated with the email
var app_id = "ZHM5whW5xsZEczTn2loffzjN";
var url = $"https://api.passage.id/v1/apps/{app_id}/users?identifier={model.patientEmail}";
var res = httpClient.GetStringAsync(url).Result;
if (JsonConvert.DeserializeObject<PassageUserFindReply>(res).Total_Users == 0) {
ModelState.AddModelError("patientEmail", "No patient found");
return View(model);
}
var patientId = JsonConvert.DeserializeObject<PassageUserFindReply>(res).Users[0].Id;
var patientCredential = db.Credentials.Where(c => (c.uniqueIdCode == patientId) && (c.provider == 0));
if (patientCredential.Count() == 0) {
ModelState.AddModelError("patientEmail", "No patient found");
return View(model);
}
var patientUuid = patientCredential.First().user;
var patient = db.Users.Where(u => u.uuid == patientUuid);
if (patient.Count() == 0 || patient.First().role != 1) {
ModelState.AddModelError("patientEmail", "No patient found");
return View(model);
}
// Store file to server
var fileId = Guid.NewGuid().ToString();
var fileName = fileId + Path.GetExtension(model.imageFile.FileName);
var filePath = Path.Combine(Server.MapPath("~/App_Data/upload_images"), fileName);
model.imageFile.SaveAs(filePath);
// Create image entity to database
var appointmentId = Guid.NewGuid().ToString();
var imageId = Guid.NewGuid().ToString();
var appointment = new Appointments {
uuid = appointmentId,
patient = patientUuid,
responsibleBy = user.uuid,
createdAt = DateTime.Now,
appointmentDate = DateTime.Now,
status = 0,
createdBy = 1
};
var image = new Images {
uuid = imageId,
appointment = appointmentId,
patient = patientUuid,
responsibleBy = user.uuid,
createdAt = DateTime.Now,
file = fileName,
status = 0,
};
// Send attached email with mailgun
var doctorName = user.displayName;
RestClient client = new RestClient(new RestClientOptions ("https://api.mailgun.net/v3/test.astrian.moe") {
Authenticator = new HttpBasicAuthenticator("api", "365900a7818241eafcbbf82e59cf99e8-5465e583-b4966e64"),
});
var request = new RestRequest("messages", Method.Post);
request.AddParameter("from", "Xpectrum <xpectrum@test.astrian.moe>");
request.AddParameter("to", model.patientEmail);
request.AddParameter("subject", "Xpectrum: New image available");
request.AddParameter("text", $"Hi {patient.First().displayName},\n\nDr. {doctorName} has uploaded a new image for you.\n\nPlease check the attachment.\n\nBest regards,\nXpectrum");
request.AddFile("attachment", filePath);
// Send request
var response = await client.ExecuteAsync(request);
Trace.WriteLine(response.Content);
return View();
} catch (Exception e) { } catch (Exception e) {
Trace.WriteLine(e); Trace.WriteLine(e);
return new HttpStatusCodeResult(HttpStatusCode.BadGateway); return RedirectToAction("Index");
} }
} }
} }

View File

@ -61,8 +61,8 @@
<Reference Include="JWT, Version=10.0.0.0, Culture=neutral, PublicKeyToken=6f98bca0f40f2ecf, processorArchitecture=MSIL"> <Reference Include="JWT, Version=10.0.0.0, Culture=neutral, PublicKeyToken=6f98bca0f40f2ecf, processorArchitecture=MSIL">
<HintPath>..\packages\JWT.10.1.0\lib\net462\JWT.dll</HintPath> <HintPath>..\packages\JWT.10.1.0\lib\net462\JWT.dll</HintPath>
</Reference> </Reference>
<Reference Include="Microsoft.Bcl.AsyncInterfaces, Version=7.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL"> <Reference Include="Microsoft.Bcl.AsyncInterfaces, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Bcl.AsyncInterfaces.7.0.0\lib\net462\Microsoft.Bcl.AsyncInterfaces.dll</HintPath> <HintPath>..\packages\Microsoft.Bcl.AsyncInterfaces.6.0.0\lib\net461\Microsoft.Bcl.AsyncInterfaces.dll</HintPath>
</Reference> </Reference>
<Reference Include="Microsoft.CSharp" /> <Reference Include="Microsoft.CSharp" />
<Reference Include="Microsoft.IdentityModel.Abstractions, Version=7.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> <Reference Include="Microsoft.IdentityModel.Abstractions, Version=7.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
@ -80,9 +80,6 @@
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL"> <Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.13.0.2\lib\net45\Newtonsoft.Json.dll</HintPath> <HintPath>..\packages\Newtonsoft.Json.13.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference> </Reference>
<Reference Include="RestSharp, Version=110.2.0.0, Culture=neutral, PublicKeyToken=598062e77f915f75, processorArchitecture=MSIL">
<HintPath>..\packages\RestSharp.110.2.0\lib\net471\RestSharp.dll</HintPath>
</Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL"> <Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll</HintPath> <HintPath>..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll</HintPath>
@ -104,11 +101,11 @@
</Reference> </Reference>
<Reference Include="System.Runtime.Serialization" /> <Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Security" /> <Reference Include="System.Security" />
<Reference Include="System.Text.Encodings.Web, Version=7.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL"> <Reference Include="System.Text.Encodings.Web, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Text.Encodings.Web.7.0.0\lib\net462\System.Text.Encodings.Web.dll</HintPath> <HintPath>..\packages\System.Text.Encodings.Web.6.0.0\lib\net461\System.Text.Encodings.Web.dll</HintPath>
</Reference> </Reference>
<Reference Include="System.Text.Json, Version=7.0.0.2, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL"> <Reference Include="System.Text.Json, Version=6.0.0.7, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Text.Json.7.0.2\lib\net462\System.Text.Json.dll</HintPath> <HintPath>..\packages\System.Text.Json.6.0.7\lib\net461\System.Text.Json.dll</HintPath>
</Reference> </Reference>
<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL"> <Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.4\lib\net461\System.Threading.Tasks.Extensions.dll</HintPath> <HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.4\lib\net461\System.Threading.Tasks.Extensions.dll</HintPath>

View File

@ -5,7 +5,6 @@ namespace FIT5032_Assignment.Models {
public class ImageUploadForm { public class ImageUploadForm {
[Required] [Required]
[Display(Name = "Assign to patient (email)")] [Display(Name = "Assign to patient (email)")]
[EmailAddress]
public string patientEmail { get; set; } public string patientEmail { get; set; }
[Required] [Required]

View File

@ -23,7 +23,7 @@
<div class="form-group"> <div class="form-group">
@Html.LabelFor(model => model.imageFile, htmlAttributes: new { @class = "control-label col-md-2" }) @Html.LabelFor(model => model.imageFile, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10"> <div class="col-md-10">
<input type="file" name="imageFile" id="imageFile" accept="image/png, image/jpeg" /> <input type="file" name="imageFile" id="imageFile" />
@Html.ValidationMessageFor(model => model.imageFile, "", new { @class = "text-danger" }) @Html.ValidationMessageFor(model => model.imageFile, "", new { @class = "text-danger" })
</div> </div>
</div> </div>

View File

@ -62,11 +62,11 @@
</dependentAssembly> </dependentAssembly>
<dependentAssembly> <dependentAssembly>
<assemblyIdentity name="System.Text.Json" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" /> <assemblyIdentity name="System.Text.Json" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-7.0.0.2" newVersion="7.0.0.2" /> <bindingRedirect oldVersion="0.0.0.0-6.0.0.7" newVersion="6.0.0.7" />
</dependentAssembly> </dependentAssembly>
<dependentAssembly> <dependentAssembly>
<assemblyIdentity name="System.Text.Encodings.Web" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" /> <assemblyIdentity name="System.Text.Encodings.Web" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0" /> <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly> </dependentAssembly>
</assemblyBinding> </assemblyBinding>
</runtime> </runtime>

View File

@ -12,7 +12,7 @@
<package id="Microsoft.AspNet.Razor" version="3.2.9" targetFramework="net48" /> <package id="Microsoft.AspNet.Razor" version="3.2.9" targetFramework="net48" />
<package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net48" /> <package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net48" />
<package id="Microsoft.AspNet.WebPages" version="3.2.9" targetFramework="net48" /> <package id="Microsoft.AspNet.WebPages" version="3.2.9" targetFramework="net48" />
<package id="Microsoft.Bcl.AsyncInterfaces" version="7.0.0" targetFramework="net48" /> <package id="Microsoft.Bcl.AsyncInterfaces" version="6.0.0" targetFramework="net48" />
<package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="2.0.1" targetFramework="net48" /> <package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="2.0.1" targetFramework="net48" />
<package id="Microsoft.IdentityModel.Abstractions" version="7.0.0" targetFramework="net48" /> <package id="Microsoft.IdentityModel.Abstractions" version="7.0.0" targetFramework="net48" />
<package id="Microsoft.IdentityModel.JsonWebTokens" version="7.0.0" targetFramework="net48" /> <package id="Microsoft.IdentityModel.JsonWebTokens" version="7.0.0" targetFramework="net48" />
@ -22,14 +22,13 @@
<package id="Microsoft.Web.Infrastructure" version="2.0.1" targetFramework="net48" /> <package id="Microsoft.Web.Infrastructure" version="2.0.1" targetFramework="net48" />
<package id="Modernizr" version="2.8.3" targetFramework="net48" /> <package id="Modernizr" version="2.8.3" targetFramework="net48" />
<package id="Newtonsoft.Json" version="13.0.2" targetFramework="net48" /> <package id="Newtonsoft.Json" version="13.0.2" targetFramework="net48" />
<package id="RestSharp" version="110.2.0" targetFramework="net48" />
<package id="System.Buffers" version="4.5.1" targetFramework="net48" /> <package id="System.Buffers" version="4.5.1" targetFramework="net48" />
<package id="System.IdentityModel.Tokens.Jwt" version="7.0.0" targetFramework="net48" /> <package id="System.IdentityModel.Tokens.Jwt" version="7.0.0" targetFramework="net48" />
<package id="System.Memory" version="4.5.5" targetFramework="net48" /> <package id="System.Memory" version="4.5.5" targetFramework="net48" />
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net48" /> <package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net48" />
<package id="System.Runtime.CompilerServices.Unsafe" version="6.0.0" targetFramework="net48" /> <package id="System.Runtime.CompilerServices.Unsafe" version="6.0.0" targetFramework="net48" />
<package id="System.Text.Encodings.Web" version="7.0.0" targetFramework="net48" /> <package id="System.Text.Encodings.Web" version="6.0.0" targetFramework="net48" />
<package id="System.Text.Json" version="7.0.2" targetFramework="net48" /> <package id="System.Text.Json" version="6.0.7" targetFramework="net48" />
<package id="System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net48" /> <package id="System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net48" />
<package id="System.ValueTuple" version="4.5.0" targetFramework="net48" /> <package id="System.ValueTuple" version="4.5.0" targetFramework="net48" />
<package id="WebGrease" version="1.6.0" targetFramework="net48" /> <package id="WebGrease" version="1.6.0" targetFramework="net48" />