2021-04-10 11:03:52 +00:00
|
|
|
using System.Security.Claims;
|
2020-06-15 18:49:54 +00:00
|
|
|
using Jellyfin.Api.Helpers;
|
|
|
|
using Jellyfin.Data.Enums;
|
2020-09-10 12:16:41 +00:00
|
|
|
using MediaBrowser.Common.Extensions;
|
2020-06-15 18:49:54 +00:00
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
|
|
|
namespace Jellyfin.Api.Auth
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Base authorization handler.
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="T">Type of Authorization Requirement.</typeparam>
|
|
|
|
public abstract class BaseAuthorizationHandler<T> : AuthorizationHandler<T>
|
|
|
|
where T : IAuthorizationRequirement
|
|
|
|
{
|
|
|
|
private readonly IUserManager _userManager;
|
|
|
|
private readonly INetworkManager _networkManager;
|
|
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="BaseAuthorizationHandler{T}"/> class.
|
|
|
|
/// </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>
|
|
|
|
protected BaseAuthorizationHandler(
|
|
|
|
IUserManager userManager,
|
|
|
|
INetworkManager networkManager,
|
|
|
|
IHttpContextAccessor httpContextAccessor)
|
|
|
|
{
|
|
|
|
_userManager = userManager;
|
|
|
|
_networkManager = networkManager;
|
|
|
|
_httpContextAccessor = httpContextAccessor;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Validate authenticated claims.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="claimsPrincipal">Request claims.</param>
|
|
|
|
/// <param name="ignoreSchedule">Whether to ignore parental control.</param>
|
|
|
|
/// <param name="localAccessOnly">Whether access is to be allowed locally only.</param>
|
2020-06-19 19:10:10 +00:00
|
|
|
/// <param name="requiredDownloadPermission">Whether validation requires download permission.</param>
|
2020-06-15 18:49:54 +00:00
|
|
|
/// <returns>Validated claim status.</returns>
|
|
|
|
protected bool ValidateClaims(
|
|
|
|
ClaimsPrincipal claimsPrincipal,
|
|
|
|
bool ignoreSchedule = false,
|
2020-06-19 19:10:10 +00:00
|
|
|
bool localAccessOnly = false,
|
|
|
|
bool requiredDownloadPermission = false)
|
2020-06-15 18:49:54 +00:00
|
|
|
{
|
2020-10-28 14:40:11 +00:00
|
|
|
// ApiKey is currently global admin, always allow.
|
|
|
|
var isApiKey = ClaimHelpers.GetIsApiKey(claimsPrincipal);
|
|
|
|
if (isApiKey)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-15 18:49:54 +00:00
|
|
|
// Ensure claim has userId.
|
|
|
|
var userId = ClaimHelpers.GetUserId(claimsPrincipal);
|
2020-07-07 15:10:51 +00:00
|
|
|
if (!userId.HasValue)
|
2020-06-15 18:49:54 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure userId links to a valid user.
|
|
|
|
var user = _userManager.GetUserById(userId.Value);
|
|
|
|
if (user == null)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure user is not disabled.
|
|
|
|
if (user.HasPermission(PermissionKind.IsDisabled))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-05 10:57:01 +00:00
|
|
|
var isInLocalNetwork = _httpContextAccessor.HttpContext != null
|
|
|
|
&& _networkManager.IsInLocalNetwork(_httpContextAccessor.HttpContext.GetNormalizedRemoteIp());
|
|
|
|
|
2020-06-15 18:49:54 +00:00
|
|
|
// User cannot access remotely and user is remote
|
|
|
|
if (!user.HasPermission(PermissionKind.EnableRemoteAccess) && !isInLocalNetwork)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (localAccessOnly && !isInLocalNetwork)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// User attempting to access out of parental control hours.
|
|
|
|
if (!ignoreSchedule
|
|
|
|
&& !user.HasPermission(PermissionKind.IsAdministrator)
|
|
|
|
&& !user.IsParentalScheduleAllowed())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-19 19:10:10 +00:00
|
|
|
// User attempting to download without permission.
|
|
|
|
if (requiredDownloadPermission
|
|
|
|
&& !user.HasPermission(PermissionKind.EnableContentDownloading))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-15 18:49:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|