| | | 1 | | using Microsoft.AspNetCore.Authentication; |
| | | 2 | | using Microsoft.AspNetCore.Authentication.Cookies; |
| | | 3 | | |
| | | 4 | | namespace ProjectTemplate.Web.Authentication; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Determines which registered authentication schemes a user may choose as an external sign-in provider. |
| | | 8 | | /// </summary> |
| | | 9 | | /// <remarks> |
| | | 10 | | /// The login page and the external challenge endpoint use the same rule, so a scheme can be challenged through |
| | | 11 | | /// <c>/External/Challenge</c> only when it is also offered on the login page. A scheme is selectable when it has a |
| | | 12 | | /// non-empty display name and is not a reserved local scheme: the cookie session scheme or the default authenticate, |
| | | 13 | | /// sign-in, or sign-out scheme. Register a display name only for schemes users are meant to choose; schemes registered |
| | | 14 | | /// without one, such as bearer-token or test schemes, cannot be challenged through the external endpoint. |
| | | 15 | | /// </remarks> |
| | | 16 | | internal static class ExternalAuthenticationProviderSchemes |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// Gets the registered schemes that users may choose as external sign-in providers. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="schemeProvider">The authentication scheme provider.</param> |
| | | 22 | | /// <returns>The selectable external provider schemes.</returns> |
| | | 23 | | internal static async Task<IReadOnlyList<AuthenticationScheme>> GetSelectableSchemesAsync( |
| | | 24 | | IAuthenticationSchemeProvider schemeProvider) |
| | | 25 | | { |
| | 4 | 26 | | ArgumentNullException.ThrowIfNull(schemeProvider); |
| | | 27 | | |
| | 4 | 28 | | HashSet<string> reservedSchemeNames = await GetReservedSchemeNamesAsync(schemeProvider).ConfigureAwait(false); |
| | 4 | 29 | | IEnumerable<AuthenticationScheme> schemes = await schemeProvider.GetAllSchemesAsync().ConfigureAwait(false); |
| | | 30 | | |
| | 4 | 31 | | return [.. schemes.Where(scheme => IsSelectable(scheme, reservedSchemeNames))]; |
| | 4 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Finds a selectable external provider scheme by name. |
| | | 36 | | /// </summary> |
| | | 37 | | /// <param name="schemeProvider">The authentication scheme provider.</param> |
| | | 38 | | /// <param name="schemeName">The scheme name supplied by the request.</param> |
| | | 39 | | /// <returns>The scheme when it exists and is selectable; otherwise <see langword="null"/>.</returns> |
| | | 40 | | internal static async Task<AuthenticationScheme?> FindSelectableSchemeAsync( |
| | | 41 | | IAuthenticationSchemeProvider schemeProvider, |
| | | 42 | | string? schemeName) |
| | | 43 | | { |
| | 6 | 44 | | ArgumentNullException.ThrowIfNull(schemeProvider); |
| | | 45 | | |
| | 6 | 46 | | if (string.IsNullOrWhiteSpace(schemeName)) |
| | | 47 | | { |
| | 0 | 48 | | return null; |
| | | 49 | | } |
| | | 50 | | |
| | 6 | 51 | | AuthenticationScheme? scheme = await schemeProvider.GetSchemeAsync(schemeName).ConfigureAwait(false); |
| | | 52 | | |
| | 6 | 53 | | if (scheme is null) |
| | | 54 | | { |
| | 2 | 55 | | return null; |
| | | 56 | | } |
| | | 57 | | |
| | 4 | 58 | | HashSet<string> reservedSchemeNames = await GetReservedSchemeNamesAsync(schemeProvider).ConfigureAwait(false); |
| | | 59 | | |
| | 4 | 60 | | return IsSelectable(scheme, reservedSchemeNames) ? scheme : null; |
| | 6 | 61 | | } |
| | | 62 | | |
| | | 63 | | private static bool IsSelectable(AuthenticationScheme scheme, HashSet<string> reservedSchemeNames) |
| | | 64 | | { |
| | 12 | 65 | | return !string.IsNullOrWhiteSpace(scheme.DisplayName) && |
| | 12 | 66 | | !reservedSchemeNames.Contains(scheme.Name); |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | private static async Task<HashSet<string>> GetReservedSchemeNamesAsync(IAuthenticationSchemeProvider schemeProvider) |
| | | 70 | | { |
| | 8 | 71 | | HashSet<string> reservedSchemeNames = new(StringComparer.Ordinal) |
| | 8 | 72 | | { |
| | 8 | 73 | | CookieAuthenticationDefaults.AuthenticationScheme |
| | 8 | 74 | | }; |
| | | 75 | | |
| | 8 | 76 | | AddSchemeName(reservedSchemeNames, await schemeProvider.GetDefaultAuthenticateSchemeAsync().ConfigureAwait(false |
| | 8 | 77 | | AddSchemeName(reservedSchemeNames, await schemeProvider.GetDefaultSignInSchemeAsync().ConfigureAwait(false)); |
| | 8 | 78 | | AddSchemeName(reservedSchemeNames, await schemeProvider.GetDefaultSignOutSchemeAsync().ConfigureAwait(false)); |
| | | 79 | | |
| | 8 | 80 | | return reservedSchemeNames; |
| | 8 | 81 | | } |
| | | 82 | | |
| | | 83 | | private static void AddSchemeName(HashSet<string> schemeNames, AuthenticationScheme? scheme) |
| | | 84 | | { |
| | 24 | 85 | | if (scheme is not null) |
| | | 86 | | { |
| | 24 | 87 | | _ = schemeNames.Add(scheme.Name); |
| | | 88 | | } |
| | 24 | 89 | | } |
| | | 90 | | } |