| | | 1 | | using System.Net; |
| | | 2 | | using System.Security.Claims; |
| | | 3 | | using FAU.Logica.Services; |
| | | 4 | | using Microsoft.AspNetCore.Http; |
| | | 5 | | |
| | | 6 | | namespace FAU.API.Services; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Implementaci�n HTTP de ICurrentUserContext. |
| | | 10 | | /// Extrae datos desde claims (JWT) y del request actual. |
| | | 11 | | /// </summary> |
| | | 12 | | public sealed class CurrentUserContext : ICurrentUserContext |
| | | 13 | | { |
| | | 14 | | private readonly IHttpContextAccessor _httpContextAccessor; |
| | | 15 | | |
| | | 16 | | // Overrides para background tasks o contextos sin HttpContext (ej. CLI, hangfire) |
| | | 17 | | private long? _overrideUserId; |
| | | 18 | | private string? _overrideHost; |
| | | 19 | | |
| | 8 | 20 | | public CurrentUserContext(IHttpContextAccessor httpContextAccessor) |
| | | 21 | | { |
| | 8 | 22 | | _httpContextAccessor = httpContextAccessor; |
| | 8 | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Permite setear el usuario manualmente (ej. background task que no tiene HttpContext). |
| | | 27 | | /// </summary> |
| | | 28 | | public void SetUserId(long? userId, string? host = null) |
| | | 29 | | { |
| | 0 | 30 | | _overrideUserId = userId; |
| | 0 | 31 | | _overrideHost = host; |
| | 0 | 32 | | } |
| | | 33 | | |
| | | 34 | | public long? UserId |
| | | 35 | | { |
| | | 36 | | get |
| | | 37 | | { |
| | 3 | 38 | | if (_overrideUserId.HasValue) return _overrideUserId.Value; |
| | | 39 | | |
| | 3 | 40 | | var user = _httpContextAccessor.HttpContext?.User; |
| | 3 | 41 | | var id = user?.FindFirstValue(ClaimTypes.NameIdentifier) |
| | 3 | 42 | | ?? user?.FindFirstValue("sub") |
| | 3 | 43 | | ?? user?.FindFirstValue("userId"); |
| | | 44 | | |
| | 3 | 45 | | return long.TryParse(id, out var parsed) ? parsed : null; |
| | | 46 | | } |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | public string? Username |
| | 1 | 50 | | => _httpContextAccessor.HttpContext?.User?.Identity?.Name |
| | 1 | 51 | | ?? _httpContextAccessor.HttpContext?.User?.FindFirstValue(ClaimTypes.Name); |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// IP del cliente que realiza la acci�n. |
| | | 55 | | /// Si la app est� detr�s de proxy/ingress, usa X-Forwarded-For (primer IP). |
| | | 56 | | /// Caso contrario usa RemoteIpAddress. |
| | | 57 | | /// </summary> |
| | | 58 | | public string? Host |
| | | 59 | | { |
| | | 60 | | get |
| | | 61 | | { |
| | 4 | 62 | | if (_overrideHost != null) return _overrideHost; |
| | | 63 | | |
| | 4 | 64 | | var http = _httpContextAccessor.HttpContext; |
| | 5 | 65 | | if (http == null) return null; |
| | | 66 | | |
| | | 67 | | // 1) Proxy (si existe). Tomar el primer valor. |
| | 3 | 68 | | var xff = http.Request.Headers["X-Forwarded-For"].ToString(); |
| | 3 | 69 | | if (!string.IsNullOrWhiteSpace(xff)) |
| | | 70 | | { |
| | 1 | 71 | | var first = xff.Split(',')[0].Trim(); |
| | 1 | 72 | | if (IPAddress.TryParse(first, out var ipParsed)) |
| | 1 | 73 | | return NormalizeIp(ipParsed); |
| | | 74 | | |
| | | 75 | | // Si viene con puerto u otro formato, devolver lo m�s cercano |
| | 0 | 76 | | return first; |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | // 2) Conexi�n directa |
| | 2 | 80 | | var remote = http.Connection.RemoteIpAddress; |
| | 2 | 81 | | return remote != null ? NormalizeIp(remote) : null; |
| | | 82 | | } |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | private static string NormalizeIp(IPAddress ip) |
| | | 86 | | { |
| | 2 | 87 | | if (ip.IsIPv4MappedToIPv6) |
| | 0 | 88 | | ip = ip.MapToIPv4(); |
| | | 89 | | |
| | 2 | 90 | | return ip.ToString(); |
| | | 91 | | } |
| | | 92 | | } |