Compare commits
2
Commits
a87ea5d64d
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2e8f269ae6 | ||
|
|
c4e430a98f |
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using xIdentityHelper;
|
||||
using System.Threading;
|
||||
using xCommons.Attributes;
|
||||
@@ -17,6 +18,13 @@ namespace xIds.Controllers
|
||||
{
|
||||
public partial class ApplicationsController
|
||||
{
|
||||
/// <summary>
|
||||
/// Create Api Key ...
|
||||
/// </summary>
|
||||
/// <param name="applicationId"></param>
|
||||
/// <param name="request"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[RequireXPowered]
|
||||
[Authorize(LocalApi.PolicyName)]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
@@ -56,14 +64,34 @@ namespace xIds.Controllers
|
||||
}
|
||||
|
||||
//
|
||||
// Create Specified Api Key ...
|
||||
// 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,
|
||||
scopes: request.Scopes,
|
||||
applicationId: applicationId,
|
||||
rateLimit: request.RateLimit,
|
||||
allowedIPs: request.AllowedIPs,
|
||||
expiration: request.Expiration,
|
||||
expiration: expirationTimeSpan,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
@@ -78,6 +106,13 @@ namespace xIds.Controllers
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove Specified Api Key ...
|
||||
/// </summary>
|
||||
/// <param name="apiKeyId"></param>
|
||||
/// <param name="applicationId"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[RequireXPowered]
|
||||
[Authorize(LocalApi.PolicyName)]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
@@ -117,7 +152,6 @@ namespace xIds.Controllers
|
||||
}
|
||||
|
||||
//
|
||||
// Create Specified Api Key ...
|
||||
var result = await provider.RemoveApiKeyAsync(
|
||||
id: apiKeyId,
|
||||
softDelete: false,
|
||||
@@ -136,6 +170,14 @@ namespace xIds.Controllers
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rotate Specified Api Key ...
|
||||
/// </summary>
|
||||
/// <param name="apiKeyId"></param>
|
||||
/// <param name="applicationId"></param>
|
||||
/// <param name="request"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[RequireXPowered]
|
||||
[Authorize(LocalApi.PolicyName)]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
@@ -192,7 +234,6 @@ namespace xIds.Controllers
|
||||
}
|
||||
|
||||
//
|
||||
// Create Specified Api Key ...
|
||||
var result = await provider.RotateApiKey(
|
||||
apiKeyId: apiKeyId,
|
||||
userInfo: userInfo,
|
||||
@@ -211,19 +252,75 @@ namespace xIds.Controllers
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Revoke Specified Api Key ...
|
||||
/// </summary>
|
||||
/// <param name="apiKeyId"></param>
|
||||
/// <param name="applicationId"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[RequireXPowered]
|
||||
[Authorize(LocalApi.PolicyName)]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
[HttpPost("Applications/{applicationId}/ApiKeys/{apiKeyId}/Revoke")]
|
||||
public async Task<ActionResult<XApiKeyDto>> RevokeApiKey(
|
||||
public async Task<ActionResult<bool>> RevokeApiKey(
|
||||
[FromRoute] Guid apiKeyId,
|
||||
[FromRoute] int applicationId,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (
|
||||
apiKeyId.IsDefaultGuid() ||
|
||||
!applicationId.IsValidIntId())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var userInfo = await GetUserInfo();
|
||||
|
||||
//
|
||||
// Retrieve Application for Validating ...
|
||||
var application = await provider.GetApplicationAsync(
|
||||
id: applicationId,
|
||||
userInfo: userInfo,
|
||||
includeBuilder: null,
|
||||
ignoreSoftDeleteds: true
|
||||
);
|
||||
if (application.IsNullOrDefault())
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var result = await provider.RevokeApiKey(
|
||||
apiKeyId: apiKeyId,
|
||||
userInfo: userInfo,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve Specified Application's Api Keys ...
|
||||
/// </summary>
|
||||
/// <param name="applicationId"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[RequireXPowered]
|
||||
[Authorize(LocalApi.PolicyName)]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
@@ -233,33 +330,176 @@ namespace xIds.Controllers
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (!applicationId.IsValidIntId())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var userInfo = await GetUserInfo();
|
||||
|
||||
//
|
||||
// Retrieve Application for Validating ...
|
||||
var application = await provider.GetApplicationAsync(
|
||||
id: applicationId,
|
||||
userInfo: userInfo,
|
||||
includeBuilder: null,
|
||||
ignoreSoftDeleteds: true
|
||||
);
|
||||
if (application.IsNullOrDefault())
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var result = await provider.GetApplicationApiKeys(
|
||||
userInfo: userInfo,
|
||||
applicationId: applicationId,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve Specified Api Keys Usages ...
|
||||
/// </summary>
|
||||
/// <param name="apiKeyId"></param>
|
||||
/// <param name="applicationId"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[RequireXPowered]
|
||||
[Authorize(LocalApi.PolicyName)]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
[HttpPost("Applications/{applicationId}/ApiKeys/{apiKeyId}/Usages")]
|
||||
public async Task<ActionResult<XApiKeyDto>> GetApiKeyUsages(
|
||||
public async Task<ActionResult<IEnumerable<XApiKeyUsageDto>>> GetApiKeyUsages(
|
||||
[FromRoute] Guid apiKeyId,
|
||||
[FromRoute] int applicationId,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (
|
||||
apiKeyId.IsDefaultGuid() ||
|
||||
!applicationId.IsValidIntId())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var userInfo = await GetUserInfo();
|
||||
|
||||
//
|
||||
// Retrieve Application for Validating ...
|
||||
var application = await provider.GetApplicationAsync(
|
||||
id: applicationId,
|
||||
userInfo: userInfo,
|
||||
includeBuilder: null,
|
||||
ignoreSoftDeleteds: true
|
||||
);
|
||||
if (application.IsNullOrDefault())
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Retrieve ApiKey for Validating ...
|
||||
var apiKey = await provider.GetApiKeyAsync(
|
||||
id: apiKeyId,
|
||||
userInfo: userInfo,
|
||||
includeBuilder: null,
|
||||
ignoreSoftDeleteds: true
|
||||
);
|
||||
if (application.IsNullOrDefault())
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var result = await provider.FindManyApiKeyUsageAsync(
|
||||
userInfo: userInfo,
|
||||
includeBuilder: null,
|
||||
ignoreSoftDeleteds: true,
|
||||
cancellationToken: cancellationToken,
|
||||
predicate: x => x.ApiKeyId == apiKeyId,
|
||||
orderBuilder: x => x.OrderBy(y => y.RequestedOn)
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validate Specifed Api Key ...
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[RequireXPowered]
|
||||
[Authorize(LocalApi.PolicyName)]
|
||||
[HttpPost("Applications/{applicationId}/ApiKeys/{apiKeyId}/Validate")]
|
||||
public async Task<ActionResult<XApiKeyDto>> ValidateApiKey(
|
||||
[FromRoute] Guid apiKeyId,
|
||||
[FromRoute] int applicationId,
|
||||
[HttpPost("Applications/ApiKeys/Validate")]
|
||||
public async Task<ActionResult<XApiKeyValidationResult>> ValidateApiKey(
|
||||
[FromBody] XApiKeyValidationRequest request,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
await ValidationProvider
|
||||
.GroupValidationBuilder()
|
||||
.AddNotNull(request)
|
||||
.AddNotEmpty(request.ApiKey)
|
||||
.AddNotEmpty(request.ClientIP)
|
||||
.ValidateGroupAsync();
|
||||
|
||||
//
|
||||
var userInfo = await GetUserInfo();
|
||||
|
||||
//
|
||||
var result = await provider.ValidateApiKey(
|
||||
userInfo: userInfo,
|
||||
apiKey: request.ApiKey,
|
||||
clientIP: request.ClientIP,
|
||||
requiredScope: request.RequiredScope,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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>
|
||||
@@ -305,7 +324,8 @@ namespace xIds.Providers
|
||||
);
|
||||
|
||||
//
|
||||
if (currentCount >= limitPerMinute) {
|
||||
if (currentCount >= limitPerMinute)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
+4
-2
@@ -29,13 +29,15 @@
|
||||
|
||||
<!-- Local Dependencies -->
|
||||
<ItemGroup>
|
||||
<PackageReference Include="xDashboard.xMessageService" Version="1.0.0" />
|
||||
<PackageReference Include="xDashboard.xStorageService" Version="1.0.0" />
|
||||
<!-- <PackageReference Include="xDashboard.xMessageService" Version="1.0.0" />
|
||||
<PackageReference Include="xDashboard.xStorageService" Version="1.0.0" /> -->
|
||||
</ItemGroup>
|
||||
|
||||
<!-- Project Dependencies -->
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Modules\xIdentityHelper\xIdentityHelper.csproj" />
|
||||
<ProjectReference Include="..\Modules\xMessageService\xMessageService.csproj" />
|
||||
<ProjectReference Include="..\Modules\xStorageService\xStorageService.csproj" />
|
||||
<ProjectReference Include="..\Modules\xIdentityService\xIdentityService.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user