last ...
This commit is contained in:
@@ -21,6 +21,7 @@ using xIdentityModels.Models;
|
||||
using xIdentityService.Configuration;
|
||||
using xIdentityService.Constants;
|
||||
using xIdentityService.Interfaces;
|
||||
using xIdentityService.Models;
|
||||
|
||||
namespace xIdentityService.Providers {
|
||||
public partial class XIdentityProvider : IXIdentityProvider {
|
||||
@@ -30,16 +31,18 @@ namespace xIdentityService.Providers {
|
||||
private readonly XValidationProvider validationProvider;
|
||||
private readonly XIdentityServiceConfiguration identityConfiguration;
|
||||
|
||||
public ILogger<XIdentityProvider> Logger { get; }
|
||||
public IXSecurityProvider SecurityProvider { get; }
|
||||
|
||||
public string BaseUrl { get; }
|
||||
public string RevisionSecretKey { get; }
|
||||
|
||||
public bool ReadResponseAsString { get; set; }
|
||||
public ILogger<XIdentityProvider> Logger { get; }
|
||||
|
||||
public XIdentityProvider (
|
||||
IXTokenProvider tokenProvider,
|
||||
IHostingEnvironment environment,
|
||||
ILogger<XIdentityProvider> logger,
|
||||
IXSecurityProvider securityProvider,
|
||||
XIdentityServiceConfiguration identityConfiguration,
|
||||
XValidationProvider validationProvider
|
||||
) {
|
||||
@@ -50,6 +53,7 @@ namespace xIdentityService.Providers {
|
||||
//
|
||||
this.environment = environment;
|
||||
Logger = logger;
|
||||
SecurityProvider = securityProvider;
|
||||
|
||||
//
|
||||
this.identityConfiguration = identityConfiguration;
|
||||
@@ -345,6 +349,149 @@ namespace xIdentityService.Providers {
|
||||
//
|
||||
return response;
|
||||
}
|
||||
public async Task<XBaseRestResponse<T>> RunRestRequestByResponse<T> (
|
||||
RestClient client,
|
||||
RestRequest request,
|
||||
XHttpMethod method,
|
||||
bool supportRefreshingTokens = true
|
||||
) {
|
||||
//
|
||||
var response = await Task.Run (async () => {
|
||||
//
|
||||
IRestResponse resp = null;
|
||||
|
||||
//
|
||||
// Call Endpoint Service using Specified Method ...
|
||||
switch (method) {
|
||||
//
|
||||
// Get ...
|
||||
case XHttpMethod.GET:
|
||||
resp = client.Get (request);
|
||||
break;
|
||||
|
||||
//
|
||||
// Put ...
|
||||
case XHttpMethod.PUT:
|
||||
resp = client.Put (request);
|
||||
break;
|
||||
|
||||
//
|
||||
// Post ...
|
||||
case XHttpMethod.POST:
|
||||
resp = client.Post (request);
|
||||
break;
|
||||
|
||||
//
|
||||
// Delete ...
|
||||
case XHttpMethod.DELETE:
|
||||
resp = client.Delete (request);
|
||||
break;
|
||||
}
|
||||
|
||||
//
|
||||
if (!resp.IsSuccessful) {
|
||||
//
|
||||
// Log Exception ...
|
||||
Logger.LogError ($"Error: {resp.ErrorMessage}");
|
||||
Logger.LogError ($"Exception: {resp.ErrorException.ToJSON()}");
|
||||
if (!resp.ErrorMessage.IsNullOrEmpty () &&
|
||||
resp.ErrorMessage.Contains ("timed out")) {
|
||||
XException.Timeout.Throw ();
|
||||
}
|
||||
|
||||
//
|
||||
// TODO: Check UnAuthorized Status here for
|
||||
// Refresh Tokens or Issue Correct Exception ...
|
||||
if (supportRefreshingTokens &&
|
||||
resp.StatusCode == System.Net.HttpStatusCode.Unauthorized) {
|
||||
//
|
||||
var isRefreshed = request.Parameters.Any (rp =>
|
||||
rp.Name == XParam.XIsRefreshed.GetStringValue () &&
|
||||
rp.Value.ToString ().ToNormalString () == true.ToJSON ());
|
||||
|
||||
//
|
||||
Logger.LogInformation ($"isRefreshed: {isRefreshed}");
|
||||
|
||||
//
|
||||
// Get Access Token ...
|
||||
XTokenResponse tokens = null;
|
||||
var accessToken = request.Parameters.FirstOrDefault (rp =>
|
||||
rp.Name.ToNormalString () == XAuthorization.Header.ToNormalString ()).Value.ToString ();
|
||||
if (!accessToken.IsNullOrEmpty ()) {
|
||||
//
|
||||
accessToken = accessToken.Replace (XAuthorization.TokenIdentifier, "");
|
||||
|
||||
//
|
||||
if (!accessToken.IsNullOrEmpty ()) {
|
||||
tokens = await tokenProvider.RetrieveToken (accessToken);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Refresh Token and then re do request ...
|
||||
if (!isRefreshed &&
|
||||
!accessToken.IsNullOrEmpty () &&
|
||||
!tokens.IsNull () &&
|
||||
tokens.IsRefreshable ()) {
|
||||
//
|
||||
request.AddHeader (XParam.XIsRefreshed.GetStringValue (), true.ToJSON ());
|
||||
|
||||
//
|
||||
// Refreshe Tokens ...
|
||||
var refreshedTokens = await RefreshTokens (tokens);
|
||||
|
||||
//
|
||||
// Renew Rest Client with Refreshed Tokens ...
|
||||
var timeout = client.Timeout;
|
||||
client = GetRestClient (refreshedTokens);
|
||||
client.Timeout = Timeout.Infinite;
|
||||
|
||||
//
|
||||
// Do Request Again using renew Client ...
|
||||
return await RunRestRequestByResponse<T> (
|
||||
client: client,
|
||||
request: request,
|
||||
method: method
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
var ex = resp.Content.ToXError ();
|
||||
if (!ex.IsNull ()) {
|
||||
throw ex.ToException ();
|
||||
}
|
||||
|
||||
//
|
||||
// UnAuthorized ...
|
||||
if (resp.StatusCode == System.Net.HttpStatusCode.Unauthorized) {
|
||||
XException.NotAuthorized.Throw ();
|
||||
}
|
||||
|
||||
//
|
||||
// NotFound ...
|
||||
if (resp.StatusCode == System.Net.HttpStatusCode.NotFound) {
|
||||
XException.NotFound.Throw ();
|
||||
}
|
||||
|
||||
//
|
||||
XException.ActionFailed.Throw ();
|
||||
}
|
||||
|
||||
//
|
||||
var result = new XBaseRestResponse<T>
|
||||
{
|
||||
Response = resp,
|
||||
Model = resp.Content.FromJSON<T> ()
|
||||
};
|
||||
|
||||
//
|
||||
return result;
|
||||
});
|
||||
|
||||
//
|
||||
return response;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get User SelectBy Param
|
||||
|
||||
Reference in New Issue
Block a user