diff options
| author | JP Dillingham <jp@dillingham.me> | 2022-10-30 17:39:39 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2022-10-30 17:39:39 -0500 |
| commit | dda92d4efc1c4b6d83a5a2a2a1fe637f6d83f58f (patch) | |
| tree | 5c6ae48fdb3c34ed818ce848d508941bdc1f3c6c | |
| parent | 70d99495146fed87fad93aae593acf0ee686f9b9 (diff) | |
| download | slskd-dda92d4efc1c4b6d83a5a2a2a1fe637f6d83f58f.tar.gz | |
fix signalR api key support
| -rw-r--r-- | src/slskd/Common/CommonExtensions.cs | 9 | ||||
| -rw-r--r-- | src/slskd/Program.cs | 38 |
2 files changed, 35 insertions, 12 deletions
diff --git a/src/slskd/Common/CommonExtensions.cs b/src/slskd/Common/CommonExtensions.cs index 9cf80a4a..29383fcb 100644 --- a/src/slskd/Common/CommonExtensions.cs +++ b/src/slskd/Common/CommonExtensions.cs @@ -20,6 +20,7 @@ namespace slskd using System; using System.Collections; using System.Collections.Generic; + using System.IdentityModel.Tokens.Jwt; using System.IO; using System.Linq; using System.Numerics; @@ -178,6 +179,14 @@ namespace slskd } /// <summary> + /// Serializes the JWT. + /// </summary> + /// <param name="jwt">The JWT.</param> + /// <returns>The serialized string.</returns> + public static string Serialize(this JwtSecurityToken jwt) + => new JwtSecurityTokenHandler().WriteToken(jwt); + + /// <summary> /// Formats byte to nearest size (KB, MB, etc.) /// </summary> /// <param name="value">The value to format.</param> diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index ea0b382f..eefbe8c6 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -584,11 +584,12 @@ namespace slskd using var runtimeMetrics = DotNetRuntimeStatsBuilder.Default().StartCollecting(); services.AddDataProtection() - .PersistKeysToFileSystem(new DirectoryInfo(Path.Combine(DataDirectory, ".DataProtection-Keys"))); + .PersistKeysToFileSystem(new DirectoryInfo(Path.Combine(DataDirectory, "misc", ".DataProtection-Keys"))); var jwtSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(OptionsAtStartup.Web.Authentication.Jwt.Key)); services.AddSingleton(jwtSigningKey); + services.AddSingleton<ISecurityService, SecurityService>(); if (!OptionsAtStartup.Web.Authentication.Disabled) { @@ -596,6 +597,7 @@ namespace slskd { options.AddPolicy(AuthPolicy.JwtOnly, policy => { + policy.RequireRole(Role.Administrator.ToString()); policy.AuthenticationSchemes.Add(JwtBearerDefaults.AuthenticationScheme); policy.RequireAuthenticatedUser(); }); @@ -628,24 +630,36 @@ namespace slskd { OnMessageReceived = context => { - // 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 - if (context.HttpContext.Request.Path.StartsWithSegments("/hub")) + // signalr authentication is stupid + if (context.Request.Path.StartsWithSegments("/hub")) { - context.Token = context.Request.Query["access_token"]; + // some clients (like the one used for agents) provide the token in a header + // in this case it'll be an API key, presented as a bearer token + if (context.Request.Headers.ContainsKey("Authorization")) + { + var key = context.Request.Headers["Authorization"].ToString().Split(' ')?[1]; + + // derive the key name and role from the key value + var service = services.BuildServiceProvider().GetRequiredService<ISecurityService>(); + var (name, role) = service.AuthenticateWithApiKey(key, callerIpAddress: context.HttpContext.Connection.RemoteIpAddress); + + // create a new, short lived jwt for the key name and role + context.Token = service.GenerateJwt(name, role, ttl: 1000).Serialize(); + } + else if (context.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 + context.Token = context.Request.Query["access_token"]; + } } return Task.CompletedTask; }, }; }) - .AddScheme<ApiKeyAuthenticationOptions, ApiKeyAuthenticationHandler>(ApiKeyAuthentication.AuthenticationScheme, options => - { - options.EnableSignalRSupport = true; - options.SignalRRoutePrefix = "/hub"; - options.Role = Role.Administrator; - }); + .AddScheme<ApiKeyAuthenticationOptions, ApiKeyAuthenticationHandler>(ApiKeyAuthentication.AuthenticationScheme, (_) => { }); } else { |
