diff --git a/google-oauth-client/src/main/java/com/google/api/client/auth/oauth2/AuthorizationCodeFlow.java b/google-oauth-client/src/main/java/com/google/api/client/auth/oauth2/AuthorizationCodeFlow.java index 4b4944fff..f6a4fa6d4 100644 --- a/google-oauth-client/src/main/java/com/google/api/client/auth/oauth2/AuthorizationCodeFlow.java +++ b/google-oauth-client/src/main/java/com/google/api/client/auth/oauth2/AuthorizationCodeFlow.java @@ -112,6 +112,9 @@ public class AuthorizationCodeFlow { /** Refresh listeners provided by the client. */ private final Collection refreshListeners; + /** Additional parameters to pass to authorization url. */ + private final Map additionalParameters; + /** * @param method method of presenting the access token to the resource server (for example {@link * BearerToken#authorizationHeaderAccessMethod}) @@ -156,6 +159,7 @@ protected AuthorizationCodeFlow(Builder builder) { clientId = Preconditions.checkNotNull(builder.clientId); authorizationServerEncodedUrl = Preconditions.checkNotNull(builder.authorizationServerEncodedUrl); + additionalParameters = builder.additionalParameters; requestInitializer = builder.requestInitializer; credentialStore = builder.credentialStore; credentialDataStore = builder.credentialDataStore; @@ -192,6 +196,11 @@ public AuthorizationCodeRequestUrl newAuthorizationUrl() { url.setCodeChallenge(pkce.getChallenge()); url.setCodeChallengeMethod(pkce.getChallengeMethod()); } + if (additionalParameters != null) { + for (Map.Entry entry : additionalParameters.entrySet()) { + url.put(entry.getKey(), entry.getValue()); + } + } return url; } @@ -549,6 +558,9 @@ public static class Builder { /** Refresh listeners provided by the client. */ Collection refreshListeners = Lists.newArrayList(); + /** Additional authorization url parameters provided by the client * */ + Map additionalParameters; + /** * @param method method of presenting the access token to the resource server (for example * {@link BearerToken#authorizationHeaderAccessMethod}) @@ -575,6 +587,7 @@ public Builder( setClientAuthentication(clientAuthentication); setClientId(clientId); setAuthorizationServerEncodedUrl(authorizationServerEncodedUrl); + setAdditionalParameters(Collections.emptyMap()); } /** Returns a new instance of an authorization code flow based on this builder. */ @@ -717,6 +730,19 @@ public Builder setAuthorizationServerEncodedUrl(String authorizationServerEncode return this; } + /** + * Sets the additional url parameters. + * + *

Overriding is only supported for the purpose of calling the super implementation and + * changing the return type, but nothing else. + * + * @since 1.11 + */ + public Builder setAdditionalParameters(Map additionalParameters) { + this.additionalParameters = Preconditions.checkNotNull(additionalParameters); + return this; + } + /** * {@link Beta}
* Returns the credential persistence store or {@code null} for none. diff --git a/google-oauth-client/src/test/java/com/google/api/client/auth/oauth2/AuthorizationCodeFlowTest.java b/google-oauth-client/src/test/java/com/google/api/client/auth/oauth2/AuthorizationCodeFlowTest.java index a63495cf1..cae90a760 100644 --- a/google-oauth-client/src/test/java/com/google/api/client/auth/oauth2/AuthorizationCodeFlowTest.java +++ b/google-oauth-client/src/test/java/com/google/api/client/auth/oauth2/AuthorizationCodeFlowTest.java @@ -22,7 +22,9 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.HashMap; import java.util.HashSet; +import java.util.Map; import java.util.Set; /** @@ -154,4 +156,24 @@ public void testPKCE() { assertTrue(methods.contains(url.getCodeChallengeMethod().toLowerCase())); assertTrue(url.getCodeChallenge().length() > 0); } + + public void testAdditionalParameters() { + Map testMap = new HashMap<>(); + testMap.put("key", "value"); + AuthorizationCodeFlow flow = + new AuthorizationCodeFlow.Builder( + BearerToken.queryParameterAccessMethod(), + new AccessTokenTransport(), + new GsonFactory(), + TOKEN_SERVER_URL, + new BasicAuthentication(CLIENT_ID, CLIENT_SECRET), + CLIENT_ID, + "https://example.com") + .setAdditionalParameters(testMap) + .build(); + + AuthorizationCodeRequestUrl url = flow.newAuthorizationUrl(); + String urlString = url.build(); + assertTrue(urlString.contains("key=value")); + } }