AnonymousLanAccessHandler.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System.Threading.Tasks;
  2. using MediaBrowser.Common.Extensions;
  3. using MediaBrowser.Common.Net;
  4. using Microsoft.AspNetCore.Authorization;
  5. using Microsoft.AspNetCore.Http;
  6. namespace Jellyfin.Api.Auth.AnonymousLanAccessPolicy
  7. {
  8. /// <summary>
  9. /// LAN access handler. Allows anonymous users.
  10. /// </summary>
  11. public class AnonymousLanAccessHandler : AuthorizationHandler<AnonymousLanAccessRequirement>
  12. {
  13. private readonly INetworkManager _networkManager;
  14. private readonly IHttpContextAccessor _httpContextAccessor;
  15. /// <summary>
  16. /// Initializes a new instance of the <see cref="AnonymousLanAccessHandler"/> class.
  17. /// </summary>
  18. /// <param name="networkManager">Instance of the <see cref="INetworkManager"/> interface.</param>
  19. /// <param name="httpContextAccessor">Instance of the <see cref="IHttpContextAccessor"/> interface.</param>
  20. public AnonymousLanAccessHandler(
  21. INetworkManager networkManager,
  22. IHttpContextAccessor httpContextAccessor)
  23. {
  24. _networkManager = networkManager;
  25. _httpContextAccessor = httpContextAccessor;
  26. }
  27. /// <inheritdoc />
  28. protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, AnonymousLanAccessRequirement requirement)
  29. {
  30. var ip = _httpContextAccessor.HttpContext?.GetNormalizedRemoteIP();
  31. // Loopback will be on LAN, so we can accept null.
  32. if (ip is null || _networkManager.IsInLocalNetwork(ip))
  33. {
  34. context.Succeed(requirement);
  35. }
  36. else
  37. {
  38. context.Fail();
  39. }
  40. return Task.CompletedTask;
  41. }
  42. }
  43. }