Skip to content

Commit

Permalink
Update auth
Browse files Browse the repository at this point in the history
  • Loading branch information
damienbod committed Jul 4, 2023
1 parent 14ceaaa commit 541affd
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 6 deletions.
1 change: 1 addition & 0 deletions EmployeePaycheck/EmployeePaycheck.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<PackageReference Include="Microsoft.Identity.Web" Version="2.12.4" />
<PackageReference Include="Microsoft.Identity.Web.UI" Version="2.12.4" />
<PackageReference Include="Microsoft.Extensions.Localization" Version="7.0.8" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="7.0.8" />
<PackageReference Include="NetEscapades.AspNetCore.SecurityHeaders" Version="0.19.0" />
<PackageReference Include="NetEscapades.AspNetCore.SecurityHeaders.TagHelpers" Version="0.19.0" />
</ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion EmployeePaycheck/Pages/Paycheck/PaycheckDetailsS3.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Authentication.Cookies;

namespace EmployeePaycheck.Pages.Paycheck;

[AllowAnonymous]
[Authorize(AuthenticationSchemes = CookieAuthenticationDefaults.AuthenticationScheme)]
public class PaycheckDetailsS3Model : PageModel
{
[BindProperty(SupportsGet = true)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<div id="pinCodeText" style="display: none"></div>

<form method="post" id="verifyEmployeePaycheck" action="" novalidate>
<input type="hidden" required id="statePresented" />
<input type="hidden" required id="statePresented" name="statePresented"/>
</form>

<script src="qrcode.min.js" nonce="@nonce"></script>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Caching.Distributed;
using VerifierInsuranceCompany.Services;
using System.Security.Claims;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;

namespace EmployeePaycheck.Pages.Paycheck;

[AllowAnonymous]
public class PaycheckVerifyEmployeeS2Model : PageModel
{
protected readonly IDistributedCache _distributedCache;

public PaycheckVerifyEmployeeS2Model(IDistributedCache distributedCache)
{
_distributedCache = distributedCache;
}

[BindProperty(SupportsGet = true)]
public string? PaycheckId { get; set; }

Expand All @@ -21,8 +33,39 @@ public IActionResult OnGet()
return Page();
}

public IActionResult OnPost()
public async Task<IActionResult> OnPostAsync()
{
if (StatePresented == null)
{
ModelState.AddModelError("StatePresented", "no vc");
return Page();
}

var credentialData = CacheData.GetFromCache(StatePresented, _distributedCache);

var claims = new List<Claim> {
new Claim("DisplayName", credentialData!.Employee.DisplayName, ClaimValueTypes.String, "damienbodsharepoint"),
new Claim("JobTitle", credentialData!.Employee.JobTitle, ClaimValueTypes.String, "damienbodsharepoint"),
new Claim("PreferredLanguage", credentialData!.Employee.PreferredLanguage, ClaimValueTypes.String, "damienbodsharepoint"),
new Claim("RevocationId", credentialData!.Employee.RevocationId, ClaimValueTypes.String, "damienbodsharepoint"),
new Claim("GivenName", credentialData!.Employee.GivenName, ClaimValueTypes.String, "damienbodsharepoint"),
new Claim("Mail", credentialData!.Employee.Mail, ClaimValueTypes.String, "damienbodsharepoint"),
new Claim("Surname", credentialData!.Employee.Surname, ClaimValueTypes.String, "damienbodsharepoint"),
};

var userIdentity = new ClaimsIdentity(claims, "entraemployee");

var userPrincipal = new ClaimsPrincipal(userIdentity);

await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
userPrincipal,
new AuthenticationProperties
{
ExpiresUtc = DateTime.UtcNow.AddMinutes(20),
IsPersistent = false,
AllowRefresh = false
});

return Redirect($"~/Paycheck/PaycheckDetailsS3/{PaycheckId}");
}
}
9 changes: 9 additions & 0 deletions EmployeePaycheck/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using VerifierInsuranceCompany;

namespace EmployeePaycheck;
Expand All @@ -16,6 +18,12 @@ public static void Main(string[] args)
builder.Services.AddHttpClient();
builder.Services.AddDistributedMemoryCache();

builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie();

// Add services to the container.
builder.Services.AddRazorPages();

Expand All @@ -36,6 +44,7 @@ public static void Main(string[] args)

app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.MapControllers();
Expand Down
4 changes: 1 addition & 3 deletions EmployeePaycheck/wwwroot/verifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,9 @@ signIn.addEventListener('click', () => {
if (respMsg.status == 'presentation_verified') {
//document.getElementById('message').innerHTML = respMsg.message;
//document.getElementById('payload').innerHTML = "Payload: " + JSON.stringify(respMsg.payload);
//document.getElementById('subject').innerHTML = respMsg.name + " is a Verified Employee";

document.getElementById('message').innerHTML = '';
document.getElementById('statePresented').value = respPresentationReq.id;

document.getElementById('message').innerHTML = '';
document.getElementById('subject').innerHTML = "Verified Employee";
clearInterval(checkStatus);
}
Expand Down

0 comments on commit 541affd

Please sign in to comment.