Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

force openIddict options to validate on startup #1263

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading