This repository was archived by the owner on Nov 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 191
Print available scheme names in exception message for invalid scheme #961
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Linq; | ||
using System.Security.Claims; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
|
@@ -62,7 +63,7 @@ public virtual async Task<AuthenticateResult> AuthenticateAsync(HttpContext cont | |
var handler = await Handlers.GetHandlerAsync(context, scheme); | ||
if (handler == null) | ||
{ | ||
throw new InvalidOperationException($"No authentication handler is configured to authenticate for the scheme: {scheme}"); | ||
throw await CreateMissingHandlerException(scheme); | ||
} | ||
|
||
var result = await handler.AuthenticateAsync(); | ||
|
@@ -96,7 +97,7 @@ public virtual async Task ChallengeAsync(HttpContext context, string scheme, Aut | |
var handler = await Handlers.GetHandlerAsync(context, scheme); | ||
if (handler == null) | ||
{ | ||
throw new InvalidOperationException($"No authentication handler is configured to handle the scheme: {scheme}"); | ||
throw await CreateMissingHandlerException(scheme); | ||
} | ||
|
||
await handler.ChallengeAsync(properties); | ||
|
@@ -124,7 +125,7 @@ public virtual async Task ForbidAsync(HttpContext context, string scheme, Authen | |
var handler = await Handlers.GetHandlerAsync(context, scheme); | ||
if (handler == null) | ||
{ | ||
throw new InvalidOperationException($"No authentication handler is configured to handle the scheme: {scheme}"); | ||
throw await CreateMissingHandlerException(scheme); | ||
} | ||
|
||
await handler.ForbidAsync(properties); | ||
|
@@ -155,13 +156,19 @@ public virtual async Task SignInAsync(HttpContext context, string scheme, Claims | |
} | ||
} | ||
|
||
var handler = await Handlers.GetHandlerAsync(context, scheme) as IAuthenticationSignInHandler; | ||
var handler = await Handlers.GetHandlerAsync(context, scheme); | ||
if (handler == null) | ||
{ | ||
throw new InvalidOperationException($"No IAuthenticationSignInHandler is configured to handle sign in for the scheme: {scheme}"); | ||
throw await CreateMissingSignInHandlerException(scheme); | ||
} | ||
|
||
var signInHandler = handler as IAuthenticationSignInHandler; | ||
if (signInHandler == null) | ||
{ | ||
throw await CreateMismatchedSignInHandlerException(scheme, handler); | ||
} | ||
|
||
await handler.SignInAsync(principal, properties); | ||
await signInHandler.SignInAsync(principal, properties); | ||
} | ||
|
||
/// <summary> | ||
|
@@ -183,13 +190,114 @@ public virtual async Task SignOutAsync(HttpContext context, string scheme, Authe | |
} | ||
} | ||
|
||
var handler = await Handlers.GetHandlerAsync(context, scheme) as IAuthenticationSignOutHandler; | ||
var handler = await Handlers.GetHandlerAsync(context, scheme); | ||
if (handler == null) | ||
{ | ||
throw new InvalidOperationException($"No IAuthenticationSignOutHandler is configured to handle sign out for the scheme: {scheme}"); | ||
throw await CreateMissingSignOutHandlerException(scheme); | ||
} | ||
|
||
var signOutHandler = handler as IAuthenticationSignOutHandler; | ||
if (signOutHandler == null) | ||
{ | ||
throw await CreateMismatchedSignOutHandlerException(scheme, handler); | ||
} | ||
|
||
await signOutHandler.SignOutAsync(properties); | ||
} | ||
|
||
private async Task<Exception> CreateMissingHandlerException(string scheme) | ||
{ | ||
var schemes = string.Join(", ", (await Schemes.GetAllSchemesAsync()).Select(sch => sch.Name)); | ||
|
||
var footer = $" Did you forget to call AddAuthentication().Add[SomeAuthHandler](\"{scheme}\",...)?"; | ||
|
||
if (string.IsNullOrEmpty(schemes)) | ||
{ | ||
return new InvalidOperationException( | ||
$"No authentication handlers are registered." + footer); | ||
} | ||
|
||
return new InvalidOperationException( | ||
$"No authentication handler is registered for the scheme '{scheme}'. The registered schemes are: {schemes}." + footer); | ||
} | ||
|
||
private async Task<string> GetAllSignInSchemeNames() | ||
{ | ||
return string.Join(", ", (await Schemes.GetAllSchemesAsync()) | ||
.Where(sch => typeof(IAuthenticationSignInHandler).IsAssignableFrom(sch.HandlerType)) | ||
.Select(sch => sch.Name)); | ||
} | ||
|
||
private async Task<Exception> CreateMissingSignInHandlerException(string scheme) | ||
{ | ||
var schemes = await GetAllSignInSchemeNames(); | ||
|
||
// CookieAuth is the only implementation of sign-in. | ||
var footer = $" Did you forget to call AddAuthentication().AddCookies(\"{scheme}\",...)?"; | ||
|
||
if (string.IsNullOrEmpty(schemes)) | ||
{ | ||
return new InvalidOperationException( | ||
$"No sign-in authentication handlers are registered." + footer); | ||
} | ||
|
||
return new InvalidOperationException( | ||
$"No sign-in authentication handler is registered for the scheme '{scheme}'. The registered sign-in schemes are: {schemes}." + footer); | ||
} | ||
|
||
private async Task<Exception> CreateMismatchedSignInHandlerException(string scheme, IAuthenticationHandler handler) | ||
{ | ||
var schemes = await GetAllSignInSchemeNames(); | ||
|
||
var mismatchError = $"The authentication handler registered for scheme '{scheme}' is '{handler.GetType().Name}' which cannot be used for SignInAsync. "; | ||
|
||
if (string.IsNullOrEmpty(schemes)) | ||
{ | ||
// CookieAuth is the only implementation of sign-in. | ||
return new InvalidOperationException(mismatchError | ||
+ $"Did you intended to call AddAuthentication().AddCookies(\"Cookies\") and SignInAsync(\"Cookies\",...)?"); | ||
} | ||
|
||
return new InvalidOperationException(mismatchError + $"The registered sign-in schemes are: {schemes}."); | ||
} | ||
|
||
private async Task<string> GetAllSignOutSchemeNames() | ||
{ | ||
return string.Join(", ", (await Schemes.GetAllSchemesAsync()) | ||
.Where(sch => typeof(IAuthenticationSignOutHandler).IsAssignableFrom(sch.HandlerType)) | ||
.Select(sch => sch.Name)); | ||
} | ||
|
||
private async Task<Exception> CreateMissingSignOutHandlerException(string scheme) | ||
{ | ||
var schemes = await GetAllSignOutSchemeNames(); | ||
|
||
var footer = $" Did you forget to call AddAuthentication().AddCookies(\"{scheme}\",...)?"; | ||
|
||
if (string.IsNullOrEmpty(schemes)) | ||
{ | ||
// CookieAuth is the most common implementation of sign-out, but OpenIdConnect and WsFederation also support it. | ||
return new InvalidOperationException($"No sign-out authentication handlers are registered." + footer); | ||
} | ||
|
||
return new InvalidOperationException( | ||
$"No sign-out authentication handler is registered for the scheme '{scheme}'. The registered sign-out schemes are: {schemes}." + footer); | ||
} | ||
|
||
private async Task<Exception> CreateMismatchedSignOutHandlerException(string scheme, IAuthenticationHandler handler) | ||
{ | ||
var schemes = await GetAllSignOutSchemeNames(); | ||
|
||
var mismatchError = $"The authentication handler registered for scheme '{scheme}' is '{handler.GetType().Name}' which cannot be used for {nameof(SignOutAsync)}. "; | ||
|
||
if (string.IsNullOrEmpty(schemes)) | ||
{ | ||
// CookieAuth is the most common implementation of sign-out, but OpenIdConnect and WsFederation also support it. | ||
return new InvalidOperationException(mismatchError | ||
+ $"Did you intended to call AddAuthentication().AddCookies(\"Cookies\") and {nameof(SignOutAsync)}(\"Cookies\",...)?"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo here too |
||
} | ||
|
||
await handler.SignOutAsync(properties); | ||
return new InvalidOperationException(mismatchError + $"The registered sign-out schemes are: {schemes}."); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo: intended => intend
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or should just be consistent and use forget?