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

49 lines
1.7 KiB
C#
Raw Normal View History

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