diff --git a/backend/api/Database/Models/UserInfo.cs b/backend/api/Database/Models/UserInfo.cs index 556c60ec0..10f14f741 100644 --- a/backend/api/Database/Models/UserInfo.cs +++ b/backend/api/Database/Models/UserInfo.cs @@ -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; } } } diff --git a/backend/api/Services/UserInfoServices.cs b/backend/api/Services/UserInfoServices.cs index e0fa02433..64320cc91 100644 --- a/backend/api/Services/UserInfoServices.cs +++ b/backend/api/Services/UserInfoServices.cs @@ -82,22 +82,19 @@ public async Task 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); } diff --git a/backend/api/Utilities/HttpContextExtensions.cs b/backend/api/Utilities/HttpContextExtensions.cs index 4d5f8bc2e..4ecb5933f 100644 --- a/backend/api/Utilities/HttpContextExtensions.cs +++ b/backend/api/Utilities/HttpContextExtensions.cs @@ -34,5 +34,13 @@ public static List 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; + } } }