BaseAuthorizationHandler.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System.Security.Claims;
  2. using Jellyfin.Api.Helpers;
  3. using Jellyfin.Data.Enums;
  4. using MediaBrowser.Common.Extensions;
  5. using MediaBrowser.Common.Net;
  6. using MediaBrowser.Controller.Library;
  7. using Microsoft.AspNetCore.Authorization;
  8. using Microsoft.AspNetCore.Http;
  9. namespace Jellyfin.Api.Auth
  10. {
  11. /// <summary>
  12. /// Base authorization handler.
  13. /// </summary>
  14. /// <typeparam name="T">Type of Authorization Requirement.</typeparam>
  15. public abstract class BaseAuthorizationHandler<T> : AuthorizationHandler<T>
  16. where T : IAuthorizationRequirement
  17. {
  18. private readonly IUserManager _userManager;
  19. private readonly INetworkManager _networkManager;
  20. private readonly IHttpContextAccessor _httpContextAccessor;
  21. /// <summary>
  22. /// Initializes a new instance of the <see cref="BaseAuthorizationHandler{T}"/> class.
  23. /// </summary>
  24. /// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param>
  25. /// <param name="networkManager">Instance of the <see cref="INetworkManager"/> interface.</param>
  26. /// <param name="httpContextAccessor">Instance of the <see cref="IHttpContextAccessor"/> interface.</param>
  27. protected BaseAuthorizationHandler(
  28. IUserManager userManager,
  29. INetworkManager networkManager,
  30. IHttpContextAccessor httpContextAccessor)
  31. {
  32. _userManager = userManager;
  33. _networkManager = networkManager;
  34. _httpContextAccessor = httpContextAccessor;
  35. }
  36. /// <summary>
  37. /// Gets a value indicating <see cref="INetworkManager"/> being used.
  38. /// </summary>
  39. protected INetworkManager NetworkManager => _networkManager;
  40. /// <summary>
  41. /// Gets a value indicating the <see cref="HttpContextAccessor"/> being used.
  42. /// </summary>
  43. protected IHttpContextAccessor HttpContextAccessor => _httpContextAccessor;
  44. /// <summary>
  45. /// Validate authenticated claims.
  46. /// </summary>
  47. /// <param name="claimsPrincipal">Request claims.</param>
  48. /// <param name="ignoreSchedule">Whether to ignore parental control.</param>
  49. /// <param name="localAccessOnly">Whether access is to be allowed locally only.</param>
  50. /// <param name="requiredDownloadPermission">Whether validation requires download permission.</param>
  51. /// <returns>Validated claim status.</returns>
  52. protected bool ValidateClaims(
  53. ClaimsPrincipal claimsPrincipal,
  54. bool ignoreSchedule = false,
  55. bool localAccessOnly = false,
  56. bool requiredDownloadPermission = false)
  57. {
  58. // ApiKey is currently global admin, always allow.
  59. var isApiKey = ClaimHelpers.GetIsApiKey(claimsPrincipal);
  60. if (isApiKey)
  61. {
  62. return true;
  63. }
  64. // Ensure claim has userId.
  65. var userId = ClaimHelpers.GetUserId(claimsPrincipal);
  66. if (!userId.HasValue)
  67. {
  68. return false;
  69. }
  70. // Ensure userId links to a valid user.
  71. var user = _userManager.GetUserById(userId.Value);
  72. if (user == null)
  73. {
  74. return false;
  75. }
  76. // Ensure user is not disabled.
  77. if (user.HasPermission(PermissionKind.IsDisabled))
  78. {
  79. return false;
  80. }
  81. var ip = _httpContextAccessor.HttpContext.GetNormalizedRemoteIp();
  82. var isInLocalNetwork = _networkManager.IsInLocalNetwork(ip);
  83. // User cannot access remotely and user is remote
  84. if (!user.HasPermission(PermissionKind.EnableRemoteAccess) && !isInLocalNetwork)
  85. {
  86. return false;
  87. }
  88. if (localAccessOnly && !isInLocalNetwork)
  89. {
  90. return false;
  91. }
  92. // User attempting to access out of parental control hours.
  93. if (!ignoreSchedule
  94. && !user.HasPermission(PermissionKind.IsAdministrator)
  95. && !user.IsParentalScheduleAllowed())
  96. {
  97. return false;
  98. }
  99. // User attempting to download without permission.
  100. if (requiredDownloadPermission
  101. && !user.HasPermission(PermissionKind.EnableContentDownloading))
  102. {
  103. return false;
  104. }
  105. return true;
  106. }
  107. }
  108. }