2021-04-10 13:03:52 +02:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
2021-09-10 11:44:50 +02:00
|
|
|
namespace Jellyfin.Api.Auth.AnonymousLanAccessPolicy
|
2021-04-10 13:03:52 +02:00
|
|
|
{
|
|
|
|
/// <summary>
|
2021-09-10 11:46:08 +02:00
|
|
|
/// LAN access handler. Allows anonymous users.
|
2021-04-10 13:03:52 +02:00
|
|
|
/// </summary>
|
2021-09-10 11:44:50 +02:00
|
|
|
public class AnonymousLanAccessHandler : AuthorizationHandler<AnonymousLanAccessRequirement>
|
2021-04-10 13:03:52 +02:00
|
|
|
{
|
2021-09-10 11:44:50 +02:00
|
|
|
private readonly INetworkManager _networkManager;
|
|
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
|
|
|
|
2021-04-10 13:03:52 +02:00
|
|
|
/// <summary>
|
2021-09-10 11:44:50 +02:00
|
|
|
/// Initializes a new instance of the <see cref="AnonymousLanAccessHandler"/> class.
|
2021-04-10 13:03:52 +02:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="networkManager">Instance of the <see cref="INetworkManager"/> interface.</param>
|
|
|
|
/// <param name="httpContextAccessor">Instance of the <see cref="IHttpContextAccessor"/> interface.</param>
|
2021-09-10 11:44:50 +02:00
|
|
|
public AnonymousLanAccessHandler(
|
2021-04-10 13:03:52 +02:00
|
|
|
INetworkManager networkManager,
|
|
|
|
IHttpContextAccessor httpContextAccessor)
|
|
|
|
{
|
2021-09-10 11:44:50 +02:00
|
|
|
_networkManager = networkManager;
|
|
|
|
_httpContextAccessor = httpContextAccessor;
|
2021-04-10 13:03:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2021-09-10 11:44:50 +02:00
|
|
|
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, AnonymousLanAccessRequirement requirement)
|
2021-04-10 13:03:52 +02:00
|
|
|
{
|
2021-09-10 11:44:50 +02:00
|
|
|
var ip = _httpContextAccessor.HttpContext?.Connection.RemoteIpAddress;
|
2021-04-10 13:03:52 +02:00
|
|
|
|
|
|
|
// Loopback will be on LAN, so we can accept null.
|
2021-09-10 11:44:50 +02:00
|
|
|
if (ip == null || _networkManager.IsInLocalNetwork(ip))
|
2021-04-10 13:03:52 +02:00
|
|
|
{
|
|
|
|
context.Succeed(requirement);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
context.Fail();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|