|
| 1 | +using Api.Services; |
| 2 | +using Microsoft.AspNetCore.Authentication.JwtBearer; |
| 3 | +using Microsoft.Identity.Abstractions; |
| 4 | +using Microsoft.Identity.Web; |
| 5 | + |
| 6 | +namespace Api.Configurations |
| 7 | +{ |
| 8 | + public static class AuthenticationConfigurations |
| 9 | + { |
| 10 | + /// <summary> |
| 11 | + /// The environment in which the backend validates tokens against a generic |
| 12 | + /// OpenID Connect issuer instead of Microsoft Entra ID. |
| 13 | + /// |
| 14 | + /// This exists solely so the armada integration tests can run against a local |
| 15 | + /// mock issuer, with no Entra app registrations and no client secrets, while |
| 16 | + /// still exercising authentication for real. |
| 17 | + /// |
| 18 | + /// Gating on the environment name rather than on a configuration flag is |
| 19 | + /// deliberate: it keeps the generic-issuer path unreachable from Development, |
| 20 | + /// Staging and Production regardless of which environment variables are set. |
| 21 | + /// </summary> |
| 22 | + public const string IntegrationTestEnvironment = "IntegrationTest"; |
| 23 | + |
| 24 | + public static bool UsesGenericOidc(this IHostEnvironment environment) => |
| 25 | + environment.IsEnvironment(IntegrationTestEnvironment); |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Registers JWT bearer authentication, the MSAL token caches and the |
| 29 | + /// downstream API clients for ISAR, SARA and Pointilla. |
| 30 | + /// </summary> |
| 31 | + public static IServiceCollection ConfigureAuthentication( |
| 32 | + this IServiceCollection services, |
| 33 | + IConfiguration configuration, |
| 34 | + IHostEnvironment environment |
| 35 | + ) |
| 36 | + { |
| 37 | + bool useRedis = configuration.GetSection("Redis").GetValue<bool>("UseRedis"); |
| 38 | + if (useRedis) |
| 39 | + { |
| 40 | + services.ConfigureRedisCache(configuration); |
| 41 | + } |
| 42 | + |
| 43 | + var authenticationBuilder = services |
| 44 | + .AddAuthentication(JwtBearerDefaults.AuthenticationScheme) |
| 45 | + .AddMicrosoftIdentityWebApi(configuration.GetSection("AzureAd")) |
| 46 | + .EnableTokenAcquisitionToCallDownstreamApi(); |
| 47 | + |
| 48 | + if (useRedis) |
| 49 | + { |
| 50 | + authenticationBuilder.AddDistributedTokenCaches(); |
| 51 | + } |
| 52 | + else |
| 53 | + { |
| 54 | + authenticationBuilder.AddInMemoryTokenCaches(); |
| 55 | + } |
| 56 | + |
| 57 | + authenticationBuilder |
| 58 | + .AddDownstreamApi(InspectionService.ServiceName, configuration.GetSection("SARA")) |
| 59 | + .AddDownstreamApi(IsarService.ServiceName, configuration.GetSection("Isar")) |
| 60 | + .AddDownstreamApi( |
| 61 | + PointillaService.ServiceName, |
| 62 | + configuration.GetSection("Pointilla") |
| 63 | + ); |
| 64 | + |
| 65 | + if (environment.UsesGenericOidc()) |
| 66 | + { |
| 67 | + ConfigureGenericOidcOverrides(services, configuration); |
| 68 | + } |
| 69 | + |
| 70 | + ConfigureSignalRQueryStringToken(services); |
| 71 | + |
| 72 | + return services; |
| 73 | + } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Redirects both halves of authentication at the mock issuer. |
| 77 | + /// |
| 78 | + /// Inbound: AddMicrosoftIdentityWebApi installs an AadIssuerValidator, which |
| 79 | + /// expects Entra-shaped issuers and performs instance discovery against |
| 80 | + /// login.microsoftonline.com. The validator is replaced with plain issuer |
| 81 | + /// validation against the mock's discovery document. |
| 82 | + /// |
| 83 | + /// This is deliberately split across the two options phases: |
| 84 | + /// |
| 85 | + /// Configure - the authority and RequireHttpsMetadata, because |
| 86 | + /// JwtBearerPostConfigureOptions throws |
| 87 | + /// "MetadataAddress or Authority must use HTTPS" for a plain |
| 88 | + /// HTTP authority, and it runs before any post-configuration |
| 89 | + /// we could register. |
| 90 | + /// PostConfigure - clearing the issuer validator, because |
| 91 | + /// Microsoft.Identity.Web installs it during |
| 92 | + /// post-configuration and the last registration wins. |
| 93 | + /// |
| 94 | + /// Outbound: IDownstreamApi resolves its bearer tokens through |
| 95 | + /// IAuthorizationHeaderProvider, so replacing that single service redirects |
| 96 | + /// the ISAR, SARA and Pointilla calls without fighting MSAL's authority |
| 97 | + /// validation. |
| 98 | + /// </summary> |
| 99 | + private static void ConfigureGenericOidcOverrides( |
| 100 | + IServiceCollection services, |
| 101 | + IConfiguration configuration |
| 102 | + ) |
| 103 | + { |
| 104 | + string authority = |
| 105 | + configuration["AzureAd:Authority"] |
| 106 | + ?? throw new InvalidOperationException( |
| 107 | + $"AzureAd:Authority is required in the {IntegrationTestEnvironment} environment" |
| 108 | + ); |
| 109 | + string audience = |
| 110 | + configuration["AzureAd:ClientId"] |
| 111 | + ?? throw new InvalidOperationException( |
| 112 | + $"AzureAd:ClientId is required in the {IntegrationTestEnvironment} environment" |
| 113 | + ); |
| 114 | + |
| 115 | + services.Configure<JwtBearerOptions>( |
| 116 | + JwtBearerDefaults.AuthenticationScheme, |
| 117 | + options => |
| 118 | + { |
| 119 | + options.Authority = authority; |
| 120 | + options.Audience = audience; |
| 121 | + // The mock issuer is plain HTTP on the test network. |
| 122 | + options.RequireHttpsMetadata = false; |
| 123 | + } |
| 124 | + ); |
| 125 | + |
| 126 | + services.PostConfigure<JwtBearerOptions>( |
| 127 | + JwtBearerDefaults.AuthenticationScheme, |
| 128 | + options => |
| 129 | + { |
| 130 | + var parameters = options.TokenValidationParameters; |
| 131 | + parameters.ValidateIssuer = true; |
| 132 | + parameters.ValidateAudience = true; |
| 133 | + parameters.ValidateLifetime = true; |
| 134 | + parameters.ValidAudience = audience; |
| 135 | + parameters.ValidAudiences = [audience]; |
| 136 | + // Drop the Entra-specific issuer validator; the issuer is taken from |
| 137 | + // the mock's discovery document instead. |
| 138 | + parameters.IssuerValidator = null; |
| 139 | + parameters.ValidIssuers = null; |
| 140 | + } |
| 141 | + ); |
| 142 | + |
| 143 | + services.AddSingleton< |
| 144 | + IAuthorizationHeaderProvider, |
| 145 | + GenericOidcAuthorizationHeaderProvider |
| 146 | + >(); |
| 147 | + } |
| 148 | + |
| 149 | + /// <summary> |
| 150 | + /// Browsers cannot set headers on WebSocket connections, so SignalR passes the |
| 151 | + /// access token in the query string instead. |
| 152 | + /// </summary> |
| 153 | + private static void ConfigureSignalRQueryStringToken(IServiceCollection services) |
| 154 | + { |
| 155 | + services.Configure<JwtBearerOptions>( |
| 156 | + JwtBearerDefaults.AuthenticationScheme, |
| 157 | + options => |
| 158 | + { |
| 159 | + options.Events ??= new JwtBearerEvents(); |
| 160 | + options.Events.OnMessageReceived = context => |
| 161 | + { |
| 162 | + if ( |
| 163 | + context.HttpContext.Request.Path.StartsWithSegments("/hub") |
| 164 | + && context.Request.Query.TryGetValue("access_token", out var token) |
| 165 | + ) |
| 166 | + { |
| 167 | + context.Token = token; |
| 168 | + } |
| 169 | + return Task.CompletedTask; |
| 170 | + }; |
| 171 | + } |
| 172 | + ); |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments