2020-06-15 14:49:54 -04:00
|
|
|
using System.Globalization;
|
2020-05-17 10:48:30 -04:00
|
|
|
using System.Security.Authentication;
|
2019-11-23 10:31:02 -05:00
|
|
|
using System.Security.Claims;
|
|
|
|
using System.Text.Encodings.Web;
|
|
|
|
using System.Threading.Tasks;
|
2019-11-24 12:25:43 -05:00
|
|
|
using Jellyfin.Api.Constants;
|
2020-05-12 22:10:35 -04:00
|
|
|
using Jellyfin.Data.Enums;
|
2019-11-23 10:31:02 -05:00
|
|
|
using MediaBrowser.Controller.Net;
|
|
|
|
using Microsoft.AspNetCore.Authentication;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
|
|
|
namespace Jellyfin.Api.Auth
|
|
|
|
{
|
2019-11-23 14:31:17 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Custom authentication handler wrapping the legacy authentication.
|
|
|
|
/// </summary>
|
2019-11-23 10:31:02 -05:00
|
|
|
public class CustomAuthenticationHandler : AuthenticationHandler<AuthenticationSchemeOptions>
|
|
|
|
{
|
|
|
|
private readonly IAuthService _authService;
|
|
|
|
|
2019-11-23 14:31:17 -05: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>
|
2019-11-23 10:31:02 -05:00
|
|
|
public CustomAuthenticationHandler(
|
|
|
|
IAuthService authService,
|
|
|
|
IOptionsMonitor<AuthenticationSchemeOptions> options,
|
|
|
|
ILoggerFactory logger,
|
|
|
|
UrlEncoder encoder,
|
|
|
|
ISystemClock clock) : base(options, logger, encoder, clock)
|
|
|
|
{
|
|
|
|
_authService = authService;
|
|
|
|
}
|
|
|
|
|
2019-11-23 14:31:17 -05:00
|
|
|
/// <inheritdoc />
|
2019-11-23 10:31:02 -05:00
|
|
|
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-06-15 14:49:54 -04:00
|
|
|
var authorizationInfo = _authService.Authenticate(Request);
|
|
|
|
if (authorizationInfo == null)
|
2019-11-23 10:31:02 -05:00
|
|
|
{
|
2020-06-01 14:42:59 -04:00
|
|
|
return Task.FromResult(AuthenticateResult.NoResult());
|
|
|
|
// TODO return when legacy API is removed.
|
|
|
|
// Don't spam the log with "Invalid User"
|
|
|
|
// return Task.FromResult(AuthenticateResult.Fail("Invalid user"));
|
2019-11-23 10:31:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var claims = new[]
|
|
|
|
{
|
2020-06-15 14:49:54 -04:00
|
|
|
new Claim(ClaimTypes.Name, authorizationInfo.User.Username),
|
|
|
|
new Claim(ClaimTypes.Role, value: authorizationInfo.User.HasPermission(PermissionKind.IsAdministrator) ? UserRoles.Administrator : UserRoles.User),
|
|
|
|
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 16:12:40 -04:00
|
|
|
new Claim(InternalClaimTypes.Token, authorizationInfo.Token),
|
2019-11-23 10:31:02 -05:00
|
|
|
};
|
2020-06-15 14:49:54 -04:00
|
|
|
|
2019-11-23 10:31:02 -05:00
|
|
|
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));
|
|
|
|
}
|
2020-05-17 10:48:30 -04:00
|
|
|
catch (AuthenticationException ex)
|
|
|
|
{
|
|
|
|
return Task.FromResult(AuthenticateResult.Fail(ex));
|
|
|
|
}
|
2019-11-23 10:31:02 -05:00
|
|
|
catch (SecurityException ex)
|
|
|
|
{
|
|
|
|
return Task.FromResult(AuthenticateResult.Fail(ex));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|