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