diff --git a/Controllers/Applications/ApplicationsController+Custom.cs b/Controllers/Applications/ApplicationsController+Custom.cs index fa9ae51..88506e0 100644 --- a/Controllers/Applications/ApplicationsController+Custom.cs +++ b/Controllers/Applications/ApplicationsController+Custom.cs @@ -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 ); diff --git a/Providers/XApplicationProvider.cs b/Providers/XApplicationProvider.cs index 7adc86a..9522698 100644 --- a/Providers/XApplicationProvider.cs +++ b/Providers/XApplicationProvider.cs @@ -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 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() + .ReverseMap(); + + // + cfg.CreateMap() + .ReverseMap(); + + // + cfg.CreateMap() + .ReverseMap(); + }; } /// @@ -288,14 +307,14 @@ namespace xIds.Providers /// /// 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) ); diff --git a/Providers/XApplicationProvider/XApplicationProvider+Enricher.cs b/Providers/XApplicationProvider/XApplicationProvider+Enricher.cs index 187d8e0..fe6b93b 100644 --- a/Providers/XApplicationProvider/XApplicationProvider+Enricher.cs +++ b/Providers/XApplicationProvider/XApplicationProvider+Enricher.cs @@ -25,7 +25,7 @@ namespace xIds.Providers ) { // - var result = source.MapConvert(); + var result = source.MapConvert(mapperConfigurationExp); result = await EnrichAsync( source: result, cancellationToken: cancellationToken @@ -88,7 +88,7 @@ namespace xIds.Providers ) { // - var result = source.MapConvert(); + var result = source.MapConvert(mapperConfigurationExp); result = await EnrichAsync( source: result, cancellationToken: cancellationToken @@ -110,7 +110,7 @@ namespace xIds.Providers ) { // - var result = source.MapConvert(); + var result = source.MapConvert(mapperConfigurationExp); result = await EnrichAsync( source: result, cancellationToken: cancellationToken diff --git a/appsettings.Development.json b/appsettings.Development.json index de9781c..81fe6ce 100644 --- a/appsettings.Development.json +++ b/appsettings.Development.json @@ -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,