3333/**
3434 * Config class to hold the clientId and clientSecret
3535 * and the endpoint URLs retrieved from the Discovery document
36- *
36+ *
3737 * @author dderose
3838 *
3939 */
@@ -42,66 +42,66 @@ public class OAuth2Config {
4242 //client id, secret
4343 private String clientId ;
4444 private String clientSecret ;
45-
45+
4646 //endpoint URLs
4747 private String intuitIdTokenIssuer ;
4848 private String intuitAuthorizationEndpoint ;
4949 private String intuitBearerTokenEndpoint ;
5050 private String intuitRevokeTokenEndpoint ;
5151 private String intuitJwksURI ;
5252 private String userProfileEndpoint ;
53-
53+
5454 //proxy config
5555 private ProxyConfig proxyConfig ;
56-
56+
5757 private static final Logger logger = LoggerImpl .getInstance ();
58-
59-
58+
59+
6060 private OAuth2Config (OAuth2ConfigBuilder builder ) {
61- this .clientId = builder .clientId ;
62- this .clientSecret = builder .clientSecret ;
63- this .intuitIdTokenIssuer = builder .intuitIdTokenIssuer ;
64- this .intuitAuthorizationEndpoint = builder .intuitAuthorizationEndpoint ;
65- this .intuitBearerTokenEndpoint = builder .intuitBearerTokenEndpoint ;
66- this .intuitRevokeTokenEndpoint = builder .intuitRevokeTokenEndpoint ;
67- this .intuitJwksURI = builder .intuitJwksURI ;
68- this .userProfileEndpoint = builder .userProfileEndpoint ;
69- this .proxyConfig = builder .proxyConfig ;
61+ this .clientId = builder .clientId ;
62+ this .clientSecret = builder .clientSecret ;
63+ this .intuitIdTokenIssuer = builder .intuitIdTokenIssuer ;
64+ this .intuitAuthorizationEndpoint = builder .intuitAuthorizationEndpoint ;
65+ this .intuitBearerTokenEndpoint = builder .intuitBearerTokenEndpoint ;
66+ this .intuitRevokeTokenEndpoint = builder .intuitRevokeTokenEndpoint ;
67+ this .intuitJwksURI = builder .intuitJwksURI ;
68+ this .userProfileEndpoint = builder .userProfileEndpoint ;
69+ this .proxyConfig = builder .proxyConfig ;
70+ }
71+
72+
73+ public String getIntuitIdTokenIssuer () {
74+ return intuitIdTokenIssuer ;
75+ }
76+
77+ public String getIntuitAuthorizationEndpoint () {
78+ return intuitAuthorizationEndpoint ;
79+ }
80+
81+ public String getIntuitBearerTokenEndpoint () {
82+ return intuitBearerTokenEndpoint ;
7083 }
71-
72-
73- public String getIntuitIdTokenIssuer () {
74- return intuitIdTokenIssuer ;
75- }
76-
77- public String getIntuitAuthorizationEndpoint () {
78- return intuitAuthorizationEndpoint ;
79- }
80-
81- public String getIntuitBearerTokenEndpoint () {
82- return intuitBearerTokenEndpoint ;
83- }
84-
85- public String getIntuitRevokeTokenEndpoint () {
84+
85+ public String getIntuitRevokeTokenEndpoint () {
8686 return intuitRevokeTokenEndpoint ;
87- }
87+ }
88+
89+ public String getIntuitJwksURI () {
90+ return intuitJwksURI ;
91+ }
8892
89- public String getIntuitJwksURI () {
90- return intuitJwksURI ;
91- }
92-
93- public String getUserProfileEndpoint () {
93+ public String getUserProfileEndpoint () {
9494 return userProfileEndpoint ;
9595 }
96-
96+
9797 public String getClientId () {
9898 return clientId ;
9999 }
100-
100+
101101 public String getClientSecret () {
102102 return clientSecret ;
103103 }
104-
104+
105105 public ProxyConfig getProxyConfig () {
106106 return proxyConfig ;
107107 }
@@ -135,10 +135,10 @@ public void setIntuitJwksURI(String intuitJwksURI) {
135135
136136
137137 public static class OAuth2ConfigBuilder {
138-
138+
139139 private String clientId ;
140140 private String clientSecret ;
141-
141+
142142 private String intuitIdTokenIssuer ;
143143 private String intuitAuthorizationEndpoint ;
144144 private String intuitBearerTokenEndpoint ;
@@ -149,7 +149,7 @@ public static class OAuth2ConfigBuilder {
149149 private String intuit_tid ;
150150 private String statusCode ;
151151 private String errorMessage ;
152-
152+
153153 private ProxyConfig proxyConfig ;
154154
155155 public OAuth2ConfigBuilder (String clientId , String clientSecret ) {
@@ -158,20 +158,20 @@ public OAuth2ConfigBuilder(String clientId, String clientSecret) {
158158 }
159159
160160 private void setFields (DiscoveryAPIResponse discoveryAPIResponse ) {
161- this .intuitIdTokenIssuer = discoveryAPIResponse .getIssuer ();
162- this .intuitAuthorizationEndpoint = discoveryAPIResponse .getAuthorizationEndpoint ();
163- this .intuitBearerTokenEndpoint = discoveryAPIResponse .getTokenEndpoint ();
164- this .intuitRevokeTokenEndpoint = discoveryAPIResponse .getRevocationEndpoint ();
165- this .intuitJwksURI = discoveryAPIResponse .getJwksUri ();
166- this .userProfileEndpoint = discoveryAPIResponse .getUserinfoEndpoint ();
167- }
161+ this .intuitIdTokenIssuer = discoveryAPIResponse .getIssuer ();
162+ this .intuitAuthorizationEndpoint = discoveryAPIResponse .getAuthorizationEndpoint ();
163+ this .intuitBearerTokenEndpoint = discoveryAPIResponse .getTokenEndpoint ();
164+ this .intuitRevokeTokenEndpoint = discoveryAPIResponse .getRevocationEndpoint ();
165+ this .intuitJwksURI = discoveryAPIResponse .getJwksUri ();
166+ this .userProfileEndpoint = discoveryAPIResponse .getUserinfoEndpoint ();
167+ }
168168
169169
170170 public OAuth2ConfigBuilder callDiscoveryAPI (Environment environment ) {
171-
171+
172172 try {
173173 DiscoveryAPIResponse discoveryAPIResponse = new DiscoveryAPIClient (proxyConfig ).callDiscoveryAPI (environment );
174-
174+
175175 if (discoveryAPIResponse != null ) {
176176 this .setFields (discoveryAPIResponse );
177177 }
@@ -194,116 +194,187 @@ public OAuth2ConfigBuilder callDiscoveryAPI (String discoveryEndpoint) {
194194 DiscoveryAPIResponse discoveryAPIResponse = new DiscoveryAPIClient (proxyConfig ).callDiscoveryAPI (discoveryEndpoint );
195195
196196 if (discoveryAPIResponse != null ) {
197- this .setFields (discoveryAPIResponse );
197+ this .setFields (discoveryAPIResponse );
198198 }
199199
200200
201201 } catch (ConnectionException e ) {
202202 logger .error ("Exception while preparing url for redirect " , e );
203- this .intuit_tid = e .getIntuit_tid ();
204- this .statusCode = e .getStatusCode ();
205- this .errorMessage = e .getErrorMessage ();
203+ this .intuit_tid = e .getIntuit_tid ();
204+ this .statusCode = e .getStatusCode ();
205+ this .errorMessage = e .getErrorMessage ();
206206 }
207- return this ;
207+ return this ;
208208 }
209-
210- public OAuth2ConfigBuilder proxyConfig (ProxyConfig proxyConfig ) {
209+
210+ public OAuth2ConfigBuilder proxyConfig (ProxyConfig proxyConfig ) {
211211 this .proxyConfig = proxyConfig ;
212212 return this ;
213213 }
214214
215215 public OAuth2Config buildConfig () {
216216 return new OAuth2Config (this );
217217 }
218-
218+
219219 }
220-
220+
221221 /**
222222 * Returns the scope value based on the Enum supplied
223- *
223+ *
224224 * @param scope
225225 * @return
226226 */
227227 public String getScopeValue (Scope scope ) {
228228 logger .debug ("Enter OAuth2config::getDefaultScope" );
229229 return PropertiesConfig .getInstance ().getProperty (scope .value ());
230230 }
231-
231+
232232 /**
233233 * Generates CSRF token
234- *
234+ *
235235 * @return
236236 */
237237 public String generateCSRFToken () {
238238 logger .debug ("Enter OAuth2config::generateCSRFToken" );
239239 return UUID .randomUUID ().toString ();
240240 }
241-
241+
242242 /**
243243 * Prepares URL to call the OAuth2 authorization endpoint using Scope, CSRF and redirectURL that is supplied
244- *
244+ *
245245 * @param scope
246246 * @param redirectUri
247247 * @param csrfToken
248248 * @return
249249 * @throws InvalidRequestException
250250 */
251251 public String prepareUrl (List <Scope > scopes , String redirectUri , String csrfToken ) throws InvalidRequestException {
252-
252+
253253 logger .debug ("Enter OAuth2config::prepareUrl" );
254254 if (scopes == null || scopes .isEmpty () || redirectUri .isEmpty () || csrfToken .isEmpty ()) {
255255 logger .error ("Invalid request for prepareUrl " );
256256 throw new InvalidRequestException ("Invalid request for prepareUrl" );
257257 }
258258 try {
259- return intuitAuthorizationEndpoint
260- + "?client_id=" + clientId
261- + "&response_type=code&scope=" + URLEncoder .encode (buildScopeString (scopes ), "UTF-8" )
262- + "&redirect_uri=" + URLEncoder .encode (redirectUri , "UTF-8" )
259+ return intuitAuthorizationEndpoint
260+ + "?client_id=" + clientId
261+ + "&response_type=code&scope=" + URLEncoder .encode (buildScopeString (scopes ), "UTF-8" )
262+ + "&redirect_uri=" + URLEncoder .encode (redirectUri , "UTF-8" )
263263 + "&state=" + csrfToken ;
264264 } catch (UnsupportedEncodingException e ) {
265265 logger .error ("Exception while preparing url for redirect " , e );
266266 throw new InvalidRequestException (e .getMessage (), e );
267267 }
268-
268+
269269 }
270-
270+
271271 private String buildScopeString (List <Scope > scopes ) {
272272 StringBuilder sb = new StringBuilder ();
273273 for (Scope scope : scopes ) {
274274 sb .append (getScopeValue (scope ) + " " );
275275 }
276276 return StringUtils .stripEnd (sb .toString (), " " );
277277 }
278-
278+
279+ private String buildScope (List <String > scopes ) {
280+ StringBuilder sb = new StringBuilder ();
281+ for (String scope : scopes ) {
282+ sb .append (scope + " " );
283+ }
284+ return StringUtils .stripEnd (sb .toString (), " " );
285+ }
286+
279287 /**
280288 * Prepares URL to call the OAuth2 authorization endpoint using Scope and redirectURL that is supplied.
281289 * A CSRF token is generated and sent in the request.
282- *
283- * @param scope
290+ *
291+ * @param scopes
284292 * @param redirectUri
285293 * @return
286294 * @throws InvalidRequestException
287295 */
288296 public String prepareUrl (List <Scope > scopes , String redirectUri ) throws InvalidRequestException {
289-
297+
290298 logger .debug ("Enter OAuth2config::prepareUrl" );
291299 if (scopes == null || scopes .isEmpty () || redirectUri .isEmpty ()) {
292300 logger .error ("Invalid request for prepareUrl " );
293301 throw new InvalidRequestException ("Invalid request for prepareUrl" );
294302 }
295-
303+
296304 try {
297- return intuitAuthorizationEndpoint
305+ return intuitAuthorizationEndpoint
298306 + "?client_id=" + clientId
299- + "&response_type=code&scope=" + URLEncoder .encode (buildScopeString (scopes ), "UTF-8" )
300- + "&redirect_uri=" + URLEncoder .encode (redirectUri , "UTF-8" )
307+ + "&response_type=code&scope=" + URLEncoder .encode (buildScopeString (scopes ), "UTF-8" )
308+ + "&redirect_uri=" + URLEncoder .encode (redirectUri , "UTF-8" )
301309 + "&state=" + generateCSRFToken ();
302310 } catch (UnsupportedEncodingException e ) {
303311 logger .error ("Exception while preparing url for redirect " , e );
304312 throw new InvalidRequestException (e .getMessage (), e );
305313 }
306-
314+
307315 }
308-
316+
317+ /**
318+ * Prepares URL to call the OAuth2 authorization endpoint using Scope and redirectURL that is supplied.
319+ * A CSRF token is generated and sent in the request.
320+ *
321+ * @param scopes
322+ * @param redirectUri
323+ * @return
324+ * @throws InvalidRequestException
325+ */
326+ public String prepareUrlWithCustomScopes (List <String > scopes , String redirectUri ) throws InvalidRequestException {
327+
328+ logger .debug ("Enter OAuth2config::prepareUrl" );
329+ if (scopes == null || scopes .isEmpty () || redirectUri .isEmpty ()) {
330+ logger .error ("Invalid request for prepareUrl " );
331+ throw new InvalidRequestException ("Invalid request for prepareUrl" );
332+ }
333+
334+ try {
335+
336+ return intuitAuthorizationEndpoint
337+ + "?client_id=" + clientId
338+ + "&response_type=code&scope=" + buildScope (scopes )
339+ + "&redirect_uri=" + URLEncoder .encode (redirectUri , "UTF-8" )
340+ + "&state=" + generateCSRFToken ();
341+ } catch (UnsupportedEncodingException e ) {
342+ logger .error ("Exception while preparing url for redirect " , e );
343+ throw new InvalidRequestException (e .getMessage (), e );
344+ }
345+
346+ }
347+
348+
349+ /**
350+ * Prepares URL to call the OAuth2 authorization endpoint using Scopes, redirectURL & csrfToken that is supplied.
351+ * A CSRF token is generated and sent in the request.
352+ *
353+ * @param scopes
354+ * @param redirectUri
355+ * @return
356+ * @throws InvalidRequestException
357+ */
358+ public String prepareUrlWithCustomScopes (List <String > scopes , String redirectUri , String csrfToken ) throws InvalidRequestException {
359+
360+ logger .debug ("Enter OAuth2config::prepareUrl" );
361+ if (scopes == null || scopes .isEmpty () || redirectUri .isEmpty ()) {
362+ logger .error ("Invalid request for prepareUrl " );
363+ throw new InvalidRequestException ("Invalid request for prepareUrl" );
364+ }
365+
366+ try {
367+
368+ return intuitAuthorizationEndpoint
369+ + "?client_id=" + clientId
370+ + "&response_type=code&scope=" + buildScope (scopes )
371+ + "&redirect_uri=" + URLEncoder .encode (redirectUri , "UTF-8" )
372+ + "&state=" + csrfToken ;
373+ } catch (UnsupportedEncodingException e ) {
374+ logger .error ("Exception while preparing url for redirect " , e );
375+ throw new InvalidRequestException (e .getMessage (), e );
376+ }
377+
378+ }
379+
309380}
0 commit comments