< Summary

Information
Class: Intentum.AspNetCore.Auth.IntentumAuthOptions
Assembly: Intentum.AspNetCore
File(s): /home/runner/work/Intentum/Intentum/src/Intentum.AspNetCore/Auth/IntentumAuthExtensions.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 4
Coverable lines: 4
Total lines: 98
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

File(s)

/home/runner/work/Intentum/Intentum/src/Intentum.AspNetCore/Auth/IntentumAuthExtensions.cs

#LineLine coverage
 1using Microsoft.AspNetCore.Authentication;
 2using Microsoft.AspNetCore.Authentication.JwtBearer;
 3using Microsoft.Extensions.DependencyInjection;
 4using Microsoft.IdentityModel.Tokens;
 5using System.Text;
 6
 7namespace Intentum.AspNetCore.Auth;
 8
 9/// <summary>
 10/// Extension methods for configuring Intentum authentication (JWT + API key).
 11/// </summary>
 12public 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>
 92public sealed class IntentumAuthOptions
 93{
 094    public string? JwtSecret { get; set; }
 095    public string? JwtIssuer { get; set; }
 096    public string? JwtAudience { get; set; }
 097    public List<ApiKeyEntry> ApiKeys { get; set; } = [];
 98}