Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow providing a custom HttpMessageHandler #257

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,11 @@ public DiscoveryClient(string authority = OidcConstants.Discovery.IssuerUrl, Htt
/// DiscoveryClient constructor which takes in app environment
/// </summary>
/// <param name="appEnvironment">app Environment</param>
public DiscoveryClient(AppEnvironment appEnvironment)
/// <param name="innerHandler">innerHandler</param>
public DiscoveryClient(AppEnvironment appEnvironment, HttpMessageHandler innerHandler = null)
{
Policy.SetAuthority(appEnvironment);//Issuer url set, used in DiscoverResponse too for validation
var handler = new HttpClientHandler();
var handler = innerHandler ?? new HttpClientHandler();
string url = "";


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ public string ServiceRequestLoggingLocationForFile
/// <param name="clientSecret"></param>
/// <param name="redirectURI"></param>
/// <param name="environment">This can either be sandbox, production or an actual discovery url</param>
public OAuth2Client(string clientID, string clientSecret, string redirectURI, string environment)
/// <param name="innerHandler"></param>
public OAuth2Client(string clientID, string clientSecret, string redirectURI, string environment, HttpMessageHandler innerHandler = null)
{

ClientID = clientID ?? throw new ArgumentNullException(nameof(clientID));
Expand All @@ -189,7 +190,7 @@ public OAuth2Client(string clientID, string clientSecret, string redirectURI, st



DiscoveryDoc = GetDiscoveryDoc();
DiscoveryDoc = GetDiscoveryDoc(innerHandler);


}
Expand Down Expand Up @@ -235,8 +236,9 @@ public OAuth2Client(string clientID, string clientSecret, string redirectURI, st
/// <summary>
/// Gets Discovery Doc
/// </summary>
/// <param name="innerHandler">innerHandler</param>
/// <returns></returns>
public DiscoveryResponse GetDiscoveryDoc()
public DiscoveryResponse GetDiscoveryDoc(HttpMessageHandler innerHandler = null)
{

DiscoveryClient discoveryClient;
Expand All @@ -246,7 +248,7 @@ public DiscoveryResponse GetDiscoveryDoc()
}
else
{
discoveryClient = new DiscoveryClient(ApplicationEnvironment);
discoveryClient = new DiscoveryClient(ApplicationEnvironment, innerHandler);
}
DiscoveryResponse discoveryResponse = discoveryClient.Get();
if(discoveryResponse.IsError==true)
Expand Down Expand Up @@ -547,9 +549,10 @@ public string GetAuthorizationURL(List<OidcScopes> scopes)
/// Gets Bearer token from Authorization code
/// </summary>
/// <param name="code"></param>
/// <param name="innerHandler"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<TokenResponse> GetBearerTokenAsync(string code, CancellationToken cancellationToken = default(CancellationToken))
public async Task<TokenResponse> GetBearerTokenAsync(string code, HttpMessageHandler innerHandler = null, CancellationToken cancellationToken = default(CancellationToken))
{
if (string.IsNullOrEmpty(DiscoveryDoc.TokenEndpoint))
{
Expand Down Expand Up @@ -577,7 +580,7 @@ public string GetAuthorizationURL(List<OidcScopes> scopes)

}

var tokenClient = new TokenClient(DiscoveryDoc.TokenEndpoint, ClientID, ClientSecret);
var tokenClient = new TokenClient(DiscoveryDoc.TokenEndpoint, ClientID, ClientSecret, innerHandler);
return await tokenClient.RequestTokenFromCodeAsync(code, RedirectURI, cancellationToken: cancellationToken).ConfigureAwait(false);
}

Expand All @@ -587,9 +590,10 @@ public string GetAuthorizationURL(List<OidcScopes> scopes)
/// </summary>
/// <param name="tokenEndpoint"></param>
/// <param name="code"></param>
/// <param name="innerHandler"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<TokenResponse> GetBearerTokenAsync(string tokenEndpoint, string code, CancellationToken cancellationToken = default(CancellationToken))
public async Task<TokenResponse> GetBearerTokenAsync(string tokenEndpoint, string code, HttpMessageHandler innerHandler = null, CancellationToken cancellationToken = default(CancellationToken))
{
if (string.IsNullOrEmpty(tokenEndpoint))
{
Expand Down Expand Up @@ -618,7 +622,7 @@ public string GetAuthorizationURL(List<OidcScopes> scopes)

}

var tokenClient = new TokenClient(tokenEndpoint, ClientID, ClientSecret);
var tokenClient = new TokenClient(tokenEndpoint, ClientID, ClientSecret, innerHandler);
return await tokenClient.RequestTokenFromCodeAsync(code, RedirectURI, cancellationToken: cancellationToken).ConfigureAwait(false);
}

Expand All @@ -627,9 +631,10 @@ public string GetAuthorizationURL(List<OidcScopes> scopes)
/// </summary>
/// <param name="refreshToken"></param>
/// <param name="extra"></param>
/// <param name="innerHandler"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<TokenResponse> RefreshTokenAsync(string refreshToken, object extra = null, CancellationToken cancellationToken = default(CancellationToken))
public async Task<TokenResponse> RefreshTokenAsync(string refreshToken, object extra = null, HttpMessageHandler innerHandler = null, CancellationToken cancellationToken = default(CancellationToken))
{
if (string.IsNullOrEmpty(DiscoveryDoc.TokenEndpoint))
{
Expand Down Expand Up @@ -657,7 +662,7 @@ public string GetAuthorizationURL(List<OidcScopes> scopes)
AdvancedLogger = LogHelper.GetAdvancedLogging(enableSerilogRequestResponseLoggingForDebug: this.EnableSerilogRequestResponseLoggingForDebug, enableSerilogRequestResponseLoggingForTrace: this.EnableSerilogRequestResponseLoggingForTrace, enableSerilogRequestResponseLoggingForConsole: this.EnableSerilogRequestResponseLoggingForConsole, enableSerilogRequestResponseLoggingForFile: this.EnableSerilogRequestResponseLoggingForFile, serviceRequestLoggingLocationForFile: this.ServiceRequestLoggingLocationForFile);
}

var tokenClient = new TokenClient(DiscoveryDoc.TokenEndpoint, ClientID, ClientSecret);
var tokenClient = new TokenClient(DiscoveryDoc.TokenEndpoint, ClientID, ClientSecret, innerHandler);
return await tokenClient.RequestRefreshTokenAsync(refreshToken, cancellationToken).ConfigureAwait(false);
}

Expand All @@ -667,9 +672,10 @@ public string GetAuthorizationURL(List<OidcScopes> scopes)
/// <param name="tokenEndpoint"></param>
/// <param name="refreshToken"></param>
/// <param name="extra"></param>
/// <param name="innerHandler"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<TokenResponse> RefreshTokenAsync(string tokenEndpoint, string refreshToken, object extra = null, CancellationToken cancellationToken = default(CancellationToken))
public async Task<TokenResponse> RefreshTokenAsync(string tokenEndpoint, string refreshToken, object extra = null, HttpMessageHandler innerHandler = null, CancellationToken cancellationToken = default(CancellationToken))
{
if (string.IsNullOrEmpty(tokenEndpoint))
{
Expand Down Expand Up @@ -697,7 +703,7 @@ public string GetAuthorizationURL(List<OidcScopes> scopes)
AdvancedLogger = LogHelper.GetAdvancedLogging(enableSerilogRequestResponseLoggingForDebug: this.EnableSerilogRequestResponseLoggingForDebug, enableSerilogRequestResponseLoggingForTrace: this.EnableSerilogRequestResponseLoggingForTrace, enableSerilogRequestResponseLoggingForConsole: this.EnableSerilogRequestResponseLoggingForConsole, enableSerilogRequestResponseLoggingForFile: this.EnableSerilogRequestResponseLoggingForFile, serviceRequestLoggingLocationForFile: this.ServiceRequestLoggingLocationForFile);
}

var tokenClient = new TokenClient(tokenEndpoint, ClientID, ClientSecret);
var tokenClient = new TokenClient(tokenEndpoint, ClientID, ClientSecret, innerHandler);
return await tokenClient.RequestRefreshTokenAsync(refreshToken, cancellationToken).ConfigureAwait(false);
}

Expand Down