Skip to content

Commit

Permalink
added ClaimTypes.NameIdentifier to claims potentially containing user id
Browse files Browse the repository at this point in the history
  • Loading branch information
maximiliancsuk committed Mar 12, 2024
1 parent 0a3217f commit c4b7f1d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
7 changes: 4 additions & 3 deletions backend/Omnikeeper.Base/Authz/HttpUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ public static HttpUser CreateAnonymousHttpUser(IEnumerable<Claim> claims)
return new HttpUser("anonymous", "anonymous", anonymousGuid, UserType.Unknown, new HashSet<string>(), claims);
}

public static HttpUser CreateHttpUserFromClaims(IEnumerable<Claim> claims, string audience, ILogger logger)
public static HttpUser CreateHttpUserFromClaimsPrincipal(ClaimsPrincipal claimsPrincipal, string audience, ILogger logger)
{
var claims = claimsPrincipal.Claims;
var username = GetUsernameFromClaims(claims);

if (username == null)
Expand All @@ -94,10 +95,10 @@ public static HttpUser CreateHttpUserFromClaims(IEnumerable<Claim> claims, strin
}
else
{
var guidString = claims.FirstOrDefault(c => c.Type == "id")?.Value;
var guidString = claimsPrincipal.FindFirstValue("id") ?? claimsPrincipal.FindFirstValue(ClaimTypes.NameIdentifier);
if (guidString == null)
{
throw new Exception("Cannot parse user id inside user token: key \"id\" not present");
throw new Exception($"Cannot parse user id inside user token: neither key \"id\" nor key {ClaimTypes.NameIdentifier} present");
}
var guid = new Guid(guidString);

Expand Down
4 changes: 2 additions & 2 deletions backend/Omnikeeper/Service/CurrentUserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ public async Task<IAuthenticatedUser> GetCurrentUser(IModelContext trans)

public string GetCurrentUsername()
{
var httpUser = HttpUserUtils.CreateHttpUserFromClaims(HttpContextAccessor.HttpContext!.User.Claims, configuration.GetSection("Authentication")["Audience"], logger);
var httpUser = HttpUserUtils.CreateHttpUserFromClaimsPrincipal(HttpContextAccessor.HttpContext!.User, configuration.GetSection("Authentication")["Audience"], logger);
return httpUser.Username;
}

private async Task<IAuthenticatedUser> _GetCurrentUser(IModelContext trans)
{
var httpUser = HttpUserUtils.CreateHttpUserFromClaims(HttpContextAccessor.HttpContext!.User.Claims, configuration.GetSection("Authentication")["Audience"], logger);
var httpUser = HttpUserUtils.CreateHttpUserFromClaimsPrincipal(HttpContextAccessor.HttpContext!.User, configuration.GetSection("Authentication")["Audience"], logger);
return await HttpUserUtils.CreateAuthenticatedUserFromHTTPUser(httpUser, userModel, LayerModel, MetaConfigurationModel, AuthRoleModel, trans);
}
}
Expand Down

0 comments on commit c4b7f1d

Please sign in to comment.