Skip to content

Commit

Permalink
force openIddict options to validate on startup instead of first auth…
Browse files Browse the repository at this point in the history
… request
  • Loading branch information
hahn-kev committed Nov 22, 2024
1 parent cdc5349 commit b970166
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions backend/LexBoxApi/Auth/AuthKernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@
using LexBoxApi.Controllers;
using LexCore.Auth;
using LexData;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Logging;
using Microsoft.OpenApi.Models;
using OpenIddict.Core;
using OpenIddict.Server;
using OpenIddict.Server.AspNetCore;
using OpenIddict.Validation;
using OpenIddict.Validation.AspNetCore;

namespace LexBoxApi.Auth;
Expand Down Expand Up @@ -219,6 +224,7 @@ public static void AddLexBoxAuth(IServiceCollection services,

var openIdOptions = configuration.GetSection("Authentication:OpenId").Get<OpenIdOptions>();
if (openIdOptions?.Enable == true) AddOpenId(services, environment);
services.AddOptions<AuthenticationOptions>().ValidateOnStart();
}

private static void AddOpenId(IServiceCollection services, IWebHostEnvironment environment)
Expand Down Expand Up @@ -287,6 +293,11 @@ private static void AddOpenId(IServiceCollection services, IWebHostEnvironment e
options.AddAudiences(Enum.GetValues<LexboxAudience>().Where(a => a != LexboxAudience.Unknown).Select(a => a.ToString()).ToArray());
options.EnableAuthorizationEntryValidation();
});
//ensure that validation happens on startup, not on the first request which requires authentication
services.AddOptions<OpenIddictCoreOptions>().ValidateOnStart();
services.AddOptions<OpenIddictServerOptions>().ValidateOnStart();
services.AddOptions<OpenIddictValidationOptions>().ValidateOnStart();
services.AddOptions<OpenIddictServerAspNetCoreOptions>().ValidateOnStart();
}

public static AuthorizationPolicyBuilder RequireDefaultLexboxAuth(this AuthorizationPolicyBuilder builder)
Expand Down

0 comments on commit b970166

Please sign in to comment.