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

58 lines
2.1 KiB
C#
Raw Normal View History

2019-05-30 14:57:15 +00:00
using System.Net;
using System.Security.Authentication;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
2020-02-17 08:58:14 +00:00
2021-09-08 10:16:58 +00:00
using ASC.Common;
using ASC.Core;
2021-09-07 19:58:01 +00:00
using ASC.Web.Core;
2021-09-24 09:08:10 +00:00
using ASC.Web.Core.Helpers;
2020-02-17 08:58:14 +00:00
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
2019-09-10 12:42:15 +00:00
namespace ASC.Api.Core.Auth
{
2021-09-08 10:16:58 +00:00
[Scope]
public class CookieAuthHandler : AuthenticationHandler<AuthenticationSchemeOptions>
{
2021-09-24 09:08:10 +00:00
private AuthorizationHelper AuthorizationHelper { get; }
2021-09-07 19:58:01 +00:00
private SecurityContext SecurityContext { get; }
private CookiesManager CookiesManager { get; }
public CookieAuthHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock) : base(options, logger, encoder, clock)
{
}
2019-09-09 12:56:33 +00:00
//
2021-09-24 09:08:10 +00:00
public CookieAuthHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock,
AuthorizationHelper authorizationHelper,
SecurityContext securityContext,
CookiesManager cookiesManager)
: this(options, logger, encoder, clock)
{
2021-09-24 09:08:10 +00:00
AuthorizationHelper = authorizationHelper;
2019-09-09 12:56:33 +00:00
SecurityContext = securityContext;
2021-09-07 19:58:01 +00:00
CookiesManager = cookiesManager;
}
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
{
2021-09-24 09:08:10 +00:00
var result = AuthorizationHelper.ProcessBasicAuthorization(out _);
2021-09-07 19:58:01 +00:00
if (!result)
{
SecurityContext.Logout();
CookiesManager.ClearCookies(CookiesType.AuthKey);
CookiesManager.ClearCookies(CookiesType.SocketIO);
}
2019-05-31 10:47:47 +00:00
return Task.FromResult(
result ?
AuthenticateResult.Success(new AuthenticationTicket(Context.User, new AuthenticationProperties(), Scheme.Name)) :
2022-01-17 08:22:52 +00:00
AuthenticateResult.Fail(new AuthenticationException(nameof(HttpStatusCode.Unauthorized)))
2019-05-31 10:47:47 +00:00
);
}
}
}