This commit is contained in:
2026-10-15 22:18:55 +03:30
parent 4eaec24187
commit be3fd31b4e
3 changed files with 26 additions and 5 deletions
+13 -2
View File
@@ -13,6 +13,7 @@ using xIdentityService.Constants;
using System.Collections.Generic; using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using System.Threading;
namespace xIds.Providers namespace xIds.Providers
{ {
@@ -56,7 +57,10 @@ namespace xIds.Providers
/// </summary> /// </summary>
/// <param name="context"></param> /// <param name="context"></param>
/// <returns></returns> /// <returns></returns>
private async Task OnAuthorizationAsync(AuthorizationFilterContext context) private async Task OnAuthorizationAsync(
AuthorizationFilterContext context,
CancellationToken cancellationToken = default
)
{ {
// //
// Access HttpContext ... // Access HttpContext ...
@@ -109,13 +113,20 @@ namespace xIds.Providers
// //
// Extract Client IP ... // Extract Client IP ...
var clientIP = http.Request.HttpContext.Connection.RemoteIpAddress?.ToString(); var clientIP = http.Request.HttpContext.Connection.RemoteIpAddress?.ToString();
var scopes =
!Scopes.HasChild()
? string.Empty
: Scopes.ToListString();
// //
// Validating ApiKey ... // Validating ApiKey ...
var validationResult = await applicationProvider var validationResult = await applicationProvider
.ValidateApiKey( .ValidateApiKey(
apiKey: apiKey, apiKey: apiKey,
clientIP: clientIP userInfo: null,
clientIP: clientIP,
requiredScope: scopes,
cancellationToken: cancellationToken
); );
if (validationResult.Errors.HasChild()) if (validationResult.Errors.HasChild())
{ {
@@ -136,7 +136,7 @@ namespace xIds.Providers
// Validate ... // Validate ...
if ( if (
id != item.Id || id != item.Id ||
!id.IsDefaultGuid() || id.IsDefaultGuid() ||
!item.IsValidForAdd() || !item.IsValidForAdd() ||
item.IsNullOrDefault() || item.IsNullOrDefault() ||
userInfo.IsNullOrDefault() userInfo.IsNullOrDefault()
@@ -303,8 +303,10 @@ namespace xIds.Providers
// //
newExpiration = TimeSpan newExpiration = TimeSpan
.FromMinutes(apiKeyConfiguration.DefaultExpirationMinutes); .FromMinutes(apiKeyConfiguration.DefaultExpirationMinutes);
expiredDate = DateTime.UtcNow.Add(newExpiration.Value);
} }
expiredDate = DateTime.UtcNow.Add(newExpiration.Value);
//
if (!expiredDate.HasValue) if (!expiredDate.HasValue)
{ {
XException.InvalidArgs.Throw(); XException.InvalidArgs.Throw();
@@ -496,6 +498,12 @@ namespace xIds.Providers
result.Errors.Add("ApiKey Rate Limit Reached ..."); result.Errors.Add("ApiKey Rate Limit Reached ...");
} }
//
var requiredScopes =
requiredScope.IsNullOrEmpty()
? []
: requiredScope.ParseListString<string>();
// //
// Audit Log ... // Audit Log ...
@@ -507,7 +515,9 @@ namespace xIds.Providers
var isScopeValid = var isScopeValid =
!allowedScopes.HasChild() || !allowedScopes.HasChild() ||
requiredScope.IsNullOrEmpty() || requiredScope.IsNullOrEmpty() ||
allowedScopes.Contains(requiredScope); requiredScopes
.All(ts => allowedScopes
.Any(asc => asc.ToNormalString() == ts.ToNormalString()));
var isScopeIpValid = var isScopeIpValid =
isIpValid && isIpValid &&
isScopeValid isScopeValid