From 37e42640ac94949a3363b3f0bb2eae9dc9661cea Mon Sep 17 00:00:00 2001 From: Christo Zietsman Date: Tue, 13 Sep 2022 13:20:43 +0200 Subject: [PATCH] Allow providing a custom HttpMessageHandler --- .../Client/DiscoveryClient.cs | 5 ++-- .../Client/OAuth2Client.cs | 30 +++++++++++-------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/IPPDotNetDevKitCSV3/Code/Intuit.Ipp.OAuth2PlatformClient/Client/DiscoveryClient.cs b/IPPDotNetDevKitCSV3/Code/Intuit.Ipp.OAuth2PlatformClient/Client/DiscoveryClient.cs index 2fd7438a..2583ea0d 100644 --- a/IPPDotNetDevKitCSV3/Code/Intuit.Ipp.OAuth2PlatformClient/Client/DiscoveryClient.cs +++ b/IPPDotNetDevKitCSV3/Code/Intuit.Ipp.OAuth2PlatformClient/Client/DiscoveryClient.cs @@ -110,10 +110,11 @@ public DiscoveryClient(string authority = OidcConstants.Discovery.IssuerUrl, Htt /// DiscoveryClient constructor which takes in app environment /// /// app Environment - public DiscoveryClient(AppEnvironment appEnvironment) + /// innerHandler + 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 = ""; diff --git a/IPPDotNetDevKitCSV3/Code/Intuit.Ipp.OAuth2PlatformClient/Client/OAuth2Client.cs b/IPPDotNetDevKitCSV3/Code/Intuit.Ipp.OAuth2PlatformClient/Client/OAuth2Client.cs index c0b2a71a..947333d6 100644 --- a/IPPDotNetDevKitCSV3/Code/Intuit.Ipp.OAuth2PlatformClient/Client/OAuth2Client.cs +++ b/IPPDotNetDevKitCSV3/Code/Intuit.Ipp.OAuth2PlatformClient/Client/OAuth2Client.cs @@ -165,7 +165,8 @@ public string ServiceRequestLoggingLocationForFile /// /// /// This can either be sandbox, production or an actual discovery url - public OAuth2Client(string clientID, string clientSecret, string redirectURI, string environment) + /// + public OAuth2Client(string clientID, string clientSecret, string redirectURI, string environment, HttpMessageHandler innerHandler = null) { ClientID = clientID ?? throw new ArgumentNullException(nameof(clientID)); @@ -189,7 +190,7 @@ public OAuth2Client(string clientID, string clientSecret, string redirectURI, st - DiscoveryDoc = GetDiscoveryDoc(); + DiscoveryDoc = GetDiscoveryDoc(innerHandler); } @@ -235,8 +236,9 @@ public OAuth2Client(string clientID, string clientSecret, string redirectURI, st /// /// Gets Discovery Doc /// + /// innerHandler /// - public DiscoveryResponse GetDiscoveryDoc() + public DiscoveryResponse GetDiscoveryDoc(HttpMessageHandler innerHandler = null) { DiscoveryClient discoveryClient; @@ -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) @@ -547,9 +549,10 @@ public string GetAuthorizationURL(List scopes) /// Gets Bearer token from Authorization code /// /// + /// /// /// - public async Task GetBearerTokenAsync(string code, CancellationToken cancellationToken = default(CancellationToken)) + public async Task GetBearerTokenAsync(string code, HttpMessageHandler innerHandler = null, CancellationToken cancellationToken = default(CancellationToken)) { if (string.IsNullOrEmpty(DiscoveryDoc.TokenEndpoint)) { @@ -577,7 +580,7 @@ public string GetAuthorizationURL(List 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); } @@ -587,9 +590,10 @@ public string GetAuthorizationURL(List scopes) /// /// /// + /// /// /// - public async Task GetBearerTokenAsync(string tokenEndpoint, string code, CancellationToken cancellationToken = default(CancellationToken)) + public async Task GetBearerTokenAsync(string tokenEndpoint, string code, HttpMessageHandler innerHandler = null, CancellationToken cancellationToken = default(CancellationToken)) { if (string.IsNullOrEmpty(tokenEndpoint)) { @@ -618,7 +622,7 @@ public string GetAuthorizationURL(List 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); } @@ -627,9 +631,10 @@ public string GetAuthorizationURL(List scopes) /// /// /// + /// /// /// - public async Task RefreshTokenAsync(string refreshToken, object extra = null, CancellationToken cancellationToken = default(CancellationToken)) + public async Task RefreshTokenAsync(string refreshToken, object extra = null, HttpMessageHandler innerHandler = null, CancellationToken cancellationToken = default(CancellationToken)) { if (string.IsNullOrEmpty(DiscoveryDoc.TokenEndpoint)) { @@ -657,7 +662,7 @@ public string GetAuthorizationURL(List 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); } @@ -667,9 +672,10 @@ public string GetAuthorizationURL(List scopes) /// /// /// + /// /// /// - public async Task RefreshTokenAsync(string tokenEndpoint, string refreshToken, object extra = null, CancellationToken cancellationToken = default(CancellationToken)) + public async Task RefreshTokenAsync(string tokenEndpoint, string refreshToken, object extra = null, HttpMessageHandler innerHandler = null, CancellationToken cancellationToken = default(CancellationToken)) { if (string.IsNullOrEmpty(tokenEndpoint)) { @@ -697,7 +703,7 @@ public string GetAuthorizationURL(List 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); }