42 lines
1.2 KiB
C#
42 lines
1.2 KiB
C#
using AspNetCore.Authentication.ApiKey;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using NejCommon.Models;
|
|
|
|
namespace NejCommon.Services;
|
|
|
|
public partial class ApiKeyProvider : IApiKeyProvider, IScopedService
|
|
{
|
|
private readonly ILogger<ApiKeyProvider> _logger;
|
|
|
|
public async Task<IApiKey> ProvideAsync(string key)
|
|
{
|
|
try
|
|
{
|
|
var lookup = ApiKey.ComputeLookupHash(key);
|
|
|
|
var apiKey = await GetApiKeySet().FirstOrDefaultAsync(x => x.LookupHash.SequenceEqual(lookup));
|
|
if (apiKey == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
if (!apiKey.VerifyStrongHash(key))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
apiKey.Key = key;
|
|
|
|
// write your validation implementation here and return an instance of a valid ApiKey or retun null for an invalid key.
|
|
// return await _apiKeyRepository.GetApiKeyAsync(key);
|
|
return apiKey;
|
|
}
|
|
catch (System.Exception exception)
|
|
{
|
|
_logger.LogError(exception, exception.Message);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public static string GenerateKey() => "nej-" + Nanoid.Nanoid.Generate(size: 256);
|
|
} |