diff options
| author | JP Dillingham <jp@dillingham.me> | 2023-01-01 23:10:20 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-01-01 23:10:20 -0600 |
| commit | bf65e2c278d3362f46a5aea3d1beedfe2488c718 (patch) | |
| tree | 95c53677f2fe5c60b1fa1f2f4999a2ea6dff1171 | |
| parent | 0b3b2d8849a0683558141ce2937f71eaf70902c3 (diff) | |
| parent | 88dc5d8dda46b33fc438f0bc5990d11c8d891869 (diff) | |
| download | slskd-bf65e2c278d3362f46a5aea3d1beedfe2488c718.tar.gz | |
Merge pull request #819 from slskd/config
Add ApiKeyOnly auth policy and use it for Relay endpoints
| -rw-r--r-- | config/slskd.example.yml | 2 | ||||
| -rw-r--r-- | src/slskd/Common/Authentication/AuthPolicy.cs | 1 | ||||
| -rw-r--r-- | src/slskd/Program.cs | 7 | ||||
| -rw-r--r-- | src/slskd/Relay/API/Controllers/RelayController.cs | 10 |
4 files changed, 14 insertions, 6 deletions
diff --git a/config/slskd.example.yml b/config/slskd.example.yml index 7c1f3c36..d65ba653 100644 --- a/config/slskd.example.yml +++ b/config/slskd.example.yml @@ -19,7 +19,7 @@ # controller: # address: https://some.site.com:5000 # ignore_certificate_errors: false -# api_key: <a 16-255 character string corresponding to one of the controller's API keys> +# api_key: <a 16-255 character string corresponding to one of the controller's 'readwrite' or 'administrator' API keys> # secret: <a 16-255 character shared secret matching the controller's config for this agent> # downloads: false # # agent config is optional when running in 'controller' mode diff --git a/src/slskd/Common/Authentication/AuthPolicy.cs b/src/slskd/Common/Authentication/AuthPolicy.cs index e355b2e6..040104da 100644 --- a/src/slskd/Common/Authentication/AuthPolicy.cs +++ b/src/slskd/Common/Authentication/AuthPolicy.cs @@ -23,6 +23,7 @@ namespace slskd public static class AuthPolicy { public const string JwtOnly = "Jwt"; + public const string ApiKeyOnly = "ApiKeyOnly"; public const string Any = "Any"; } } diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index c909d7a4..9cae6e02 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -626,6 +626,13 @@ namespace slskd policy.RequireAuthenticatedUser(); }); + options.AddPolicy(AuthPolicy.ApiKeyOnly, policy => + { + policy.RequireRole(Role.ReadWrite.ToString(), Role.Administrator.ToString()); + policy.AuthenticationSchemes.Add(ApiKeyAuthentication.AuthenticationScheme); + policy.RequireAuthenticatedUser(); + }); + options.AddPolicy(AuthPolicy.Any, policy => { policy.AuthenticationSchemes.Add(ApiKeyAuthentication.AuthenticationScheme); diff --git a/src/slskd/Relay/API/Controllers/RelayController.cs b/src/slskd/Relay/API/Controllers/RelayController.cs index 7e7f3e22..7183283f 100644 --- a/src/slskd/Relay/API/Controllers/RelayController.cs +++ b/src/slskd/Relay/API/Controllers/RelayController.cs @@ -57,7 +57,7 @@ namespace slskd.Relay private IOptionsMonitor<Options> OptionsMonitor { get; } [HttpPut("")] - [Authorize(Policy = AuthPolicy.Any)] + [Authorize(Policy = AuthPolicy.JwtOnly)] public async Task<IActionResult> Connect() { if (!OptionsAtStartup.Relay.Enabled || !new[] { RelayMode.Agent, RelayMode.Debug }.Contains(OperationMode)) @@ -70,7 +70,7 @@ namespace slskd.Relay } [HttpDelete("")] - [Authorize(Policy = AuthPolicy.Any)] + [Authorize(Policy = AuthPolicy.JwtOnly)] public async Task<IActionResult> Disconnect() { if (!OptionsAtStartup.Relay.Enabled || !new[] { RelayMode.Agent, RelayMode.Debug }.Contains(OperationMode)) @@ -83,7 +83,7 @@ namespace slskd.Relay } [HttpGet("downloads/{token}")] - [Authorize(Policy = AuthPolicy.Any)] + [Authorize(Policy = AuthPolicy.ApiKeyOnly)] public IActionResult DownloadFile([FromRoute]string token) { if (!OptionsAtStartup.Relay.Enabled || !new[] { RelayMode.Controller, RelayMode.Debug }.Contains(OperationMode)) @@ -132,7 +132,7 @@ namespace slskd.Relay [RequestSizeLimit(10L * 1024L * 1024L * 1024L)] [RequestFormLimits(MultipartBodyLengthLimit = 10L * 1024L * 1024L * 1024L)] [DisableFormValueModelBinding] - [Authorize(Policy = AuthPolicy.Any)] + [Authorize(Policy = AuthPolicy.ApiKeyOnly)] public async Task<IActionResult> UploadFile(string token) { if (!OptionsAtStartup.Relay.Enabled || !new[] { RelayMode.Controller, RelayMode.Debug }.Contains(OperationMode)) @@ -217,7 +217,7 @@ namespace slskd.Relay /// <param name="token">The unique identifier for the request.</param> /// <returns></returns> [HttpPost("shares/{token}")] - [Authorize(Policy = AuthPolicy.Any)] + [Authorize(Policy = AuthPolicy.ApiKeyOnly)] public async Task<IActionResult> UploadShares(string token) { if (!OptionsAtStartup.Relay.Enabled || !new[] { RelayMode.Controller, RelayMode.Debug }.Contains(OperationMode)) |
