2020-06-15 18:49:54 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2020-08-06 14:17:45 +00:00
|
|
|
|
using Jellyfin.Api.Constants;
|
2020-06-15 18:49:54 +00:00
|
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
|
2020-08-06 14:17:45 +00:00
|
|
|
|
namespace Jellyfin.Api.Auth.LocalAccessOrRequiresElevationPolicy
|
2020-06-15 18:49:54 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2020-08-06 23:59:48 +00:00
|
|
|
|
/// Local access or require elevated privileges handler.
|
2020-06-15 18:49:54 +00:00
|
|
|
|
/// </summary>
|
2020-08-06 14:17:45 +00:00
|
|
|
|
public class LocalAccessOrRequiresElevationHandler : BaseAuthorizationHandler<LocalAccessOrRequiresElevationRequirement>
|
2020-06-15 18:49:54 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2020-08-06 14:17:45 +00:00
|
|
|
|
/// Initializes a new instance of the <see cref="LocalAccessOrRequiresElevationHandler"/> class.
|
2020-06-15 18:49:54 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param>
|
|
|
|
|
/// <param name="networkManager">Instance of the <see cref="INetworkManager"/> interface.</param>
|
|
|
|
|
/// <param name="httpContextAccessor">Instance of the <see cref="IHttpContextAccessor"/> interface.</param>
|
2020-08-06 14:17:45 +00:00
|
|
|
|
public LocalAccessOrRequiresElevationHandler(
|
2020-06-15 18:49:54 +00:00
|
|
|
|
IUserManager userManager,
|
|
|
|
|
INetworkManager networkManager,
|
|
|
|
|
IHttpContextAccessor httpContextAccessor)
|
|
|
|
|
: base(userManager, networkManager, httpContextAccessor)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-08-06 14:17:45 +00:00
|
|
|
|
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, LocalAccessOrRequiresElevationRequirement requirement)
|
2020-06-15 18:49:54 +00:00
|
|
|
|
{
|
2020-08-06 14:17:45 +00:00
|
|
|
|
var validated = ValidateClaims(context.User, localAccessOnly: true);
|
|
|
|
|
if (validated || context.User.IsInRole(UserRoles.Administrator))
|
|
|
|
|
{
|
|
|
|
|
context.Succeed(requirement);
|
|
|
|
|
}
|
|
|
|
|
else
|
2020-06-15 18:49:54 +00:00
|
|
|
|
{
|
|
|
|
|
context.Fail();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|