DocSpace-client/web/ASC.Web.Core/CookiesManager.cs

253 lines
8.2 KiB
C#
Raw Normal View History

2022-03-15 18:00:53 +00:00
// (c) Copyright Ascensio System SIA 2010-2022
//
// This program is a free software product.
// You can redistribute it and/or modify it under the terms
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
// Foundation. In accordance with Section 7(a) of the GNU AGPL its Section 15 shall be amended
// to the effect that Ascensio System SIA expressly excludes the warranty of non-infringement of
// any third-party rights.
//
// This program is distributed WITHOUT ANY WARRANTY, without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For details, see
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
//
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
//
// The interactive user interfaces in modified source and object code versions of the Program must
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
//
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
// trademark law for use of our trademarks.
//
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
2019-06-07 08:59:07 +00:00
using Constants = ASC.Core.Users.Constants;
2019-06-07 08:59:07 +00:00
2022-03-17 15:13:38 +00:00
namespace ASC.Web.Core;
public enum CookiesType
{
AuthKey,
SocketIO
}
[Scope]
public class CookiesManager
2019-06-07 08:59:07 +00:00
{
2022-03-25 16:26:06 +00:00
private const string AuthCookiesName = "asc_auth_key";
private const string SocketIOCookiesName = "socketio.sid";
2022-03-17 15:13:38 +00:00
private IHttpContextAccessor HttpContextAccessor { get; }
private UserManager UserManager { get; }
private SecurityContext SecurityContext { get; }
private TenantCookieSettingsHelper TenantCookieSettingsHelper { get; }
private TenantManager TenantManager { get; }
private CoreBaseSettings CoreBaseSettings { get; }
public CookiesManager(
IHttpContextAccessor httpContextAccessor,
UserManager userManager,
SecurityContext securityContext,
TenantCookieSettingsHelper tenantCookieSettingsHelper,
TenantManager tenantManager,
CoreBaseSettings coreBaseSettings)
2019-06-07 08:59:07 +00:00
{
2022-03-17 15:13:38 +00:00
HttpContextAccessor = httpContextAccessor;
UserManager = userManager;
SecurityContext = securityContext;
TenantCookieSettingsHelper = tenantCookieSettingsHelper;
TenantManager = tenantManager;
CoreBaseSettings = coreBaseSettings;
2019-06-07 08:59:07 +00:00
}
2022-03-17 15:13:38 +00:00
private static string GetCookiesName(CookiesType type)
2019-06-07 08:59:07 +00:00
{
2022-03-17 15:13:38 +00:00
return type switch
2019-06-07 08:59:07 +00:00
{
2022-03-25 16:26:06 +00:00
CookiesType.AuthKey => AuthCookiesName,
CookiesType.SocketIO => SocketIOCookiesName,
2019-06-07 08:59:07 +00:00
2022-03-17 15:13:38 +00:00
_ => string.Empty,
};
}
2019-06-07 08:59:07 +00:00
2022-03-17 15:13:38 +00:00
public string GetRequestVar(CookiesType type)
{
if (HttpContextAccessor?.HttpContext == null)
2019-06-07 08:59:07 +00:00
{
2022-03-17 15:13:38 +00:00
return "";
}
var cookie = HttpContextAccessor.HttpContext.Request.Query[GetCookiesName(type)].FirstOrDefault() ?? HttpContextAccessor.HttpContext.Request.Form[GetCookiesName(type)].FirstOrDefault();
2019-06-07 08:59:07 +00:00
2022-03-17 15:13:38 +00:00
return string.IsNullOrEmpty(cookie) ? GetCookies(type) : cookie;
}
2019-06-07 08:59:07 +00:00
2022-03-17 15:13:38 +00:00
public void SetCookies(CookiesType type, string value, bool session = false)
{
if (HttpContextAccessor?.HttpContext == null)
{
return;
2019-06-07 08:59:07 +00:00
}
2022-03-17 15:13:38 +00:00
var options = new CookieOptions
2019-06-07 08:59:07 +00:00
{
2022-03-17 15:13:38 +00:00
Expires = GetExpiresDate(session)
};
2019-06-07 08:59:07 +00:00
2022-03-17 15:13:38 +00:00
if (type == CookiesType.AuthKey)
{
options.HttpOnly = true;
2019-06-07 08:59:07 +00:00
2022-03-17 15:13:38 +00:00
if (HttpContextAccessor.HttpContext.Request.GetUrlRewriter().Scheme == "https")
{
2022-03-17 15:13:38 +00:00
options.Secure = true;
2019-06-07 08:59:07 +00:00
2022-03-17 15:13:38 +00:00
if (CoreBaseSettings.Personal)
{
2022-03-17 15:13:38 +00:00
options.SameSite = SameSiteMode.None;
}
}
2022-03-17 15:13:38 +00:00
}
2019-06-07 08:59:07 +00:00
2022-03-17 15:13:38 +00:00
HttpContextAccessor.HttpContext.Response.Cookies.Append(GetCookiesName(type), value, options);
}
public void SetCookies(CookiesType type, string value, string domain, bool session = false)
{
if (HttpContextAccessor?.HttpContext == null)
{
return;
2019-06-07 08:59:07 +00:00
}
2022-03-17 15:13:38 +00:00
var options = new CookieOptions
2019-06-07 08:59:07 +00:00
{
2022-03-17 15:13:38 +00:00
Expires = GetExpiresDate(session),
Domain = domain
};
2019-06-07 08:59:07 +00:00
2022-03-17 15:13:38 +00:00
if (type == CookiesType.AuthKey)
{
options.HttpOnly = true;
2019-06-07 08:59:07 +00:00
2022-03-17 15:13:38 +00:00
if (HttpContextAccessor.HttpContext.Request.GetUrlRewriter().Scheme == "https")
{
2022-03-17 15:13:38 +00:00
options.Secure = true;
2019-06-07 08:59:07 +00:00
2022-03-17 15:13:38 +00:00
if (CoreBaseSettings.Personal)
{
2022-03-17 15:13:38 +00:00
options.SameSite = SameSiteMode.None;
}
}
2019-06-07 08:59:07 +00:00
}
2022-03-17 15:13:38 +00:00
HttpContextAccessor.HttpContext.Response.Cookies.Append(GetCookiesName(type), value, options);
}
2019-06-07 08:59:07 +00:00
2022-03-17 15:13:38 +00:00
public string GetCookies(CookiesType type)
{
if (HttpContextAccessor?.HttpContext != null)
2019-06-07 08:59:07 +00:00
{
2022-03-17 15:13:38 +00:00
var cookieName = GetCookiesName(type);
2019-06-07 08:59:07 +00:00
2022-03-17 15:13:38 +00:00
if (HttpContextAccessor.HttpContext.Request.Cookies.ContainsKey(cookieName))
{
2022-03-17 15:13:38 +00:00
return HttpContextAccessor.HttpContext.Request.Cookies[cookieName] ?? "";
}
2019-06-07 08:59:07 +00:00
}
2022-03-17 15:13:38 +00:00
return "";
}
2019-06-07 08:59:07 +00:00
2022-03-17 15:13:38 +00:00
public void ClearCookies(CookiesType type)
{
if (HttpContextAccessor?.HttpContext == null)
2019-06-07 08:59:07 +00:00
{
2022-03-17 15:13:38 +00:00
return;
2019-06-07 08:59:07 +00:00
}
2022-03-17 15:13:38 +00:00
if (HttpContextAccessor.HttpContext.Request.Cookies.ContainsKey(GetCookiesName(type)))
2019-06-07 08:59:07 +00:00
{
2022-03-17 15:13:38 +00:00
HttpContextAccessor.HttpContext.Response.Cookies.Delete(GetCookiesName(type), new CookieOptions() { Expires = DateTime.Now.AddDays(-3) });
}
}
2019-06-07 08:59:07 +00:00
2022-03-17 15:13:38 +00:00
private DateTime? GetExpiresDate(bool session)
{
DateTime? expires = null;
2019-06-07 08:59:07 +00:00
2022-03-17 15:13:38 +00:00
if (!session)
{
var tenant = TenantManager.GetCurrentTenant().Id;
expires = TenantCookieSettingsHelper.GetExpiresTime(tenant);
2019-06-07 08:59:07 +00:00
}
2022-03-17 15:13:38 +00:00
return expires;
}
public void SetLifeTime(int lifeTime)
{
var tenant = TenantManager.GetCurrentTenant();
if (!UserManager.IsUserInGroup(SecurityContext.CurrentAccount.ID, Constants.GroupAdmin.ID))
2019-06-07 08:59:07 +00:00
{
2022-03-17 15:13:38 +00:00
throw new SecurityException();
2019-06-07 08:59:07 +00:00
}
2022-03-17 15:13:38 +00:00
var settings = TenantCookieSettingsHelper.GetForTenant(tenant.Id);
if (lifeTime > 0)
2019-06-07 08:59:07 +00:00
{
2019-08-15 14:48:55 +00:00
settings.Index += 1;
2022-03-17 15:13:38 +00:00
settings.LifeTime = lifeTime;
}
else
{
settings.LifeTime = 0;
}
2019-06-07 08:59:07 +00:00
2022-03-17 15:13:38 +00:00
TenantCookieSettingsHelper.SetForTenant(tenant.Id, settings);
2019-06-07 08:59:07 +00:00
2022-03-17 15:13:38 +00:00
var cookie = SecurityContext.AuthenticateMe(SecurityContext.CurrentAccount.ID);
2019-06-07 08:59:07 +00:00
2022-03-17 15:13:38 +00:00
SetCookies(CookiesType.AuthKey, cookie);
}
2019-08-08 09:26:58 +00:00
2022-03-17 15:13:38 +00:00
public int GetLifeTime(int tenantId)
{
return TenantCookieSettingsHelper.GetForTenant(tenantId).LifeTime;
}
2019-06-07 08:59:07 +00:00
2022-03-17 15:13:38 +00:00
public void ResetUserCookie(Guid? userId = null)
{
var settings = TenantCookieSettingsHelper.GetForUser(userId ?? SecurityContext.CurrentAccount.ID);
settings.Index += 1;
TenantCookieSettingsHelper.SetForUser(userId ?? SecurityContext.CurrentAccount.ID, settings);
2019-06-07 08:59:07 +00:00
2022-03-17 15:13:38 +00:00
if (!userId.HasValue)
{
2019-09-20 15:53:27 +00:00
var cookie = SecurityContext.AuthenticateMe(SecurityContext.CurrentAccount.ID);
2022-03-17 15:13:38 +00:00
2019-09-09 12:56:33 +00:00
SetCookies(CookiesType.AuthKey, cookie);
2019-06-07 08:59:07 +00:00
}
}
2022-03-17 15:13:38 +00:00
public void ResetTenantCookie()
{
var tenant = TenantManager.GetCurrentTenant();
if (!UserManager.IsUserInGroup(SecurityContext.CurrentAccount.ID, Constants.GroupAdmin.ID))
{
throw new SecurityException();
}
var settings = TenantCookieSettingsHelper.GetForTenant(tenant.Id);
settings.Index += 1;
TenantCookieSettingsHelper.SetForTenant(tenant.Id, settings);
var cookie = SecurityContext.AuthenticateMe(SecurityContext.CurrentAccount.ID);
SetCookies(CookiesType.AuthKey, cookie);
}
}