jellyfin-server/Jellyfin.Api/Auth/AnonymousLanAccessPolicy/AnonymousLanAccessHandler.cs

49 lines
1.7 KiB
C#
Raw Normal View History

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;
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>
public class AnonymousLanAccessHandler : AuthorizationHandler<AnonymousLanAccessRequirement>
2021-04-10 11:03:52 +00:00
{
private readonly INetworkManager _networkManager;
private readonly IHttpContextAccessor _httpContextAccessor;
2021-04-10 11:03:52 +00:00
/// <summary>
/// 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>
public AnonymousLanAccessHandler(
2021-04-10 11:03:52 +00:00
INetworkManager networkManager,
IHttpContextAccessor httpContextAccessor)
{
_networkManager = networkManager;
_httpContextAccessor = httpContextAccessor;
2021-04-10 11:03:52 +00:00
}
/// <inheritdoc />
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;
}
}
}