diff options
| author | JP Dillingham <jp@dillingham.me> | 2022-10-02 15:24:50 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2022-10-02 15:24:50 -0500 |
| commit | 30b2b42ccc9a51be4aee4c74b9184f5da1509932 (patch) | |
| tree | eef09958a58c001be389d697466b474ab283c325 /src | |
| parent | 78f7309c6db71581a91189526abf2ef7ad0897ac (diff) | |
| download | slskd-30b2b42ccc9a51be4aee4c74b9184f5da1509932.tar.gz | |
add options for api keys
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Core/Options.cs | 61 | ||||
| -rw-r--r-- | src/slskd/slskd.csproj | 1 |
2 files changed, 62 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> diff --git a/src/slskd/slskd.csproj b/src/slskd/slskd.csproj index f35a6485..1f0973b1 100644 --- a/src/slskd/slskd.csproj +++ b/src/slskd/slskd.csproj @@ -43,6 +43,7 @@ <ItemGroup> <PackageReference Include="FluentFTP" Version="39.0.1" /> + <PackageReference Include="IPAddressRange" Version="4.2.0" /> <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.7" /> <PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="5.0.0" /> <PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="6.0.0"> |
