- Fix HttpContext RetrieveTokenAsXTokenResponse Extension Method;

- Add Extract Api Key Extension Method from HttpContext;
- Add Support for Token Introspect Validation in XIdentityService;
This commit is contained in:
2026-10-23 19:52:31 +03:30
parent 72354dad4f
commit d43428e0b6
4 changed files with 186 additions and 99 deletions
+1 -1
View File
@@ -141,7 +141,7 @@ namespace xIdentityService.Extensions
/// Retrieve All Required Tokens /// Retrieve All Required Tokens
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public static async Task<XTokenResponse> RetrieveTokensAsXTokenResponse(HttpContext context) public static async Task<XTokenResponse> RetrieveTokensAsXTokenResponse(this HttpContext context)
{ {
// //
var accessToken = await GetAccessToken(context); var accessToken = await GetAccessToken(context);
+10
View File
@@ -6,6 +6,16 @@ namespace xIdentityService.Extensions
{ {
public static class XRequestExtensions public static class XRequestExtensions
{ {
/// <summary>
/// Extract ApiKey from Request Headers ...
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static string ExtractApiKey(this HttpContext source)
{
return source.Request.ExtractApiKey();
}
/// <summary> /// <summary>
/// Extract ApiKey from Request Headers ... /// Extract ApiKey from Request Headers ...
/// </summary> /// </summary>
+1
View File
@@ -44,6 +44,7 @@ namespace xIdentityService.Interfaces
#region Authentication ... #region Authentication ...
Task<DiscoveryDocumentResponse> RequestDiscoveryDocument(); Task<DiscoveryDocumentResponse> RequestDiscoveryDocument();
Task<XTokenResponse> RequestScopeAccessToken(string scope); Task<XTokenResponse> RequestScopeAccessToken(string scope);
Task<TokenIntrospectionResponse> Validate(XTokenResponse model);
Task<TokenIntrospectionResponse> Introspection(XTokenResponse model); Task<TokenIntrospectionResponse> Introspection(XTokenResponse model);
Task<XLoginResponse> Login(XLoginRequest model); Task<XLoginResponse> Login(XLoginRequest model);
Task Logout(XTokenResponse model); Task Logout(XTokenResponse model);
+102 -26
View File
@@ -9,24 +9,29 @@ using System.Collections.Generic;
using xIdentityModels.Extensions; using xIdentityModels.Extensions;
using xIdentityService.Constants; using xIdentityService.Constants;
namespace xIdentityService.Providers { namespace xIdentityService.Providers
public partial class XIdentityProvider { {
public partial class XIdentityProvider
{
// //
#region Authentication Handlers ... #region Authentication Handlers ...
/// <summary> /// <summary>
/// Request for Discovery Document /// Request for Discovery Document
/// </summary> /// </summary>
/// <returns>an instance of DiscoveryDocumentResponse</returns> /// <returns>an instance of DiscoveryDocumentResponse</returns>
public async Task<DiscoveryDocumentResponse> RequestDiscoveryDocument () { public async Task<DiscoveryDocumentResponse> RequestDiscoveryDocument()
{
// //
var httpClient = GetHttpClient(); var httpClient = GetHttpClient();
// //
try { try
{
// //
var result = httpClient var result = httpClient
.GetDiscoveryDocumentAsync(identityConfiguration.Authority) .GetDiscoveryDocumentAsync(identityConfiguration.Authority)
.ContinueWith (docTask => { .ContinueWith(docTask =>
{
// //
httpClient.Dispose(); httpClient.Dispose();
return docTask.Result; return docTask.Result;
@@ -34,7 +39,9 @@ namespace xIdentityService.Providers {
// //
return await result; return await result;
} catch { }
catch
{
throw XException.ActionFailed.ToException(); throw XException.ActionFailed.ToException();
} }
} }
@@ -44,14 +51,18 @@ namespace xIdentityService.Providers {
/// </summary> /// </summary>
/// <param name="scope">a member of <see>XApiScope</see></param> /// <param name="scope">a member of <see>XApiScope</see></param>
/// <returns>an instance of <see>XTokenResponse</see></returns> /// <returns>an instance of <see>XTokenResponse</see></returns>
public async Task<XTokenResponse> RequestScopeAccessToken (string scope) { public async Task<XTokenResponse> RequestScopeAccessToken(string scope)
{
// //
// Validate Args ... // Validate Args ...
var scopeName = string.Empty; var scopeName = string.Empty;
try { try
{
scopeName = scope; // scope.GetStringValue (); scopeName = scope; // scope.GetStringValue ();
} catch { } }
if (scopeName.IsNullOrEmpty ()) { catch { }
if (scopeName.IsNullOrEmpty())
{
XException.InvalidArgs.Throw(); XException.InvalidArgs.Throw();
} }
@@ -70,7 +81,8 @@ namespace xIdentityService.Providers {
// //
// Save Token ... // Save Token ...
if (!response.IsNull ()) { if (!response.IsNull())
{
await tokenProvider.AddOrUpdateToken(response); await tokenProvider.AddOrUpdateToken(response);
} }
@@ -83,7 +95,8 @@ namespace xIdentityService.Providers {
/// </summary> /// </summary>
/// <param name="model">Authentication Tokens</param> /// <param name="model">Authentication Tokens</param>
/// <returns>an instance of TokenIntrospectionResponse</returns> /// <returns>an instance of TokenIntrospectionResponse</returns>
public async Task<TokenIntrospectionResponse> Introspection (XTokenResponse model) { public async Task<TokenIntrospectionResponse> Introspection(XTokenResponse model)
{
// //
// Validate Args ... // Validate Args ...
await validationProvider await validationProvider
@@ -96,15 +109,18 @@ namespace xIdentityService.Providers {
var httpClient = GetHttpClient(); var httpClient = GetHttpClient();
// //
try { try
{
// //
var discoDoc = await RequestDiscoveryDocument(); var discoDoc = await RequestDiscoveryDocument();
if (discoDoc.IsError) { if (discoDoc.IsError)
{
XException.ActionFailed.Throw(); XException.ActionFailed.Throw();
} }
// //
var response = await httpClient.IntrospectTokenAsync (new TokenIntrospectionRequest { var response = await httpClient.IntrospectTokenAsync(new TokenIntrospectionRequest
{
// //
Address = discoDoc.IntrospectionEndpoint, Address = discoDoc.IntrospectionEndpoint,
ClientId = identityConfiguration.ClientId, ClientId = identityConfiguration.ClientId,
@@ -113,23 +129,75 @@ namespace xIdentityService.Providers {
// //
Token = model.AccessToken Token = model.AccessToken
}); });
if (response.IsError) { if (response.IsError)
{
throw new Exception(response.Error); throw new Exception(response.Error);
} }
// //
return response; return response;
} catch { }
catch
{
throw XException.ActionFailed.ToException(); throw XException.ActionFailed.ToException();
} }
} }
/// <summary>
/// Validate Specified Token Model ...
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public async Task<TokenIntrospectionResponse> Validate(XTokenResponse model)
{
//
// Validate Args ...
await validationProvider
.GroupValidationBuilder()
.AddNotNull(model)
.AddNotEmpty(model.AccessToken)
.ValidateGroupAsync();
//
var httpClient = GetHttpClient();
//
TokenIntrospectionResponse response = null;
try
{
//
var discoDoc = await RequestDiscoveryDocument();
if (discoDoc.IsError)
{
XException.ActionFailed.Throw();
}
//
response = await httpClient.IntrospectTokenAsync(new TokenIntrospectionRequest
{
//
Address = discoDoc.IntrospectionEndpoint,
ClientId = identityConfiguration.ClientId,
ClientSecret = identityConfiguration.ClientSecret,
//
Token = model.AccessToken
});
}
catch
{ }
//
return response;
}
/// <summary> /// <summary>
/// Authenticate a User /// Authenticate a User
/// </summary> /// </summary>
/// <param name="model">User Login Required Info, an instance of <see>XLoginRequest</see></param> /// <param name="model">User Login Required Info, an instance of <see>XLoginRequest</see></param>
/// <returns>an instance of <see>XTokenResponse</see></returns> /// <returns>an instance of <see>XTokenResponse</see></returns>
public async Task<XTokenResponse> Authenticate (XLoginRequest model) { public async Task<XTokenResponse> Authenticate(XLoginRequest model)
{
// //
// Validate Args ... // Validate Args ...
validationProvider validationProvider
@@ -155,7 +223,8 @@ namespace xIdentityService.Providers {
// //
// Save Token ... // Save Token ...
if (!response.IsNull ()) { if (!response.IsNull())
{
await tokenProvider.AddOrUpdateToken(response); await tokenProvider.AddOrUpdateToken(response);
} }
@@ -168,7 +237,8 @@ namespace xIdentityService.Providers {
/// </summary> /// </summary>
/// <param name="model">User Login Required Info, an instance of <see>XLoginRequest</see></param> /// <param name="model">User Login Required Info, an instance of <see>XLoginRequest</see></param>
/// <returns>an instance of <see>XTokenResponse</see></returns> /// <returns>an instance of <see>XTokenResponse</see></returns>
public async Task<XLoginResponse> Login (XLoginRequest model) { public async Task<XLoginResponse> Login(XLoginRequest model)
{
// //
// Validate Args ... // Validate Args ...
validationProvider validationProvider
@@ -193,7 +263,8 @@ namespace xIdentityService.Providers {
); );
// //
if (!response.IsNull ()) { if (!response.IsNull())
{
// //
var tokens = response.ToXTokenResponse(); var tokens = response.ToXTokenResponse();
await tokenProvider.AddOrUpdateToken(tokens); await tokenProvider.AddOrUpdateToken(tokens);
@@ -208,7 +279,8 @@ namespace xIdentityService.Providers {
/// </summary> /// </summary>
/// <param name="model"></param> /// <param name="model"></param>
/// <returns></returns> /// <returns></returns>
public async Task Logout (XTokenResponse model) { public async Task Logout(XTokenResponse model)
{
// //
await validationProvider await validationProvider
.GroupValidationBuilder() .GroupValidationBuilder()
@@ -219,7 +291,8 @@ namespace xIdentityService.Providers {
// //
var isExists = await tokenProvider var isExists = await tokenProvider
.IsTokenExists(model.AccessToken); .IsTokenExists(model.AccessToken);
if (isExists) { if (isExists)
{
await tokenProvider await tokenProvider
.RemoveToken(model.AccessToken); .RemoveToken(model.AccessToken);
} }
@@ -230,7 +303,8 @@ namespace xIdentityService.Providers {
/// </summary> /// </summary>
/// <param name="model">Authentication Tokens</param> /// <param name="model">Authentication Tokens</param>
/// <returns>an instance of <see>XTokenResponse</see></returns> /// <returns>an instance of <see>XTokenResponse</see></returns>
public async Task<XTokenResponse> RefreshTokens (XTokenResponse model) { public async Task<XTokenResponse> RefreshTokens(XTokenResponse model)
{
// //
// Validate Args ... // Validate Args ...
validationProvider validationProvider
@@ -257,7 +331,8 @@ namespace xIdentityService.Providers {
request, request,
XHttpMethod.POST XHttpMethod.POST
); );
if (!response.IsNull ()) { if (!response.IsNull())
{
await tokenProvider.UpdateToken(response); await tokenProvider.UpdateToken(response);
} }
@@ -277,7 +352,8 @@ namespace xIdentityService.Providers {
public async Task ChangePassword( public async Task ChangePassword(
XTokenResponse tokens, XTokenResponse tokens,
XActionRequest model XActionRequest model
) { )
{
// //
// Validate Args ... // Validate Args ...
await validationProvider await validationProvider