Initial Commit ...
This commit is contained in:
@@ -0,0 +1,406 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using RestSharp;
|
||||
using RestSharp.Authenticators;
|
||||
using xCommons.Constants;
|
||||
using xCommons.Extensions;
|
||||
using xCommons.Providers;
|
||||
using xExceptions.Constants;
|
||||
using xHttpService.Constants;
|
||||
using xIdentityModels.Constants;
|
||||
using xIdentityModels.Extensions;
|
||||
using xIdentityModels.Models;
|
||||
using xIdentityService.Configuration;
|
||||
using xIdentityService.Constants;
|
||||
using xIdentityService.Interfaces;
|
||||
|
||||
namespace xIdentityService.Providers {
|
||||
public partial class XIdentityProvider : IXIdentityProvider {
|
||||
private readonly IXTokenProvider tokenProvider;
|
||||
private readonly IHostingEnvironment environment;
|
||||
private JsonSerializerSettings jsonSerializerSettings;
|
||||
private readonly XValidationProvider validationProvider;
|
||||
private readonly XIdentityServiceConfiguration identityConfiguration;
|
||||
|
||||
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,
|
||||
XIdentityServiceConfiguration identityConfiguration,
|
||||
XValidationProvider validationProvider
|
||||
) {
|
||||
//
|
||||
BaseUrl = identityConfiguration.Authority;
|
||||
RevisionSecretKey = identityConfiguration.XRevisionSecretKey;
|
||||
|
||||
//
|
||||
this.environment = environment;
|
||||
Logger = logger;
|
||||
|
||||
//
|
||||
this.identityConfiguration = identityConfiguration;
|
||||
this.validationProvider = validationProvider;
|
||||
this.jsonSerializerSettings = new JsonSerializerSettings {
|
||||
ContractResolver = new CamelCasePropertyNamesContractResolver ()
|
||||
};
|
||||
|
||||
//
|
||||
this.tokenProvider = tokenProvider;
|
||||
}
|
||||
|
||||
//
|
||||
#region Tools ...
|
||||
/// <summary>
|
||||
/// Get an Instance of Http Client
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public HttpClient GetHttpClient () {
|
||||
//
|
||||
HttpClient httpClient = null;
|
||||
|
||||
//
|
||||
// httpClient = new HttpClient ();
|
||||
var httpClientHandler = new HttpClientHandler () {
|
||||
ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => {
|
||||
//
|
||||
Logger.LogInformation (
|
||||
$"XSSL Handler: {Environment.NewLine}, sender: {sender}, {Environment.NewLine} cert: {cert}, {Environment.NewLine} chain: {chain}, {Environment.NewLine} sslPolicyErrors: {sslPolicyErrors}"
|
||||
);
|
||||
|
||||
//
|
||||
return true;
|
||||
},
|
||||
ClientCertificateOptions = ClientCertificateOption.Manual,
|
||||
};
|
||||
httpClient = new HttpClient (httpClientHandler);
|
||||
|
||||
//
|
||||
// Set Timeout ...
|
||||
// TODO: Fix this ...
|
||||
// httpClient.Timeout = TimeSpan
|
||||
// .FromSeconds (identityConfiguration.DefaultClientTimeout);
|
||||
|
||||
//
|
||||
return httpClient;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get an Instance of RestClient
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public RestClient GetRestClient (XTokenResponse tokens = null) {
|
||||
//
|
||||
var client = new RestClient (BaseUrl);
|
||||
|
||||
//
|
||||
// Set Authenticator ...
|
||||
if (!tokens.IsNull () &&
|
||||
!tokens.AccessToken.IsNullOrEmpty ()) {
|
||||
client.Authenticator = new JwtAuthenticator (tokens.AccessToken);
|
||||
}
|
||||
|
||||
//
|
||||
// Refresh Token ...
|
||||
if (!tokens.IsNull () &&
|
||||
!tokens.RefreshToken.IsNullOrEmpty ()) {
|
||||
client.AddDefaultHeader (XAuthorization.RefreshToken, tokens.RefreshToken);
|
||||
}
|
||||
|
||||
//
|
||||
// ExpiresAt ...
|
||||
if (!tokens.IsNull () &&
|
||||
tokens.ExpiresAt > 0) {
|
||||
client.AddDefaultHeader (XAuthorization.ExpiresAt, tokens.ExpiresAt.AsHttpParamString ());
|
||||
}
|
||||
|
||||
//
|
||||
// Set Default Timeout ...
|
||||
client.Timeout = identityConfiguration.DefaultClientTimeout;
|
||||
|
||||
//
|
||||
// SSL Validation Handler ...
|
||||
client.RemoteCertificateValidationCallback = (sender, cert, chain, sslPolicyErrors) => {
|
||||
//
|
||||
Logger.LogInformation (
|
||||
$"XSSL Handler: {Environment.NewLine}, sender: {sender}, {Environment.NewLine} cert: {cert}, {Environment.NewLine} chain: {chain}, {Environment.NewLine} sslPolicyErrors: {sslPolicyErrors}"
|
||||
);
|
||||
|
||||
//
|
||||
return true;
|
||||
};
|
||||
|
||||
//
|
||||
return client;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get an Instance of RestRequest
|
||||
/// </summary>
|
||||
/// <param name="endpoint"></param>
|
||||
/// <param name="params"></param>
|
||||
/// <returns></returns>
|
||||
public RestRequest GetRestRequet (
|
||||
XApiAccountEndpoint endpoint,
|
||||
IDictionary<string, string> @params = null,
|
||||
bool addXPoweredValue = true,
|
||||
IDictionary<string, string> headers = null
|
||||
) {
|
||||
//
|
||||
var url = endpoint.GetStringValue ();
|
||||
|
||||
//
|
||||
// Add Route Payloads ...
|
||||
if (!@params.IsNull () && @params.Count > 0) {
|
||||
//
|
||||
var enumerator = @params.GetEnumerator ();
|
||||
while (enumerator.MoveNext ()) {
|
||||
//
|
||||
var item = enumerator.Current;
|
||||
|
||||
//
|
||||
url = url.Replace (item.Key, item.Value.ToUrlEncoded ());
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Create request ...
|
||||
var request = new RestRequest (url, DataFormat.Json);
|
||||
|
||||
//
|
||||
if (addXPoweredValue) {
|
||||
request.AddHeader (XAuthorization.XPoweredBy, identityConfiguration.XPoweredValue);
|
||||
}
|
||||
|
||||
//
|
||||
// Add Headers to request ...
|
||||
if (!headers.IsNull () && headers.Count > 0) {
|
||||
//
|
||||
var enumerator = headers.GetEnumerator ();
|
||||
while (enumerator.MoveNext ()) {
|
||||
//
|
||||
var item = enumerator.Current;
|
||||
request.AddHeader (item.Key, item.Value);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return request;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handle Call Specific Request and retrieve Response
|
||||
/// </summary>
|
||||
/// <param name="client"></param>
|
||||
/// <param name="request"></param>
|
||||
/// <param name="method"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public async Task<T> RunRestRequest<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 RunRestRequest<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 = resp.Content.FromJSON<T> ();
|
||||
return result;
|
||||
});
|
||||
|
||||
//
|
||||
return response;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get User SelectBy Param
|
||||
/// </summary>
|
||||
/// <param name="model"></param>
|
||||
/// <param name="forceNotNull"></param>
|
||||
/// <param name="excludes"></param>
|
||||
/// <returns></returns>
|
||||
public string GetUserSelectByParam (
|
||||
XActionRequest model,
|
||||
bool forceNotNull = true,
|
||||
ICollection<XUserSelectBy> excludes = null
|
||||
) {
|
||||
//
|
||||
// Validate Args ...
|
||||
validationProvider.NotNull (model);
|
||||
|
||||
//
|
||||
var id = model.UserId;
|
||||
var userName = model.UserName;
|
||||
var email = model.Email;
|
||||
var phoneNumber = model.MobileNumber;
|
||||
|
||||
//
|
||||
if (excludes != null) {
|
||||
//
|
||||
if (excludes.Contains (XUserSelectBy.ID)) {
|
||||
id = null;
|
||||
}
|
||||
if (excludes.Contains (XUserSelectBy.Email)) {
|
||||
email = null;
|
||||
}
|
||||
if (excludes.Contains (XUserSelectBy.MobileNumber)) {
|
||||
phoneNumber = null;
|
||||
}
|
||||
if (excludes.Contains (XUserSelectBy.Username)) {
|
||||
userName = null;
|
||||
}
|
||||
}
|
||||
|
||||
var selectByParam =
|
||||
id.IsNullOrEmpty () ?
|
||||
userName.IsNullOrEmpty () ?
|
||||
email.IsNullOrEmpty () ?
|
||||
phoneNumber.IsNullOrEmpty () ? null : phoneNumber : email : userName : id;
|
||||
|
||||
//
|
||||
// Check result ...
|
||||
if (forceNotNull &&
|
||||
selectByParam.IsNullOrEmpty ()) {
|
||||
XException.InvalidData.Throw ();
|
||||
}
|
||||
|
||||
//
|
||||
return selectByParam;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using xCommons.Extensions;
|
||||
using xIdentityModels.Models;
|
||||
using xIdentityService.Interfaces;
|
||||
|
||||
namespace xIdentityService.Providers {
|
||||
public partial class XInMemoryTokenProvider : IXTokenProvider {
|
||||
private IDictionary<string, XTokenResponse> DbContext;
|
||||
|
||||
public XInMemoryTokenProvider () {
|
||||
//
|
||||
this.DbContext = new Dictionary<string, XTokenResponse> ();
|
||||
}
|
||||
|
||||
public async Task<bool> IsTokenExists (string accessToken) {
|
||||
//
|
||||
// Validate Args ...
|
||||
if (accessToken.IsNullOrEmpty ()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
//
|
||||
// Process Result ...
|
||||
var result = DbContext.Keys.Any (k => k == accessToken);
|
||||
return await Task.FromResult (result);
|
||||
}
|
||||
|
||||
public async Task<bool> IsTokenExists (XTokenResponse tokens) {
|
||||
//
|
||||
// Validate Args ...
|
||||
if (tokens.IsNull () ||
|
||||
tokens.AccessToken.IsNullOrEmpty () ||
|
||||
!await IsTokenExists (tokens.AccessToken)) {
|
||||
return false;
|
||||
};
|
||||
|
||||
//
|
||||
// Process Result ...
|
||||
var result = await IsTokenExists (tokens.AccessToken);
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<bool> RemoveToken (string accessToken) {
|
||||
//
|
||||
// Validate Args ...
|
||||
if (accessToken.IsNullOrEmpty ()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
//
|
||||
// Process Result ...
|
||||
var result = DbContext.Remove (accessToken);
|
||||
return await Task.FromResult (result);
|
||||
}
|
||||
|
||||
public async Task<bool> RemoveToken (XTokenResponse tokens) {
|
||||
//
|
||||
// Validate Args ...
|
||||
if (tokens.IsNull () ||
|
||||
tokens.AccessToken.IsNullOrEmpty () ||
|
||||
!await IsTokenExists (tokens.AccessToken)) {
|
||||
return false;
|
||||
};
|
||||
|
||||
//
|
||||
// Process Result ...
|
||||
var result = await RemoveToken (tokens.AccessToken);
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<XTokenResponse> RetrieveToken (string accessToken) {
|
||||
//
|
||||
// Validate Args ...
|
||||
if (accessToken.IsNullOrEmpty () ||
|
||||
!await IsTokenExists (accessToken)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
//
|
||||
// Process Result ...
|
||||
var result = DbContext[accessToken];
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<bool> AddToken (XTokenResponse tokens) {
|
||||
//
|
||||
// Validate Args ...
|
||||
if (tokens.IsNull () ||
|
||||
tokens.AccessToken.IsNullOrEmpty () ||
|
||||
await IsTokenExists (tokens.AccessToken)) {
|
||||
return false;
|
||||
};
|
||||
|
||||
//
|
||||
// Process Result ...
|
||||
try {
|
||||
//
|
||||
DbContext.Add (tokens.AccessToken, tokens);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateToken (XTokenResponse tokens) {
|
||||
//
|
||||
// Validate Args ...
|
||||
if (tokens.IsNull () ||
|
||||
tokens.AccessToken.IsNullOrEmpty () ||
|
||||
!await IsTokenExists (tokens.AccessToken)) {
|
||||
return false;
|
||||
};
|
||||
|
||||
//
|
||||
// Process Result ...
|
||||
try {
|
||||
//
|
||||
DbContext[tokens.AccessToken] = tokens;
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> AddOrUpdateToken (XTokenResponse tokens) {
|
||||
//
|
||||
// Validate Args ...
|
||||
if (tokens.IsNull () ||
|
||||
tokens.AccessToken.IsNullOrEmpty ()
|
||||
) {
|
||||
return false;
|
||||
};
|
||||
|
||||
//
|
||||
// Process Result ...
|
||||
try {
|
||||
//
|
||||
var isExists = await IsTokenExists (tokens);
|
||||
if (isExists) {
|
||||
return await UpdateToken (tokens);
|
||||
} else {
|
||||
return await AddToken (tokens);
|
||||
}
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,215 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Web;
|
||||
using xCommons.Extensions;
|
||||
using xExceptions.Constants;
|
||||
using xIdentityService.Configuration;
|
||||
using xIdentityService.Interfaces;
|
||||
|
||||
namespace xIdentityService.Providers {
|
||||
public class XProxyHelper : IXProxyHelper {
|
||||
//
|
||||
#region Props ...
|
||||
public XProxyConfiguration Configuration { get; }
|
||||
#endregion
|
||||
|
||||
//
|
||||
#region Constructor ...
|
||||
public XProxyHelper (XProxyConfiguration configuration) {
|
||||
this.Configuration = configuration;
|
||||
}
|
||||
#endregion
|
||||
|
||||
//
|
||||
#region Actions ...
|
||||
/// <summary>
|
||||
/// Validate XProxy Configuration for Client ...
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public bool ValidateConfiguration () {
|
||||
//
|
||||
var result = !Configuration.IsNull () &&
|
||||
!Configuration.Host.IsNullOrEmpty () &&
|
||||
Configuration.Host.IsValidUrl () &&
|
||||
Configuration.Routes.HasChild ();
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// retrie Normalize Host ...
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string NormalizeHost () {
|
||||
//
|
||||
var result = string.Empty;
|
||||
|
||||
//
|
||||
if (!Configuration.IsNull () &&
|
||||
!Configuration.Host.IsNullOrEmpty () &&
|
||||
Configuration.Host.IsValidUrl ()
|
||||
) {
|
||||
//
|
||||
result = Configuration.Host
|
||||
.ToNormalString ();
|
||||
|
||||
//
|
||||
if (result.EndsWith ("/")) {
|
||||
result = result.Substring (0, result.Length - 1);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// retrieve all registred Routes ...
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public IEnumerable<XProxyRouteDescriptor> GetRoutes () {
|
||||
//
|
||||
// Validate Args ...
|
||||
if (Configuration.IsNull ()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
//
|
||||
return Configuration.Routes;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve all Prepared url of Routes ...
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public IEnumerable<string> GetRouteUrls () {
|
||||
//
|
||||
var normalHost = NormalizeHost ();
|
||||
if (normalHost.IsNullOrEmpty ()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
//
|
||||
var result = GetRoutes ()
|
||||
.Select (r => $"{normalHost}{r.Route}");
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Find an Specific Route ...
|
||||
/// </summary>
|
||||
/// <param name="whereClause"></param>
|
||||
/// <returns></returns>
|
||||
public XProxyRouteDescriptor FindRoute (Expression<Func<XProxyRouteDescriptor, bool>> whereClause) {
|
||||
//
|
||||
// Validate Arg ...
|
||||
if (whereClause.IsNull ()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
//
|
||||
var whereFunc = whereClause
|
||||
.Compile ();
|
||||
|
||||
//
|
||||
var result = GetRoutes ()
|
||||
.FirstOrDefault (whereFunc);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Prepare a request url for a Proxy Route ...
|
||||
/// </summary>
|
||||
/// <param name="url"></param>
|
||||
/// <param name="descriptor"></param>
|
||||
/// <returns></returns>
|
||||
public string PrepareActionUrlForXProxyRoute (
|
||||
string url,
|
||||
XProxyRouteDescriptor descriptor
|
||||
) {
|
||||
//
|
||||
if (!url.IsValidUrl () ||
|
||||
url.IsNullOrEmpty () ||
|
||||
!ValidateConfiguration ()
|
||||
) {
|
||||
throw XException.InvalidArgs.ToException ();
|
||||
}
|
||||
|
||||
//
|
||||
// find Route ...
|
||||
var routeDescriptor = GetRoutes ()
|
||||
.FirstOrDefault (r => r
|
||||
.IsSameContent (descriptor)
|
||||
);
|
||||
|
||||
//
|
||||
// Validate Route Descriptor ...
|
||||
if (
|
||||
routeDescriptor.IsNull () ||
|
||||
routeDescriptor.Route.IsNullOrEmpty ()
|
||||
) {
|
||||
throw XException.InvalidArgs.ToException ();
|
||||
}
|
||||
|
||||
//
|
||||
// Retrieve and Validate Host Address ...
|
||||
var host = NormalizeHost ();
|
||||
if (!host.IsValidUrl () ||
|
||||
host.IsNullOrEmpty ()
|
||||
) {
|
||||
throw XException.InvalidArgs.ToException ();
|
||||
}
|
||||
|
||||
//
|
||||
url = $"{host}{routeDescriptor.Route}/{HttpUtility.UrlEncode(url)}";
|
||||
|
||||
//
|
||||
return url;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Prepare a request url for a Selected Proxy Route ...
|
||||
/// </summary>
|
||||
/// <param name="url"></param>
|
||||
/// <param name="whereClause"></param>
|
||||
/// <returns></returns>
|
||||
public string PrepareActionUrlForXProxyRoute (
|
||||
string url,
|
||||
Expression<Func<XProxyRouteDescriptor, bool>> whereClause
|
||||
) {
|
||||
//
|
||||
if (!url.IsValidUrl () ||
|
||||
url.IsNullOrEmpty () ||
|
||||
whereClause.IsNull () ||
|
||||
!ValidateConfiguration ()
|
||||
) {
|
||||
throw XException.InvalidArgs.ToException ();
|
||||
}
|
||||
|
||||
//
|
||||
var descriptor = FindRoute (whereClause);
|
||||
|
||||
//
|
||||
// find Route ...
|
||||
var result = PrepareActionUrlForXProxyRoute (
|
||||
url: url,
|
||||
descriptor: descriptor
|
||||
);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
|
||||
//
|
||||
#region Private ...
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user