| | | 1 | | using System.Net.Http.Headers; |
| | | 2 | | using ArturRios.Util.WebApi.Security.Constants; |
| | | 3 | | using ArturRios.Util.WebApi.Security.Enums; |
| | | 4 | | using ArturRios.Util.WebApi.Security.Interfaces; |
| | | 5 | | using Microsoft.AspNetCore.Http; |
| | | 6 | | |
| | | 7 | | namespace ArturRios.Util.WebApi.Security.Extensions; |
| | | 8 | | |
| | | 9 | | /// <summary>Extension methods for reading the authentication token of the current request and the user attached |
| | | 10 | | /// to it by <see cref="Middleware.AuthenticationMiddleware"/>.</summary> |
| | | 11 | | public static class HttpContextExtensions |
| | | 12 | | { |
| | | 13 | | private const string BearerScheme = "Bearer"; |
| | | 14 | | |
| | | 15 | | /// <param name="context">The current HTTP context.</param> |
| | | 16 | | extension(HttpContext context) |
| | | 17 | | { |
| | | 18 | | /// <summary>Gets the authenticated user attached to the request.</summary> |
| | | 19 | | /// <returns>The authenticated user, or <see langword="null"/> if the request is not authenticated.</returns> |
| | | 20 | | public IAuthenticatedUser? GetUser() => |
| | 11 | 21 | | context.Items[AuthenticationItemKeys.User] as IAuthenticatedUser; |
| | | 22 | | |
| | | 23 | | /// <summary>Gets the authenticated user as the app's own identity type.</summary> |
| | | 24 | | /// <typeparam name="TUser">The app's identity type.</typeparam> |
| | | 25 | | /// <returns>The authenticated user, or <see langword="null"/> if the request is not authenticated or the |
| | | 26 | | /// attached user is of another type.</returns> |
| | | 27 | | public TUser? GetUser<TUser>() where TUser : class, IAuthenticatedUser => |
| | 4 | 28 | | context.Items[AuthenticationItemKeys.User] as TUser; |
| | | 29 | | |
| | | 30 | | /// <summary>Extracts the raw authentication token from the <c>Authorization: Bearer</c> header, the named |
| | | 31 | | /// cookie, or either (header first).</summary> |
| | | 32 | | /// <param name="source">Where to read the token from.</param> |
| | | 33 | | /// <param name="cookieName">The cookie name used when <paramref name="source"/> is <see cref="TokenSource.Cooki |
| | | 34 | | /// or <see cref="TokenSource.Either"/>.</param> |
| | | 35 | | /// <returns>The token, or <see cref="string.Empty"/> when none is found.</returns> |
| | | 36 | | public string ExtractToken(TokenSource source, string cookieName) |
| | | 37 | | { |
| | 23 | 38 | | ArgumentNullException.ThrowIfNull(context); |
| | | 39 | | |
| | 23 | 40 | | return source switch |
| | 23 | 41 | | { |
| | 16 | 42 | | TokenSource.Header => TokenFromHeader(context), |
| | 4 | 43 | | TokenSource.Cookie => TokenFromCookie(context, cookieName), |
| | 3 | 44 | | TokenSource.Either => TokenFromHeader(context) is { Length: > 0 } header |
| | 3 | 45 | | ? header |
| | 3 | 46 | | : TokenFromCookie(context, cookieName), |
| | 0 | 47 | | _ => string.Empty |
| | 23 | 48 | | }; |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary>Attaches <paramref name="user"/> to the request, where <c>GetUser</c> and the authorization |
| | | 52 | | /// filters look for it.</summary> |
| | | 53 | | /// <param name="user">The authenticated user.</param> |
| | | 54 | | internal void SetUser(IAuthenticatedUser user) => |
| | 7 | 55 | | context.Items[AuthenticationItemKeys.User] = user; |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | private static string TokenFromHeader(HttpContext context) |
| | | 59 | | { |
| | 19 | 60 | | var header = context.Request.Headers.Authorization.FirstOrDefault(); |
| | | 61 | | |
| | 19 | 62 | | if (string.IsNullOrWhiteSpace(header) || !AuthenticationHeaderValue.TryParse(header, out var parsed) || |
| | 19 | 63 | | !string.Equals(parsed.Scheme, BearerScheme, StringComparison.OrdinalIgnoreCase) || |
| | 19 | 64 | | string.IsNullOrWhiteSpace(parsed.Parameter)) |
| | | 65 | | { |
| | 8 | 66 | | return string.Empty; |
| | | 67 | | } |
| | | 68 | | |
| | 11 | 69 | | return parsed.Parameter.Trim(); |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | private static string TokenFromCookie(HttpContext context, string cookieName) |
| | | 73 | | { |
| | 6 | 74 | | var value = context.Request.Cookies[cookieName]; |
| | | 75 | | |
| | 6 | 76 | | return string.IsNullOrWhiteSpace(value) ? string.Empty : value.Trim(); |
| | | 77 | | } |
| | | 78 | | } |