From c6b23c8d082a9ead282a622940ffd904865677bf Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 30 Oct 2022 15:25:06 -0500 Subject: add security service, move security stuff --- .../Common/Authentication/ApiKeyAuthentication.cs | 75 ++++------------------ 1 file changed, 14 insertions(+), 61 deletions(-) (limited to 'src/slskd/Common/Authentication/ApiKeyAuthentication.cs') diff --git a/src/slskd/Common/Authentication/ApiKeyAuthentication.cs b/src/slskd/Common/Authentication/ApiKeyAuthentication.cs index 9225b5a8..a41a9d59 100644 --- a/src/slskd/Common/Authentication/ApiKeyAuthentication.cs +++ b/src/slskd/Common/Authentication/ApiKeyAuthentication.cs @@ -20,13 +20,11 @@ using Microsoft.Extensions.Options; namespace slskd.Authentication { using System; - using System.Linq; using System.Security.Principal; using System.Text.Encodings.Web; using System.Threading.Tasks; using Microsoft.AspNetCore.Authentication; using Microsoft.Extensions.Logging; - using NetTools; /// /// API key authentication. @@ -47,23 +45,23 @@ namespace slskd.Authentication /// /// Initializes a new instance of the class. /// - /// The current slskd options snapshot. /// An options monitor. + /// The security service. /// A logger factory. /// A url encoder. /// A system clock interface. public ApiKeyAuthenticationHandler( - IOptionsSnapshot slskdOptionsSnapshot, IOptionsMonitor apiKeyOptionsMonitor, + ISecurityService securityService, ILoggerFactory logger, UrlEncoder urlEncoder, ISystemClock systemClock) : base(apiKeyOptionsMonitor, logger, urlEncoder, systemClock) { - SlskdOptionsSnapshot = slskdOptionsSnapshot; + Security = securityService; } - private IOptionsSnapshot SlskdOptionsSnapshot { get; } + private ISecurityService Security { get; } /// /// Authenticates via API key. @@ -73,49 +71,25 @@ namespace slskd.Authentication { await Task.Yield(); - var slskdOptions = SlskdOptionsSnapshot.Value; - var apiKeyOptions = OptionsMonitor.CurrentValue; - - string key = default; - - if (Request.Headers.TryGetValue("X-API-Key", out var headerKeyValue)) - { - key = headerKeyValue; - } - else if (apiKeyOptions.EnableSignalRSupport && Request.Path.StartsWithSegments(apiKeyOptions.SignalRRoutePrefix) && Request.Query.ContainsKey("access_token")) - { - // assign the request token from the access_token query parameter - // but only if the destination is a SignalR hub - // https://docs.microsoft.com/en-us/aspnet/core/signalr/authn-and-authz?view=aspnetcore-5.0 - key = Request.Query["access_token"]; - } - else + if (!Request.Headers.TryGetValue("X-API-Key", out var headerKeyValue)) { return AuthenticateResult.NoResult(); } - var matchingRecord = slskdOptions.Web.Authentication.ApiKeys - .FirstOrDefault(kvp => kvp.Value.Key == key); - - if (matchingRecord.Key == null) + try { - return AuthenticateResult.Fail(new Exception("The provided API key does not match an existing key")); - } + var key = Security.AuthenticateWithApiKey(headerKeyValue, Request.HttpContext.Connection.RemoteIpAddress); - var ip = Request.HttpContext.Connection.RemoteIpAddress; + var identity = new GenericIdentity(key.Name); + var principal = new GenericPrincipal(identity, new[] { key.Role.ToString() }); + var ticket = new AuthenticationTicket(principal, new AuthenticationProperties(), ApiKeyAuthentication.AuthenticationScheme); - if (!matchingRecord.Value.Cidr.Split(',') - .Select(cidr => IPAddressRange.Parse(cidr)) - .Any(range => range.Contains(ip))) + return AuthenticateResult.Success(ticket); + } + catch (Exception ex) { - return AuthenticateResult.Fail(new Exception("The remote IP address is not within the range specified for the key")); + return AuthenticateResult.Fail(ex); } - - var identity = new GenericIdentity(matchingRecord.Key); - var principal = new GenericPrincipal(identity, new[] { apiKeyOptions.Role.ToString() }); - var ticket = new AuthenticationTicket(principal, new AuthenticationProperties(), ApiKeyAuthentication.AuthenticationScheme); - - return AuthenticateResult.Success(ticket); } } @@ -124,26 +98,5 @@ namespace slskd.Authentication /// public class ApiKeyAuthenticationOptions : AuthenticationSchemeOptions { - /// - /// Initializes a new instance of the class. - /// - public ApiKeyAuthenticationOptions() - { - } - - /// - /// Gets or sets the route prefix used to identify SignalR authentication attempts. - /// - public string SignalRRoutePrefix { get; set; } - - /// - /// Gets or sets a value indicating whether to support SignalR authentication. - /// - public bool EnableSignalRSupport { get; set; } - - /// - /// Gets or sets the role for authenticated tickets. - /// - public Role Role { get; set; } = Role.Administrator; } } -- cgit v1.2.3