Move APIKeys to file and allow read vs read/write
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user