123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- using System.Threading.Tasks;
- using Jellyfin.Api.Auth;
- using MediaBrowser.Common.Extensions;
- using MediaBrowser.Common.Net;
- using MediaBrowser.Controller.Library;
- using Microsoft.AspNetCore.Authorization;
- using Microsoft.AspNetCore.Http;
- namespace Jellyfin.Api.Auth.NetworkAccessPolicy
- {
- /// <summary>
- /// Local access handler.
- /// </summary>
- public class NetworkAccessHandler : BaseAuthorizationHandler<NetworkAccessRequirement>
- {
- /// <summary>
- /// Initializes a new instance of the <see cref="NetworkAccessHandler"/> 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>
- public NetworkAccessHandler(
- IUserManager userManager,
- INetworkManager networkManager,
- IHttpContextAccessor httpContextAccessor)
- : base(userManager, networkManager, httpContextAccessor)
- {
- }
- /// <inheritdoc />
- protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, NetworkAccessRequirement requirement)
- {
- var ip = HttpContextAccessor.HttpContext?.Connection.RemoteIpAddress;
- // Loopback will be on LAN, so we can accept null.
- if (ip == null || NetworkManager.IsInLocalNetwork(ip))
- {
- context.Succeed(requirement);
- }
- else
- {
- context.Fail();
- }
- return Task.CompletedTask;
- }
- }
- }
|