Skip to content

Commit

Permalink
feat: perf improvements in BasePathStrategy and RemoteAuthenticationC…
Browse files Browse the repository at this point in the history
…allbackStrategy (#654)
  • Loading branch information
rikbosch committed Jul 1, 2023
1 parent e49797c commit ac1c58a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
Expand Up @@ -11,14 +11,14 @@ public class BasePathStrategy : IMultiTenantStrategy
{
public Task<string?> GetIdentifierAsync(object context)
{
if(!(context is HttpContext httpContext))
if (!(context is HttpContext httpContext))
throw new MultiTenantException(null,
new ArgumentException($"\"{nameof(context)}\" type must be of type HttpContext", nameof(context)));

var path = httpContext.Request.Path;

var pathSegments =
path.Value?.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
path.Value?.Split('/', 2, StringSplitOptions.RemoveEmptyEntries);

if (pathSegments is null || pathSegments.Length == 0)
return Task.FromResult<string?>(null);
Expand Down
Expand Up @@ -27,15 +27,15 @@ public RemoteAuthenticationCallbackStrategy(ILogger<RemoteAuthenticationCallback

public async virtual Task<string?> GetIdentifierAsync(object context)
{
if(!(context is HttpContext httpContext))
if (!(context is HttpContext httpContext))
throw new MultiTenantException(null,
new ArgumentException($"\"{nameof(context)}\" type must be of type HttpContext", nameof(context)));

var schemes = httpContext.RequestServices.GetRequiredService<IAuthenticationSchemeProvider>();

foreach (var scheme in (await schemes.GetRequestHandlerSchemesAsync()).
Where(s => typeof(IAuthenticationRequestHandler).IsAssignableFrom(s.HandlerType)))
// Where(s => s.HandlerType.ImplementsOrInheritsUnboundGeneric(typeof(RemoteAuthenticationHandler<>))))
// Where(s => s.HandlerType.ImplementsOrInheritsUnboundGeneric(typeof(RemoteAuthenticationHandler<>))))
{
// Unfortnately we can't rely on the ShouldHandleAsync method since OpenId Connect handler doesn't use it.
// Instead we'll get the paths to check from the options.
Expand Down Expand Up @@ -77,14 +77,14 @@ public RemoteAuthenticationCallbackStrategy(ILogger<RemoteAuthenticationCallback
var formOptions = new FormOptions { BufferBody = true, MemoryBufferThreshold = 1048576 };

var form = await httpContext.Request.ReadFormAsync(formOptions);
state = form.Where(i => i.Key.ToLowerInvariant() == "state").Single().Value;
state = form.Single(i => string.Equals(i.Key, "state", StringComparison.OrdinalIgnoreCase)).Value;
}

var properties = ((dynamic)options).StateDataFormat.Unprotect(state) as AuthenticationProperties;

if (properties == null)
{
if(logger != null)
if (logger != null)
logger.LogWarning("A tenant could not be determined because no state paraameter passed with the remote authentication callback.");
return null;
}
Expand Down

0 comments on commit ac1c58a

Please sign in to comment.