mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 12:11:10 +00:00
Fixes 401 Unauthorized API responses being incorrectly cached
This commit is contained in:
1 parent
f246f0bf75
commit
d6af519e17
37 files changed
+298
-174
No files matched your search
@@ -1,7 +1,6 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Presentation.Web.Common.Models.Common;
|
||||
using Microsoft.Extensions.Caching.Hybrid;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
@@ -41,15 +40,15 @@ public class CachedAuthorizationHandler : DelegatingHandler
|
||||
string requestPath = request.RequestUri!.AbsolutePath;
|
||||
// if it's any endpoint other than the one for the authorization request, fire it away towards the original API service
|
||||
if (!requestPath.EndsWith(AUTHORIZATION_ENDPOINT, StringComparison.OrdinalIgnoreCase))
|
||||
return await base.SendAsync(request, cancellationToken);
|
||||
return await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
|
||||
// otherwise, check the hybrid cache to see if there is a cached authorization
|
||||
CachedResponseModel response = await _hybridCache.GetOrCreateAsync(
|
||||
AUTHORIZATION_ENDPOINT,
|
||||
async (cancellationToken) =>
|
||||
{
|
||||
// perform the actual API call if cache is empty or expired
|
||||
HttpResponseMessage result = await base.SendAsync(request, cancellationToken);
|
||||
string content = await result.Content.ReadAsStringAsync(cancellationToken);
|
||||
HttpResponseMessage result = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
|
||||
string content = await result.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
|
||||
// cache both status code and content
|
||||
return new CachedResponseModel
|
||||
{
|
||||
@@ -63,6 +62,10 @@ public class CachedAuthorizationHandler : DelegatingHandler
|
||||
},
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
// if the API returned 401 Unauthorized (i.e. token expired), DO NOT cache this response - it will be used for authorization endpoints even after successful login,
|
||||
// resulting in incorrect login redirection cycles!
|
||||
if (response.StatusCode == HttpStatusCode.Unauthorized)
|
||||
await _hybridCache.RemoveAsync(AUTHORIZATION_ENDPOINT, cancellationToken).ConfigureAwait(false);
|
||||
return new HttpResponseMessage(response.StatusCode)
|
||||
{
|
||||
Content = new StringContent(response.Content)
|
||||
|
||||
@@ -66,8 +66,6 @@ public class UrlService : IUrlService
|
||||
foreach (PropertyInfo prop in additionalRouteValues.GetType().GetProperties())
|
||||
routeValues.Add(prop.Name, prop.GetValue(additionalRouteValues));
|
||||
|
||||
// create route values based on whether the controller is localized
|
||||
object values = needsCulture ? new { culture, action } : new { action };
|
||||
return _linkGenerator.GetUriByAction(httpContext: httpContext, action: action, controller: controller, values: routeValues, scheme: scheme);
|
||||
return _linkGenerator.GetUriByAction(httpContext: httpContext, action: action, controller: controller, values: routeValues, scheme: scheme);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user