aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Api/Auth/CustomAuthenticationHandler.cs
diff options
context:
space:
mode:
authorClaus Vium <clausvium@gmail.com>2019-11-23 16:31:02 +0100
committerClaus Vium <clausvium@gmail.com>2019-11-23 16:31:02 +0100
commit3f651de24c76f9980fac690e51fa93b3d1163f72 (patch)
treebf6dea80f4a76c2f3e7147918ee56460b0cdc181 /Jellyfin.Api/Auth/CustomAuthenticationHandler.cs
parent05b7e2280843f25e48c2300b135f171aee0a54ea (diff)
Add authentication and remove versioning
Diffstat (limited to 'Jellyfin.Api/Auth/CustomAuthenticationHandler.cs')
-rw-r--r--Jellyfin.Api/Auth/CustomAuthenticationHandler.cs53
1 files changed, 53 insertions, 0 deletions
diff --git a/Jellyfin.Api/Auth/CustomAuthenticationHandler.cs b/Jellyfin.Api/Auth/CustomAuthenticationHandler.cs
new file mode 100644
index 000000000..bb6192b03
--- /dev/null
+++ b/Jellyfin.Api/Auth/CustomAuthenticationHandler.cs
@@ -0,0 +1,53 @@
+using System.Security.Claims;
+using System.Text.Encodings.Web;
+using System.Threading.Tasks;
+using MediaBrowser.Controller.Net;
+using Microsoft.AspNetCore.Authentication;
+using Microsoft.Extensions.Logging;
+using Microsoft.Extensions.Options;
+
+namespace Jellyfin.Api.Auth
+{
+ public class CustomAuthenticationHandler : AuthenticationHandler<AuthenticationSchemeOptions>
+ {
+ private readonly IAuthService _authService;
+
+ public CustomAuthenticationHandler(
+ IAuthService authService,
+ IOptionsMonitor<AuthenticationSchemeOptions> options,
+ ILoggerFactory logger,
+ UrlEncoder encoder,
+ ISystemClock clock) : base(options, logger, encoder, clock)
+ {
+ _authService = authService;
+ }
+
+ protected override Task<AuthenticateResult> HandleAuthenticateAsync()
+ {
+ var authenticatedAttribute = new AuthenticatedAttribute();
+ try
+ {
+ var user = _authService.Authenticate(Request, authenticatedAttribute);
+ if (user == null)
+ {
+ return Task.FromResult(AuthenticateResult.Fail("Invalid user"));
+ }
+
+ var claims = new[]
+ {
+ new Claim(ClaimTypes.Name, user.Name),
+ new Claim(ClaimTypes.Role, user.Policy.IsAdministrator ? "Administrator" : "User"),
+ };
+ var identity = new ClaimsIdentity(claims, Scheme.Name);
+ var principal = new ClaimsPrincipal(identity);
+ var ticket = new AuthenticationTicket(principal, Scheme.Name);
+
+ return Task.FromResult(AuthenticateResult.Success(ticket));
+ }
+ catch (SecurityException ex)
+ {
+ return Task.FromResult(AuthenticateResult.Fail(ex));
+ }
+ }
+ }
+}