mirror of
https://github.com/jellyfin/jellyfin.git
synced 2025-12-19 07:23:05 +03:00
49 lines
1.7 KiB
C#
49 lines
1.7 KiB
C#
|
|
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;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|