DocSpace-buildtools/web/ASC.Web.Core/Helpers/AuthorizationHelper.cs

68 lines
2.6 KiB
C#
Raw Normal View History

2019-06-07 08:59:07 +00:00
namespace ASC.Web.Core.Helpers
{
2021-09-24 09:08:10 +00:00
[Scope]
2019-06-07 08:59:07 +00:00
public class AuthorizationHelper
{
2020-09-18 07:59:23 +00:00
private IHttpContextAccessor HttpContextAccessor { get; }
private UserManager UserManager { get; }
private SecurityContext SecurityContext { get; }
private PasswordHasher PasswordHasher { get; }
public AuthorizationHelper(
IHttpContextAccessor httpContextAccessor,
UserManager userManager,
SecurityContext securityContext,
PasswordHasher passwordHasher)
2019-06-07 08:59:07 +00:00
{
2019-09-09 12:56:33 +00:00
HttpContextAccessor = httpContextAccessor;
UserManager = userManager;
SecurityContext = securityContext;
2020-09-18 07:59:23 +00:00
PasswordHasher = passwordHasher;
2019-06-07 08:59:07 +00:00
}
2019-09-09 12:56:33 +00:00
public bool ProcessBasicAuthorization(out string authCookie)
2019-06-07 08:59:07 +00:00
{
authCookie = null;
try
{
//Try basic
2021-09-24 09:08:10 +00:00
var authorization = HttpContextAccessor.HttpContext.Request.Cookies["asc_auth_key"] ?? HttpContextAccessor.HttpContext.Request.Headers["Authorization"].ToString();
2019-06-07 08:59:07 +00:00
if (string.IsNullOrEmpty(authorization))
{
return false;
}
authorization = authorization.Trim();
if (0 <= authorization.IndexOf("Basic", 0))
{
var arr = Encoding.ASCII.GetString(Convert.FromBase64String(authorization.Substring(6))).Split(new[] { ':' });
var username = arr[0];
var password = arr[1];
var u = UserManager.GetUserByEmail(username);
if (u != null && u.Id != ASC.Core.Users.Constants.LostUser.Id)
2019-06-07 08:59:07 +00:00
{
2020-09-18 07:59:23 +00:00
var passwordHash = PasswordHasher.GetClientPassword(password);
authCookie = SecurityContext.AuthenticateMe(u.Email, passwordHash);
2019-06-07 08:59:07 +00:00
}
}
else if (0 <= authorization.IndexOf("Bearer", 0))
{
authorization = authorization.Substring("Bearer ".Length);
if (SecurityContext.AuthenticateMe(authorization))
{
authCookie = authorization;
}
}
else
{
if (SecurityContext.AuthenticateMe(authorization))
{
authCookie = authorization;
}
}
}
catch (Exception) { }
return SecurityContext.IsAuthenticated;
}
}
}