2626import com .nimbusds .openid .connect .sdk .op .OIDCProviderMetadata ;
2727import java .io .IOException ;
2828import java .io .InputStream ;
29- import java .net .Proxy ;
3029import java .net .HttpURLConnection ;
3130import java .net .URL ;
3231import javax .net .ssl .HttpsURLConnection ;
@@ -46,7 +45,7 @@ public class ConfidentialClient implements OAuth2Client {
4645
4746 private static final Logger LOGGER = LoggerFactory .getLogger (ConfidentialClient .class );
4847 private final Configuration config ;
49- private final OIDCProviderMetadata providerMetadata ;
48+ private OIDCProviderMetadata providerMetadata ;
5049 private RequestOptions requestOptions ;
5150 private TokenRequestBuilder tokenRequestBuilder ;
5251 private long jwsIssuedAt ;
@@ -104,21 +103,7 @@ public ConfidentialClient(final Configuration config)
104103 this .config = config ;
105104 LOGGER .debug ("Finished initialising configuration" );
106105
107- LOGGER .debug ("Attempting to get response from Well Known URI" );
108- try (InputStream stream = config .getWellKnownUrl ().openStream ()) {
109- final String providerInfo = IOUtils .readInputStreamToString (stream );
110- this .providerMetadata = OIDCProviderMetadata .parse (providerInfo );
111- } catch (final ParseException e ) {
112- throw new AuthServerMetadataContentException ("Content of WellKnownUri has errors: " +
113- config .getWellKnownUrl ().toString (), e );
114- } catch (final IOException e ) {
115- throw new AuthServerMetadataException ("Error retrieving contents from WellKnownUri: " +
116- config .getWellKnownUrl ().toString (), e );
117- }
118- LOGGER .debug ("Response received from Well Known URI" );
119-
120- this .tokenRequestBuilder =
121- new TokenRequestBuilder ().uri (this .providerMetadata .getTokenEndpointURI ());
106+ this .requestProviderMetadata (config , null );
122107 }
123108
124109 /**
@@ -138,33 +123,7 @@ public ConfidentialClient(final Configuration config, RequestOptions requestOpti
138123 this .config = config ;
139124 LOGGER .debug ("Finished initialising configuration" );
140125
141- LOGGER .debug ("Attempting to get response from Well Known URI" );
142- URL wellKnownURL = config .getWellKnownUrl ();
143-
144- try {
145- HttpURLConnection conn = (HttpURLConnection ) wellKnownURL .openConnection (requestOptions .getProxy ());
146- HttpsURLConnection sslConn = null ;
147- if (conn instanceof HttpsURLConnection ) {
148- sslConn = (HttpsURLConnection )conn ;
149- sslConn .setHostnameVerifier (requestOptions .getHostnameVerifier ());
150- sslConn .setSSLSocketFactory (requestOptions .getSslSocketFactory ());
151- }
152-
153- InputStream stream = conn .getInputStream ();
154-
155- final String providerInfo = IOUtils .readInputStreamToString (stream );
156- this .providerMetadata = OIDCProviderMetadata .parse (providerInfo );
157- } catch (final ParseException e ) {
158- throw new AuthServerMetadataContentException ("Content of WellKnownUri has errors: " +
159- config .getWellKnownUrl ().toString (), e );
160- } catch (final IOException e ) {
161- throw new AuthServerMetadataException ("Error retrieving contents from WellKnownUri: " +
162- config .getWellKnownUrl ().toString (), e );
163- }
164- LOGGER .debug ("Response received from Well Known URI" );
165-
166- this .tokenRequestBuilder =
167- new TokenRequestBuilder ().uri (this .providerMetadata .getTokenEndpointURI ());
126+ this .requestProviderMetadata (config , requestOptions );
168127 }
169128
170129 /**
@@ -225,6 +184,40 @@ public String getAccessToken() throws AccessTokenException, SigningJwsException
225184 return this .fetchAccessToken ();
226185 }
227186
187+ private void requestProviderMetadata (Configuration config , RequestOptions requestOptions ) throws AuthServerMetadataContentException , AuthServerMetadataException {
188+ LOGGER .debug ("Attempting to get response from Well Known URI" );
189+ URL wellKnownURL = config .getWellKnownUrl ();
190+ InputStream stream ;
191+
192+ try {
193+ if (requestOptions == null ) stream = wellKnownURL .openStream ();
194+ else {
195+ HttpURLConnection conn = (HttpURLConnection ) wellKnownURL .openConnection (requestOptions .getProxy ());
196+ HttpsURLConnection sslConn = null ;
197+ if (conn instanceof HttpsURLConnection ) {
198+ sslConn = (HttpsURLConnection ) conn ;
199+ sslConn .setHostnameVerifier (requestOptions .getHostnameVerifier ());
200+ sslConn .setSSLSocketFactory (requestOptions .getSslSocketFactory ());
201+ }
202+
203+ stream = conn .getInputStream ();
204+ }
205+
206+ final String providerInfo = IOUtils .readInputStreamToString (stream );
207+ this .providerMetadata = OIDCProviderMetadata .parse (providerInfo );
208+ } catch (final ParseException e ) {
209+ throw new AuthServerMetadataContentException ("Content of WellKnownUri has errors: " +
210+ config .getWellKnownUrl ().toString (), e );
211+ } catch (final IOException e ) {
212+ throw new AuthServerMetadataException ("Error retrieving contents from WellKnownUri: " +
213+ config .getWellKnownUrl ().toString (), e );
214+ }
215+ LOGGER .debug ("Response received from Well Known URI" );
216+
217+ this .tokenRequestBuilder =
218+ new TokenRequestBuilder ().uri (this .providerMetadata .getTokenEndpointURI ());
219+ }
220+
228221 private boolean isCachedTokenValid () {
229222 if (this .accessToken == null ) {
230223 return false ;
0 commit comments