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

214 lines
7.8 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;
2019-06-07 08:59:07 +00:00
using ASC.Core;
using ASC.Core.Tenants;
using ASC.Core.Users;
using ASC.Web.Studio.Utility;
2019-07-02 15:30:31 +00:00
using Microsoft.AspNetCore.Http;
2019-06-07 08:59:07 +00:00
using SecurityContext = ASC.Core.SecurityContext;
namespace ASC.Web.Core
{
public enum CookiesType
{
AuthKey,
SocketIO
}
2019-07-02 15:30:31 +00:00
public static class CookiesManager
2019-06-07 08:59:07 +00:00
{
private const string AuthCookiesName = "asc_auth_key";
private const string SocketIOCookiesName = "socketio.sid";
private static string GetCookiesName(CookiesType type)
{
switch (type)
{
case CookiesType.AuthKey: return AuthCookiesName;
case CookiesType.SocketIO: return SocketIOCookiesName;
}
return string.Empty;
}
2019-07-02 15:30:31 +00:00
public static string GetRequestVar(this HttpContext httpContext, CookiesType type)
2019-06-07 08:59:07 +00:00
{
2019-07-02 15:30:31 +00:00
if (httpContext == null) return "";
2019-06-07 08:59:07 +00:00
2019-07-02 15:30:31 +00:00
var cookie = httpContext.Request.Query[GetCookiesName(type)].FirstOrDefault() ?? httpContext.Request.Form[GetCookiesName(type)].FirstOrDefault();
2019-06-07 08:59:07 +00:00
2019-07-02 15:30:31 +00:00
return string.IsNullOrEmpty(cookie) ? httpContext.GetCookies(type) : cookie;
2019-06-07 08:59:07 +00:00
}
2019-07-02 15:30:31 +00:00
public static void SetCookies(this HttpContext httpContext, CookiesType type, string value, bool session = false)
2019-06-07 08:59:07 +00:00
{
2019-07-02 15:30:31 +00:00
if (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
if (httpContext.Request.GetUrlRewriter().Scheme == "https")
{
options.Secure = true;
}
}
2019-06-07 08:59:07 +00:00
httpContext.Response.Cookies.Append(GetCookiesName(type), value, options);
2019-06-07 08:59:07 +00:00
}
2019-07-02 15:30:31 +00:00
public static void SetCookies(this HttpContext httpContext, CookiesType type, string value, string domain, bool session = false)
2019-06-07 08:59:07 +00:00
{
2019-07-02 15:30:31 +00:00
if (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
if (httpContext.Request.GetUrlRewriter().Scheme == "https")
{
options.Secure = true;
}
}
httpContext.Response.Cookies.Append(GetCookiesName(type), value, options);
2019-06-07 08:59:07 +00:00
}
2019-07-02 15:30:31 +00:00
public static string GetCookies(this HttpContext httpContext, CookiesType type)
2019-06-07 08:59:07 +00:00
{
2019-07-02 15:30:31 +00:00
if (httpContext != null)
2019-06-07 08:59:07 +00:00
{
var cookieName = GetCookiesName(type);
if (httpContext.Request.Cookies.ContainsKey(cookieName))
return httpContext.Request.Cookies[cookieName] ?? "";
2019-06-07 08:59:07 +00:00
}
return "";
}
2019-07-02 15:30:31 +00:00
public static void ClearCookies(this HttpContext httpContext, CookiesType type)
2019-06-07 08:59:07 +00:00
{
2019-07-02 15:30:31 +00:00
if (httpContext == null) return;
2019-06-07 08:59:07 +00:00
if (httpContext.Request.Cookies.ContainsKey(GetCookiesName(type)))
{
httpContext.Response.Cookies.Delete(GetCookiesName(type), new CookieOptions() { Expires = DateTime.Now.AddDays(-3) });
}
2019-06-07 08:59:07 +00:00
}
private static DateTime GetExpiresDate(bool session)
{
var expires = DateTime.MinValue;
if (!session)
{
var tenant = CoreContext.TenantManager.GetCurrentTenant().TenantId;
expires = TenantCookieSettings.GetExpiresTime(tenant);
}
return expires;
}
2019-07-02 15:30:31 +00:00
public static void SetLifeTime(this HttpContext httpContext, int lifeTime)
2019-06-07 08:59:07 +00:00
{
2019-08-09 12:28:19 +00:00
var tenant = CoreContext.TenantManager.GetCurrentTenant(httpContext);
2019-08-08 09:26:58 +00:00
if (!CoreContext.UserManager.IsUserInGroup(tenant, SecurityContext.CurrentAccount.ID, Constants.GroupAdmin.ID))
2019-06-07 08:59:07 +00:00
{
throw new SecurityException();
}
2019-08-08 09:26:58 +00:00
var settings = TenantCookieSettings.GetForTenant(tenant.TenantId);
2019-06-07 08:59:07 +00:00
if (lifeTime > 0)
{
settings.Index = settings.Index + 1;
settings.LifeTime = lifeTime;
}
else
{
settings.LifeTime = 0;
}
2019-08-08 09:26:58 +00:00
TenantCookieSettings.SetForTenant(tenant.TenantId, settings);
2019-06-07 08:59:07 +00:00
2019-08-09 12:28:19 +00:00
var cookie = SecurityContext.AuthenticateMe(tenant.TenantId, SecurityContext.CurrentAccount.ID);
2019-06-07 08:59:07 +00:00
2019-07-02 15:30:31 +00:00
httpContext.SetCookies(CookiesType.AuthKey, cookie);
2019-06-07 08:59:07 +00:00
}
public static int GetLifeTime()
{
return TenantCookieSettings.GetForTenant(TenantProvider.CurrentTenantID).LifeTime;
}
2019-08-09 12:28:19 +00:00
public static void ResetUserCookie(this HttpContext httpContext, int tenantId, Guid? userId = null)
2019-06-07 08:59:07 +00:00
{
var settings = TenantCookieSettings.GetForUser(userId ?? SecurityContext.CurrentAccount.ID);
settings.Index = settings.Index + 1;
TenantCookieSettings.SetForUser(userId ?? SecurityContext.CurrentAccount.ID, settings);
if (!userId.HasValue)
{
2019-08-09 12:28:19 +00:00
var cookie = SecurityContext.AuthenticateMe(tenantId, SecurityContext.CurrentAccount.ID);
2019-06-07 08:59:07 +00:00
2019-07-02 15:30:31 +00:00
httpContext.SetCookies(CookiesType.AuthKey, cookie);
2019-06-07 08:59:07 +00:00
}
}
2019-07-02 15:30:31 +00:00
public static void ResetTenantCookie(this HttpContext httpContext)
2019-06-07 08:59:07 +00:00
{
2019-08-09 12:28:19 +00:00
var tenant = CoreContext.TenantManager.GetCurrentTenant(httpContext);
2019-08-08 09:26:58 +00:00
if (!CoreContext.UserManager.IsUserInGroup(tenant, SecurityContext.CurrentAccount.ID, Constants.GroupAdmin.ID))
2019-06-07 08:59:07 +00:00
{
throw new SecurityException();
}
2019-08-08 09:26:58 +00:00
var settings = TenantCookieSettings.GetForTenant(tenant.TenantId);
2019-06-07 08:59:07 +00:00
settings.Index = settings.Index + 1;
2019-08-08 09:26:58 +00:00
TenantCookieSettings.SetForTenant(tenant.TenantId, settings);
2019-06-07 08:59:07 +00:00
2019-08-09 12:28:19 +00:00
var cookie = SecurityContext.AuthenticateMe(tenant.TenantId, SecurityContext.CurrentAccount.ID);
2019-07-02 15:30:31 +00:00
httpContext.SetCookies(CookiesType.AuthKey, cookie);
2019-06-07 08:59:07 +00:00
}
}
}