2021-04-10 07:03:52 -04:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
2021-04-11 12:17:05 -04:00
|
|
|
namespace Jellyfin.Api.Auth.LocalNetworkAccessPolicy
|
2021-04-10 07:03:52 -04:00
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Local access handler.
|
|
|
|
/// </summary>
|
2021-04-11 12:17:05 -04:00
|
|
|
public class LocalNetworkAccessHandler : BaseAuthorizationHandler<LocalNetworkAccessRequirement>
|
2021-04-10 07:03:52 -04:00
|
|
|
{
|
|
|
|
/// <summary>
|
2021-04-11 12:17:05 -04:00
|
|
|
/// Initializes a new instance of the <see cref="LocalNetworkAccessHandler"/> class.
|
2021-04-10 07:03:52 -04:00
|
|
|
/// </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>
|
2021-04-11 12:17:05 -04:00
|
|
|
public LocalNetworkAccessHandler(
|
2021-04-10 07:03:52 -04:00
|
|
|
IUserManager userManager,
|
|
|
|
INetworkManager networkManager,
|
|
|
|
IHttpContextAccessor httpContextAccessor)
|
|
|
|
: base(userManager, networkManager, httpContextAccessor)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2021-04-11 12:17:05 -04:00
|
|
|
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, LocalNetworkAccessRequirement requirement)
|
2021-04-10 07:03:52 -04:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|