BaseAuthorizationHandler.cs 4.1 KB

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