This commit is contained in:
2026-10-05 22:30:29 +03:30
parent ac0ebe601e
commit e379faaa07
6 changed files with 214 additions and 48 deletions
@@ -1,5 +1,6 @@
using System;
using System.Linq;
using xIds.Helpers;
using System.Threading;
using xCommons.Extensions;
using xIdentityModels.Dtos;
@@ -7,6 +8,7 @@ using xExceptions.Constants;
using System.Threading.Tasks;
using xIdentityModels.Models;
using System.Collections.Generic;
using System.Security.Cryptography;
namespace xIds.Providers
{
@@ -80,15 +82,43 @@ namespace xIds.Providers
{
expiration = TimeSpan.FromMinutes(apiKeyConfiguration.MaxExpirationMinutes);
}
//
var
//
var item = new XApiKeyDto
// Generate New Key ...
(string plainKey, string keyHash, string prefix) = XApiKeyHelper.Generate();
var expireAt = DateTime.UtcNow.Add(expiration.Value);
//
var _allowedScopes = scopes.ToJSON();
var _allowedIPs = allowedIPs.ToListString();
//
// Instance Dto Item ...
var result = new XApiKeyDto
{
}
RevokedAt = null,
KeyHash = keyHash,
LastUsedAt = null,
KeyPrefix = prefix,
ExpiresAt = expireAt,
RevokedBy = string.Empty,
AllowedIPs = _allowedIPs,
CreatedOn = DateTime.UtcNow,
ApplicationId = applicationId,
AllowedScopes = _allowedScopes,
RateLimitPerMinute = rateLimit.Value
};
//
// Add Item to Database ...
result = await AddApiKeyAsync(
item: item,
userInfo: userInfo,
cancellationToken: cancellationToken
);
//
return result;
}
/// <summary>
@@ -103,7 +133,65 @@ namespace xIds.Providers
XUserClaimsInfoDto userInfo = null,
CancellationToken cancellationToken = default
)
{ }
{
//
// Validate ...
if (
apiKeyId.IsDefaultGuid() ||
userInfo.IsNullOrDefault())
{
XException.InvalidArgs.Throw();
}
//
// Retrieve ApiKey ...
var item = await GetApiKeyAsync(
id: apiKeyId,
userInfo: userInfo,
includeBuilder: null,
ignoreSoftDeleteds: true,
cancellationToken: cancellationToken
);
if (item.IsNullOrDefault())
{
XException.NotFound.Throw();
}
//
// Permission Checking ...
var application = await GetApplicationAsync(
userInfo: userInfo,
includeBuilder: null,
id: item.ApplicationId,
ignoreSoftDeleteds: true,
cancellationToken: cancellationToken
);
if (application.IsNullOrDefault())
{
XException.NotFound.Throw();
}
if (!IsOwned(application, userInfo))
{
XException.NotAllowed.Throw();
}
//
// Update Revoke Info ...
item.RevokedAt = DateTime.UtcNow;
item.RevokedBy = userInfo.UserId;
//
item = await UpdateApiKeyAsync(
id: item.Id,
item: item,
userInfo: userInfo,
cancellationToken: cancellationToken
);
//
var result = !item.IsNullOrDefault();
return result;
}
/// <summary>
/// Renew Specified Api Key ...
@@ -119,7 +207,61 @@ namespace xIds.Providers
XUserClaimsInfoDto userInfo = null,
CancellationToken cancellationToken = default
)
{ }
{
//
// Validate ...
if (
apiKeyId.IsDefaultGuid() ||
userInfo.IsNullOrDefault()
)
{
XException.InvalidArgs.Throw();
}
//
// Retrieve Item ...
var result = await GetApiKeyAsync(
id: apiKeyId,
userInfo: userInfo,
includeBuilder: null,
ignoreSoftDeleteds: true,
cancellationToken: cancellationToken
);
if (result.IsNullOrDefault())
{
XException.NotFound.Throw();
}
//
// Expired Date ...
DateTime? expiredDate = null;
if (!newExpiration.HasValue)
{
//
newExpiration = TimeSpan
.FromMinutes(apiKeyConfiguration.DefaultExpirationMinutes);
expiredDate = DateTime.UtcNow.Add(newExpiration.Value);
}
if (!expiredDate.HasValue)
{
XException.InvalidArgs.Throw();
}
//
// Update Item ...
result.ExpiresAt = expiredDate.Value;
//
result = await UpdateApiKeyAsync(
item: result,
id: result.Id,
userInfo: userInfo,
cancellationToken: cancellationToken
);
//
return result;
}
/// <summary>
/// Retrieve All Api Keys for Specified Application ...
@@ -129,11 +271,52 @@ namespace xIds.Providers
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<IEnumerable<XApiKeyDto>> GetApplicationApiKeys(
Guid applicationId,
int applicationId,
XUserClaimsInfoDto userInfo = null,
CancellationToken cancellationToken = default
)
{ }
{
//
// Validate ...
if (
userInfo.IsNullOrDefault() ||
!applicationId.IsValidIntId()
)
{
XException.InvalidArgs.Throw();
}
//
// Permission Checking ...
var application = await GetApplicationAsync(
userInfo: userInfo,
includeBuilder: null,
id: applicationId,
ignoreSoftDeleteds: true,
cancellationToken: cancellationToken
);
if (application.IsNullOrDefault())
{
XException.NotFound.Throw();
}
if (!IsOwned(application, userInfo))
{
XException.NotAllowed.Throw();
}
//
var result = await FindManyApiKeyAsync(
userInfo: userInfo,
includeBuilder: null,
ignoreSoftDeleteds: true,
cancellationToken: cancellationToken,
orderBuilder: x => x.OrderBy(y => y.Id),
predicate: x => x.ApplicationId == applicationId
);
//
return result;
}
/// <summary>
/// Validate Specified Api Key in based on ClientIP ...
@@ -149,7 +332,10 @@ namespace xIds.Providers
XUserClaimsInfoDto userInfo = null,
CancellationToken cancellationToken = default
)
{ }
{
//
}
#endregion
}
}