- 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:
@@ -141,7 +141,7 @@ namespace xIdentityService.Extensions
|
||||
/// Retrieve All Required Tokens
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static async Task<XTokenResponse> RetrieveTokensAsXTokenResponse(HttpContext context)
|
||||
public static async Task<XTokenResponse> RetrieveTokensAsXTokenResponse(this HttpContext context)
|
||||
{
|
||||
//
|
||||
var accessToken = await GetAccessToken(context);
|
||||
|
||||
@@ -6,6 +6,16 @@ namespace xIdentityService.Extensions
|
||||
{
|
||||
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>
|
||||
/// Extract ApiKey from Request Headers ...
|
||||
/// </summary>
|
||||
|
||||
@@ -44,6 +44,7 @@ namespace xIdentityService.Interfaces
|
||||
#region Authentication ...
|
||||
Task<DiscoveryDocumentResponse> RequestDiscoveryDocument();
|
||||
Task<XTokenResponse> RequestScopeAccessToken(string scope);
|
||||
Task<TokenIntrospectionResponse> Validate(XTokenResponse model);
|
||||
Task<TokenIntrospectionResponse> Introspection(XTokenResponse model);
|
||||
Task<XLoginResponse> Login(XLoginRequest model);
|
||||
Task Logout(XTokenResponse model);
|
||||
|
||||
@@ -9,33 +9,40 @@ using System.Collections.Generic;
|
||||
using xIdentityModels.Extensions;
|
||||
using xIdentityService.Constants;
|
||||
|
||||
namespace xIdentityService.Providers {
|
||||
public partial class XIdentityProvider {
|
||||
namespace xIdentityService.Providers
|
||||
{
|
||||
public partial class XIdentityProvider
|
||||
{
|
||||
//
|
||||
#region Authentication Handlers ...
|
||||
/// <summary>
|
||||
/// Request for Discovery Document
|
||||
/// </summary>
|
||||
/// <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
|
||||
.GetDiscoveryDocumentAsync (identityConfiguration.Authority)
|
||||
.ContinueWith (docTask => {
|
||||
.GetDiscoveryDocumentAsync(identityConfiguration.Authority)
|
||||
.ContinueWith(docTask =>
|
||||
{
|
||||
//
|
||||
httpClient.Dispose ();
|
||||
httpClient.Dispose();
|
||||
return docTask.Result;
|
||||
});
|
||||
|
||||
//
|
||||
return await result;
|
||||
} catch {
|
||||
throw XException.ActionFailed.ToException ();
|
||||
}
|
||||
catch
|
||||
{
|
||||
throw XException.ActionFailed.ToException();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,25 +51,29 @@ namespace xIdentityService.Providers {
|
||||
/// </summary>
|
||||
/// <param name="scope">a member of <see>XApiScope</see></param>
|
||||
/// <returns>an instance of <see>XTokenResponse</see></returns>
|
||||
public async Task<XTokenResponse> RequestScopeAccessToken (string scope) {
|
||||
public async Task<XTokenResponse> RequestScopeAccessToken(string scope)
|
||||
{
|
||||
//
|
||||
// Validate Args ...
|
||||
var scopeName = string.Empty;
|
||||
try {
|
||||
try
|
||||
{
|
||||
scopeName = scope; // scope.GetStringValue ();
|
||||
} catch { }
|
||||
if (scopeName.IsNullOrEmpty ()) {
|
||||
XException.InvalidArgs.Throw ();
|
||||
}
|
||||
catch { }
|
||||
if (scopeName.IsNullOrEmpty())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var client = GetRestClient ();
|
||||
var request = GetRestRequet (
|
||||
var client = GetRestClient();
|
||||
var request = GetRestRequet(
|
||||
XApiAccountEndpoint.RequestScopeAccessToken,
|
||||
headers : new Dictionary<string, string> { { XParam.XScope.GetStringValue (), scope.ToString () } });
|
||||
headers: new Dictionary<string, string> { { XParam.XScope.GetStringValue(), scope.ToString() } });
|
||||
|
||||
//
|
||||
var response = await RunRestRequest<XTokenResponse> (
|
||||
var response = await RunRestRequest<XTokenResponse>(
|
||||
client,
|
||||
request,
|
||||
XHttpMethod.POST
|
||||
@@ -70,8 +81,9 @@ namespace xIdentityService.Providers {
|
||||
|
||||
//
|
||||
// Save Token ...
|
||||
if (!response.IsNull ()) {
|
||||
await tokenProvider.AddOrUpdateToken (response);
|
||||
if (!response.IsNull())
|
||||
{
|
||||
await tokenProvider.AddOrUpdateToken(response);
|
||||
}
|
||||
|
||||
//
|
||||
@@ -83,45 +95,100 @@ namespace xIdentityService.Providers {
|
||||
/// </summary>
|
||||
/// <param name="model">Authentication Tokens</param>
|
||||
/// <returns>an instance of TokenIntrospectionResponse</returns>
|
||||
public async Task<TokenIntrospectionResponse> Introspection (XTokenResponse model) {
|
||||
public async Task<TokenIntrospectionResponse> Introspection(XTokenResponse model)
|
||||
{
|
||||
//
|
||||
// Validate Args ...
|
||||
await validationProvider
|
||||
.GroupValidationBuilder ()
|
||||
.AddNotNull (model)
|
||||
.AddNotEmpty (model.AccessToken)
|
||||
.ValidateGroupAsync ();
|
||||
.GroupValidationBuilder()
|
||||
.AddNotNull(model)
|
||||
.AddNotEmpty(model.AccessToken)
|
||||
.ValidateGroupAsync();
|
||||
|
||||
//
|
||||
var httpClient = GetHttpClient ();
|
||||
var httpClient = GetHttpClient();
|
||||
|
||||
//
|
||||
try {
|
||||
try
|
||||
{
|
||||
//
|
||||
var discoDoc = await RequestDiscoveryDocument ();
|
||||
if (discoDoc.IsError) {
|
||||
XException.ActionFailed.Throw ();
|
||||
var discoDoc = await RequestDiscoveryDocument();
|
||||
if (discoDoc.IsError)
|
||||
{
|
||||
XException.ActionFailed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var response = await httpClient.IntrospectTokenAsync (new TokenIntrospectionRequest {
|
||||
var response = await httpClient.IntrospectTokenAsync(new TokenIntrospectionRequest
|
||||
{
|
||||
//
|
||||
Address = discoDoc.IntrospectionEndpoint,
|
||||
ClientId = identityConfiguration.ClientId,
|
||||
ClientSecret = identityConfiguration.ClientSecret,
|
||||
ClientId = identityConfiguration.ClientId,
|
||||
ClientSecret = identityConfiguration.ClientSecret,
|
||||
|
||||
//
|
||||
Token = model.AccessToken
|
||||
//
|
||||
Token = model.AccessToken
|
||||
});
|
||||
if (response.IsError) {
|
||||
throw new Exception (response.Error);
|
||||
if (response.IsError)
|
||||
{
|
||||
throw new Exception(response.Error);
|
||||
}
|
||||
|
||||
//
|
||||
return response;
|
||||
} catch {
|
||||
throw XException.ActionFailed.ToException ();
|
||||
}
|
||||
catch
|
||||
{
|
||||
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>
|
||||
@@ -129,25 +196,26 @@ namespace xIdentityService.Providers {
|
||||
/// </summary>
|
||||
/// <param name="model">User Login Required Info, an instance of <see>XLoginRequest</see></param>
|
||||
/// <returns>an instance of <see>XTokenResponse</see></returns>
|
||||
public async Task<XTokenResponse> Authenticate (XLoginRequest model) {
|
||||
public async Task<XTokenResponse> Authenticate(XLoginRequest model)
|
||||
{
|
||||
//
|
||||
// Validate Args ...
|
||||
validationProvider
|
||||
.GroupValidationBuilder ()
|
||||
.AddNotNull (model)
|
||||
.AddNotEmpty (
|
||||
.GroupValidationBuilder()
|
||||
.AddNotNull(model)
|
||||
.AddNotEmpty(
|
||||
model.UserSelectBy,
|
||||
model.Password
|
||||
)
|
||||
.ValidateGroup ();
|
||||
.ValidateGroup();
|
||||
|
||||
//
|
||||
var client = GetRestClient ();
|
||||
var request = GetRestRequet (XApiAccountEndpoint.Authenticate);
|
||||
request.AddJsonBody (model);
|
||||
var client = GetRestClient();
|
||||
var request = GetRestRequet(XApiAccountEndpoint.Authenticate);
|
||||
request.AddJsonBody(model);
|
||||
|
||||
//
|
||||
var response = await RunRestRequest<XTokenResponse> (
|
||||
var response = await RunRestRequest<XTokenResponse>(
|
||||
client,
|
||||
request,
|
||||
XHttpMethod.POST
|
||||
@@ -155,8 +223,9 @@ namespace xIdentityService.Providers {
|
||||
|
||||
//
|
||||
// Save Token ...
|
||||
if (!response.IsNull ()) {
|
||||
await tokenProvider.AddOrUpdateToken (response);
|
||||
if (!response.IsNull())
|
||||
{
|
||||
await tokenProvider.AddOrUpdateToken(response);
|
||||
}
|
||||
|
||||
//
|
||||
@@ -168,35 +237,37 @@ namespace xIdentityService.Providers {
|
||||
/// </summary>
|
||||
/// <param name="model">User Login Required Info, an instance of <see>XLoginRequest</see></param>
|
||||
/// <returns>an instance of <see>XTokenResponse</see></returns>
|
||||
public async Task<XLoginResponse> Login (XLoginRequest model) {
|
||||
public async Task<XLoginResponse> Login(XLoginRequest model)
|
||||
{
|
||||
//
|
||||
// Validate Args ...
|
||||
validationProvider
|
||||
.GroupValidationBuilder ()
|
||||
.AddNotNull (model, model.Device)
|
||||
.AddNotEmpty (
|
||||
.GroupValidationBuilder()
|
||||
.AddNotNull(model, model.Device)
|
||||
.AddNotEmpty(
|
||||
model.UserSelectBy,
|
||||
model.Password
|
||||
)
|
||||
.ValidateGroup ();
|
||||
.ValidateGroup();
|
||||
|
||||
//
|
||||
var client = GetRestClient ();
|
||||
var request = GetRestRequet (XApiAccountEndpoint.Login);
|
||||
request.AddJsonBody (model);
|
||||
var client = GetRestClient();
|
||||
var request = GetRestRequet(XApiAccountEndpoint.Login);
|
||||
request.AddJsonBody(model);
|
||||
|
||||
//
|
||||
var response = await RunRestRequest<XLoginResponse> (
|
||||
var response = await RunRestRequest<XLoginResponse>(
|
||||
client,
|
||||
request,
|
||||
XHttpMethod.POST
|
||||
);
|
||||
|
||||
//
|
||||
if (!response.IsNull ()) {
|
||||
if (!response.IsNull())
|
||||
{
|
||||
//
|
||||
var tokens = response.ToXTokenResponse ();
|
||||
await tokenProvider.AddOrUpdateToken (tokens);
|
||||
var tokens = response.ToXTokenResponse();
|
||||
await tokenProvider.AddOrUpdateToken(tokens);
|
||||
}
|
||||
|
||||
//
|
||||
@@ -208,20 +279,22 @@ namespace xIdentityService.Providers {
|
||||
/// </summary>
|
||||
/// <param name="model"></param>
|
||||
/// <returns></returns>
|
||||
public async Task Logout (XTokenResponse model) {
|
||||
public async Task Logout(XTokenResponse model)
|
||||
{
|
||||
//
|
||||
await validationProvider
|
||||
.GroupValidationBuilder ()
|
||||
.AddNotNull (model)
|
||||
.AddNotEmpty (model.AccessToken)
|
||||
.ValidateGroupAsync ();
|
||||
.GroupValidationBuilder()
|
||||
.AddNotNull(model)
|
||||
.AddNotEmpty(model.AccessToken)
|
||||
.ValidateGroupAsync();
|
||||
|
||||
//
|
||||
var isExists = await tokenProvider
|
||||
.IsTokenExists (model.AccessToken);
|
||||
if (isExists) {
|
||||
.IsTokenExists(model.AccessToken);
|
||||
if (isExists)
|
||||
{
|
||||
await tokenProvider
|
||||
.RemoveToken (model.AccessToken);
|
||||
.RemoveToken(model.AccessToken);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -230,35 +303,37 @@ namespace xIdentityService.Providers {
|
||||
/// </summary>
|
||||
/// <param name="model">Authentication Tokens</param>
|
||||
/// <returns>an instance of <see>XTokenResponse</see></returns>
|
||||
public async Task<XTokenResponse> RefreshTokens (XTokenResponse model) {
|
||||
public async Task<XTokenResponse> RefreshTokens(XTokenResponse model)
|
||||
{
|
||||
//
|
||||
// Validate Args ...
|
||||
validationProvider
|
||||
.GroupValidationBuilder ()
|
||||
.AddNotNull (model)
|
||||
.AddNotEmpty (
|
||||
.GroupValidationBuilder()
|
||||
.AddNotNull(model)
|
||||
.AddNotEmpty(
|
||||
model.AccessToken,
|
||||
model.RefreshToken
|
||||
)
|
||||
.ValidateGroup ();
|
||||
.ValidateGroup();
|
||||
|
||||
//
|
||||
var client = GetRestClient (model);
|
||||
var client = GetRestClient(model);
|
||||
|
||||
//
|
||||
var request = GetRestRequet (
|
||||
var request = GetRestRequet(
|
||||
XApiAccountEndpoint.RefreshTokens,
|
||||
headers : model.ToHeaderDictionary ());
|
||||
request.AddJsonBody (model);
|
||||
headers: model.ToHeaderDictionary());
|
||||
request.AddJsonBody(model);
|
||||
|
||||
//
|
||||
var response = await RunRestRequest<XTokenResponse> (
|
||||
var response = await RunRestRequest<XTokenResponse>(
|
||||
client,
|
||||
request,
|
||||
XHttpMethod.POST
|
||||
);
|
||||
if (!response.IsNull ()) {
|
||||
await tokenProvider.UpdateToken (response);
|
||||
if (!response.IsNull())
|
||||
{
|
||||
await tokenProvider.UpdateToken(response);
|
||||
}
|
||||
|
||||
//
|
||||
@@ -274,39 +349,40 @@ namespace xIdentityService.Providers {
|
||||
/// <param name="tokens">Authentication Tokens</param>
|
||||
/// <param name="model">an instance of <see>XActionRequest</see> class which provider requirement for action</param>
|
||||
/// <returns></returns>
|
||||
public async Task ChangePassword (
|
||||
public async Task ChangePassword(
|
||||
XTokenResponse tokens,
|
||||
XActionRequest model
|
||||
) {
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate Args ...
|
||||
await validationProvider
|
||||
.GroupValidationBuilder ()
|
||||
.AddNotNull (tokens, model)
|
||||
.AddNotEmpty (
|
||||
.GroupValidationBuilder()
|
||||
.AddNotNull(tokens, model)
|
||||
.AddNotEmpty(
|
||||
model.Lang,
|
||||
model.Password,
|
||||
model.NewPassword,
|
||||
model.ReturnUrl)
|
||||
.AddNotNull (model.Device)
|
||||
.ValidateGroupAsync ();
|
||||
.AddNotNull(model.Device)
|
||||
.ValidateGroupAsync();
|
||||
|
||||
//
|
||||
// Validate User Select By Param ...
|
||||
var userSelectByParam = GetUserSelectByParam (
|
||||
var userSelectByParam = GetUserSelectByParam(
|
||||
model,
|
||||
forceNotNull : true,
|
||||
excludes : null);
|
||||
forceNotNull: true,
|
||||
excludes: null);
|
||||
|
||||
//
|
||||
var client = GetRestClient (tokens);
|
||||
var request = GetRestRequet (
|
||||
var client = GetRestClient(tokens);
|
||||
var request = GetRestRequet(
|
||||
XApiAccountEndpoint.ChangePassword,
|
||||
headers : tokens.ToHeaderDictionary ());
|
||||
request.AddJsonBody (model);
|
||||
headers: tokens.ToHeaderDictionary());
|
||||
request.AddJsonBody(model);
|
||||
|
||||
//
|
||||
var response = await RunRestRequest<Object> (
|
||||
var response = await RunRestRequest<Object>(
|
||||
client,
|
||||
request,
|
||||
XHttpMethod.POST
|
||||
|
||||
Reference in New Issue
Block a user