BaseAuthorizationHandler.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System.Net;
  2. using System.Security.Claims;
  3. using Jellyfin.Api.Helpers;
  4. using Jellyfin.Data.Enums;
  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. /// Validate authenticated claims.
  38. /// </summary>
  39. /// <param name="claimsPrincipal">Request claims.</param>
  40. /// <param name="ignoreSchedule">Whether to ignore parental control.</param>
  41. /// <param name="localAccessOnly">Whether access is to be allowed locally only.</param>
  42. /// <returns>Validated claim status.</returns>
  43. protected bool ValidateClaims(
  44. ClaimsPrincipal claimsPrincipal,
  45. bool ignoreSchedule = false,
  46. bool localAccessOnly = false)
  47. {
  48. // Ensure claim has userId.
  49. var userId = ClaimHelpers.GetUserId(claimsPrincipal);
  50. if (userId == null)
  51. {
  52. return false;
  53. }
  54. // Ensure userId links to a valid user.
  55. var user = _userManager.GetUserById(userId.Value);
  56. if (user == null)
  57. {
  58. return false;
  59. }
  60. // Ensure user is not disabled.
  61. if (user.HasPermission(PermissionKind.IsDisabled))
  62. {
  63. return false;
  64. }
  65. var ip = NormalizeIp(_httpContextAccessor.HttpContext.Connection.RemoteIpAddress).ToString();
  66. var isInLocalNetwork = _networkManager.IsInLocalNetwork(ip);
  67. // User cannot access remotely and user is remote
  68. if (!user.HasPermission(PermissionKind.EnableRemoteAccess) && !isInLocalNetwork)
  69. {
  70. return false;
  71. }
  72. if (localAccessOnly && !isInLocalNetwork)
  73. {
  74. return false;
  75. }
  76. // User attempting to access out of parental control hours.
  77. if (!ignoreSchedule
  78. && !user.HasPermission(PermissionKind.IsAdministrator)
  79. && !user.IsParentalScheduleAllowed())
  80. {
  81. return false;
  82. }
  83. return true;
  84. }
  85. private static IPAddress NormalizeIp(IPAddress ip)
  86. {
  87. return ip.IsIPv4MappedToIPv6 ? ip.MapToIPv4() : ip;
  88. }
  89. }
  90. }