-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add OIDC authentication to UI / API (#913)
- Loading branch information
Showing
88 changed files
with
3,643 additions
and
740 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
[{ | ||
"ClientId": "bdms-client", | ||
"Description": "Client for Authorization Code flow with PKCE", | ||
"RequireClientSecret": false, | ||
"AlwaysIncludeUserClaimsInIdToken": true, | ||
"AllowedGrantTypes": [ | ||
"authorization_code" | ||
], | ||
"AllowedResponseTypes": [ | ||
"code", | ||
"id_token" | ||
], | ||
"AllowAccessTokensViaBrowser": true, | ||
"RedirectUris": [ | ||
"http://localhost:3000" | ||
], | ||
"AllowedScopes": [ | ||
"openid", | ||
"profile", | ||
"email" | ||
], | ||
"AccessTokenType": "JWT", | ||
"IdentityTokenLifetime": 3600, | ||
"AccessTokenLifetime": 3600 | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
[ | ||
{ | ||
"SubjectId":"sub_admin", | ||
"Username":"admin", | ||
"Password":"swissforages", | ||
"Claims": [ | ||
{ | ||
"Type": "name", | ||
"Value": "Admin User", | ||
"ValueType": "string" | ||
}, | ||
{ | ||
"Type": "family_name", | ||
"Value": "User", | ||
"ValueType": "string" | ||
}, | ||
{ | ||
"Type": "given_name", | ||
"Value": "Admin", | ||
"ValueType": "string" | ||
}, | ||
{ | ||
"Type": "email", | ||
"Value": "admin.user@local.dev", | ||
"ValueType": "string" | ||
}, | ||
{ | ||
"Type": "email_verified", | ||
"Value": "true", | ||
"ValueType": "boolean" | ||
} | ||
] | ||
}, | ||
{ | ||
"SubjectId":"sub_editor", | ||
"Username":"editor", | ||
"Password":"swissforages", | ||
"Claims": [ | ||
{ | ||
"Type": "name", | ||
"Value": "Editor User", | ||
"ValueType": "string" | ||
}, | ||
{ | ||
"Type": "family_name", | ||
"Value": "User", | ||
"ValueType": "string" | ||
}, | ||
{ | ||
"Type": "given_name", | ||
"Value": "Editor", | ||
"ValueType": "string" | ||
}, | ||
{ | ||
"Type": "email", | ||
"Value": "editor.user@local.dev", | ||
"ValueType": "string" | ||
}, | ||
{ | ||
"Type": "email_verified", | ||
"Value": "true", | ||
"ValueType": "boolean" | ||
} | ||
] | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
49 changes: 49 additions & 0 deletions
49
src/api/Authentication/DatabaseAuthenticationClaimsTransformation.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using BDMS.Models; | ||
using Microsoft.AspNetCore.Authentication; | ||
using System.Security.Claims; | ||
|
||
namespace BDMS.Authentication; | ||
|
||
public class DatabaseAuthenticationClaimsTransformation : IClaimsTransformation | ||
{ | ||
private readonly BdmsContext dbContext; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="DatabaseAuthenticationClaimsTransformation"/> class. | ||
/// </summary> | ||
/// <param name="dbContext">The EF database context containing data for the BDMS application.</param> | ||
/// <exception cref="ArgumentNullException">If <paramref name="dbContext"/> is <c>null</c>.</exception> | ||
public DatabaseAuthenticationClaimsTransformation(BdmsContext dbContext) | ||
{ | ||
this.dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext)); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public async Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal) | ||
{ | ||
var userId = principal.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier); | ||
if (userId is null) return principal; | ||
|
||
var authenticatedUser = dbContext.Users.FirstOrDefault(u => u.SubjectId == userId.Value) | ||
?? new User | ||
{ | ||
SubjectId = userId.Value, | ||
Password = "Undefined", // TODO: Remove with #911 | ||
}; | ||
if (authenticatedUser.IsDisabled) return principal; | ||
|
||
authenticatedUser.FirstName = principal.Claims.FirstOrDefault(c => c.Type == ClaimTypes.GivenName)?.Value ?? authenticatedUser.FirstName; | ||
authenticatedUser.LastName = principal.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Surname)?.Value ?? authenticatedUser.LastName; | ||
authenticatedUser.Name = $"{authenticatedUser.FirstName[0]}. {authenticatedUser.LastName}"; | ||
dbContext.Update(authenticatedUser); | ||
await dbContext.SaveChangesAsync().ConfigureAwait(false); | ||
|
||
var claimsIdentity = new ClaimsIdentity(); | ||
claimsIdentity.AddClaim(new Claim(ClaimTypes.Name, authenticatedUser.Name)); | ||
if (authenticatedUser.IsAdmin) claimsIdentity.AddClaim(new Claim(ClaimTypes.Role, PolicyNames.Admin)); | ||
else if (authenticatedUser.IsViewer) claimsIdentity.AddClaim(new Claim(ClaimTypes.Role, PolicyNames.Viewer)); | ||
|
||
principal.AddIdentity(claimsIdentity); | ||
return principal; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.