From 8fadf7bdd6b253895a273fe6ffc8bd4943f07ee9 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 13 Dec 2025 11:48:45 -0600 Subject: tweak validation --- src/slskd/Core/Options.cs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index ced7c96f..43363654 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -1944,6 +1944,8 @@ namespace slskd /// Gets the primary API key. Defaults to 'Administrator' role and CIDR '0.0.0.0/0,::/0'. /// role and CIDR can be supplied by prefixing the key with 'role=[role];cidr=[cidrs];[key]'. /// + [Argument('k', "api-key")] + [EnvironmentVariable("API_KEY")] [Description("Administrative API key value")] [Secret] [RequiresRestart] @@ -1957,7 +1959,6 @@ namespace slskd public IEnumerable Validate(ValidationContext validationContext) { - // TODO: test me! var results = new List(); if (string.IsNullOrWhiteSpace(ApiKey)) @@ -1976,15 +1977,24 @@ namespace slskd { results.Add(new ValidationResult("API key must be between 16 and 255 characters")); } + + return results; } + /* + if multiple tuples are supplied, verify that: + + * if a tuple starts with 'role=', it's defining a role and it must match one of the Role enum values + * if a tuple starts with 'cidr=', it's defining a list of CIDRs which need to be valid + * if a tuple doesn't start with a prefix, it's the API key itself and it must be between 16 and 255 characters + */ foreach (var tuple in tuples) { if (tuple.StartsWith("role=", StringComparison.OrdinalIgnoreCase)) { var role = tuple.Split('=').LastOrDefault(); - if (!Enum.TryParse(typeof(Role), role, out _)) + if (!Enum.TryParse(typeof(Role), value: role, ignoreCase: true, out _)) { results.Add(new ValidationResult($"API key role must be one of: {string.Join(", ", Enum.GetNames(typeof(Role)))}")); } -- cgit v1.2.3