90 lines
3.7 KiB
C#
90 lines
3.7 KiB
C#
using MaxInventory.Authentication.Abstractions;
|
|
using MaxInventory.Authentication.Configuration;
|
|
using Microsoft.AspNetCore.Authentication;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace MaxInventory.Authentication.Providers.Oidc;
|
|
|
|
public sealed class OidcAuthProvider : IAuthProvider
|
|
{
|
|
public const string Scheme = "oidc";
|
|
|
|
public string Mode => AuthModes.Oidc;
|
|
|
|
public AuthenticationBuilder Configure(
|
|
AuthenticationBuilder builder,
|
|
IConfiguration configuration)
|
|
{
|
|
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 = false;
|
|
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
|
|
};
|
|
|
|
options.Events = new OpenIdConnectEvents()
|
|
{
|
|
OnRedirectToIdentityProvider = context =>
|
|
{
|
|
var builder = new UriBuilder(context.ProtocolMessage.RedirectUri);
|
|
builder.Scheme = "https";
|
|
builder.Port = 44374;// 1;
|
|
context.ProtocolMessage.RedirectUri = builder.ToString();
|
|
return Task.FromResult(0);
|
|
}
|
|
};
|
|
});
|
|
}
|
|
}
|