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

131 lines
5.1 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-02-17 08:58:14 +00:00
using ASC.Common;
2019-09-10 12:42:15 +00:00
using ASC.Core;
using ASC.Security.Cryptography;
using ASC.Web.Studio.Core;
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
{
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,
2019-09-23 12:20:08 +00:00
EmailValidationKeyProvider emailValidationKeyProvider,
SetupInfo setupInfo,
TenantManager tenantManager,
UserManager userManager,
AuthManager authManager,
2020-07-02 14:11:59 +00:00
AuthContext authContext,
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;
2019-09-17 12:42:32 +00:00
EmailValidationKeyProvider = emailValidationKeyProvider;
2019-09-23 12:20:08 +00:00
SetupInfo = setupInfo;
TenantManager = tenantManager;
UserManager = userManager;
AuthManager = authManager;
AuthContext = authContext;
2020-07-02 14:11:59 +00:00
ServiceProvider = serviceProvider;
2019-09-10 13:31:03 +00:00
}
public SecurityContext SecurityContext { get; }
2019-09-17 12:42:32 +00:00
public EmailValidationKeyProvider EmailValidationKeyProvider { get; }
2019-09-23 12:20:08 +00:00
public SetupInfo SetupInfo { get; }
public TenantManager TenantManager { get; }
public UserManager UserManager { get; }
public AuthManager AuthManager { get; }
public AuthContext AuthContext { get; }
2020-07-02 14:11:59 +00:00
public 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();
var emailValidationKeyModel = scope.ServiceProvider.GetService<EmailValidationKeyModel>();
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-07-02 14:11:59 +00:00
checkKeyResult = emailValidationKeyModel.Validate();
}
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
{
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))
{
SecurityContext.AuthenticateMe(emailValidationKeyModel.UiD.Value, claims);
2019-09-27 12:28:51 +00:00
}
else
{
SecurityContext.AuthenticateMe(ASC.Core.Configuration.Constants.CoreSystem, claims);
}
2019-09-27 09:18:48 +00:00
}
else
{
2019-09-27 12:28:51 +00:00
SecurityContext.AuthenticateMe(SecurityContext.CurrentAccount, claims);
2019-09-27 09:18:48 +00:00
}
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);
}
}
2019-11-01 09:37:02 +00:00
public static class ConfirmAuthHandlerExtension
{
2020-02-17 08:58:14 +00:00
public static DIHelper AddConfirmAuthHandler(this DIHelper services)
2019-11-01 09:37:02 +00:00
{
return services
.AddSecurityContextService()
.AddEmailValidationKeyProviderService()
.AddSetupInfo()
.AddTenantManagerService()
.AddUserManagerService()
.AddAuthManager()
.AddAuthContextService();
}
}
2019-09-10 12:42:15 +00:00
}