| | | 1 | | using Microsoft.AspNetCore.Authentication; |
| | | 2 | | using Microsoft.AspNetCore.Authentication.JwtBearer; |
| | | 3 | | using Microsoft.Extensions.DependencyInjection; |
| | | 4 | | using Microsoft.IdentityModel.Tokens; |
| | | 5 | | using System.Text; |
| | | 6 | | |
| | | 7 | | namespace Intentum.AspNetCore.Auth; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Extension methods for configuring Intentum authentication (JWT + API key). |
| | | 11 | | /// </summary> |
| | | 12 | | public static class IntentumAuthExtensions |
| | | 13 | | { |
| | | 14 | | private const string RoleAdmin = "Admin"; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Adds Intentum authentication with API key support. |
| | | 18 | | /// </summary> |
| | | 19 | | public static AuthenticationBuilder AddIntentumApiKeyAuth( |
| | | 20 | | this IServiceCollection services, |
| | | 21 | | Action<ApiKeyAuthenticationOptions> configure) |
| | | 22 | | { |
| | | 23 | | return services.AddAuthentication(ApiKeyAuthenticationHandler.SchemeName) |
| | | 24 | | .AddScheme<ApiKeyAuthenticationOptions, ApiKeyAuthenticationHandler>( |
| | | 25 | | ApiKeyAuthenticationHandler.SchemeName, configure); |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Adds Intentum authentication with both JWT and API key support. |
| | | 30 | | /// </summary> |
| | | 31 | | public static AuthenticationBuilder AddIntentumAuth( |
| | | 32 | | this IServiceCollection services, |
| | | 33 | | Action<IntentumAuthOptions> configure) |
| | | 34 | | { |
| | | 35 | | var options = new IntentumAuthOptions(); |
| | | 36 | | configure(options); |
| | | 37 | | |
| | | 38 | | var builder = services.AddAuthentication(opts => |
| | | 39 | | { |
| | | 40 | | opts.DefaultScheme = "IntentumComposite"; |
| | | 41 | | opts.DefaultChallengeScheme = "IntentumComposite"; |
| | | 42 | | }); |
| | | 43 | | |
| | | 44 | | if (!string.IsNullOrEmpty(options.JwtSecret)) |
| | | 45 | | { |
| | | 46 | | builder.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, jwt => |
| | | 47 | | { |
| | | 48 | | jwt.TokenValidationParameters = new TokenValidationParameters |
| | | 49 | | { |
| | | 50 | | ValidateIssuer = !string.IsNullOrEmpty(options.JwtIssuer), |
| | | 51 | | ValidIssuer = options.JwtIssuer, |
| | | 52 | | ValidateAudience = !string.IsNullOrEmpty(options.JwtAudience), |
| | | 53 | | ValidAudience = options.JwtAudience, |
| | | 54 | | ValidateIssuerSigningKey = true, |
| | | 55 | | IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(options.JwtSecret)), |
| | | 56 | | ValidateLifetime = true |
| | | 57 | | }; |
| | | 58 | | }); |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | builder.AddScheme<ApiKeyAuthenticationOptions, ApiKeyAuthenticationHandler>( |
| | | 62 | | ApiKeyAuthenticationHandler.SchemeName, apiKeyOpts => |
| | | 63 | | { |
| | | 64 | | apiKeyOpts.ValidKeys = options.ApiKeys; |
| | | 65 | | }); |
| | | 66 | | |
| | | 67 | | builder.AddPolicyScheme("IntentumComposite", "JWT or API Key", policyOpts => |
| | | 68 | | { |
| | | 69 | | policyOpts.ForwardDefaultSelector = context => |
| | | 70 | | { |
| | | 71 | | if (context.Request.Headers.ContainsKey("Authorization")) |
| | | 72 | | return JwtBearerDefaults.AuthenticationScheme; |
| | | 73 | | if (context.Request.Headers.ContainsKey("X-Api-Key") || |
| | | 74 | | context.Request.Query.ContainsKey("api_key")) |
| | | 75 | | return ApiKeyAuthenticationHandler.SchemeName; |
| | | 76 | | return JwtBearerDefaults.AuthenticationScheme; |
| | | 77 | | }; |
| | | 78 | | }); |
| | | 79 | | |
| | | 80 | | services.AddAuthorizationBuilder() |
| | | 81 | | .AddPolicy(RoleAdmin, policy => policy.RequireRole(RoleAdmin)) |
| | | 82 | | .AddPolicy("Analyst", policy => policy.RequireRole(RoleAdmin, "Analyst")) |
| | | 83 | | .AddPolicy("ReadOnly", policy => policy.RequireRole(RoleAdmin, "Analyst", "ReadOnly")); |
| | | 84 | | |
| | | 85 | | return builder; |
| | | 86 | | } |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// Combined authentication options for JWT + API key. |
| | | 91 | | /// </summary> |
| | | 92 | | public sealed class IntentumAuthOptions |
| | | 93 | | { |
| | 0 | 94 | | public string? JwtSecret { get; set; } |
| | 0 | 95 | | public string? JwtIssuer { get; set; } |
| | 0 | 96 | | public string? JwtAudience { get; set; } |
| | 0 | 97 | | public List<ApiKeyEntry> ApiKeys { get; set; } = []; |
| | | 98 | | } |