diff options
| author | BaronGreenback <jimcartlidge@yahoo.co.uk> | 2021-04-10 12:03:52 +0100 |
|---|---|---|
| committer | BaronGreenback <jimcartlidge@yahoo.co.uk> | 2021-04-10 12:03:52 +0100 |
| commit | 5fb7557763566ef4345aa563c4c1b6d81ce3ff57 (patch) | |
| tree | 7507b20629eae57d9743a47bda8a1eb96b35eb5e /Jellyfin.Api/Auth | |
| parent | 221d9373e857d008ac9c1426e856ae3da1200701 (diff) | |
Network Access Policy
Diffstat (limited to 'Jellyfin.Api/Auth')
3 files changed, 70 insertions, 1 deletions
diff --git a/Jellyfin.Api/Auth/BaseAuthorizationHandler.cs b/Jellyfin.Api/Auth/BaseAuthorizationHandler.cs index 7d68aecf9..456f45d97 100644 --- a/Jellyfin.Api/Auth/BaseAuthorizationHandler.cs +++ b/Jellyfin.Api/Auth/BaseAuthorizationHandler.cs @@ -1,4 +1,4 @@ -using System.Security.Claims; +using System.Security.Claims; using Jellyfin.Api.Helpers; using Jellyfin.Data.Enums; using MediaBrowser.Common.Extensions; @@ -37,6 +37,16 @@ namespace Jellyfin.Api.Auth } /// <summary> + /// Gets a value indicating <see cref="INetworkManager"/> being used. + /// </summary> + protected INetworkManager NetworkManager => _networkManager; + + /// <summary> + /// Gets a value indicating the <see cref="HttpContextAccessor"/> being used. + /// </summary> + protected IHttpContextAccessor HttpContextAccessor => _httpContextAccessor; + + /// <summary> /// Validate authenticated claims. /// </summary> /// <param name="claimsPrincipal">Request claims.</param> diff --git a/Jellyfin.Api/Auth/NetworkAccessPolicy/NetworkAccessHandler.cs b/Jellyfin.Api/Auth/NetworkAccessPolicy/NetworkAccessHandler.cs new file mode 100644 index 000000000..e6b33f565 --- /dev/null +++ b/Jellyfin.Api/Auth/NetworkAccessPolicy/NetworkAccessHandler.cs @@ -0,0 +1,48 @@ +using System.Threading.Tasks; +using Jellyfin.Api.Auth; +using MediaBrowser.Common.Extensions; +using MediaBrowser.Common.Net; +using MediaBrowser.Controller.Library; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; + +namespace Jellyfin.Api.Auth.NetworkAccessPolicy +{ + /// <summary> + /// Local access handler. + /// </summary> + public class NetworkAccessHandler : BaseAuthorizationHandler<NetworkAccessRequirement> + { + /// <summary> + /// Initializes a new instance of the <see cref="NetworkAccessHandler"/> class. + /// </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> + public NetworkAccessHandler( + IUserManager userManager, + INetworkManager networkManager, + IHttpContextAccessor httpContextAccessor) + : base(userManager, networkManager, httpContextAccessor) + { + } + + /// <inheritdoc /> + protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, NetworkAccessRequirement requirement) + { + 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; + } + } +} diff --git a/Jellyfin.Api/Auth/NetworkAccessPolicy/NetworkAccessRequirement.cs b/Jellyfin.Api/Auth/NetworkAccessPolicy/NetworkAccessRequirement.cs new file mode 100644 index 000000000..b5431501b --- /dev/null +++ b/Jellyfin.Api/Auth/NetworkAccessPolicy/NetworkAccessRequirement.cs @@ -0,0 +1,11 @@ +using Microsoft.AspNetCore.Authorization; + +namespace Jellyfin.Api.Auth.NetworkAccessPolicy +{ + /// <summary> + /// The local network authorization requirement. + /// </summary> + public class NetworkAccessRequirement : IAuthorizationRequirement + { + } +} |
