DocSpace-buildtools/common/ASC.Core.Common/Security/Authentication/CookieStorage.cs

124 lines
4.0 KiB
C#
Raw Normal View History

2022-02-15 11:52:43 +00:00
namespace ASC.Core.Security.Authentication;
[Scope]
public class CookieStorage
{
2022-02-15 11:52:43 +00:00
private const string DateTimeFormat = "yyyy-MM-dd HH:mm:ss,fff";
private readonly InstanceCrypto _instanceCrypto;
private readonly TenantCookieSettingsHelper _tenantCookieSettingsHelper;
private readonly HttpContext _httpContext;
private readonly ILog _logger;
public CookieStorage(
InstanceCrypto instanceCrypto,
TenantCookieSettingsHelper tenantCookieSettingsHelper,
IOptionsMonitor<ILog> options)
{
2022-02-15 11:52:43 +00:00
_instanceCrypto = instanceCrypto;
_tenantCookieSettingsHelper = tenantCookieSettingsHelper;
_logger = options.CurrentValue;
}
2022-02-15 11:52:43 +00:00
public CookieStorage(
IHttpContextAccessor httpContextAccessor,
InstanceCrypto instanceCrypto,
TenantCookieSettingsHelper tenantCookieSettingsHelper,
IOptionsMonitor<ILog> options)
: this(instanceCrypto, tenantCookieSettingsHelper, options)
{
_httpContext = httpContextAccessor.HttpContext;
}
2019-10-18 08:48:27 +00:00
2022-02-15 11:52:43 +00:00
public bool DecryptCookie(string cookie, out int tenant, out Guid userid, out int indexTenant, out DateTime expire, out int indexUser)
{
tenant = Tenant.DefaultTenant;
userid = Guid.Empty;
indexTenant = 0;
expire = DateTime.MaxValue;
indexUser = 0;
2019-10-18 08:48:27 +00:00
2022-02-15 11:52:43 +00:00
if (string.IsNullOrEmpty(cookie))
2020-04-28 15:11:10 +00:00
{
2022-02-15 11:52:43 +00:00
return false;
2020-04-28 15:11:10 +00:00
}
2022-02-15 11:52:43 +00:00
try
{
2022-02-15 11:52:43 +00:00
cookie = (HttpUtility.UrlDecode(cookie) ?? "").Replace(' ', '+');
var s = _instanceCrypto.Decrypt(cookie).Split('$');
2022-02-15 11:52:43 +00:00
if (1 < s.Length)
{
2022-02-15 11:52:43 +00:00
tenant = int.Parse(s[1]);
}
2022-02-15 11:52:43 +00:00
if (4 < s.Length)
{
userid = new Guid(s[4]);
}
if (5 < s.Length)
{
indexTenant = int.Parse(s[5]);
}
if (6 < s.Length)
{
2022-02-15 11:52:43 +00:00
expire = DateTime.ParseExact(s[6], DateTimeFormat, CultureInfo.InvariantCulture);
}
2022-02-15 11:52:43 +00:00
if (7 < s.Length)
{
2022-02-15 11:52:43 +00:00
indexUser = int.Parse(s[7]);
}
2022-02-15 11:52:43 +00:00
return true;
}
catch (Exception err)
{
_logger.ErrorFormat("Authenticate error: cookie {0}, tenant {1}, userid {2}, indexTenant {3}, expire {4}: {5}",
cookie, tenant, userid, indexTenant, expire.ToString(DateTimeFormat), err);
}
2022-02-15 11:52:43 +00:00
return false;
}
2022-02-15 11:52:43 +00:00
public string EncryptCookie(int tenant, Guid userid)
{
var settingsTenant = _tenantCookieSettingsHelper.GetForTenant(tenant);
var expires = _tenantCookieSettingsHelper.GetExpiresTime(tenant);
var settingsUser = _tenantCookieSettingsHelper.GetForUser(tenant, userid);
2022-02-15 11:52:43 +00:00
return EncryptCookie(tenant, userid, settingsTenant.Index, expires, settingsUser.Index);
}
public string EncryptCookie(int tenant, Guid userid, int indexTenant, DateTime expires, int indexUser)
{
var s = string.Format("{0}${1}${2}${3}${4}${5}${6}${7}",
string.Empty, //login
tenant,
string.Empty, //password
GetUserDepenencySalt(),
userid.ToString("N"),
indexTenant,
expires.ToString(DateTimeFormat, CultureInfo.InvariantCulture),
indexUser);
return _instanceCrypto.Encrypt(s);
}
2022-02-15 11:52:43 +00:00
private string GetUserDepenencySalt()
{
var data = string.Empty;
try
{
2022-02-15 11:52:43 +00:00
if (_httpContext?.Request != null)
{
2022-02-15 11:52:43 +00:00
var forwarded = _httpContext.Request.Headers["X-Forwarded-For"].ToString();
data = string.IsNullOrEmpty(forwarded) ? _httpContext.Request.GetUserHostAddress() : forwarded.Split(':')[0];
}
}
2022-02-15 11:52:43 +00:00
catch { }
return Hasher.Base64Hash(data ?? string.Empty, HashAlg.SHA256);
}
2022-02-15 11:52:43 +00:00
}