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

70 lines
2.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
2019-09-10 12:42:15 +00:00
using ASC.Core;
using ASC.Security.Cryptography;
using ASC.Web.Studio.Utility;
2019-09-23 15:36:22 +00:00
2019-09-10 12:42:15 +00:00
using Microsoft.AspNetCore.Authentication;
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)
{
}
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
{
2019-09-26 13:36:53 +00:00
var emailValidationKeyModel = EmailValidationKeyModel.FromRequest(Context.Request);
2019-09-23 15:36:22 +00:00
2019-09-26 13:36:53 +00:00
if (SecurityContext.IsAuthenticated && emailValidationKeyModel.Type != ConfirmType.EmailChange)
2019-09-10 12:42:15 +00:00
{
return Task.FromResult(AuthenticateResult.Success(new AuthenticationTicket(Context.User, new AuthenticationProperties(), Scheme.Name)));
}
2019-09-26 13:36:53 +00:00
var checkKeyResult = emailValidationKeyModel.Validate();
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(CoreContext.TenantManager.GetCurrentTenant().TenantId, emailValidationKeyModel.UiD.Value, claims);
}
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);
}
}
}