Compare commits
8
Commits
e379faaa07
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5085fc54a4 | ||
|
|
2e8f269ae6 | ||
|
|
c4e430a98f | ||
|
|
a87ea5d64d | ||
|
|
b00391517a | ||
|
|
e1e151a834 | ||
|
|
cd2b06b913 | ||
|
|
48395457e6 |
@@ -5,6 +5,11 @@ namespace xIds.Configurations
|
||||
/// </summary>
|
||||
public class XApiKeyConfiguration
|
||||
{
|
||||
/// <summary>
|
||||
/// Generated Api Keys Prefix ...
|
||||
/// </summary>
|
||||
public string ApiKeyPrefix { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Default expiration in minutes (e.g., 43200 = 30 days)
|
||||
/// </summary>
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using xIdentityHelper;
|
||||
using xCommons.Attributes;
|
||||
using xCommons.Extensions;
|
||||
using xIdentityModels.Dtos;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using static IdentityServer4.IdentityServerConstants;
|
||||
|
||||
namespace xIds.Controllers
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using IdentityModel.Client;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using xIdentityHelper;
|
||||
using xCommons.Attributes;
|
||||
using xCommons.Extensions;
|
||||
using IdentityModel.Client;
|
||||
using xExceptions.Constants;
|
||||
using xIdentityModels.Extensions;
|
||||
using System.Threading.Tasks;
|
||||
using xIdentityModels.Models;
|
||||
using static IdentityServer4.IdentityServerConstants;
|
||||
using static xIdentityHelper.XApiScopeHelper;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using xIdentityModels.Extensions;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using static xIdentityHelper.XApiScopeHelper;
|
||||
using static IdentityServer4.IdentityServerConstants;
|
||||
|
||||
namespace xIds.Controllers
|
||||
{
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using xModels.Dtos;
|
||||
using xIdentityHelper;
|
||||
using xCommons.Attributes;
|
||||
using xCommons.Extensions;
|
||||
using xExceptions.Constants;
|
||||
using xIdentityHelper;
|
||||
using xIdentityModels.Constants;
|
||||
using xIdentityModels.Dtos;
|
||||
using xExceptions.Constants;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using xIdentityModels.Constants;
|
||||
using System.Collections.Generic;
|
||||
using xIdentityModels.Navigations;
|
||||
using xModels.Dtos;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using static IdentityServer4.IdentityServerConstants;
|
||||
|
||||
namespace xIds.Controllers
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using xCommons.Attributes;
|
||||
using xCommons.Constants;
|
||||
using xCommons.Attributes;
|
||||
using xIdentityModels.Dtos;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
|
||||
namespace xIds.Controllers
|
||||
{
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using xModels.Dtos;
|
||||
using xIdentityHelper;
|
||||
using xCommons.Attributes;
|
||||
using xCommons.Extensions;
|
||||
using xIdentityModels.Dtos;
|
||||
using System.Threading.Tasks;
|
||||
using xIdentityModels.Models;
|
||||
using xModels.Dtos;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using static IdentityServer4.IdentityServerConstants;
|
||||
|
||||
namespace xIds.Controllers
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using xModels.Dtos;
|
||||
using xIdentityHelper;
|
||||
using xCommons.Attributes;
|
||||
using xCommons.Extensions;
|
||||
using xIdentityHelper;
|
||||
using xModels.Dtos;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using static IdentityServer4.IdentityServerConstants;
|
||||
|
||||
namespace xIds.Controllers
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using xIdentityHelper;
|
||||
using xCommons.Attributes;
|
||||
using xCommons.Extensions;
|
||||
using xExceptions.Constants;
|
||||
using xIdentityModels.Constants;
|
||||
using System.Threading.Tasks;
|
||||
using xIdentityModels.Models;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using xIdentityModels.Constants;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using static IdentityServer4.IdentityServerConstants;
|
||||
|
||||
namespace xIds.Controllers
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using xModels.Dtos;
|
||||
using xIdentityHelper;
|
||||
using xCommons.Attributes;
|
||||
using xCommons.Extensions;
|
||||
using xIdentityHelper;
|
||||
using xIdentityModels.Dtos;
|
||||
using xModels.Dtos;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using static IdentityServer4.IdentityServerConstants;
|
||||
|
||||
namespace xIds.Controllers
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
using System.Linq;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using xIdentityHelper;
|
||||
using xCommons.Attributes;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using static IdentityServer4.IdentityServerConstants;
|
||||
|
||||
namespace xIds.Controllers
|
||||
@@ -156,6 +156,19 @@ namespace xIds.Controllers
|
||||
//
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
[RequireXPowered]
|
||||
[HttpGet("Test/HiApiKeyAccess")]
|
||||
[Authorize(LocalApi.PolicyName)]
|
||||
[Authorize(Policy = XPolicies.ApiKeyAccess)]
|
||||
public ActionResult<string> HiApiKeyAccess()
|
||||
{
|
||||
//
|
||||
var result = $"Hi ApiKey Access, is Passed ...";
|
||||
|
||||
//
|
||||
return Ok(result);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using xCommons.Configurations;
|
||||
using xIds.Interfaces;
|
||||
using xCommons.Providers;
|
||||
using xIds.Controllers.Base;
|
||||
using xIds.Interfaces;
|
||||
using xCommons.Configurations;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace xIds.Controllers
|
||||
{
|
||||
|
||||
@@ -0,0 +1,253 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using xModels.Dtos;
|
||||
using System.Threading;
|
||||
using xIdentityHelper;
|
||||
using xCommons.Attributes;
|
||||
using xCommons.Extensions;
|
||||
using xIdentityModels.Dtos;
|
||||
using xExceptions.Constants;
|
||||
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
|
||||
{
|
||||
//
|
||||
#region ApiKey ...
|
||||
/// <summary>
|
||||
/// Get Specified ApiKey ...
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[RequireXPowered]
|
||||
[HttpGet("ApiKeys/{id}")]
|
||||
[Authorize(LocalApi.PolicyName)]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
public async Task<ActionResult<XApiKeyDto>> GetApiKey(
|
||||
[FromRoute] Guid id,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (id.IsDefaultGuid())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var userInfo = await GetUserInfo();
|
||||
|
||||
//
|
||||
var result = await provider.GetApiKeyAsync(
|
||||
id: id,
|
||||
userInfo: userInfo,
|
||||
includeBuilder: null,
|
||||
ignoreSoftDeleteds: true,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove Specified ApiKey ...
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[RequireXPowered]
|
||||
[HttpDelete("ApiKeys/{id}")]
|
||||
[Authorize(LocalApi.PolicyName)]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
public async Task<ActionResult<XApiKeyDto>> RemoveApiKey(
|
||||
[FromRoute] Guid id,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (id.IsDefaultGuid())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var userInfo = await GetUserInfo();
|
||||
|
||||
//
|
||||
var result = await provider.RemoveApiKeyAsync(
|
||||
id: id,
|
||||
softDelete: false,
|
||||
userInfo: userInfo,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update Specified ApiKey ...
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[RequireXPowered]
|
||||
[HttpPut("ApiKeys/{id}")]
|
||||
[Authorize(LocalApi.PolicyName)]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
public async Task<ActionResult<XApiKeyDto>> UpdateApiKey(
|
||||
[FromRoute] Guid id,
|
||||
[FromBody] XApiKeyDto item,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (
|
||||
id.IsDefaultGuid() ||
|
||||
!item.IsValidForAdd()
|
||||
)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var userInfo = await GetUserInfo();
|
||||
|
||||
//
|
||||
var result = await provider.UpdateApiKeyAsync(
|
||||
id: id,
|
||||
item: item,
|
||||
userInfo: userInfo,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get All Specified ApiKeys ...
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[RequireXPowered]
|
||||
[HttpGet("ApiKeys")]
|
||||
[Authorize(LocalApi.PolicyName)]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
public async Task<ActionResult<IEnumerable<XApiKeyDto>>> GetAllApiKeys(
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
var userInfo = await GetUserInfo();
|
||||
|
||||
//
|
||||
var result = await provider.GetAllApiKeyAsync(
|
||||
userInfo: userInfo,
|
||||
includeBuilder: null,
|
||||
ignoreSoftDeleteds: true,
|
||||
cancellationToken: cancellationToken,
|
||||
orderBuilder: x => x.OrderBy(y => y.CreatedOn)
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Query ApiKeys ...
|
||||
/// </summary>
|
||||
/// <param name="query"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[RequireXPowered]
|
||||
[HttpGet("ApiKeys/Query")]
|
||||
[Authorize(LocalApi.PolicyName)]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
public async Task<ActionResult<XQueryResult<XApiKeyDto>>> QueryApiKeys(
|
||||
[FromRoute] XQuery query,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
var userInfo = await GetUserInfo();
|
||||
|
||||
//
|
||||
var result = await provider.QueryApiKeyAsync(
|
||||
query: query,
|
||||
predicate: null,
|
||||
userInfo: userInfo,
|
||||
includeBuilder: null,
|
||||
ignoreSoftDeleteds: true,
|
||||
cancellationToken: cancellationToken,
|
||||
orderBuilder: x => x.OrderBy(y => y.CreatedOn)
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,299 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using xModels.Dtos;
|
||||
using xIdentityHelper;
|
||||
using System.Threading;
|
||||
using xCommons.Attributes;
|
||||
using xCommons.Extensions;
|
||||
using xIdentityModels.Dtos;
|
||||
using xExceptions.Constants;
|
||||
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
|
||||
{
|
||||
//
|
||||
#region ApiKeyUsage ...
|
||||
/// <summary>
|
||||
/// Add Api Key Usage ...
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[RequireXPowered]
|
||||
[HttpPost("ApiKeys/Usages")]
|
||||
[Authorize(LocalApi.PolicyName)]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
public async Task<ActionResult<XApiKeyUsageDto>> AddApiKeyUsage(
|
||||
[FromBody] XApiKeyUsageDto item,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (item.IsValidForAdd())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var userInfo = await GetUserInfo();
|
||||
|
||||
//
|
||||
var result = await provider.AddApiKeyUsageAsync(
|
||||
item: item,
|
||||
userInfo: userInfo,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get Specified Api Key Usage ...
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[RequireXPowered]
|
||||
[Authorize(LocalApi.PolicyName)]
|
||||
[HttpGet("ApiKeys/Usages/{id}")]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
public async Task<ActionResult<XApiKeyUsageDto>> GetApiKeyUsage(
|
||||
[FromRoute] long id,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (id <= 0)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var userInfo = await GetUserInfo();
|
||||
|
||||
//
|
||||
var result = await provider.GetApiKeyUsageAsync(
|
||||
id: id,
|
||||
userInfo: userInfo,
|
||||
includeBuilder: null,
|
||||
ignoreSoftDeleteds: true,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove Specified Api Key Usage ...
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[RequireXPowered]
|
||||
[Authorize(LocalApi.PolicyName)]
|
||||
[HttpDelete("ApiKeys/Usages/{id}")]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
public async Task<ActionResult<XApiKeyUsageDto>> RemoveApiKeyUsage(
|
||||
[FromRoute] long id,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (id <= 0)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var userInfo = await GetUserInfo();
|
||||
|
||||
//
|
||||
var result = await provider.RemoveApiKeyUsageAsync(
|
||||
id: id,
|
||||
softDelete: false,
|
||||
userInfo: userInfo,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update Specified Api Key Usage ...
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[RequireXPowered]
|
||||
[Authorize(LocalApi.PolicyName)]
|
||||
[HttpPut("ApiKeys/Usages/{id}")]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
public async Task<ActionResult<XApiKeyUsageDto>> UpdateApiKeyUsage(
|
||||
[FromRoute] long id,
|
||||
[FromBody] XApiKeyUsageDto item,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (
|
||||
id <= 0 ||
|
||||
!item.IsValidForAdd()
|
||||
)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var userInfo = await GetUserInfo();
|
||||
|
||||
//
|
||||
var result = await provider.UpdateApiKeyUsageAsync(
|
||||
id: id,
|
||||
item: item,
|
||||
userInfo: userInfo,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get All Api Key Usages ...
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[RequireXPowered]
|
||||
[HttpGet("ApiKeys/Usages")]
|
||||
[Authorize(LocalApi.PolicyName)]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
public async Task<ActionResult<IEnumerable<XApiKeyUsageDto>>> GetAllApiKeyUsages(
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
var userInfo = await GetUserInfo();
|
||||
|
||||
//
|
||||
var result = await provider.GetAllApiKeyUsageAsync(
|
||||
userInfo: userInfo,
|
||||
includeBuilder: null,
|
||||
ignoreSoftDeleteds: true,
|
||||
cancellationToken: cancellationToken,
|
||||
orderBuilder: x => x.OrderBy(y => y.RequestedOn)
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Query Api Key Usages ...
|
||||
/// </summary>
|
||||
/// <param name="query"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[RequireXPowered]
|
||||
[Authorize(LocalApi.PolicyName)]
|
||||
[HttpGet("ApiKeys/Usages/Query")]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
public async Task<ActionResult<XQueryResult<XApiKeyUsageDto>>> QueryApiKeyUsages(
|
||||
[FromRoute] XQuery query,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
var userInfo = await GetUserInfo();
|
||||
|
||||
//
|
||||
var result = await provider.QueryApiKeyUsageAsync(
|
||||
query: query,
|
||||
predicate: null,
|
||||
userInfo: userInfo,
|
||||
includeBuilder: null,
|
||||
ignoreSoftDeleteds: true,
|
||||
cancellationToken: cancellationToken,
|
||||
orderBuilder: x => x.OrderBy(y => y.RequestedOn)
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,315 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using xModels.Dtos;
|
||||
using xIdentityHelper;
|
||||
using System.Threading;
|
||||
using xCommons.Attributes;
|
||||
using xCommons.Extensions;
|
||||
using xIdentityModels.Dtos;
|
||||
using xExceptions.Constants;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using xIdentityModels.Extensions;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using static IdentityServer4.IdentityServerConstants;
|
||||
|
||||
namespace xIds.Controllers
|
||||
{
|
||||
public partial class ApplicationsController
|
||||
{
|
||||
//
|
||||
#region Application ...
|
||||
/// <summary>
|
||||
/// Create Application ...
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Sample request:
|
||||
///
|
||||
/// {
|
||||
/// "type": 0,
|
||||
/// "name": "string",
|
||||
/// "description": "string",
|
||||
/// }
|
||||
///
|
||||
/// </remarks>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost("")]
|
||||
[RequireXPowered]
|
||||
[Authorize(LocalApi.PolicyName)]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
public async Task<ActionResult<XApplicationDto>> CreateApplication(
|
||||
[FromBody] XApplicationDto item,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (!item.IsValidForAdd())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var userInfo = await GetUserInfo();
|
||||
|
||||
//
|
||||
var result = await provider.AddApplicationAsync(
|
||||
item: item,
|
||||
userInfo: userInfo,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update Specified Application ...
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Sample request:
|
||||
///
|
||||
/// {
|
||||
/// "id": 0,
|
||||
/// "type": 0,
|
||||
/// "name": "string",
|
||||
/// "description": "string",
|
||||
/// }
|
||||
///
|
||||
/// </remarks>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPut("")]
|
||||
[RequireXPowered]
|
||||
[Authorize(LocalApi.PolicyName)]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
public async Task<ActionResult<XApplicationDto>> UpdateApplication(
|
||||
[FromBody] XApplicationDto item,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (!item.IsValidForAdd())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var userInfo = await GetUserInfo();
|
||||
|
||||
//
|
||||
var result = await provider.UpdateApplicationAsync(
|
||||
item: item,
|
||||
id: item.Id,
|
||||
userInfo: userInfo,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove Specified Application ...
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[RequireXPowered]
|
||||
[HttpDelete("{id}")]
|
||||
[Authorize(LocalApi.PolicyName)]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
public async Task<ActionResult<XApplicationDto>> RemoveApplication(
|
||||
[FromRoute] int id,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (!id.IsValidIntId())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var userInfo = await GetUserInfo();
|
||||
|
||||
//
|
||||
var result = await provider.RemoveApplicationAsync(
|
||||
id: id,
|
||||
softDelete: false,
|
||||
userInfo: userInfo,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get Specified Application ...
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[RequireXPowered]
|
||||
[HttpGet("{id}")]
|
||||
[Authorize(LocalApi.PolicyName)]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
public async Task<ActionResult<XApplicationDto>> GetApplication(
|
||||
[FromRoute] int id,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (!id.IsValidIntId())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var userInfo = await GetUserInfo();
|
||||
|
||||
//
|
||||
var result = await provider.GetApplicationAsync(
|
||||
id: id,
|
||||
userInfo: userInfo,
|
||||
includeBuilder: null,
|
||||
ignoreSoftDeleteds: true,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get All Application ...
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("")]
|
||||
[RequireXPowered]
|
||||
[Authorize(LocalApi.PolicyName)]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
public async Task<ActionResult<IEnumerable<XApplicationDto>>> GetAllApplications(
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
var userInfo = await GetUserInfo();
|
||||
|
||||
//
|
||||
var result = await provider.GetAllApplicationAsync(
|
||||
userInfo: userInfo,
|
||||
includeBuilder: null,
|
||||
ignoreSoftDeleteds: true,
|
||||
cancellationToken: cancellationToken,
|
||||
orderBuilder: x => x.OrderBy(y => y.Id)
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Query Applications ...
|
||||
/// </summary>
|
||||
/// <param name="query"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[RequireXPowered]
|
||||
[HttpGet("Query")]
|
||||
[Authorize(LocalApi.PolicyName)]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
public async Task<ActionResult<XQueryResult<XApplicationDto>>> QueryApplications(
|
||||
[FromRoute] XQuery query,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
var userInfo = await GetUserInfo();
|
||||
|
||||
//
|
||||
var result = await provider.QueryApplicationAsync(
|
||||
query: query,
|
||||
predicate: null,
|
||||
userInfo: userInfo,
|
||||
includeBuilder: null,
|
||||
ignoreSoftDeleteds: true,
|
||||
cancellationToken: cancellationToken,
|
||||
orderBuilder: x => x.OrderBy(y => y.Id)
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,556 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
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
|
||||
{
|
||||
/// <summary>
|
||||
/// Show 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)]
|
||||
[HttpGet("{applicationId}/ApiKeys/{apiKeyId}/Show")]
|
||||
public async Task<ActionResult<string>> ShowApiKey(
|
||||
[FromRoute] Guid apiKeyId,
|
||||
[FromRoute] int applicationId,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (
|
||||
apiKeyId.IsDefaultGuid() ||
|
||||
!applicationId.IsValidIntId())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var userInfo = await GetUserInfo();
|
||||
|
||||
//
|
||||
var result = await provider.ShowApiKey(
|
||||
userInfo: userInfo,
|
||||
apiKeyId: apiKeyId,
|
||||
applicationId: applicationId,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <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)]
|
||||
[HttpPost("{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();
|
||||
}
|
||||
|
||||
//
|
||||
// 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: expirationTimeSpan,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <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)]
|
||||
[HttpDelete("{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();
|
||||
}
|
||||
|
||||
//
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <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)]
|
||||
[HttpPost("{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();
|
||||
}
|
||||
|
||||
//
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <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("{applicationId}/ApiKeys/{apiKeyId}/Revoke")]
|
||||
public async Task<ActionResult<bool>> RevokeApiKey(
|
||||
[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();
|
||||
}
|
||||
|
||||
//
|
||||
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)]
|
||||
[HttpGet("{applicationId}/ApiKeys")]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
public async Task<ActionResult<IEnumerable<XApiKeyDto>>> GetApiKeys(
|
||||
[FromRoute] int applicationId,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
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)]
|
||||
[HttpGet("{applicationId}/ApiKeys/{apiKeyId}/Usages")]
|
||||
public async Task<ActionResult<IEnumerable<XApiKeyUsageDto>>> GetApiKeyUsages(
|
||||
[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();
|
||||
}
|
||||
|
||||
//
|
||||
// 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]
|
||||
[HttpPost("ApiKeys/Validate")]
|
||||
[Authorize(LocalApi.PolicyName)]
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using xIds.Interfaces;
|
||||
using xCommons.Providers;
|
||||
using xIds.Configurations;
|
||||
using xIds.Controllers.Base;
|
||||
using xCommons.Configurations;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace xIds.Controllers
|
||||
{
|
||||
/// <summary>
|
||||
/// Provide all available tools for manipulating users and accounts
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
public partial class ApplicationsController : XIBaseController
|
||||
{
|
||||
private readonly IXApplicationProvider provider;
|
||||
private readonly XApiKeyConfiguration apiKeyConfiguration;
|
||||
|
||||
public ApplicationsController(
|
||||
IXApplicationProvider provider,
|
||||
IXIdentityManager identityManager,
|
||||
XAppConfiguration appConfiguration,
|
||||
ILogger<ApplicationsController> logger,
|
||||
XValidationProvider validationProvider,
|
||||
XApiKeyConfiguration apiKeyConfiguration
|
||||
) : base(
|
||||
logger,
|
||||
appConfiguration,
|
||||
identityManager,
|
||||
validationProvider
|
||||
)
|
||||
{
|
||||
this.provider = provider;
|
||||
this.apiKeyConfiguration = apiKeyConfiguration;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -25,7 +25,9 @@ using xIdentityModels.Configurations;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.HttpOverrides;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using xIdentityService.Constants;
|
||||
|
||||
namespace xIds.DI
|
||||
{
|
||||
@@ -147,6 +149,67 @@ namespace xIds.DI
|
||||
services.AddSingleton<XDataServiceConfiguration>(dataServiceConfiguration);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add Specified ApiKey Configurations Instance ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="configuration"></param>
|
||||
public static void AddXApiKeyConfiguration(
|
||||
this IServiceCollection source,
|
||||
XApiKeyConfiguration configuration
|
||||
)
|
||||
{
|
||||
//
|
||||
if (configuration.IsNull())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
source.AddSingleton(configuration);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extract APpi KLey Configurations from app settings
|
||||
/// and Register in Services ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="configuration"></param>
|
||||
public static void AddXApiKeyConfiguration(
|
||||
this IServiceCollection source,
|
||||
IConfiguration configuration
|
||||
)
|
||||
{
|
||||
//
|
||||
var config = configuration.GetXApiKeyConfiguration();
|
||||
if (config.IsNull())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
source.AddXApiKeyConfiguration(config);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register Application Provider ...
|
||||
/// Managing Applications/ApiKeys/ApiKeyUsages ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="configuration"></param>
|
||||
public static void AddXApplicationProvider(
|
||||
this IServiceCollection source,
|
||||
IConfiguration configuration
|
||||
)
|
||||
{
|
||||
//
|
||||
source.AddXApiKeyConfiguration(configuration);
|
||||
|
||||
//
|
||||
source.AddMemoryCache();
|
||||
source.AddScoped<IXApplicationProvider, XApplicationProvider>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register All Requirements For XIdentityServer Usage
|
||||
/// </summary>
|
||||
@@ -336,15 +399,22 @@ namespace xIds.DI
|
||||
/// </summary>
|
||||
/// <param name="services"></param>
|
||||
/// <param name="configuration"></param>
|
||||
/// <param name="addApiKeyAuthentication"></param>
|
||||
public static void AddXIdentityServerAuthentication(
|
||||
this IServiceCollection services,
|
||||
IConfiguration configuration
|
||||
IConfiguration configuration,
|
||||
bool addApiKeyAuthentication = false
|
||||
)
|
||||
{
|
||||
//
|
||||
services.AddXIdentityResourceConfiguration(configuration);
|
||||
services.AddAuthentication()
|
||||
.AddJwtBearer(options =>
|
||||
|
||||
//
|
||||
var authBuilder = services.AddAuthentication();
|
||||
|
||||
//
|
||||
// Add JWT Bearer ...
|
||||
authBuilder.AddJwtBearer(options =>
|
||||
{
|
||||
//
|
||||
options.SaveToken = true;
|
||||
@@ -354,6 +424,16 @@ namespace xIds.DI
|
||||
options.ForwardDefault = XAuthentication.IDENTITY_SERVER_LOCAL_API;
|
||||
})
|
||||
.AddLocalApi();
|
||||
|
||||
//
|
||||
if (addApiKeyAuthentication)
|
||||
{
|
||||
//
|
||||
authBuilder.AddScheme<AuthenticationSchemeOptions, XApiKeyAuthenticationHandler>(
|
||||
XAuthenticationScheme.XApiKey.GetStringValue(),
|
||||
options => {}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
+24
-19
@@ -9,11 +9,29 @@ namespace xIds.Helpers
|
||||
/// </summary>
|
||||
public static class XApiKeyHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Compute Hash of Specified Content using SHA256 ...
|
||||
/// </summary>
|
||||
/// <param name="plain"></param>
|
||||
/// <returns></returns>
|
||||
public static string ComputeHash(string plain)
|
||||
{
|
||||
//
|
||||
using var sha256 = SHA256.Create();
|
||||
var hashBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(plain));
|
||||
var result = BitConverter.ToString(hashBytes)
|
||||
.Replace("-", "")
|
||||
.ToLower();
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generate an Api Key ...
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static (string plainKey, string hash, string prefix) Generate()
|
||||
public static (string plainKey, string hash, string prefix) Generate(string keyPrefix)
|
||||
{
|
||||
//
|
||||
// Generate 32 bytes of cryptographically secure random data
|
||||
@@ -25,16 +43,12 @@ namespace xIds.Helpers
|
||||
|
||||
//
|
||||
// Format: xapp_{base64url}
|
||||
var plainKey = $"xapp_{Convert.ToBase64String(randomBytes)
|
||||
var plainKey = $"{keyPrefix}_{Convert.ToBase64String(randomBytes)
|
||||
.Replace("+", "-").Replace("/", "_").TrimEnd('=')}";
|
||||
|
||||
//
|
||||
// Hash with SHA256 for storage (never store plain key)
|
||||
using (var sha256 = SHA256.Create())
|
||||
{
|
||||
//
|
||||
var hashBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(plainKey));
|
||||
var hash = BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
|
||||
// Compute Hash ...
|
||||
var hash = ComputeHash(plainKey);
|
||||
|
||||
//
|
||||
// Prefix for quick identification (first 12 chars)
|
||||
@@ -43,7 +57,6 @@ namespace xIds.Helpers
|
||||
//
|
||||
return (plainKey, hash, prefix);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verify Generating Api Key ...
|
||||
@@ -57,16 +70,8 @@ namespace xIds.Helpers
|
||||
)
|
||||
{
|
||||
//
|
||||
using (var sha256 = SHA256.Create())
|
||||
{
|
||||
//
|
||||
var hashBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(plainKey));
|
||||
var computedHash = BitConverter.ToString(hashBytes)
|
||||
.Replace("-", "").ToLower();
|
||||
|
||||
//
|
||||
return computedHash == storedHash;
|
||||
}
|
||||
var hash = ComputeHash(plainKey);
|
||||
return hash == storedHash;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -176,11 +176,13 @@ namespace xIds.Interfaces
|
||||
/// Add Specified Item ...
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="apiKey"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<XApiKeyDto> AddApiKeyAsync(
|
||||
XApiKeyDto item,
|
||||
string apiKey = null,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
@@ -194,7 +196,7 @@ namespace xIds.Interfaces
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<XApiKeyDto> UpdateApiKeyAsync(
|
||||
int id,
|
||||
Guid id,
|
||||
XApiKeyDto item,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
@@ -209,7 +211,7 @@ namespace xIds.Interfaces
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<XApiKeyDto> RemoveApiKeyAsync(
|
||||
int id,
|
||||
Guid id,
|
||||
bool softDelete = true,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
@@ -225,7 +227,7 @@ namespace xIds.Interfaces
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<XApiKeyDto> GetApiKeyAsync(
|
||||
int id,
|
||||
Guid id,
|
||||
bool ignoreSoftDeleteds = true,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
Func<IQueryable<XApiKey>, IIncludableQueryable<XApiKey, object>> includeBuilder = null,
|
||||
@@ -348,7 +350,7 @@ namespace xIds.Interfaces
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<XApiKeyUsageDto> UpdateApiKeyUsageAsync(
|
||||
int id,
|
||||
long id,
|
||||
XApiKeyUsageDto item,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
@@ -363,7 +365,7 @@ namespace xIds.Interfaces
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<XApiKeyUsageDto> RemoveApiKeyUsageAsync(
|
||||
int id,
|
||||
long id,
|
||||
bool softDelete = true,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
@@ -379,7 +381,7 @@ namespace xIds.Interfaces
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<XApiKeyUsageDto> GetApiKeyUsageAsync(
|
||||
int id,
|
||||
long id,
|
||||
bool ignoreSoftDeleteds = true,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
Func<IQueryable<XApiKeyUsage>, IIncludableQueryable<XApiKeyUsage, object>> includeBuilder = null,
|
||||
@@ -480,6 +482,21 @@ namespace xIds.Interfaces
|
||||
|
||||
//
|
||||
#region Custom Actions ...
|
||||
/// <summary>
|
||||
/// Show Specifid Api Key ...
|
||||
/// </summary>
|
||||
/// <param name="apiKeyId"></param>
|
||||
/// <param name="applicationId"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<string> ShowApiKey(
|
||||
Guid apiKeyId,
|
||||
int applicationId,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Generate a New ApiKey and Add It to DB ...
|
||||
/// </summary>
|
||||
@@ -547,12 +564,14 @@ namespace xIds.Interfaces
|
||||
/// </summary>
|
||||
/// <param name="apiKey"></param>
|
||||
/// <param name="clientIP"></param>
|
||||
/// <param name="requiredScope"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<bool> ValidateApiKey(
|
||||
Task<XApiKeyValidationResult> ValidateApiKey(
|
||||
string apiKey,
|
||||
string clientIP,
|
||||
string requiredScope = null,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
@@ -632,7 +651,7 @@ namespace xIds.Interfaces
|
||||
/// <param name="sources"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<XApiKeyDto> EnrichRangeAsync(
|
||||
Task<IEnumerable<XApiKeyDto>> EnrichRangeAsync(
|
||||
IEnumerable<XApiKey> sources,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
@@ -643,7 +662,7 @@ namespace xIds.Interfaces
|
||||
/// <param name="sources"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<XApiKeyDto> EnrichRangeAsync(
|
||||
Task<IEnumerable<XApiKeyDto>> EnrichRangeAsync(
|
||||
IEnumerable<XApiKeyDto> sources,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
@@ -654,7 +673,7 @@ namespace xIds.Interfaces
|
||||
/// <param name="sources"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<XApiKeyUsageDto> EnrichRangeAsync(
|
||||
Task<IEnumerable<XApiKeyUsageDto>> EnrichRangeAsync(
|
||||
IEnumerable<XApiKeyUsage> sources,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
@@ -665,7 +684,7 @@ namespace xIds.Interfaces
|
||||
/// <param name="sources"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<XApplicationDto> EnrichRangeAsync(
|
||||
Task<IEnumerable<XApplicationDto>> EnrichRangeAsync(
|
||||
IEnumerable<XApplication> sources,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
@@ -676,7 +695,7 @@ namespace xIds.Interfaces
|
||||
/// <param name="sources"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<XApiKeyUsageDto> EnrichRangeAsync(
|
||||
Task<IEnumerable<XApiKeyUsageDto>> EnrichRangeAsync(
|
||||
IEnumerable<XApiKeyUsageDto> sources,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
@@ -687,7 +706,7 @@ namespace xIds.Interfaces
|
||||
/// <param name="sources"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<XApplicationDto> EnrichRangeAsync(
|
||||
Task<IEnumerable<XApplicationDto>> EnrichRangeAsync(
|
||||
IEnumerable<XApplicationDto> sources,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
using System.Linq;
|
||||
using IdentityModel;
|
||||
using xIds.Interfaces;
|
||||
using xCommons.Extensions;
|
||||
using System.Threading.Tasks;
|
||||
using System.Security.Claims;
|
||||
using System.Text.Encodings.Web;
|
||||
using xIdentityService.Constants;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
|
||||
namespace xIds.Providers
|
||||
{
|
||||
/// <summary>
|
||||
/// an Authentication Handler for ApiKey's based App's ...
|
||||
/// </summary>
|
||||
public class XApiKeyAuthenticationHandler : AuthenticationHandler<AuthenticationSchemeOptions>
|
||||
{
|
||||
private readonly IXApplicationProvider applicationProvider;
|
||||
|
||||
//
|
||||
public string HeaderName { get; } = XHeader.ApiKey.GetStringValue();
|
||||
public string AuthenticationScheme { get; } = XAuthenticationScheme.XApiKey.GetStringValue();
|
||||
|
||||
public XApiKeyAuthenticationHandler(
|
||||
UrlEncoder encoder,
|
||||
ISystemClock clock,
|
||||
ILoggerFactory logger,
|
||||
IXApplicationProvider applicationProvider,
|
||||
IOptionsMonitor<AuthenticationSchemeOptions> options
|
||||
) : base(options, logger, encoder, clock)
|
||||
{
|
||||
this.applicationProvider = applicationProvider;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handle Authentication ...
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
|
||||
{
|
||||
//
|
||||
var apiKey =
|
||||
!Request.Headers.ContainsKey(HeaderName)
|
||||
? string.Empty
|
||||
: Request.Headers[HeaderName].ToString();
|
||||
if (apiKey.IsNullOrEmpty())
|
||||
{
|
||||
return AuthenticateResult.NoResult();
|
||||
}
|
||||
|
||||
//
|
||||
var clientIP = Request.HttpContext.Connection.RemoteIpAddress?.ToString();
|
||||
|
||||
//
|
||||
var validationResult = await applicationProvider.ValidateApiKey(
|
||||
apiKey: apiKey,
|
||||
clientIP: clientIP
|
||||
);
|
||||
var isValid = !validationResult.Errors.HasChild();
|
||||
if (!isValid)
|
||||
{
|
||||
//
|
||||
var message = validationResult.Errors.ToListString('\n');
|
||||
return AuthenticateResult.Fail(message);
|
||||
}
|
||||
|
||||
//
|
||||
var claims = new[]
|
||||
{
|
||||
new Claim(ClaimTypes.Name, validationResult.OwnerId),
|
||||
new Claim("application_id", validationResult.ApplicationId.ToString()),
|
||||
new Claim("auth_type", "apikey"),
|
||||
new Claim(JwtClaimTypes.Scope, "apikey"),
|
||||
};
|
||||
|
||||
//
|
||||
var scopeClaims = validationResult.Scopes
|
||||
.Select(s => new Claim(JwtClaimTypes.Scope, s));
|
||||
|
||||
//
|
||||
var identity = new ClaimsIdentity(
|
||||
claims.Union(scopeClaims),
|
||||
AuthenticationScheme
|
||||
);
|
||||
|
||||
//
|
||||
var principal = new ClaimsPrincipal(identity);
|
||||
|
||||
//
|
||||
var ticket = new AuthenticationTicket(
|
||||
principal,
|
||||
AuthenticationScheme
|
||||
);
|
||||
|
||||
//
|
||||
return AuthenticateResult.Success(ticket);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,8 @@ using System.Linq.Expressions;
|
||||
using IdentityServer4.Extensions;
|
||||
using xIdentityModels.Extensions;
|
||||
using Microsoft.EntityFrameworkCore.Query;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using AutoMapper;
|
||||
|
||||
namespace xIds.Providers
|
||||
{
|
||||
@@ -21,13 +23,16 @@ namespace xIds.Providers
|
||||
/// </summary>
|
||||
public partial class XApplicationProvider : IXApplicationProvider
|
||||
{
|
||||
private readonly IMemoryCache cache;
|
||||
private readonly string permittedRole;
|
||||
private readonly XIdentityDbContext dbContext;
|
||||
private readonly IXIdentityManager identityManager;
|
||||
private readonly XDataServiceConfiguration configuration;
|
||||
private readonly XApiKeyConfiguration apiKeyConfiguration;
|
||||
private readonly Action<IMapperConfigurationExpression> mapperConfigurationExp;
|
||||
|
||||
public XApplicationProvider(
|
||||
IMemoryCache cache,
|
||||
XIdentityDbContext dbContext,
|
||||
IXIdentityManager identityManager,
|
||||
XDataServiceConfiguration configuration,
|
||||
@@ -36,11 +41,29 @@ namespace xIds.Providers
|
||||
)
|
||||
{
|
||||
//
|
||||
this.cache = cache;
|
||||
this.dbContext = dbContext;
|
||||
this.permittedRole = permittedRole;
|
||||
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>
|
||||
@@ -249,7 +272,7 @@ namespace xIds.Providers
|
||||
{
|
||||
//
|
||||
// Global Result ...
|
||||
var result = dbContext.ApiKeyUsage.AsQueryable();
|
||||
var result = dbContext.ApiKeyUsages.AsQueryable();
|
||||
|
||||
//
|
||||
// Handle Filtering ...
|
||||
@@ -276,5 +299,45 @@ namespace xIds.Providers
|
||||
// Return result ...
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handle Rate Limit Checking ...
|
||||
/// </summary>
|
||||
/// <param name="apiKeyId"></param>
|
||||
/// <param name="limitPerMinute"></param>
|
||||
/// <returns></returns>
|
||||
private bool CheckRateLimit(
|
||||
Guid apiKeyId,
|
||||
int limitPerMinute
|
||||
)
|
||||
{
|
||||
//
|
||||
var cacheKey = $"ratelimit:{apiKeyId}";
|
||||
var currentCount = cache.GetOrCreate(
|
||||
cacheKey,
|
||||
entry =>
|
||||
{
|
||||
//
|
||||
entry.SlidingExpiration = TimeSpan.FromMinutes(1);
|
||||
return 0;
|
||||
}
|
||||
);
|
||||
|
||||
//
|
||||
if (currentCount >= limitPerMinute)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
//
|
||||
cache.Set(
|
||||
cacheKey,
|
||||
currentCount + 1,
|
||||
TimeSpan.FromMinutes(1)
|
||||
);
|
||||
|
||||
//
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -26,11 +26,13 @@ namespace xIds.Providers
|
||||
/// Add Specified Item ...
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="apiKey"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XApiKeyDto> AddApiKeyAsync(
|
||||
XApiKeyDto item,
|
||||
string apiKey = null,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
@@ -85,7 +87,12 @@ namespace xIds.Providers
|
||||
item.CreatedOn = DateTime.UtcNow;
|
||||
|
||||
//
|
||||
var entity = item.MapConvert<XApiKey, XApiKeyDto>();
|
||||
var entity = item.MapConvert<XApiKey, XApiKeyDto>(mapperConfigurationExp);
|
||||
if (!apiKey.IsNullOrEmpty())
|
||||
{
|
||||
entity.Key = apiKey;
|
||||
}
|
||||
|
||||
var entry = await dbContext.ApiKeys.AddAsync(
|
||||
entity: entity,
|
||||
cancellationToken: cancellationToken
|
||||
@@ -170,7 +177,14 @@ namespace xIds.Providers
|
||||
exists = exists.UpdateData(
|
||||
updateWith: item,
|
||||
propertyWhiteList: [],
|
||||
propertyBlackList: []
|
||||
propertyBlackList: [
|
||||
nameof(XApiKey.Id),
|
||||
nameof(XApiKey.KeyHash),
|
||||
nameof(XApiKey.KeyPrefix),
|
||||
nameof(XApiKey.CreatedOn),
|
||||
nameof(XApiKey.Application),
|
||||
nameof(XApiKey.ApplicationId),
|
||||
]
|
||||
);
|
||||
|
||||
//
|
||||
|
||||
@@ -123,6 +123,8 @@ namespace xIds.Providers
|
||||
updateWith: item,
|
||||
propertyWhiteList: [],
|
||||
propertyBlackList: [
|
||||
nameof(XApiKeyUsage.Id),
|
||||
nameof(XApiKeyUsage.ApiKeyId),
|
||||
nameof(XApiKeyUsage.ResponseTimeMs),
|
||||
]
|
||||
);
|
||||
|
||||
@@ -138,7 +138,9 @@ namespace xIds.Providers
|
||||
updateWith: item,
|
||||
propertyWhiteList: [],
|
||||
propertyBlackList: [
|
||||
nameof(XApiKey.Id),
|
||||
nameof(XApplication.ApiKeys),
|
||||
nameof(XApplication.CreatedOn),
|
||||
]
|
||||
);
|
||||
|
||||
|
||||
@@ -8,7 +8,8 @@ using xExceptions.Constants;
|
||||
using System.Threading.Tasks;
|
||||
using xIdentityModels.Models;
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Cryptography;
|
||||
using xIdentityModels.Extensions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace xIds.Providers
|
||||
{
|
||||
@@ -16,6 +17,67 @@ namespace xIds.Providers
|
||||
{
|
||||
//
|
||||
#region Custom Actions ...
|
||||
/// <summary>
|
||||
/// Show Specifid Api Key ...
|
||||
/// </summary>
|
||||
/// <param name="apiKeyId"></param>
|
||||
/// <param name="applicationId"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<string> ShowApiKey(
|
||||
Guid apiKeyId,
|
||||
int applicationId,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (
|
||||
apiKeyId.IsDefaultGuid() ||
|
||||
userInfo.IsNullOrDefault() ||
|
||||
!applicationId.IsValidIntId()
|
||||
)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Check Application Exists ...
|
||||
var application = await GetApplicationAsync(
|
||||
id: applicationId,
|
||||
userInfo: userInfo,
|
||||
includeBuilder: null,
|
||||
ignoreSoftDeleteds: true,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
if (application.IsNullOrDefault())
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Validate Owner ...
|
||||
if (!IsOwned(application, userInfo))
|
||||
{
|
||||
XException.NotAllowed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Retrieve ApiKey Entity ...
|
||||
// Since ApiKey is Only Exists on ApiKey Entity ...
|
||||
var entity = await dbContext.ApiKeys
|
||||
.FirstOrDefaultAsync(x => x.Id == apiKeyId);
|
||||
if (entity.IsNullOrDefault())
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
return entity.Key;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generate a New ApiKey and Add It to DB ...
|
||||
/// </summary>
|
||||
@@ -85,7 +147,7 @@ namespace xIds.Providers
|
||||
|
||||
//
|
||||
// Generate New Key ...
|
||||
(string plainKey, string keyHash, string prefix) = XApiKeyHelper.Generate();
|
||||
(string plainKey, string keyHash, string prefix) = XApiKeyHelper.Generate(apiKeyConfiguration.ApiKeyPrefix);
|
||||
var expireAt = DateTime.UtcNow.Add(expiration.Value);
|
||||
|
||||
//
|
||||
@@ -112,7 +174,8 @@ namespace xIds.Providers
|
||||
//
|
||||
// Add Item to Database ...
|
||||
result = await AddApiKeyAsync(
|
||||
item: item,
|
||||
item: result,
|
||||
apiKey: plainKey,
|
||||
userInfo: userInfo,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
@@ -323,18 +386,158 @@ namespace xIds.Providers
|
||||
/// </summary>
|
||||
/// <param name="apiKey"></param>
|
||||
/// <param name="clientIP"></param>
|
||||
/// <param name="requiredScope"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> ValidateApiKey(
|
||||
public async Task<XApiKeyValidationResult> ValidateApiKey(
|
||||
string apiKey,
|
||||
string clientIP,
|
||||
string requiredScope = null,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
var result = new XApiKeyValidationResult();
|
||||
|
||||
//
|
||||
// Validate ...
|
||||
var isValid =
|
||||
!apiKey.IsNullOrEmpty() &&
|
||||
!clientIP.IsNullOrEmpty();
|
||||
if (!isValid)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Detect Specified Api Key ...
|
||||
var apiKeyHash = XApiKeyHelper.ComputeHash(apiKey);
|
||||
var keyModel = await dbContext.ApiKeys
|
||||
.FirstOrDefaultAsync(
|
||||
ak => ak.KeyHash == apiKeyHash,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
isValid = !keyModel.IsNullOrDefault();
|
||||
if (!isValid)
|
||||
{
|
||||
//
|
||||
result.Errors.Add("ApiKey not found ...");
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Here we Have to Parse Allowed Ips and Scopes ...
|
||||
var allowedIps = (keyModel.AllowedIPs ?? string.Empty)
|
||||
.ParseListString<string>();
|
||||
var allowedScopes = (keyModel.AllowedScopes ?? string.Empty)
|
||||
.FromJSON<IEnumerable<string>>();
|
||||
result.Scopes = [.. allowedScopes];
|
||||
|
||||
//
|
||||
// Check Application is Exists and Active ...
|
||||
var application = await dbContext.Applications
|
||||
.FirstOrDefaultAsync(x => x.Id == keyModel.ApplicationId);
|
||||
if (!application.IsNullOrDefault())
|
||||
{
|
||||
result.OwnerId = application.OwnerId;
|
||||
result.ApplicationId = application.Id;
|
||||
}
|
||||
|
||||
//
|
||||
// Check Application is Exists and Active or not ...
|
||||
var isApplicationActive =
|
||||
//
|
||||
// Application Exists ...
|
||||
!application.IsNullOrDefault() &&
|
||||
//
|
||||
// Check Application is Active ...
|
||||
application.IsActive
|
||||
//
|
||||
;
|
||||
if (!isApplicationActive)
|
||||
{
|
||||
result.Errors.Add("Related Application is Inactive ...");
|
||||
}
|
||||
|
||||
//
|
||||
// Check Key is Active or not ...
|
||||
var isKeyActive =
|
||||
//
|
||||
// Check Api Key is Active ...
|
||||
// Not Revoked ...
|
||||
// Not Expired ...
|
||||
keyModel.IsActive()
|
||||
//
|
||||
;
|
||||
if (!isKeyActive)
|
||||
{
|
||||
//
|
||||
if (keyModel.IsExpired())
|
||||
{
|
||||
result.Errors.Add("ApiKey is Expired ...");
|
||||
}
|
||||
|
||||
//
|
||||
if (keyModel.IsRevoked())
|
||||
{
|
||||
result.Errors.Add("ApiKey is Revoked ...");
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Checking Rate Limit ...
|
||||
var isRateLimitPassed = CheckRateLimit(keyModel.Id, keyModel.RateLimitPerMinute);
|
||||
if (!isRateLimitPassed)
|
||||
{
|
||||
result.Errors.Add("ApiKey Rate Limit Reached ...");
|
||||
}
|
||||
|
||||
//
|
||||
// Audit Log ...
|
||||
|
||||
//
|
||||
// Check Scope and IP is Valid or not ...
|
||||
var isIpValid =
|
||||
!allowedIps.HasChild() ||
|
||||
allowedIps.Contains(clientIP);
|
||||
var isScopeValid =
|
||||
!allowedScopes.HasChild() ||
|
||||
requiredScope.IsNullOrEmpty() ||
|
||||
allowedScopes.Contains(requiredScope);
|
||||
var isScopeIpValid =
|
||||
isIpValid &&
|
||||
isScopeValid
|
||||
;
|
||||
if (!isScopeIpValid)
|
||||
{
|
||||
//
|
||||
if (!isIpValid)
|
||||
{
|
||||
result.Errors.Add("Client IP not Allowed to Use Resource ...");
|
||||
}
|
||||
|
||||
//
|
||||
if (!isScopeValid)
|
||||
{
|
||||
result.Errors.Add("Required Scoped is Invalid ...");
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
isValid =
|
||||
isKeyActive &&
|
||||
isScopeIpValid &&
|
||||
isRateLimitPassed &&
|
||||
isApplicationActive;
|
||||
if (isValid)
|
||||
{
|
||||
result.Errors.Clear();
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using xCommons.Extensions;
|
||||
using xIdentityModels.Dtos;
|
||||
using System.Threading.Tasks;
|
||||
using xIdentityModels.Models;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace xIds.Providers
|
||||
{
|
||||
@@ -22,7 +23,17 @@ namespace xIds.Providers
|
||||
XApiKey source,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{ }
|
||||
{
|
||||
//
|
||||
var result = source.MapConvert<XApiKeyDto, XApiKey>(mapperConfigurationExp);
|
||||
result = await EnrichAsync(
|
||||
source: result,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Enrich Api Key ...
|
||||
@@ -34,7 +45,36 @@ namespace xIds.Providers
|
||||
XApiKeyDto source,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{ }
|
||||
{
|
||||
//
|
||||
if (
|
||||
!source.IsNullOrDefault() &&
|
||||
!source.Application.IsNullOrDefault()
|
||||
)
|
||||
{
|
||||
//
|
||||
var application = await dbContext.Applications
|
||||
.FirstOrDefaultAsync(
|
||||
x => x.Id == source.ApplicationId,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
if (!application.IsNullOrDefault())
|
||||
{
|
||||
//
|
||||
var dto = await EnrichAsync(
|
||||
source: application,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
if (!dto.IsNullOrDefault())
|
||||
{
|
||||
source.Application = dto;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return source;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Enrich Api Key Usage ...
|
||||
@@ -46,7 +86,17 @@ namespace xIds.Providers
|
||||
XApiKeyUsage source,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{ }
|
||||
{
|
||||
//
|
||||
var result = source.MapConvert<XApiKeyUsageDto, XApiKeyUsage>(mapperConfigurationExp);
|
||||
result = await EnrichAsync(
|
||||
source: result,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Enrich Application ...
|
||||
@@ -58,7 +108,17 @@ namespace xIds.Providers
|
||||
XApplication source,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{ }
|
||||
{
|
||||
//
|
||||
var result = source.MapConvert<XApplicationDto, XApplication>(mapperConfigurationExp);
|
||||
result = await EnrichAsync(
|
||||
source: result,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Enrich Api Key Usage ...
|
||||
@@ -70,7 +130,12 @@ namespace xIds.Providers
|
||||
XApiKeyUsageDto source,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{ }
|
||||
{
|
||||
//
|
||||
// There is not any Additional Enrichment ...
|
||||
var result = await Task.FromResult(source);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Enrich Application ...
|
||||
@@ -82,7 +147,24 @@ namespace xIds.Providers
|
||||
XApplicationDto source,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{ }
|
||||
{
|
||||
//
|
||||
if (
|
||||
!source.IsNullOrDefault() &&
|
||||
!source.OwnerId.IsNullOrEmpty()
|
||||
)
|
||||
{
|
||||
//
|
||||
var owner = await GetOwner(source.OwnerId);
|
||||
if (!owner.IsNullOrDefault())
|
||||
{
|
||||
source.Owner = owner;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return source;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Enrich a Collection of Api Key(s) ...
|
||||
@@ -94,7 +176,17 @@ namespace xIds.Providers
|
||||
IEnumerable<XApiKey> sources,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{ }
|
||||
{
|
||||
//
|
||||
var tasks = sources
|
||||
.Select(s => EnrichAsync(s, cancellationToken));
|
||||
|
||||
//
|
||||
var result = await Task.WhenAll(tasks);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Enrich a Collection of Api Key(s) ...
|
||||
@@ -106,7 +198,17 @@ namespace xIds.Providers
|
||||
IEnumerable<XApiKeyDto> sources,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{ }
|
||||
{
|
||||
//
|
||||
var tasks = sources
|
||||
.Select(s => EnrichAsync(s, cancellationToken));
|
||||
|
||||
//
|
||||
var result = await Task.WhenAll(tasks);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Enrich a Collection of Api Key Usage(s) ...
|
||||
@@ -118,7 +220,17 @@ namespace xIds.Providers
|
||||
IEnumerable<XApiKeyUsage> sources,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{ }
|
||||
{
|
||||
//
|
||||
var tasks = sources
|
||||
.Select(s => EnrichAsync(s, cancellationToken));
|
||||
|
||||
//
|
||||
var result = await Task.WhenAll(tasks);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Enrich a Collection of Application(s) ...
|
||||
@@ -130,7 +242,17 @@ namespace xIds.Providers
|
||||
IEnumerable<XApplication> sources,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{ }
|
||||
{
|
||||
//
|
||||
var tasks = sources
|
||||
.Select(s => EnrichAsync(s, cancellationToken));
|
||||
|
||||
//
|
||||
var result = await Task.WhenAll(tasks);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Enrich a Collection of Api Key Usage(s) ...
|
||||
@@ -142,7 +264,17 @@ namespace xIds.Providers
|
||||
IEnumerable<XApiKeyUsageDto> sources,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{ }
|
||||
{
|
||||
//
|
||||
var tasks = sources
|
||||
.Select(s => EnrichAsync(s, cancellationToken));
|
||||
|
||||
//
|
||||
var result = await Task.WhenAll(tasks);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Enrich a Collection of Application(s) ...
|
||||
@@ -154,7 +286,17 @@ namespace xIds.Providers
|
||||
IEnumerable<XApplicationDto> sources,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{ }
|
||||
{
|
||||
//
|
||||
var tasks = sources
|
||||
.Select(s => EnrichAsync(s, cancellationToken));
|
||||
|
||||
//
|
||||
var result = await Task.WhenAll(tasks);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
+18
-11
@@ -1,20 +1,20 @@
|
||||
using System;
|
||||
using xIds.DI;
|
||||
using System.IO;
|
||||
using xIds.Constants;
|
||||
using xIds.Providers;
|
||||
using Newtonsoft.Json;
|
||||
using xIds.Interfaces;
|
||||
using System.Reflection;
|
||||
using xStorageService.DI;
|
||||
using xCommons.Extensions;
|
||||
using xCommons.Configurations;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using xCommons.Configurations;
|
||||
using xCommons.Extensions;
|
||||
using xStorageService.DI;
|
||||
using xIds.Constants;
|
||||
using xIds.DI;
|
||||
using xIds.Interfaces;
|
||||
using xIds.Providers;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace xIds
|
||||
{
|
||||
@@ -57,13 +57,20 @@ namespace xIds
|
||||
// services.AddInMemoryXIdentityServer (Configuration);
|
||||
services.AddEfSupportXIdentityServer(Configuration);
|
||||
|
||||
//
|
||||
// Services Application and Api Key Management Services ...
|
||||
services.AddXApplicationProvider(Configuration);
|
||||
|
||||
//
|
||||
services.AddControllers()
|
||||
.AddNewtonsoftJson(x => x.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore);
|
||||
|
||||
//
|
||||
// Register Authentication ...
|
||||
services.AddXIdentityServerAuthentication(Configuration);
|
||||
services.AddXIdentityServerAuthentication(
|
||||
configuration: Configuration,
|
||||
addApiKeyAuthentication: true
|
||||
);
|
||||
|
||||
//
|
||||
// Register Authorization ...
|
||||
|
||||
@@ -37,11 +37,12 @@
|
||||
"MaxFileSize": 41943040
|
||||
},
|
||||
"ApiKeyConfiguration": {
|
||||
"ApiKeyPrefix": "xapp",
|
||||
"DefaultRateLimit": 60,
|
||||
"EnableAuditLog": false,
|
||||
"MaxKeysPerApplication": "10",
|
||||
"MaxExpirationMinutes": "525600",
|
||||
"DefaultExpirationMinutes": "43200"
|
||||
"MaxKeysPerApplication": 10,
|
||||
"MaxExpirationMinutes": 525600,
|
||||
"DefaultExpirationMinutes": 43200
|
||||
},
|
||||
"DbSeeder": {
|
||||
"UpdateExists": false,
|
||||
|
||||
+5
-2
@@ -29,13 +29,16 @@
|
||||
|
||||
<!-- 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>
|
||||
|
||||
<!-- IdentityServer -->
|
||||
|
||||
Reference in New Issue
Block a user