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

Minor perf improvements in BasePathStrategy and RemoteAuthenticationCallbackStrategy #654

Merged
merged 2 commits into from
Jul 1, 2023
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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