DocSpace-client/common/ASC.Api.Core/Auth/ConfirmAuthHandler.cs

118 lines
4.7 KiB
C#
Raw Normal View History

2019-09-27 12:28:51 +00:00
using System;
using System.Collections.Generic;
2019-09-10 12:42:15 +00:00
using System.Net;
using System.Security.Authentication;
2019-09-23 15:36:22 +00:00
using System.Security.Claims;
2019-09-10 12:42:15 +00:00
using System.Text.Encodings.Web;
using System.Threading.Tasks;
2019-09-23 15:36:22 +00:00
2020-10-22 17:57:18 +00:00
using ASC.Common;
2019-09-10 12:42:15 +00:00
using ASC.Core;
using ASC.Security.Cryptography;
2020-02-17 08:58:14 +00:00
2019-09-10 12:42:15 +00:00
using Microsoft.AspNetCore.Authentication;
2020-07-02 14:11:59 +00:00
using Microsoft.Extensions.DependencyInjection;
2019-09-10 12:42:15 +00:00
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace ASC.Api.Core.Auth
{
2020-10-22 17:57:18 +00:00
[Scope(Additional = typeof(ConfirmAuthHandlerExtension))]
2019-09-10 12:42:15 +00:00
public class ConfirmAuthHandler : AuthenticationHandler<AuthenticationSchemeOptions>
{
private readonly SecurityContext _securityContext;
private readonly UserManager _userManager;
private readonly IServiceProvider _serviceProvider;
public ConfirmAuthHandler(
IOptionsMonitor<AuthenticationSchemeOptions> options,
ILoggerFactory logger,
UrlEncoder encoder,
ISystemClock clock) :
base(options, logger, encoder, clock)
{ }
2019-09-17 12:42:32 +00:00
public ConfirmAuthHandler(
IOptionsMonitor<AuthenticationSchemeOptions> options,
ILoggerFactory logger,
UrlEncoder encoder,
ISystemClock clock,
2019-09-17 12:42:32 +00:00
SecurityContext securityContext,
2021-04-27 17:27:23 +00:00
UserManager userManager,
2020-07-02 14:11:59 +00:00
IServiceProvider serviceProvider) :
2019-09-17 12:42:32 +00:00
base(options, logger, encoder, clock)
2019-09-10 13:31:03 +00:00
{
_securityContext = securityContext;
_userManager = userManager;
_serviceProvider = serviceProvider;
2019-09-10 13:31:03 +00:00
}
2019-09-10 12:42:15 +00:00
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
{
using var scope = _serviceProvider.CreateScope();
2020-10-07 10:45:53 +00:00
var emailValidationKeyHelper = scope.ServiceProvider.GetService<EmailValidationKeyModelHelper>();
var emailValidationKeyModel = emailValidationKeyHelper.GetModel();
2019-09-23 15:36:22 +00:00
2019-09-27 15:53:40 +00:00
if (!emailValidationKeyModel.Type.HasValue)
2019-09-10 12:42:15 +00:00
{
return _securityContext.IsAuthenticated
2019-09-27 15:53:40 +00:00
? Task.FromResult(AuthenticateResult.Success(new AuthenticationTicket(Context.User, new AuthenticationProperties(), Scheme.Name)))
: Task.FromResult(AuthenticateResult.Fail(new AuthenticationException(HttpStatusCode.Unauthorized.ToString())));
2019-09-10 12:42:15 +00:00
}
EmailValidationKeyProvider.ValidationResult checkKeyResult;
try
{
2020-10-07 10:45:53 +00:00
checkKeyResult = emailValidationKeyHelper.Validate(emailValidationKeyModel);
}
catch (ArgumentNullException)
{
checkKeyResult = EmailValidationKeyProvider.ValidationResult.Invalid;
}
2019-09-10 12:42:15 +00:00
2019-09-24 12:27:13 +00:00
var claims = new List<Claim>()
2019-09-23 15:36:22 +00:00
{
2019-09-26 13:36:53 +00:00
new Claim(ClaimTypes.Role, emailValidationKeyModel.Type.ToString())
2019-09-23 15:36:22 +00:00
};
2019-09-27 12:28:51 +00:00
if (checkKeyResult == EmailValidationKeyProvider.ValidationResult.Ok)
2019-09-23 15:36:22 +00:00
{
2021-04-27 17:27:23 +00:00
Guid userId;
if (!_securityContext.IsAuthenticated)
2019-09-27 09:18:48 +00:00
{
2019-09-27 12:28:51 +00:00
if (emailValidationKeyModel.UiD.HasValue && !emailValidationKeyModel.UiD.Equals(Guid.Empty))
2021-04-27 17:27:23 +00:00
userId = emailValidationKeyModel.UiD.Value;
2019-09-27 12:28:51 +00:00
else
{
if(emailValidationKeyModel.Type == Web.Studio.Utility.ConfirmType.EmailActivation
|| emailValidationKeyModel.Type == Web.Studio.Utility.ConfirmType.EmpInvite
|| emailValidationKeyModel.Type == Web.Studio.Utility.ConfirmType.LinkInvite)
2021-04-27 17:27:23 +00:00
userId = ASC.Core.Configuration.Constants.CoreSystem.ID;
else
userId = _userManager.GetUserByEmail(emailValidationKeyModel.Email).ID;
2019-09-27 12:28:51 +00:00
}
2019-09-27 09:18:48 +00:00
}
else
userId = _securityContext.CurrentAccount.ID;
2021-04-27 17:27:23 +00:00
_securityContext.AuthenticateMeWithoutCookie(userId, claims);
2019-09-23 15:36:22 +00:00
}
2019-09-10 12:42:15 +00:00
var result = checkKeyResult switch
{
EmailValidationKeyProvider.ValidationResult.Ok => AuthenticateResult.Success(new AuthenticationTicket(Context.User, new AuthenticationProperties(), Scheme.Name)),
_ => AuthenticateResult.Fail(new AuthenticationException(HttpStatusCode.Unauthorized.ToString()))
};
return Task.FromResult(result);
}
}
2020-10-22 17:57:18 +00:00
public class ConfirmAuthHandlerExtension
{
public static void Register(DIHelper services) =>
2020-10-22 17:57:18 +00:00
services.TryAdd<EmailValidationKeyModelHelper>();
}
2019-09-10 12:42:15 +00:00
}