Skip to content

Commit

Permalink
fix a number of auth tests failing due to cooke challenge not returni…
Browse files Browse the repository at this point in the history
…ng a 401, now it always returns a 401 unless it's for oauth
  • Loading branch information
hahn-kev committed May 29, 2024
1 parent d15d01a commit 8c03571
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion backend/LexBoxApi/Auth/AuthKernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,11 @@ public static void AddLexBoxAuth(IServiceCollection services,
context.Request.Headers.Authorization.ToString().StartsWith("Bearer") &&
context.RequestServices.GetService<IOptions<OpenIdOptions>>()?.Value.Enable == true)
{
//todo this breaks CanUseBearerAuth test
//fow now this will use oauth
return OpenIddictValidationAspNetCoreDefaults.AuthenticationScheme;
}

if (context.Request.IsJwtRequest())
{
return JwtBearerDefaults.AuthenticationScheme;
Expand All @@ -100,6 +102,7 @@ public static void AddLexBoxAuth(IServiceCollection services,
{
return CookieAuthenticationDefaults.AuthenticationScheme;
}

if (context.Request.IsJwtOverBasicAuth(out var jwt))
{
context.Features.Set(new JwtOverBasicAuthFeature(jwt));
Expand All @@ -114,8 +117,23 @@ public static void AddLexBoxAuth(IServiceCollection services,
configuration.Bind("Authentication:Cookie", options);
options.LoginPath = "/login";
options.Cookie.Name = AuthCookieName;
// options.ForwardChallenge = JwtBearerDefaults.AuthenticationScheme;
options.ForwardForbid = JwtBearerDefaults.AuthenticationScheme;
options.Events = new()
{
OnRedirectToLogin = context =>
{
if (context.Request.Path.StartsWithSegments("/api/oauth") &&
context.Response.StatusCode == StatusCodes.Status200OK)
{
context.Response.Redirect(context.RedirectUri);
}
else
{
context.Response.StatusCode = StatusCodes.Status401Unauthorized;
}
return Task.CompletedTask;
}
};
})
.AddJwtBearer(options =>
{
Expand Down Expand Up @@ -157,6 +175,7 @@ public static void AddLexBoxAuth(IServiceCollection services,
googleOptions.ClientId = googleConfig.ClientId;
googleOptions.ClientSecret = googleConfig.ClientSecret;
}

googleOptions.CallbackPath = "/api/login/signin-google";
googleOptions.Events.OnTicketReceived = async context =>
{
Expand Down

0 comments on commit 8c03571

Please sign in to comment.