using Microsoft.AspNetCore.Http; using Mirea.Api.Security.Services; using System; using System.Security; namespace Mirea.Api.Security.Common.Domain; internal class RequestContextInfo { public RequestContextInfo(HttpContext context, CookieOptionsParameters cookieOptions) { var ipEntity = context.Connection.RemoteIpAddress; if (string.IsNullOrEmpty(ipEntity?.ToString())) throw new SecurityException("Ip is required for authorization."); var ip = ipEntity.MapToIPv4().ToString(); var userAgent = context.Request.Headers.UserAgent.ToString(); var fingerprint = context.Request.Cookies[CookieNames.FingerprintToken]; if (string.IsNullOrEmpty(fingerprint)) { fingerprint = Guid.NewGuid().ToString().Replace("-", "") + GeneratorKey.GenerateString(32); cookieOptions.SetCookie(context, CookieNames.FingerprintToken, fingerprint); } UserAgent = userAgent; Fingerprint = fingerprint; Ip = ip; RefreshToken = context.Request.Cookies["refresh_token"] ?? string.Empty; } public string UserAgent { get; private set; } public string Ip { get; private set; } public string Fingerprint { get; private set; } public string RefreshToken { get; private set; } }