2019-05-30 14:57:15 +00:00
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Security.Authentication;
|
|
|
|
|
using System.Security.Claims;
|
2019-05-17 08:32:23 +00:00
|
|
|
|
using System.Text.Encodings.Web;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using ASC.Core;
|
|
|
|
|
using Microsoft.AspNetCore.Authentication;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
|
|
|
|
|
namespace ASC.Web.Api.Handlers
|
|
|
|
|
{
|
|
|
|
|
public class CookieAuthHandler : AuthenticationHandler<AuthenticationSchemeOptions>
|
|
|
|
|
{
|
|
|
|
|
public CookieAuthHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock) : base(options, logger, encoder, clock)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-17 11:58:09 +00:00
|
|
|
|
public CookieAuthHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock, IHttpContextAccessor httpContextAccessor)
|
2019-05-17 08:32:23 +00:00
|
|
|
|
: this(options, logger, encoder, clock)
|
|
|
|
|
{
|
2019-05-17 11:58:09 +00:00
|
|
|
|
Common.HttpContext.Configure(httpContextAccessor);
|
2019-05-17 08:32:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
|
|
|
|
|
{
|
2019-05-17 11:58:09 +00:00
|
|
|
|
var result = SecurityContext.AuthenticateMe(Context.Request.Cookies["asc_auth_key"]);
|
2019-05-17 08:32:23 +00:00
|
|
|
|
|
2019-05-30 14:57:15 +00:00
|
|
|
|
if (!result)
|
|
|
|
|
{
|
|
|
|
|
throw new AuthenticationException(HttpStatusCode.Unauthorized.ToString());
|
|
|
|
|
}
|
|
|
|
|
return Task.FromResult(AuthenticateResult.Success(new AuthenticationTicket(Context.User, new AuthenticationProperties(), Scheme.Name)));
|
2019-05-17 08:32:23 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|