31 lines
945 B
C#
31 lines
945 B
C#
namespace TodoApi.Helpers
|
|
{
|
|
public interface IApiKeyValidator
|
|
{
|
|
bool CanRead(string? apiKey);
|
|
bool CanWrite(string? apiKey);
|
|
}
|
|
|
|
public class ApiKeyValidator(ApiKeys? apiKeys) : IApiKeyValidator
|
|
{
|
|
private readonly ApiKeys _apiKeys = apiKeys ?? new ApiKeys();
|
|
|
|
public bool CanRead(string? apiKey)
|
|
{
|
|
if (apiKey == null) return false;
|
|
|
|
// Verify the provided apiKey is in our configuration
|
|
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);
|
|
}
|
|
}
|
|
}
|