Move APIKeys to file and allow read vs read/write

This commit is contained in:
2023-11-23 11:41:58 -05:00
parent 15f8385005
commit a7045ea9a4
12 changed files with 100 additions and 17 deletions

View File

@@ -2,19 +2,29 @@ namespace TodoApi.Helpers
{
public interface IApiKeyValidator
{
bool Validate(string? apiKey);
bool CanRead(string? apiKey);
bool CanWrite(string? apiKey);
}
public class ApiKeyValidator(List<string>? apiKeys) : IApiKeyValidator
public class ApiKeyValidator(ApiKeys? apiKeys) : IApiKeyValidator
{
private readonly List<string>? _apiKeys = apiKeys;
private readonly ApiKeys _apiKeys = apiKeys ?? new ApiKeys();
public bool Validate(string? apiKey)
public bool CanRead(string? apiKey)
{
if (_apiKeys == null) return false;
if (apiKey == null) return false;
// Verify the provided apiKey is in our configuration
return _apiKeys.Contains(apiKey!.ToLower());
return _apiKeys.ReadOnly.Contains(apiKey, StringComparison.OrdinalIgnoreCase) ||
_apiKeys.ReadWrite.Contains(apiKey, StringComparison.OrdinalIgnoreCase);
}
public bool CanWrite(string? apiKey)
{
if (apiKey == null) return false;
// Verify the provided apiKey is in our configuration
return _apiKeys.ReadWrite.Contains(apiKey, StringComparison.OrdinalIgnoreCase);
}
}
}