From bfa91ce1aa965e8d5473f18005e499f28d1705d3 Mon Sep 17 00:00:00 2001 From: Ccjcarkbla Date: Tue, 24 Feb 2026 13:15:45 -0500 Subject: [PATCH] OIDC --- .../Configuration/AuthEnvironment.cs | 10 ++++ .../Core/AdminRoleClaimsTransformation.cs | 4 ++ .../Providers/Oidc/OidcAuthProvider.cs | 59 ++++++++++++++++++- FutureSecrets.example.json | 9 +++ FutureSecrets.json | 9 +++ 5 files changed, 89 insertions(+), 2 deletions(-) diff --git a/Authentication/Configuration/AuthEnvironment.cs b/Authentication/Configuration/AuthEnvironment.cs index f4af1e5..a989e7f 100644 --- a/Authentication/Configuration/AuthEnvironment.cs +++ b/Authentication/Configuration/AuthEnvironment.cs @@ -12,5 +12,15 @@ public static class AuthEnvironment public const string AdfsHomeRealm = "ADFS_WHR"; public const string AdfsAuthType = "ADFS_WAUTH"; public const string AdfsValidIssuers = "ADFS_VALID_ISSUERS"; + + public const string OidcAuthority = "OIDC_AUTHORITY"; + public const string OidcClientId = "OIDC_CLIENT_ID"; + public const string OidcClientSecret = "OIDC_CLIENT_SECRET"; + public const string OidcCallbackPath = "OIDC_CALLBACK_PATH"; + public const string OidcSignedOutCallbackPath = "OIDC_SIGNOUT_CALLBACK_PATH"; + public const string OidcScopes = "OIDC_SCOPES"; + public const string OidcRoleClaimType = "OIDC_ROLE_CLAIM_TYPE"; + public const string OidcNameClaimType = "OIDC_NAME_CLAIM_TYPE"; + public const string OidcValidIssuers = "OIDC_VALID_ISSUERS"; } diff --git a/Authentication/Core/AdminRoleClaimsTransformation.cs b/Authentication/Core/AdminRoleClaimsTransformation.cs index fb80408..f45a422 100644 --- a/Authentication/Core/AdminRoleClaimsTransformation.cs +++ b/Authentication/Core/AdminRoleClaimsTransformation.cs @@ -12,7 +12,11 @@ public sealed class AdminRoleClaimsTransformation : IClaimsTransformation { "groups", "group", + "role", + "roles", ClaimTypes.GroupSid, + ClaimTypes.Role, + "http://schemas.microsoft.com/ws/2008/06/identity/claims/role", "http://schemas.xmlsoap.org/claims/Group" }; diff --git a/Authentication/Providers/Oidc/OidcAuthProvider.cs b/Authentication/Providers/Oidc/OidcAuthProvider.cs index f66e8ba..a3c0586 100644 --- a/Authentication/Providers/Oidc/OidcAuthProvider.cs +++ b/Authentication/Providers/Oidc/OidcAuthProvider.cs @@ -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 + }; + }); } } diff --git a/FutureSecrets.example.json b/FutureSecrets.example.json index fa52df6..d6171ea 100644 --- a/FutureSecrets.example.json +++ b/FutureSecrets.example.json @@ -8,6 +8,15 @@ "ADFS_WHR": "", "ADFS_WAUTH": "", "ADFS_CALLBACK_PATH": "/signin-wsfed", + "OIDC_AUTHORITY": "https://adfs.example.com/adfs", + "OIDC_CLIENT_ID": "CHANGE_ME", + "OIDC_CLIENT_SECRET": "CHANGE_ME", + "OIDC_CALLBACK_PATH": "/signin-oidc", + "OIDC_SIGNOUT_CALLBACK_PATH": "/signout-callback-oidc", + "OIDC_SCOPES": "openid;profile;email;role", + "OIDC_ROLE_CLAIM_TYPE": "http://schemas.microsoft.com/ws/2008/06/identity/claims/role", + "OIDC_NAME_CLAIM_TYPE": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name", + "OIDC_VALID_ISSUERS": "", "ConnectionStrings": { "PrimaryDb": "Server=localhost;Database=maintenance;User Id=svc_errorpages;Password=CHANGE_ME;TrustServerCertificate=True", "GroupLookupDb": "Server=localhost;Database=GroupLookup;User Id=svc_errorpages;Password=CHANGE_ME;TrustServerCertificate=True" diff --git a/FutureSecrets.json b/FutureSecrets.json index 3236072..547a106 100644 --- a/FutureSecrets.json +++ b/FutureSecrets.json @@ -8,6 +8,15 @@ "ADFS_WHR": "https://adfs.t.g:44373", "ADFS_WAUTH": "urn:oasis:names:tc:SAML:2.0:ac:classes:TLSClient", "ADFS_CALLBACK_PATH": "/signin-wsfed", + "OIDC_AUTHORITY": "https://adfs.omb.gov/adfs", + "OIDC_CLIENT_ID": "CHANGE_ME", + "OIDC_CLIENT_SECRET": "CHANGE_ME", + "OIDC_CALLBACK_PATH": "/signin-oidc", + "OIDC_SIGNOUT_CALLBACK_PATH": "/signout-callback-oidc", + "OIDC_SCOPES": "openid;profile;email;role", + "OIDC_ROLE_CLAIM_TYPE": "http://schemas.microsoft.com/ws/2008/06/identity/claims/role", + "OIDC_NAME_CLAIM_TYPE": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name", + "OIDC_VALID_ISSUERS": "", "ConnectionStrings": { "PrimaryDb": "Server=localhost;Database=maintenance;User Id=svc_errorpages;Password=CHANGE_ME;TrustServerCertificate=True", "GroupLookupDb": "Server=localhost;Database=GroupLookup;User Id=svc_errorpages;Password=CHANGE_ME;TrustServerCertificate=True"