Skip to content

Commit

Permalink
Handle objectId request in HttpContextExtensions
Browse files Browse the repository at this point in the history
  • Loading branch information
betaniat committed Aug 13, 2024
1 parent b5f535e commit e7d017e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
2 changes: 0 additions & 2 deletions backend/api/Database/Models/UserInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ public class UserInfo
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public string Id { get; set; }
[Required]
public string Username { get; set; }
[Required]
public string Oid { get; set; }
}
}
11 changes: 4 additions & 7 deletions backend/api/Services/UserInfoServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,19 @@ public async Task<UserInfo> Update(UserInfo userInfo)
{
if (httpContextAccessor.HttpContext == null)
throw new HttpRequestException("User Info can only be requested in authenticated HTTP requests.");
var claims = httpContextAccessor.HttpContext.GetRequestedClaims();

var objectIdClaim = claims.FirstOrDefault(c => c.Type == "oid");
if (objectIdClaim is null)
string? objectId = httpContextAccessor.HttpContext.GetUserObjectId();
if (objectId is null)
{
logger.LogWarning("User objectId is null so it will not be added to the database.");
return null;
}
var userInfo = await ReadByOid(objectIdClaim.Value);
var userInfo = await ReadByOid(objectId);
if (userInfo is null)
{
var preferredUsernameClaim = claims.FirstOrDefault(c => c.Type == "preferred_username");
var newUserInfo = new UserInfo
{
Username = preferredUsernameClaim!.Value,
Oid = objectIdClaim.Value
Oid = objectId
};
userInfo = await Create(newUserInfo);
}
Expand Down
8 changes: 8 additions & 0 deletions backend/api/Utilities/HttpContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,13 @@ public static List<string> GetRequestedRoleNames(this HttpContext client)
var jwtSecurityToken = handler.ReadJwtToken(accessTokenBase64);
return jwtSecurityToken.Claims.ToList();
}

public static string? GetUserObjectId(this HttpContext client)
{
var claims = client.GetRequestedClaims();
var objectIdClaim = claims.FirstOrDefault(c => c.Type == "oid");
if (objectIdClaim is null) { return null; }
return objectIdClaim.Value;
}
}
}

0 comments on commit e7d017e

Please sign in to comment.