diff options
| author | JP Dillingham <jp@dillingham.me> | 2022-10-30 18:50:33 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-10-30 18:50:33 -0500 |
| commit | ea845d19fd3bf1acca08f1062526d1cfe90716f9 (patch) | |
| tree | 238f9ebc9215dddb0517b2311372ce25f17aff85 /src/slskd/Program.cs | |
| parent | 5c19d4fd6a7eb98d83a5f8c7e2c3092cb2b3b62a (diff) | |
| parent | 093763beaea0f891410f83febea67b22005bd406 (diff) | |
| download | slskd-ea845d19fd3bf1acca08f1062526d1cfe90716f9.tar.gz | |
Merge pull request #717 from slskd/tls
Rework API authentication
Diffstat (limited to 'src/slskd/Program.cs')
| -rw-r--r-- | src/slskd/Program.cs | 38 |
1 files changed, 26 insertions, 12 deletions
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 { |
