OIDC
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
using maintenance.Authentication.Abstractions;
|
||||
using maintenance.Authentication.Configuration;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
|
||||
namespace maintenance.Authentication.Providers.Oidc;
|
||||
|
||||
@@ -14,8 +16,61 @@ public sealed class OidcAuthProvider : IAuthProvider
|
||||
AuthenticationBuilder builder,
|
||||
IConfiguration configuration)
|
||||
{
|
||||
throw new NotSupportedException(
|
||||
"OIDC provider is not implemented yet. Set AUTH_MODE=WSFED for current deployment.");
|
||||
var authority = configuration[AuthEnvironment.OidcAuthority];
|
||||
var clientId = configuration[AuthEnvironment.OidcClientId];
|
||||
var clientSecret = configuration[AuthEnvironment.OidcClientSecret];
|
||||
var callbackPath = configuration[AuthEnvironment.OidcCallbackPath] ?? "/signin-oidc";
|
||||
var signedOutCallbackPath = configuration[AuthEnvironment.OidcSignedOutCallbackPath] ?? "/signout-callback-oidc";
|
||||
var scopes = (configuration[AuthEnvironment.OidcScopes] ?? "openid;profile;email;role")
|
||||
.Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
||||
var roleClaimType = configuration[AuthEnvironment.OidcRoleClaimType]
|
||||
?? "http://schemas.microsoft.com/ws/2008/06/identity/claims/role";
|
||||
var nameClaimType = configuration[AuthEnvironment.OidcNameClaimType]
|
||||
?? "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name";
|
||||
var configuredValidIssuers = configuration[AuthEnvironment.OidcValidIssuers];
|
||||
|
||||
if (string.IsNullOrWhiteSpace(authority))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Missing required configuration: {AuthEnvironment.OidcAuthority}");
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(clientId))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Missing required configuration: {AuthEnvironment.OidcClientId}");
|
||||
}
|
||||
|
||||
return builder.AddOpenIdConnect(Scheme, options =>
|
||||
{
|
||||
options.Authority = authority;
|
||||
options.ClientId = clientId;
|
||||
options.ClientSecret = clientSecret;
|
||||
options.CallbackPath = callbackPath;
|
||||
options.SignedOutCallbackPath = signedOutCallbackPath;
|
||||
options.ResponseType = "code";
|
||||
options.SaveTokens = true;
|
||||
options.GetClaimsFromUserInfoEndpoint = true;
|
||||
|
||||
options.Scope.Clear();
|
||||
foreach (var scope in scopes)
|
||||
{
|
||||
options.Scope.Add(scope);
|
||||
}
|
||||
|
||||
var validIssuers = configuredValidIssuers?
|
||||
.Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
|
||||
.Where(v => !string.IsNullOrWhiteSpace(v))
|
||||
.ToArray();
|
||||
|
||||
options.TokenValidationParameters = new TokenValidationParameters
|
||||
{
|
||||
ValidateIssuer = validIssuers is { Length: > 0 },
|
||||
ValidIssuers = validIssuers,
|
||||
NameClaimType = nameClaimType,
|
||||
RoleClaimType = roleClaimType
|
||||
};
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user