| | | 1 | | using Estud.Back.Auth.Schemes; |
| | | 2 | | using System.Security.Claims; |
| | | 3 | | using System.Text.RegularExpressions; |
| | | 4 | | using Microsoft.AspNetCore.Authentication; |
| | | 5 | | using Microsoft.AspNetCore.Mvc.Controllers; |
| | | 6 | | |
| | | 7 | | namespace Estud.Back.Extensions; |
| | | 8 | | |
| | | 9 | | public static partial class HttpExtensions |
| | | 10 | | { |
| | | 11 | | extension(HttpResponse response) |
| | | 12 | | { |
| | | 13 | | public void AppendJWTCookie(string token, AuthSettings settings) |
| | | 14 | | { |
| | 854 | 15 | | response.Cookies.Append( |
| | 854 | 16 | | JwtBearerScheme.Cookie, |
| | 854 | 17 | | token, |
| | 854 | 18 | | new CookieOptions |
| | 854 | 19 | | { |
| | 854 | 20 | | Path = "/", |
| | 854 | 21 | | HttpOnly = true, |
| | 854 | 22 | | Secure = settings.CookieSecure, |
| | 854 | 23 | | SameSite = settings.CookieSameSite, |
| | 854 | 24 | | MaxAge = TimeSpan.FromMinutes(settings.ExpirationTimeInMinutes), |
| | 854 | 25 | | } |
| | 854 | 26 | | ); |
| | 854 | 27 | | } |
| | | 28 | | |
| | | 29 | | public void DeleteJWTCookie(AuthSettings settings) |
| | | 30 | | { |
| | 24 | 31 | | response.Cookies.Delete( |
| | 24 | 32 | | JwtBearerScheme.Cookie, |
| | 24 | 33 | | new CookieOptions |
| | 24 | 34 | | { |
| | 24 | 35 | | Path = "/", |
| | 24 | 36 | | HttpOnly = true, |
| | 24 | 37 | | SameSite = settings.CookieSameSite, |
| | 24 | 38 | | Secure = settings.CookieSecure, |
| | 24 | 39 | | } |
| | 24 | 40 | | ); |
| | 24 | 41 | | } |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | [GeneratedRegex(@"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}", RegexOptions.IgnoreCase)] |
| | | 45 | | private static partial Regex GuidPattern(); |
| | | 46 | | |
| | 0 | 47 | | private static string NormalizePath(string path) => GuidPattern().Replace(path, "{id}"); |
| | | 48 | | |
| | | 49 | | extension(HttpContext context) |
| | | 50 | | { |
| | | 51 | | public string GetTargetControllerName() |
| | | 52 | | { |
| | 3304 | 53 | | var endpoint = context.GetEndpoint(); |
| | 3304 | 54 | | if (endpoint == null) return NormalizePath(context.Request.Path.ToString()); |
| | | 55 | | |
| | 3304 | 56 | | var action = endpoint.Metadata.GetMetadata<ControllerActionDescriptor>(); |
| | 6606 | 57 | | if (action != null) return $"{action.ControllerName}Controller"; |
| | | 58 | | |
| | 2 | 59 | | if (endpoint is RouteEndpoint routeEndpoint) |
| | 2 | 60 | | return routeEndpoint.RoutePattern.RawText ?? endpoint.DisplayName ?? NormalizePath(context.Request.Path. |
| | | 61 | | |
| | 0 | 62 | | return endpoint.DisplayName ?? NormalizePath(context.Request.Path.ToString()); |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | public string GetIpAddress() |
| | | 66 | | { |
| | 0 | 67 | | return context.Connection.RemoteIpAddress?.ToString() ?? "-"; |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | public string GetUserAgent() |
| | | 71 | | { |
| | 0 | 72 | | return context.Request.Headers.UserAgent.FirstOrDefault() ?? "-"; |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | public async Task SignInTwoFactorUserIdSchemeAsync(int userId) |
| | | 76 | | { |
| | | 77 | | // Store user ID in Identity cookie for 2FA verification step |
| | 20 | 78 | | var identity = new ClaimsIdentity(IdentityConstants.TwoFactorUserIdScheme); |
| | 20 | 79 | | identity.AddClaim(new Claim(ClaimTypes.Name, userId.ToString())); |
| | 20 | 80 | | await context!.SignInAsync(IdentityConstants.TwoFactorUserIdScheme, new ClaimsPrincipal(identity)); |
| | 20 | 81 | | } |
| | | 82 | | } |
| | | 83 | | } |