2019-09-26 13:36:53 +00:00
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (!SecurityContext.IsAuthenticated)
|
|
|
|
|
{
|
|
|
|
|
SecurityContext.AuthenticateMe(ASC.Core.Configuration.Constants.CoreSystem, claims);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
SecurityContext.AuthenticateMe(SecurityContext.CurrentAccount, claims);
|
|
|
|
|
}
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|