2020-06-15 11:49:54 -07:00
|
|
|
using System.Globalization;
|
2019-11-23 08:31:02 -07:00
|
|
|
using System.Security.Claims;
|
|
|
|
using System.Text.Encodings.Web;
|
|
|
|
using System.Threading.Tasks;
|
2019-11-24 10:25:43 -07:00
|
|
|
using Jellyfin.Api.Constants;
|
2020-05-12 19:10:35 -07:00
|
|
|
using Jellyfin.Data.Enums;
|
2020-11-08 08:10:33 -07:00
|
|
|
using MediaBrowser.Controller.Authentication;
|
2019-11-23 08:31:02 -07:00
|
|
|
using MediaBrowser.Controller.Net;
|
|
|
|
using Microsoft.AspNetCore.Authentication;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
|
|
|
namespace Jellyfin.Api.Auth
|
|
|
|
{
|
2019-11-23 12:31:17 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Custom authentication handler wrapping the legacy authentication.
|
|
|
|
/// </summary>
|
2019-11-23 08:31:02 -07:00
|
|
|
public class CustomAuthenticationHandler : AuthenticationHandler<AuthenticationSchemeOptions>
|
|
|
|
{
|
|
|
|
private readonly IAuthService _authService;
|
2020-12-01 06:32:46 -07:00
|
|
|
private readonly ILogger<CustomAuthenticationHandler> _logger;
|
2019-11-23 08:31:02 -07:00
|
|
|
|
2019-11-23 12:31:17 -07: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>
|
2019-11-23 08:31:02 -07:00
|
|
|
public CustomAuthenticationHandler(
|
|
|
|
IAuthService authService,
|
|
|
|
IOptionsMonitor<AuthenticationSchemeOptions> options,
|
|
|
|
ILoggerFactory logger,
|
2023-10-23 15:10:31 -07:00
|
|
|
UrlEncoder encoder)
|
|
|
|
: base(options, logger, encoder)
|
2019-11-23 08:31:02 -07:00
|
|
|
{
|
|
|
|
_authService = authService;
|
2020-12-01 06:32:46 -07:00
|
|
|
_logger = logger.CreateLogger<CustomAuthenticationHandler>();
|
2019-11-23 08:31:02 -07:00
|
|
|
}
|
|
|
|
|
2019-11-23 12:31:17 -07:00
|
|
|
/// <inheritdoc />
|
2021-05-20 20:56:59 -07:00
|
|
|
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
|
2019-11-23 08:31:02 -07:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2021-05-20 20:56:59 -07:00
|
|
|
var authorizationInfo = await _authService.Authenticate(Request).ConfigureAwait(false);
|
2021-11-13 07:27:28 -07:00
|
|
|
if (!authorizationInfo.HasToken)
|
|
|
|
{
|
|
|
|
return AuthenticateResult.NoResult();
|
|
|
|
}
|
|
|
|
|
2020-10-14 16:58:33 -07:00
|
|
|
var role = UserRoles.User;
|
2020-10-28 07:40:11 -07:00
|
|
|
if (authorizationInfo.IsApiKey || authorizationInfo.User.HasPermission(PermissionKind.IsAdministrator))
|
2019-11-23 08:31:02 -07:00
|
|
|
{
|
2020-10-14 16:58:33 -07:00
|
|
|
role = UserRoles.Administrator;
|
2019-11-23 08:31:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
var claims = new[]
|
|
|
|
{
|
2020-10-14 16:58:33 -07:00
|
|
|
new Claim(ClaimTypes.Name, authorizationInfo.User?.Username ?? string.Empty),
|
|
|
|
new Claim(ClaimTypes.Role, role),
|
2020-06-15 11:49:54 -07:00
|
|
|
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 13:12:40 -07:00
|
|
|
new Claim(InternalClaimTypes.Token, authorizationInfo.Token),
|
2020-10-28 07:40:11 -07:00
|
|
|
new Claim(InternalClaimTypes.IsApiKey, authorizationInfo.IsApiKey.ToString(CultureInfo.InvariantCulture))
|
2019-11-23 08:31:02 -07:00
|
|
|
};
|
2020-06-15 11:49:54 -07:00
|
|
|
|
2019-11-23 08:31:02 -07:00
|
|
|
var identity = new ClaimsIdentity(claims, Scheme.Name);
|
|
|
|
var principal = new ClaimsPrincipal(identity);
|
|
|
|
var ticket = new AuthenticationTicket(principal, Scheme.Name);
|
|
|
|
|
2021-05-20 20:56:59 -07:00
|
|
|
return AuthenticateResult.Success(ticket);
|
2019-11-23 08:31:02 -07:00
|
|
|
}
|
2020-05-17 07:48:30 -07:00
|
|
|
catch (AuthenticationException ex)
|
|
|
|
{
|
2020-12-01 06:32:46 -07:00
|
|
|
_logger.LogDebug(ex, "Error authenticating with {Handler}", nameof(CustomAuthenticationHandler));
|
2021-05-20 20:56:59 -07:00
|
|
|
return AuthenticateResult.NoResult();
|
2020-05-17 07:48:30 -07:00
|
|
|
}
|
2019-11-23 08:31:02 -07:00
|
|
|
catch (SecurityException ex)
|
|
|
|
{
|
2021-05-20 20:56:59 -07:00
|
|
|
return AuthenticateResult.Fail(ex);
|
2019-11-23 08:31:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|