Skip to content

Commit

Permalink
feat: add anonymous access level to local test (#8413)
Browse files Browse the repository at this point in the history
Co-authored-by: Ronny Birkeli <[email protected]>
  • Loading branch information
RonnyB71 and Ronny Birkeli authored Apr 27, 2022
1 parent 31e1710 commit 00f3468
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
41 changes: 25 additions & 16 deletions src/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,25 @@ public IActionResult Error()
[HttpPost]
public async Task<ActionResult> LogInTestUser(StartAppModel startAppModel)
{
UserProfile profile = await _userProfileService.GetUser(startAppModel.UserId);

List<Claim> claims = new List<Claim>();
string issuer = _generalSettings.Hostname;
claims.Add(new Claim(ClaimTypes.NameIdentifier, profile.UserId.ToString(), ClaimValueTypes.String, issuer));
claims.Add(new Claim(AltinnCoreClaimTypes.UserId, profile.UserId.ToString(), ClaimValueTypes.String, issuer));
claims.Add(new Claim(AltinnCoreClaimTypes.UserName, profile.UserName, ClaimValueTypes.String, issuer));
claims.Add(new Claim(AltinnCoreClaimTypes.PartyID, profile.PartyId.ToString(), ClaimValueTypes.Integer32, issuer));
claims.Add(new Claim(AltinnCoreClaimTypes.AuthenticationLevel, startAppModel.AuthenticationLevel, ClaimValueTypes.Integer32, issuer));

ClaimsIdentity identity = new ClaimsIdentity(_generalSettings.GetClaimsIdentity);
identity.AddClaims(claims);
ClaimsPrincipal principal = new ClaimsPrincipal(identity);

string token = _authenticationService.GenerateToken(principal, int.Parse(_generalSettings.GetJwtCookieValidityTime));
CreateJwtCookieAndAppendToResponse(token);
if (startAppModel.AuthenticationLevel != "-1")
{
UserProfile profile = await _userProfileService.GetUser(startAppModel.UserId);

List<Claim> claims = new List<Claim>();
string issuer = _generalSettings.Hostname;
claims.Add(new Claim(ClaimTypes.NameIdentifier, profile.UserId.ToString(), ClaimValueTypes.String, issuer));
claims.Add(new Claim(AltinnCoreClaimTypes.UserId, profile.UserId.ToString(), ClaimValueTypes.String, issuer));
claims.Add(new Claim(AltinnCoreClaimTypes.UserName, profile.UserName, ClaimValueTypes.String, issuer));
claims.Add(new Claim(AltinnCoreClaimTypes.PartyID, profile.PartyId.ToString(), ClaimValueTypes.Integer32, issuer));
claims.Add(new Claim(AltinnCoreClaimTypes.AuthenticationLevel, startAppModel.AuthenticationLevel, ClaimValueTypes.Integer32, issuer));

ClaimsIdentity identity = new ClaimsIdentity(_generalSettings.GetClaimsIdentity);
identity.AddClaims(claims);
ClaimsPrincipal principal = new ClaimsPrincipal(identity);

string token = _authenticationService.GenerateToken(principal, int.Parse(_generalSettings.GetJwtCookieValidityTime));
CreateJwtCookieAndAppendToResponse(token);
}

Application app = await _localApp.GetApplicationMetadata(startAppModel.AppPathSelection);

Expand Down Expand Up @@ -247,6 +250,12 @@ private List<SelectListItem> GetAuthenticationLevels(int defaultAuthLevel)
{
return new()
{
new()
{
Value = "-1",
Text = "Ikke autentisert",
Selected = defaultAuthLevel == -1
},
new()
{
Value = "0",
Expand Down
2 changes: 1 addition & 1 deletion src/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
<label for="exampleInputEmail1">Select your authentication level</label>
@Html.DropDownListFor(model => model.AuthenticationLevel, Model.AuthenticationLevels, new { Class = "form-control" })
</div>
<button type="submit" class="btn btn-primary">Sign in</button>
<button type="submit" class="btn btn-primary">Proceed to app</button>
}

<div class="alert alert-light" role="alert">
Expand Down

0 comments on commit 00f3468

Please sign in to comment.