< Summary - Jellyfin

Information
Class: Jellyfin.Api.Auth.CustomAuthenticationHandler
Assembly: Jellyfin.Api
File(s): /srv/git/jellyfin/Jellyfin.Api/Auth/CustomAuthenticationHandler.cs
Line coverage
100%
Covered lines: 4
Uncovered lines: 0
Coverable lines: 4
Total lines: 88
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%

File(s)

/srv/git/jellyfin/Jellyfin.Api/Auth/CustomAuthenticationHandler.cs

#LineLine coverage
 1using System.Globalization;
 2using System.Security.Claims;
 3using System.Text.Encodings.Web;
 4using System.Threading.Tasks;
 5using Jellyfin.Api.Constants;
 6using Jellyfin.Data.Enums;
 7using MediaBrowser.Controller.Authentication;
 8using MediaBrowser.Controller.Net;
 9using Microsoft.AspNetCore.Authentication;
 10using Microsoft.Extensions.Logging;
 11using Microsoft.Extensions.Options;
 12
 13namespace Jellyfin.Api.Auth
 14{
 15    /// <summary>
 16    /// Custom authentication handler wrapping the legacy authentication.
 17    /// </summary>
 18    public class CustomAuthenticationHandler : AuthenticationHandler<AuthenticationSchemeOptions>
 19    {
 20        private readonly IAuthService _authService;
 21        private readonly ILogger<CustomAuthenticationHandler> _logger;
 22
 23        /// <summary>
 24        /// Initializes a new instance of the <see cref="CustomAuthenticationHandler" /> class.
 25        /// </summary>
 26        /// <param name="authService">The jellyfin authentication service.</param>
 27        /// <param name="options">Options monitor.</param>
 28        /// <param name="logger">The logger.</param>
 29        /// <param name="encoder">The url encoder.</param>
 30        public CustomAuthenticationHandler(
 31            IAuthService authService,
 32            IOptionsMonitor<AuthenticationSchemeOptions> options,
 33            ILoggerFactory logger,
 34            UrlEncoder encoder)
 19235            : base(options, logger, encoder)
 36        {
 19237            _authService = authService;
 19238            _logger = logger.CreateLogger<CustomAuthenticationHandler>();
 19239        }
 40
 41        /// <inheritdoc />
 42        protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
 43        {
 44            try
 45            {
 46                var authorizationInfo = await _authService.Authenticate(Request).ConfigureAwait(false);
 47                if (!authorizationInfo.HasToken)
 48                {
 49                    return AuthenticateResult.NoResult();
 50                }
 51
 52                var role = UserRoles.User;
 53                if (authorizationInfo.IsApiKey || authorizationInfo.User.HasPermission(PermissionKind.IsAdministrator))
 54                {
 55                    role = UserRoles.Administrator;
 56                }
 57
 58                var claims = new[]
 59                {
 60                    new Claim(ClaimTypes.Name, authorizationInfo.User?.Username ?? string.Empty),
 61                    new Claim(ClaimTypes.Role, role),
 62                    new Claim(InternalClaimTypes.UserId, authorizationInfo.UserId.ToString("N", CultureInfo.InvariantCul
 63                    new Claim(InternalClaimTypes.DeviceId, authorizationInfo.DeviceId),
 64                    new Claim(InternalClaimTypes.Device, authorizationInfo.Device),
 65                    new Claim(InternalClaimTypes.Client, authorizationInfo.Client),
 66                    new Claim(InternalClaimTypes.Version, authorizationInfo.Version),
 67                    new Claim(InternalClaimTypes.Token, authorizationInfo.Token),
 68                    new Claim(InternalClaimTypes.IsApiKey, authorizationInfo.IsApiKey.ToString(CultureInfo.InvariantCult
 69                };
 70
 71                var identity = new ClaimsIdentity(claims, Scheme.Name);
 72                var principal = new ClaimsPrincipal(identity);
 73                var ticket = new AuthenticationTicket(principal, Scheme.Name);
 74
 75                return AuthenticateResult.Success(ticket);
 76            }
 77            catch (AuthenticationException ex)
 78            {
 79                _logger.LogDebug(ex, "Error authenticating with {Handler}", nameof(CustomAuthenticationHandler));
 80                return AuthenticateResult.NoResult();
 81            }
 82            catch (SecurityException ex)
 83            {
 84                return AuthenticateResult.Fail(ex);
 85            }
 86        }
 87    }
 88}