@@ -72,6 +72,10 @@ public class OAuthCodeHandler implements IOAuthHandler {
7272 protected int timeout ;
7373 @ Nullable
7474 protected String state ;
75+
76+ @ Nullable
77+ protected final String scope ;
78+
7579 @ Nullable
7680 protected String codeChallenge ;
7781
@@ -93,6 +97,29 @@ public OAuthCodeHandler(
9397 @ NotNull String callbackEndpoint ,
9498 @ NotNull String redirectUri ,
9599 int callbackPort
100+ ) {
101+ this (clientId , secretId , authUrl , tokenURL , callbackEndpoint , redirectUri , callbackPort , null );
102+ }
103+
104+ /**
105+ * Constructs an OAuthHandler with required parameters.
106+ *
107+ * @param clientId the OAuth client ID
108+ * @param secretId the OAuth client secret (nullable for PKCE-only flows)
109+ * @param authUrl the authorization endpoint URL
110+ * @param tokenURL the token exchange endpoint URL
111+ * @param callbackPort the port on which the temporary server will listen for the callback
112+ * @param scope requested scope
113+ */
114+ public OAuthCodeHandler (
115+ @ NotNull String clientId ,
116+ @ Nullable String secretId ,
117+ @ NotNull String authUrl ,
118+ @ NotNull String tokenURL ,
119+ @ NotNull String callbackEndpoint ,
120+ @ NotNull String redirectUri ,
121+ int callbackPort ,
122+ @ Nullable String scope
96123 ) {
97124 this .clientId = clientId ;
98125 this .secretId = secretId ;
@@ -101,9 +128,9 @@ public OAuthCodeHandler(
101128 this .callbackEndpoint = callbackEndpoint ;
102129 this .callbackPort = callbackPort ;
103130 this .redirectUri = redirectUri ;
131+ this .scope = scope ;
104132 }
105133
106-
107134 /**
108135 * Sets the timeout (in seconds) to wait for the OAuth callback response.
109136 *
@@ -277,6 +304,9 @@ protected String buildAuthUrl() throws IOException {
277304 if (codeChallenge != null ) {
278305 builder .withCodeChallenge (codeChallenge );
279306 }
307+ if (CommonUtils .isNotEmpty (scope )) {
308+ builder .withScope (scope );
309+ }
280310 return builder .build ();
281311 }
282312
@@ -298,6 +328,8 @@ public static class OAuthCodeHandlerBuilder<T extends OAuthCodeHandler> {
298328 protected String redirectUri ;
299329 protected int callbackPort = 0 ;
300330
331+ protected String scope ;
332+
301333 protected int timeout = OAuthConstants .AUTH_DEFAULT_SSO_TIMEOUT ;
302334 protected String callbackEndpoint = OAuthConstants .DEFAULT_CALLBACK_ENDPOINT ;
303335 protected String state ;
@@ -343,6 +375,12 @@ public OAuthCodeHandlerBuilder<T> withRedirectUri(@NotNull String redirectUri) {
343375 return this ;
344376 }
345377
378+ @ NotNull
379+ public OAuthCodeHandlerBuilder <T > withScope (@ NotNull String scope ) {
380+ this .scope = scope ;
381+ return this ;
382+ }
383+
346384 @ NotNull
347385 public T build () {
348386 if (CommonUtils .isEmpty (clientId )) {
@@ -370,7 +408,7 @@ public T build() {
370408 @ NotNull
371409 protected T createOAuthCodeHandler () {
372410 //noinspection unchecked
373- return (T ) new OAuthCodeHandler (clientId , secretId , authUrl , tokenURL , callbackEndpoint , redirectUri , callbackPort );
411+ return (T ) new OAuthCodeHandler (clientId , secretId , authUrl , tokenURL , callbackEndpoint , redirectUri , callbackPort , scope );
374412 }
375413 }
376414}
0 commit comments