Skip to content

Commit 3ac887c

Browse files
committed
API fixes
1 parent 23cb395 commit 3ac887c

File tree

1 file changed

+40
-4
lines changed

1 file changed

+40
-4
lines changed

Intersect.Server/Web/ApiService.cs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ internal partial class ApiService : ApplicationService<ServerContext, IApiServic
5050
private WebApplication? _app;
5151
private static readonly Assembly Assembly = typeof(ApiService).Assembly;
5252

53+
private static readonly string[] ChallengePaths = [
54+
"/api",
55+
"/assets",
56+
"/avatar",
57+
];
58+
5359
private static string GetOptionsName<TOptions>() => typeof(TOptions).Name.Replace("Options", string.Empty);
5460

5561
// ReSharper disable once MemberCanBeMadeStatic.Local
@@ -173,8 +179,24 @@ internal partial class ApiService : ApplicationService<ServerContext, IApiServic
173179
options =>
174180
{
175181
// Commenting this out fixes no redirect
176-
// Uncommenting fixed something else, but I can't remember what it was
182+
// Uncommenting fixed API consumers with expired tokens
177183
// options.ForwardChallenge = JwtBearerDefaults.AuthenticationScheme;
184+
185+
// So the thing that was broken if the above was commented out was the editor (or presumably
186+
// anything that had an expired token) -- I believe the below fixes it
187+
options.ForwardDefaultSelector = context =>
188+
{
189+
var requestPath = context.Request.Path;
190+
foreach (var challengePath in ChallengePaths)
191+
{
192+
if (requestPath.StartsWithSegments(challengePath))
193+
{
194+
return JwtBearerDefaults.AuthenticationScheme;
195+
}
196+
}
197+
198+
return null;
199+
};
178200
options.Events.OnSignedIn += async (context) => { };
179201
options.Events.OnSigningIn += async (context) => { };
180202
options.Events.OnSigningOut += async (context) => { };
@@ -242,6 +264,12 @@ internal partial class ApiService : ApplicationService<ServerContext, IApiServic
242264
},
243265
OnChallenge = async context =>
244266
{
267+
if (context.AuthenticateFailure != null)
268+
{
269+
// This was needed to make sure authentication failures didn't return 200
270+
context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
271+
context.HandleResponse();
272+
}
245273
},
246274
OnMessageReceived = async context => { },
247275
OnTokenValidated = async context =>
@@ -306,9 +334,17 @@ internal partial class ApiService : ApplicationService<ServerContext, IApiServic
306334
"Bearer-to-Cookie Fallback",
307335
pso =>
308336
{
309-
pso.ForwardDefaultSelector = context => context.Request.Headers.Authorization.Count > 0
310-
? JwtBearerDefaults.AuthenticationScheme
311-
: CookieAuthenticationDefaults.AuthenticationScheme;
337+
pso.ForwardDefaultSelector = context =>
338+
{
339+
if (context.Request.Headers.Authorization.Count > 0)
340+
{
341+
return JwtBearerDefaults.AuthenticationScheme;
342+
}
343+
else
344+
{
345+
return CookieAuthenticationDefaults.AuthenticationScheme;
346+
}
347+
};
312348
}
313349
);
314350

0 commit comments

Comments
 (0)