Compare commits

...
1 Commits
Author SHA1 Message Date
saherelm c4e430a98f last ... 2026-10-09 14:20:47 +03:30
2 changed files with 243 additions and 22 deletions
@@ -1,4 +1,5 @@
using System; using System;
using System.Linq;
using xIdentityHelper; using xIdentityHelper;
using System.Threading; using System.Threading;
using xCommons.Attributes; using xCommons.Attributes;
@@ -17,6 +18,13 @@ namespace xIds.Controllers
{ {
public partial class ApplicationsController public partial class ApplicationsController
{ {
/// <summary>
/// Create Api Key ...
/// </summary>
/// <param name="applicationId"></param>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[RequireXPowered] [RequireXPowered]
[Authorize(LocalApi.PolicyName)] [Authorize(LocalApi.PolicyName)]
[Authorize(Policy = XPolicies.EnabledUser)] [Authorize(Policy = XPolicies.EnabledUser)]
@@ -56,7 +64,6 @@ namespace xIds.Controllers
} }
// //
// Create Specified Api Key ...
var result = await provider.CreateApiKey( var result = await provider.CreateApiKey(
userInfo: userInfo, userInfo: userInfo,
scopes: request.Scopes, scopes: request.Scopes,
@@ -78,6 +85,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] [RequireXPowered]
[Authorize(LocalApi.PolicyName)] [Authorize(LocalApi.PolicyName)]
[Authorize(Policy = XPolicies.EnabledUser)] [Authorize(Policy = XPolicies.EnabledUser)]
@@ -117,7 +131,6 @@ namespace xIds.Controllers
} }
// //
// Create Specified Api Key ...
var result = await provider.RemoveApiKeyAsync( var result = await provider.RemoveApiKeyAsync(
id: apiKeyId, id: apiKeyId,
softDelete: false, softDelete: false,
@@ -136,6 +149,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] [RequireXPowered]
[Authorize(LocalApi.PolicyName)] [Authorize(LocalApi.PolicyName)]
[Authorize(Policy = XPolicies.EnabledUser)] [Authorize(Policy = XPolicies.EnabledUser)]
@@ -192,7 +213,6 @@ namespace xIds.Controllers
} }
// //
// Create Specified Api Key ...
var result = await provider.RotateApiKey( var result = await provider.RotateApiKey(
apiKeyId: apiKeyId, apiKeyId: apiKeyId,
userInfo: userInfo, userInfo: userInfo,
@@ -211,19 +231,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] [RequireXPowered]
[Authorize(LocalApi.PolicyName)] [Authorize(LocalApi.PolicyName)]
[Authorize(Policy = XPolicies.EnabledUser)] [Authorize(Policy = XPolicies.EnabledUser)]
[HttpPost("Applications/{applicationId}/ApiKeys/{apiKeyId}/Revoke")] [HttpPost("Applications/{applicationId}/ApiKeys/{apiKeyId}/Revoke")]
public async Task<ActionResult<XApiKeyDto>> RevokeApiKey( public async Task<ActionResult<bool>> RevokeApiKey(
[FromRoute] Guid apiKeyId, [FromRoute] Guid apiKeyId,
[FromRoute] int applicationId, [FromRoute] int applicationId,
CancellationToken cancellationToken = default 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] [RequireXPowered]
[Authorize(LocalApi.PolicyName)] [Authorize(LocalApi.PolicyName)]
[Authorize(Policy = XPolicies.EnabledUser)] [Authorize(Policy = XPolicies.EnabledUser)]
@@ -233,33 +309,176 @@ namespace xIds.Controllers
CancellationToken cancellationToken = default 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] [RequireXPowered]
[Authorize(LocalApi.PolicyName)] [Authorize(LocalApi.PolicyName)]
[Authorize(Policy = XPolicies.EnabledUser)] [Authorize(Policy = XPolicies.EnabledUser)]
[HttpPost("Applications/{applicationId}/ApiKeys/{apiKeyId}/Usages")] [HttpPost("Applications/{applicationId}/ApiKeys/{apiKeyId}/Usages")]
public async Task<ActionResult<XApiKeyDto>> GetApiKeyUsages( public async Task<ActionResult<IEnumerable<XApiKeyUsageDto>>> GetApiKeyUsages(
[FromRoute] Guid apiKeyId, [FromRoute] Guid apiKeyId,
[FromRoute] int applicationId, [FromRoute] int applicationId,
CancellationToken cancellationToken = default CancellationToken cancellationToken = default
) )
{ {
throw new NotImplementedException(); //
} try
{
[RequireXPowered] //
[Authorize(LocalApi.PolicyName)] // Validate ...
[HttpPost("Applications/{applicationId}/ApiKeys/{apiKeyId}/Validate")] if (
public async Task<ActionResult<XApiKeyDto>> ValidateApiKey( apiKeyId.IsDefaultGuid() ||
[FromRoute] Guid apiKeyId, !applicationId.IsValidIntId())
[FromRoute] int applicationId, {
CancellationToken cancellationToken = default XException.InvalidArgs.Throw();
) }
{
throw new NotImplementedException(); //
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/ApiKeys/Validate")]
public async Task<ActionResult<XApiKeyValidationResult>> ValidateApiKey(
[FromBody] XApiKeyValidationRequest request,
CancellationToken cancellationToken = default
)
{
//
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;
}
}
} }
} }
+4 -2
View File
@@ -29,13 +29,15 @@
<!-- Local Dependencies --> <!-- Local Dependencies -->
<ItemGroup> <ItemGroup>
<PackageReference Include="xDashboard.xMessageService" Version="1.0.0" /> <!-- <PackageReference Include="xDashboard.xMessageService" Version="1.0.0" />
<PackageReference Include="xDashboard.xStorageService" Version="1.0.0" /> <PackageReference Include="xDashboard.xStorageService" Version="1.0.0" /> -->
</ItemGroup> </ItemGroup>
<!-- Project Dependencies --> <!-- Project Dependencies -->
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Modules\xIdentityHelper\xIdentityHelper.csproj" /> <ProjectReference Include="..\Modules\xIdentityHelper\xIdentityHelper.csproj" />
<ProjectReference Include="..\Modules\xMessageService\xMessageService.csproj" />
<ProjectReference Include="..\Modules\xStorageService\xStorageService.csproj" />
<ProjectReference Include="..\Modules\xIdentityService\xIdentityService.csproj" /> <ProjectReference Include="..\Modules\xIdentityService\xIdentityService.csproj" />
</ItemGroup> </ItemGroup>