last ...
This commit is contained in:
@@ -63,6 +63,27 @@ namespace xIds.Controllers
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Noramlize TimeSpan ...
|
||||
var expirationTimeSpan = TimeSpan
|
||||
.FromMinutes(apiKeyConfiguration.DefaultExpirationMinutes);
|
||||
if (
|
||||
request.ExpirationMinutes.HasValue &&
|
||||
request.ExpirationMinutes.Value > 0
|
||||
)
|
||||
{
|
||||
//
|
||||
// Normalize Expiration Minutes ...
|
||||
int minutes = request.ExpirationMinutes.Value;
|
||||
if (minutes > apiKeyConfiguration.MaxExpirationMinutes)
|
||||
{
|
||||
minutes = apiKeyConfiguration.MaxExpirationMinutes;
|
||||
}
|
||||
|
||||
//
|
||||
expirationTimeSpan = TimeSpan.FromMinutes(minutes);
|
||||
}
|
||||
|
||||
//
|
||||
var result = await provider.CreateApiKey(
|
||||
userInfo: userInfo,
|
||||
@@ -70,7 +91,7 @@ namespace xIds.Controllers
|
||||
applicationId: applicationId,
|
||||
rateLimit: request.RateLimit,
|
||||
allowedIPs: request.AllowedIPs,
|
||||
expiration: request.Expiration,
|
||||
expiration: expirationTimeSpan,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ using IdentityServer4.Extensions;
|
||||
using xIdentityModels.Extensions;
|
||||
using Microsoft.EntityFrameworkCore.Query;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using AutoMapper;
|
||||
|
||||
namespace xIds.Providers
|
||||
{
|
||||
@@ -28,6 +29,7 @@ namespace xIds.Providers
|
||||
private readonly IXIdentityManager identityManager;
|
||||
private readonly XDataServiceConfiguration configuration;
|
||||
private readonly XApiKeyConfiguration apiKeyConfiguration;
|
||||
private readonly Action<IMapperConfigurationExpression> mapperConfigurationExp;
|
||||
|
||||
public XApplicationProvider(
|
||||
IMemoryCache cache,
|
||||
@@ -45,6 +47,23 @@ namespace xIds.Providers
|
||||
this.configuration = configuration;
|
||||
this.identityManager = identityManager;
|
||||
this.apiKeyConfiguration = apiKeyConfiguration;
|
||||
|
||||
//
|
||||
// Configure Mapper Options ...
|
||||
mapperConfigurationExp = cfg =>
|
||||
{
|
||||
//
|
||||
cfg.CreateMap<XApiKey, XApiKeyDto>()
|
||||
.ReverseMap();
|
||||
|
||||
//
|
||||
cfg.CreateMap<XApplication, XApplicationDto>()
|
||||
.ReverseMap();
|
||||
|
||||
//
|
||||
cfg.CreateMap<XApiKeyUsage, XApiKeyUsageDto>()
|
||||
.ReverseMap();
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -288,14 +307,14 @@ namespace xIds.Providers
|
||||
/// <param name="limitPerMinute"></param>
|
||||
/// <returns></returns>
|
||||
private bool CheckRateLimit(
|
||||
Guid apiKeyId,
|
||||
Guid apiKeyId,
|
||||
int limitPerMinute
|
||||
)
|
||||
{
|
||||
//
|
||||
var cacheKey = $"ratelimit:{apiKeyId}";
|
||||
var currentCount = cache.GetOrCreate(
|
||||
cacheKey,
|
||||
cacheKey,
|
||||
entry =>
|
||||
{
|
||||
//
|
||||
@@ -305,14 +324,15 @@ namespace xIds.Providers
|
||||
);
|
||||
|
||||
//
|
||||
if (currentCount >= limitPerMinute) {
|
||||
if (currentCount >= limitPerMinute)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
//
|
||||
cache.Set(
|
||||
cacheKey,
|
||||
currentCount + 1,
|
||||
cacheKey,
|
||||
currentCount + 1,
|
||||
TimeSpan.FromMinutes(1)
|
||||
);
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ namespace xIds.Providers
|
||||
)
|
||||
{
|
||||
//
|
||||
var result = source.MapConvert<XApiKeyDto, XApiKey>();
|
||||
var result = source.MapConvert<XApiKeyDto, XApiKey>(mapperConfigurationExp);
|
||||
result = await EnrichAsync(
|
||||
source: result,
|
||||
cancellationToken: cancellationToken
|
||||
@@ -88,7 +88,7 @@ namespace xIds.Providers
|
||||
)
|
||||
{
|
||||
//
|
||||
var result = source.MapConvert<XApiKeyUsageDto, XApiKeyUsage>();
|
||||
var result = source.MapConvert<XApiKeyUsageDto, XApiKeyUsage>(mapperConfigurationExp);
|
||||
result = await EnrichAsync(
|
||||
source: result,
|
||||
cancellationToken: cancellationToken
|
||||
@@ -110,7 +110,7 @@ namespace xIds.Providers
|
||||
)
|
||||
{
|
||||
//
|
||||
var result = source.MapConvert<XApplicationDto, XApplication>();
|
||||
var result = source.MapConvert<XApplicationDto, XApplication>(mapperConfigurationExp);
|
||||
result = await EnrichAsync(
|
||||
source: result,
|
||||
cancellationToken: cancellationToken
|
||||
|
||||
@@ -37,12 +37,12 @@
|
||||
"MaxFileSize": 41943040
|
||||
},
|
||||
"ApiKeyConfiguration": {
|
||||
"ApiPrefix": "xapp",
|
||||
"ApiKeyPrefix": "xapp",
|
||||
"DefaultRateLimit": 60,
|
||||
"EnableAuditLog": false,
|
||||
"MaxKeysPerApplication": "10",
|
||||
"MaxExpirationMinutes": "525600",
|
||||
"DefaultExpirationMinutes": "43200"
|
||||
"MaxKeysPerApplication": 10,
|
||||
"MaxExpirationMinutes": 525600,
|
||||
"DefaultExpirationMinutes": 43200
|
||||
},
|
||||
"DbSeeder": {
|
||||
"UpdateExists": false,
|
||||
|
||||
Reference in New Issue
Block a user