From ccc753b045c1869e4e5146122a2603c44db05945 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 2 Oct 2022 17:37:51 -0500 Subject: add signalr support --- .../Common/Authentication/ApiKeyAuthentication.cs | 61 ++++++++++++++++++---- src/slskd/Program.cs | 8 ++- 2 files changed, 58 insertions(+), 11 deletions(-) diff --git a/src/slskd/Common/Authentication/ApiKeyAuthentication.cs b/src/slskd/Common/Authentication/ApiKeyAuthentication.cs index 835f3b23..08cfa3b6 100644 --- a/src/slskd/Common/Authentication/ApiKeyAuthentication.cs +++ b/src/slskd/Common/Authentication/ApiKeyAuthentication.cs @@ -27,22 +27,23 @@ namespace slskd.Authentication using Microsoft.AspNetCore.Authentication; using Microsoft.Extensions.Logging; using NetTools; + using static slskd.Authentication.ApiKeyAuthenticationHandler; /// - /// Passthrough authentication. + /// API key authentication. /// public static class ApiKeyAuthentication { /// - /// Gets the Passthrough authentication scheme name. + /// Gets the API key authentication scheme name. /// public static string AuthenticationScheme { get; } = "API Key"; } /// - /// Handles passthrough authentication. + /// Handles API key authentication. /// - public class ApiKeyAuthenticationHandler : AuthenticationHandler + public class ApiKeyAuthenticationHandler : AuthenticationHandler { /// /// Initializes a new instance of the class. @@ -54,32 +55,44 @@ namespace slskd.Authentication /// A system clock interface. public ApiKeyAuthenticationHandler( IOptionsSnapshot slskdOptionsSnapshot, - IOptionsMonitor apiKeyOptionsMonitor, + IOptionsMonitor apiKeyOptionsMonitor, ILoggerFactory logger, UrlEncoder urlEncoder, ISystemClock systemClock) : base(apiKeyOptionsMonitor, logger, urlEncoder, systemClock) { - Console.WriteLine("init"); SlskdOptionsSnapshot = slskdOptionsSnapshot; } private IOptionsSnapshot SlskdOptionsSnapshot { get; } /// - /// Authenticates using the configured and . + /// Authenticates via API key. /// /// A successful authentication result containing a default ticket. protected override async Task HandleAuthenticateAsync() { await Task.Yield(); - if (!Request.Headers.TryGetValue("X-API-Key", out var key)) + 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")) + { + key = Request.Query["access_token"]; + } + else { return AuthenticateResult.NoResult(); } - var matchingRecord = SlskdOptionsSnapshot.Value.Web.Authentication.ApiKeys + var matchingRecord = slskdOptions.Web.Authentication.ApiKeys .FirstOrDefault(kvp => kvp.Value.Key == key); if (matchingRecord.Key == null) @@ -97,10 +110,38 @@ namespace slskd.Authentication } var identity = new GenericIdentity(matchingRecord.Key); - var principal = new GenericPrincipal(identity, new[] { Role.Administrator.ToString() }); + var principal = new GenericPrincipal(identity, new[] { apiKeyOptions.Role.ToString() }); var ticket = new AuthenticationTicket(principal, new AuthenticationProperties(), ApiKeyAuthentication.AuthenticationScheme); return AuthenticateResult.Success(ticket); } + + /// + /// API key authentication options. + /// + 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; + } } } diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 4d9f6830..7b727308 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -70,6 +70,7 @@ namespace slskd using Soulseek; using Utility.CommandLine; using Utility.EnvironmentVariables; + using static slskd.Authentication.ApiKeyAuthenticationHandler; using IOFile = System.IO.File; /// @@ -612,7 +613,12 @@ namespace slskd }); services.AddAuthentication(ApiKeyAuthentication.AuthenticationScheme) - .AddScheme(ApiKeyAuthentication.AuthenticationScheme, configureOptions: null); + .AddScheme(ApiKeyAuthentication.AuthenticationScheme, options => + { + options.EnableSignalRSupport = true; + options.SignalRRoutePrefix = "/hub"; + options.Role = Role.Administrator; + }); } else { -- cgit v1.2.3