2024-10-31 04:12:02 +03:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Mirea.Api.Security.Services;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Security;
|
|
|
|
|
|
|
|
|
|
namespace Mirea.Api.Security.Common.Domain;
|
|
|
|
|
|
|
|
|
|
internal class RequestContextInfo
|
|
|
|
|
{
|
2024-12-26 13:38:43 +03:00
|
|
|
|
public RequestContextInfo(HttpContext context, Model.CookieOptions cookieOptions)
|
2024-10-31 04:12:02 +03:00
|
|
|
|
{
|
|
|
|
|
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);
|
2024-11-04 03:15:13 +03:00
|
|
|
|
cookieOptions.SetCookie(context, CookieNames.FingerprintToken, fingerprint, DateTimeOffset.UtcNow.AddYears(10));
|
2024-10-31 04:12:02 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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; }
|
|
|
|
|
}
|