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

123 lines
4.8 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>
{
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;
2021-04-27 17:27:23 +00:00
UserManager = userManager;
2020-07-02 14:11:59 +00:00
ServiceProvider = serviceProvider;
2019-09-10 13:31:03 +00:00
}
2020-08-12 09:58:08 +00:00
private SecurityContext SecurityContext { get; }
2021-04-27 17:27:23 +00:00
private UserManager UserManager { get; }
private IServiceProvider ServiceProvider { get; }
2019-09-10 12:42:15 +00:00
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
{
2020-07-02 14:11:59 +00:00
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
{
2019-09-27 15:53:40 +00:00
return SecurityContext.IsAuthenticated
? 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;
2019-09-27 12:28:51 +00:00
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
{
2021-07-10 17:27:15 +00:00
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
{
2021-04-27 17:27:23 +00:00
userId = SecurityContext.CurrentAccount.ID;
2019-09-27 09:18:48 +00:00
}
2021-04-27 17:27:23 +00:00
2021-08-18 14:04:16 +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)
{
services.TryAdd<EmailValidationKeyModelHelper>();
}
}
2019-09-10 12:42:15 +00:00
}