jellyfin/Jellyfin.Api/Auth/CustomAuthenticationHandler.cs

90 lines
3.8 KiB
C#
Raw Normal View History

using System.Globalization;
using System.Security.Claims;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
2019-11-24 17:25:43 +00:00
using Jellyfin.Api.Constants;
2020-05-13 02:10:35 +00:00
using Jellyfin.Data.Enums;
using MediaBrowser.Controller.Authentication;
using MediaBrowser.Controller.Net;
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Jellyfin.Api.Auth
{
2019-11-23 19:31:17 +00:00
/// <summary>
/// Custom authentication handler wrapping the legacy authentication.
/// </summary>
public class CustomAuthenticationHandler : AuthenticationHandler<AuthenticationSchemeOptions>
{
private readonly IAuthService _authService;
private readonly ILogger<CustomAuthenticationHandler> _logger;
2019-11-23 19:31:17 +00:00
/// <summary>
/// Initializes a new instance of the <see cref="CustomAuthenticationHandler" /> class.
/// </summary>
/// <param name="authService">The jellyfin authentication service.</param>
/// <param name="options">Options monitor.</param>
/// <param name="logger">The logger.</param>
/// <param name="encoder">The url encoder.</param>
/// <param name="clock">The system clock.</param>
public CustomAuthenticationHandler(
IAuthService authService,
IOptionsMonitor<AuthenticationSchemeOptions> options,
ILoggerFactory logger,
UrlEncoder encoder,
ISystemClock clock) : base(options, logger, encoder, clock)
{
_authService = authService;
_logger = logger.CreateLogger<CustomAuthenticationHandler>();
}
2019-11-23 19:31:17 +00:00
/// <inheritdoc />
2021-05-21 03:56:59 +00:00
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
{
try
{
2021-05-21 03:56:59 +00:00
var authorizationInfo = await _authService.Authenticate(Request).ConfigureAwait(false);
if (!authorizationInfo.HasToken)
{
return AuthenticateResult.NoResult();
}
2020-10-14 23:58:33 +00:00
var role = UserRoles.User;
2020-10-28 14:40:11 +00:00
if (authorizationInfo.IsApiKey || authorizationInfo.User.HasPermission(PermissionKind.IsAdministrator))
{
2020-10-14 23:58:33 +00:00
role = UserRoles.Administrator;
}
var claims = new[]
{
2020-10-14 23:58:33 +00:00
new Claim(ClaimTypes.Name, authorizationInfo.User?.Username ?? string.Empty),
new Claim(ClaimTypes.Role, role),
new Claim(InternalClaimTypes.UserId, authorizationInfo.UserId.ToString("N", CultureInfo.InvariantCulture)),
new Claim(InternalClaimTypes.DeviceId, authorizationInfo.DeviceId),
new Claim(InternalClaimTypes.Device, authorizationInfo.Device),
new Claim(InternalClaimTypes.Client, authorizationInfo.Client),
new Claim(InternalClaimTypes.Version, authorizationInfo.Version),
2020-06-16 20:12:40 +00:00
new Claim(InternalClaimTypes.Token, authorizationInfo.Token),
2020-10-28 14:40:11 +00:00
new Claim(InternalClaimTypes.IsApiKey, authorizationInfo.IsApiKey.ToString(CultureInfo.InvariantCulture))
};
var identity = new ClaimsIdentity(claims, Scheme.Name);
var principal = new ClaimsPrincipal(identity);
var ticket = new AuthenticationTicket(principal, Scheme.Name);
2021-05-21 03:56:59 +00:00
return AuthenticateResult.Success(ticket);
}
catch (AuthenticationException ex)
{
_logger.LogDebug(ex, "Error authenticating with {Handler}", nameof(CustomAuthenticationHandler));
2021-05-21 03:56:59 +00:00
return AuthenticateResult.NoResult();
}
catch (SecurityException ex)
{
2021-05-21 03:56:59 +00:00
return AuthenticateResult.Fail(ex);
}
}
}
}