last ...
This commit is contained in:
@@ -0,0 +1,265 @@
|
||||
using System;
|
||||
using xIdentityHelper;
|
||||
using System.Threading;
|
||||
using xCommons.Attributes;
|
||||
using xCommons.Extensions;
|
||||
using xIdentityModels.Dtos;
|
||||
using xExceptions.Constants;
|
||||
using xIdentityModels.Models;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Collections.Generic;
|
||||
using xIdentityModels.Extensions;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using static IdentityServer4.IdentityServerConstants;
|
||||
|
||||
namespace xIds.Controllers
|
||||
{
|
||||
public partial class ApplicationsController
|
||||
{
|
||||
[RequireXPowered]
|
||||
[Authorize(LocalApi.PolicyName)]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
[HttpPost("Applications/{applicationId}/ApiKeys/Create")]
|
||||
public async Task<ActionResult<XApiKeyDto>> CreateApiKey(
|
||||
[FromRoute] int applicationId,
|
||||
[FromBody] XCreateApiKeyRequest request,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (
|
||||
!request.IsValid() ||
|
||||
!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();
|
||||
}
|
||||
|
||||
//
|
||||
// Create Specified Api Key ...
|
||||
var result = await provider.CreateApiKey(
|
||||
userInfo: userInfo,
|
||||
scopes: request.Scopes,
|
||||
applicationId: applicationId,
|
||||
rateLimit: request.RateLimit,
|
||||
allowedIPs: request.AllowedIPs,
|
||||
expiration: request.Expiration,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
[RequireXPowered]
|
||||
[Authorize(LocalApi.PolicyName)]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
[HttpDelete("Applications/{applicationId}/ApiKeys/{apiKeyId}")]
|
||||
public async Task<ActionResult<XApiKeyDto>> RemoveApiKey(
|
||||
[FromRoute] Guid apiKeyId,
|
||||
[FromRoute] int applicationId,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
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();
|
||||
}
|
||||
|
||||
//
|
||||
// Create Specified Api Key ...
|
||||
var result = await provider.RemoveApiKeyAsync(
|
||||
id: apiKeyId,
|
||||
softDelete: false,
|
||||
userInfo: userInfo,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
[RequireXPowered]
|
||||
[Authorize(LocalApi.PolicyName)]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
[HttpPost("Applications/{applicationId}/ApiKeys/{apiKeyId}")]
|
||||
public async Task<ActionResult<XApiKeyDto>> RotateApiKey(
|
||||
[FromRoute] Guid apiKeyId,
|
||||
[FromRoute] int applicationId,
|
||||
[FromBody] XRotateApiKeyRequest request,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (
|
||||
apiKeyId.IsDefaultGuid() ||
|
||||
!applicationId.IsValidIntId())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Normalize ...
|
||||
if (
|
||||
request.IsNullOrDefault() ||
|
||||
request.NewExpiration.IsNull() ||
|
||||
!request.NewExpiration.HasValue
|
||||
)
|
||||
{
|
||||
//
|
||||
var timeSpan = TimeSpan.FromMinutes(apiKeyConfiguration.DefaultExpirationMinutes);
|
||||
request = new XRotateApiKeyRequest
|
||||
{
|
||||
NewExpiration = timeSpan
|
||||
};
|
||||
}
|
||||
|
||||
//
|
||||
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();
|
||||
}
|
||||
|
||||
//
|
||||
// Create Specified Api Key ...
|
||||
var result = await provider.RotateApiKey(
|
||||
apiKeyId: apiKeyId,
|
||||
userInfo: userInfo,
|
||||
newExpiration: request.NewExpiration,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
[RequireXPowered]
|
||||
[Authorize(LocalApi.PolicyName)]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
[HttpPost("Applications/{applicationId}/ApiKeys/{apiKeyId}/Revoke")]
|
||||
public async Task<ActionResult<XApiKeyDto>> RevokeApiKey(
|
||||
[FromRoute] Guid apiKeyId,
|
||||
[FromRoute] int applicationId,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
[RequireXPowered]
|
||||
[Authorize(LocalApi.PolicyName)]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
[HttpPost("Applications/{applicationId}/ApiKeys")]
|
||||
public async Task<ActionResult<IEnumerable<XApiKeyDto>>> GetApiKeys(
|
||||
[FromRoute] int applicationId,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
[RequireXPowered]
|
||||
[Authorize(LocalApi.PolicyName)]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
[HttpPost("Applications/{applicationId}/ApiKeys/{apiKeyId}/Usages")]
|
||||
public async Task<ActionResult<XApiKeyDto>> GetApiKeyUsages(
|
||||
[FromRoute] Guid apiKeyId,
|
||||
[FromRoute] int applicationId,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
[RequireXPowered]
|
||||
[Authorize(LocalApi.PolicyName)]
|
||||
[HttpPost("Applications/{applicationId}/ApiKeys/{apiKeyId}/Validate")]
|
||||
public async Task<ActionResult<XApiKeyDto>> ValidateApiKey(
|
||||
[FromRoute] Guid apiKeyId,
|
||||
[FromRoute] int applicationId,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user