2019-05-30 14:57:15 +00:00
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Security.Authentication;
|
2019-05-17 08:32:23 +00:00
|
|
|
|
using System.Text.Encodings.Web;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using ASC.Core;
|
|
|
|
|
using Microsoft.AspNetCore.Authentication;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
|
2019-07-09 10:29:53 +00:00
|
|
|
|
namespace ASC.Web.Core
|
2019-05-17 08:32:23 +00:00
|
|
|
|
{
|
|
|
|
|
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-06-06 08:44:38 +00:00
|
|
|
|
|
2019-05-17 08:32:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
|
|
|
|
|
{
|
2019-05-30 15:21:30 +00:00
|
|
|
|
var token = Context.Request.Cookies["asc_auth_key"] ?? Context.Request.Headers["Authorization"];
|
|
|
|
|
var result = SecurityContext.AuthenticateMe(token);
|
2019-05-17 08:32:23 +00:00
|
|
|
|
|
2019-05-31 10:47:47 +00:00
|
|
|
|
return Task.FromResult(
|
|
|
|
|
result ?
|
|
|
|
|
AuthenticateResult.Success(new AuthenticationTicket(Context.User, new AuthenticationProperties(), Scheme.Name)) :
|
|
|
|
|
AuthenticateResult.Fail(new AuthenticationException(HttpStatusCode.Unauthorized.ToString()))
|
|
|
|
|
);
|
2019-05-17 08:32:23 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|