diff options
| author | JP Dillingham <jp@dillingham.me> | 2022-10-03 07:55:21 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-10-03 07:55:21 -0500 |
| commit | 454c0d5cfd230d27077557c954169ef22e7ffcea (patch) | |
| tree | f58ec0dd82b235c3123926488ff6aefa4d61dd3a /src/slskd/Core | |
| parent | 78f7309c6db71581a91189526abf2ef7ad0897ac (diff) | |
| parent | 6028ae895cf9832a3ecce4cabdbd73bd98940b73 (diff) | |
| download | slskd-454c0d5cfd230d27077557c954169ef22e7ffcea.tar.gz | |
Merge pull request #674 from slskd/api-keys
Add the ability to authenticate API calls with an API key
Diffstat (limited to 'src/slskd/Core')
| -rw-r--r-- | src/slskd/Core/Options.cs | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 44cc4c71..4c146d32 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -28,6 +28,7 @@ namespace slskd using System.Text.Json.Serialization; using System.Text.RegularExpressions; using FluentFTP; + using NetTools; using slskd.Configuration; using slskd.Validation; using Soulseek.Diagnostics; @@ -127,6 +128,16 @@ namespace slskd public bool GenerateCertificate { get; init; } = false; /// <summary> + /// Gets a value indicating whether to generate a random API key. + /// </summary> + [Argument('k', "generate-api-key")] + [Description("generate random API key for HTTP/S")] + [Obsolete("USed only for documentation; see Program for actual implementation")] + [JsonIgnore] + [YamlIgnore] + public bool GenerateApiKey { get; init; } = false; + + /// <summary> /// Gets a value indicating whether the application should run in debug mode. /// </summary> [Argument('d', "debug")] @@ -1215,6 +1226,12 @@ namespace slskd public JwtOptions Jwt { get; init; } = new JwtOptions(); /// <summary> + /// Gets API keys. + /// </summary> + [Validate] + public Dictionary<string, ApiKeyOptions> ApiKeys { get; init; } = new Dictionary<string, ApiKeyOptions>(); + + /// <summary> /// JWT options. /// </summary> public class JwtOptions @@ -1240,6 +1257,50 @@ namespace slskd [RequiresRestart] public int Ttl { get; init; } = 604800000; } + + /// <summary> + /// API key options. + /// </summary> + public class ApiKeyOptions : IValidatableObject + { + /// <summary> + /// Gets the API key value. + /// </summary> + [Description("API key value")] + [StringLength(255, MinimumLength = 16)] + [Secret] + public string Key { get; init; } + + /// <summary> + /// Gets the comma separated list of CIDRs that are authorized to use the key. + /// </summary> + [Description("An optional comma separated list of CIDRs that are authorized to use the key")] + public string Cidr { get; init; } = "0.0.0.0/0,::/0"; + + /// <summary> + /// Extended validation. + /// </summary> + /// <param name="validationContext"></param> + /// <returns></returns> + public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) + { + var results = new List<ValidationResult>(); + + foreach (var cidr in Cidr.Split(',')) + { + try + { + _ = IPAddressRange.Parse(cidr); + } + catch (Exception ex) + { + results.Add(new ValidationResult($"CIDR {cidr} is invalid: {ex.Message}")); + } + } + + return results; + } + } } /// <summary> |
