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

252 lines
8.9 KiB
C#
Raw Normal View History

2019-06-07 08:59:07 +00:00
/*
*
* (c) Copyright Ascensio System Limited 2010-2018
*
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
* In accordance with Section 7(a) of the GNU GPL 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 more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
*
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
*
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
*
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
* relevant author attributions when distributing the software. If the display of the logo in its graphic
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
* in every copy of the program you distribute.
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
*
*/
using System;
using System.Linq;
using System.Security;
using System.Web;
2020-02-17 08:58:14 +00:00
using ASC.Common;
2019-06-07 08:59:07 +00:00
using ASC.Core;
using ASC.Core.Tenants;
using ASC.Core.Users;
2020-02-17 08:58:14 +00:00
2019-07-02 15:30:31 +00:00
using Microsoft.AspNetCore.Http;
2020-02-17 08:58:14 +00:00
2020-02-20 14:57:48 +00:00
2019-06-07 08:59:07 +00:00
using SecurityContext = ASC.Core.SecurityContext;
namespace ASC.Web.Core
{
public enum CookiesType
{
AuthKey,
SocketIO
}
2020-10-19 15:53:15 +00:00
[Scope]
2019-09-09 12:56:33 +00:00
public class CookiesManager
2019-06-07 08:59:07 +00:00
{
private const string AuthCookiesName = "asc_auth_key";
private const string SocketIOCookiesName = "socketio.sid";
2020-08-12 09:58:08 +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; }
2019-09-17 08:07:46 +00:00
public CookiesManager(
IHttpContextAccessor httpContextAccessor,
UserManager userManager,
SecurityContext securityContext,
TenantCookieSettingsHelper tenantCookieSettingsHelper,
TenantManager tenantManager,
CoreBaseSettings coreBaseSettings)
2019-09-09 12:56:33 +00:00
{
HttpContextAccessor = httpContextAccessor;
UserManager = userManager;
SecurityContext = securityContext;
TenantCookieSettingsHelper = tenantCookieSettingsHelper;
2019-09-17 08:07:46 +00:00
TenantManager = tenantManager;
CoreBaseSettings = coreBaseSettings;
2019-09-09 12:56:33 +00:00
}
2019-06-07 08:59:07 +00:00
private static string GetCookiesName(CookiesType type)
{
2019-08-15 15:13:25 +00:00
return type switch
2019-06-07 08:59:07 +00:00
{
2019-08-15 15:13:25 +00:00
CookiesType.AuthKey => AuthCookiesName,
CookiesType.SocketIO => SocketIOCookiesName,
2019-06-07 08:59:07 +00:00
2019-08-15 15:13:25 +00:00
_ => string.Empty,
};
2019-06-07 08:59:07 +00:00
}
2019-09-09 12:56:33 +00:00
public string GetRequestVar(CookiesType type)
2019-06-07 08:59:07 +00:00
{
2019-09-09 12:56:33 +00:00
if (HttpContextAccessor?.HttpContext == null) return "";
2019-06-07 08:59:07 +00:00
2019-09-09 12:56:33 +00:00
var cookie = HttpContextAccessor.HttpContext.Request.Query[GetCookiesName(type)].FirstOrDefault() ?? HttpContextAccessor.HttpContext.Request.Form[GetCookiesName(type)].FirstOrDefault();
2019-06-07 08:59:07 +00:00
2019-09-09 12:56:33 +00:00
return string.IsNullOrEmpty(cookie) ? GetCookies(type) : cookie;
2019-06-07 08:59:07 +00:00
}
2019-09-09 12:56:33 +00:00
public void SetCookies(CookiesType type, string value, bool session = false)
2019-06-07 08:59:07 +00:00
{
2019-09-09 12:56:33 +00:00
if (HttpContextAccessor?.HttpContext == null) return;
2019-06-07 08:59:07 +00:00
var options = new CookieOptions
{
Expires = GetExpiresDate(session)
};
2019-06-07 08:59:07 +00:00
if (type == CookiesType.AuthKey)
{
options.HttpOnly = true;
2019-06-07 08:59:07 +00:00
2019-09-09 12:56:33 +00:00
if (HttpContextAccessor.HttpContext.Request.GetUrlRewriter().Scheme == "https")
{
options.Secure = true;
2021-06-03 09:34:28 +00:00
if (CoreBaseSettings.Personal)
{
options.SameSite = SameSiteMode.None;
}
}
}
2019-06-07 08:59:07 +00:00
2019-09-09 12:56:33 +00:00
HttpContextAccessor.HttpContext.Response.Cookies.Append(GetCookiesName(type), value, options);
2019-06-07 08:59:07 +00:00
}
2019-09-09 12:56:33 +00:00
public void SetCookies(CookiesType type, string value, string domain, bool session = false)
2019-06-07 08:59:07 +00:00
{
2019-09-09 12:56:33 +00:00
if (HttpContextAccessor?.HttpContext == null) return;
2019-06-07 08:59:07 +00:00
var options = new CookieOptions
{
Expires = GetExpiresDate(session),
Domain = domain
};
2019-06-07 08:59:07 +00:00
if (type == CookiesType.AuthKey)
{
options.HttpOnly = true;
2019-06-07 08:59:07 +00:00
2019-09-09 12:56:33 +00:00
if (HttpContextAccessor.HttpContext.Request.GetUrlRewriter().Scheme == "https")
{
options.Secure = true;
2021-06-03 09:34:28 +00:00
if (CoreBaseSettings.Personal)
{
options.SameSite = SameSiteMode.None;
}
}
}
2019-09-09 12:56:33 +00:00
HttpContextAccessor.HttpContext.Response.Cookies.Append(GetCookiesName(type), value, options);
2019-06-07 08:59:07 +00:00
}
2019-09-09 12:56:33 +00:00
public string GetCookies(CookiesType type)
2019-06-07 08:59:07 +00:00
{
2019-09-09 12:56:33 +00:00
if (HttpContextAccessor?.HttpContext != null)
2019-06-07 08:59:07 +00:00
{
var cookieName = GetCookiesName(type);
2019-09-09 12:56:33 +00:00
if (HttpContextAccessor.HttpContext.Request.Cookies.ContainsKey(cookieName))
return HttpContextAccessor.HttpContext.Request.Cookies[cookieName] ?? "";
2019-06-07 08:59:07 +00:00
}
return "";
}
2019-09-09 12:56:33 +00:00
public void ClearCookies(CookiesType type)
2019-06-07 08:59:07 +00:00
{
2019-09-09 12:56:33 +00:00
if (HttpContextAccessor?.HttpContext == null) return;
2019-06-07 08:59:07 +00:00
2019-09-09 12:56:33 +00:00
if (HttpContextAccessor.HttpContext.Request.Cookies.ContainsKey(GetCookiesName(type)))
{
2019-09-09 12:56:33 +00:00
HttpContextAccessor.HttpContext.Response.Cookies.Delete(GetCookiesName(type), new CookieOptions() { Expires = DateTime.Now.AddDays(-3) });
}
2019-06-07 08:59:07 +00:00
}
2021-11-30 19:16:23 +00:00
private DateTime? GetExpiresDate(bool session)
2019-06-07 08:59:07 +00:00
{
2021-11-30 19:16:23 +00:00
DateTime? expires = null;
2019-06-07 08:59:07 +00:00
if (!session)
{
2019-09-17 08:07:46 +00:00
var tenant = TenantManager.GetCurrentTenant().TenantId;
expires = TenantCookieSettingsHelper.GetExpiresTime(tenant);
2019-06-07 08:59:07 +00:00
}
return expires;
}
2019-09-09 12:56:33 +00:00
public void SetLifeTime(int lifeTime)
2019-06-07 08:59:07 +00:00
{
2019-09-17 12:42:32 +00:00
var tenant = TenantManager.GetCurrentTenant();
if (!UserManager.IsUserInGroup(SecurityContext.CurrentAccount.ID, Constants.GroupAdmin.ID))
2019-06-07 08:59:07 +00:00
{
throw new SecurityException();
}
var settings = TenantCookieSettingsHelper.GetForTenant(tenant.TenantId);
2019-06-07 08:59:07 +00:00
if (lifeTime > 0)
{
2019-08-15 14:48:55 +00:00
settings.Index += 1;
2019-06-07 08:59:07 +00:00
settings.LifeTime = lifeTime;
}
else
{
settings.LifeTime = 0;
}
TenantCookieSettingsHelper.SetForTenant(tenant.TenantId, settings);
2019-06-07 08:59:07 +00:00
2019-09-20 15:53:27 +00:00
var cookie = SecurityContext.AuthenticateMe(SecurityContext.CurrentAccount.ID);
2019-06-07 08:59:07 +00:00
2019-09-09 12:56:33 +00:00
SetCookies(CookiesType.AuthKey, cookie);
2019-06-07 08:59:07 +00:00
}
2019-09-09 12:56:33 +00:00
public int GetLifeTime(int tenantId)
2019-06-07 08:59:07 +00:00
{
return TenantCookieSettingsHelper.GetForTenant(tenantId).LifeTime;
2019-06-07 08:59:07 +00:00
}
2019-09-20 15:53:27 +00:00
public void ResetUserCookie(Guid? userId = null)
2019-06-07 08:59:07 +00:00
{
var settings = TenantCookieSettingsHelper.GetForUser(userId ?? SecurityContext.CurrentAccount.ID);
2019-08-15 14:48:55 +00:00
settings.Index += 1;
TenantCookieSettingsHelper.SetForUser(userId ?? SecurityContext.CurrentAccount.ID, settings);
2019-06-07 08:59:07 +00:00
if (!userId.HasValue)
{
2019-09-20 15:53:27 +00:00
var cookie = SecurityContext.AuthenticateMe(SecurityContext.CurrentAccount.ID);
2019-06-07 08:59:07 +00:00
2019-09-09 12:56:33 +00:00
SetCookies(CookiesType.AuthKey, cookie);
2019-06-07 08:59:07 +00:00
}
}
2019-09-09 12:56:33 +00:00
public void ResetTenantCookie()
2019-06-07 08:59:07 +00:00
{
2019-09-17 12:42:32 +00:00
var tenant = TenantManager.GetCurrentTenant();
2019-08-08 09:26:58 +00:00
if (!UserManager.IsUserInGroup(SecurityContext.CurrentAccount.ID, Constants.GroupAdmin.ID))
2019-06-07 08:59:07 +00:00
{
throw new SecurityException();
}
var settings = TenantCookieSettingsHelper.GetForTenant(tenant.TenantId);
2019-08-15 14:48:55 +00:00
settings.Index += 1;
TenantCookieSettingsHelper.SetForTenant(tenant.TenantId, settings);
2019-06-07 08:59:07 +00:00
2019-09-20 15:53:27 +00:00
var cookie = SecurityContext.AuthenticateMe(SecurityContext.CurrentAccount.ID);
2019-09-09 12:56:33 +00:00
SetCookies(CookiesType.AuthKey, cookie);
2019-06-07 08:59:07 +00:00
}
}
}