Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support additional request parameters for 3LO #1070

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ public class AuthorizationCodeFlow {
/** Refresh listeners provided by the client. */
private final Collection<CredentialRefreshListener> refreshListeners;

/** Additional parameters to pass to authorization url. */
private final Map<String, String> additionalParameters;

/**
* @param method method of presenting the access token to the resource server (for example {@link
* BearerToken#authorizationHeaderAccessMethod})
Expand All @@ -131,7 +134,8 @@ public AuthorizationCodeFlow(
GenericUrl tokenServerUrl,
HttpExecuteInterceptor clientAuthentication,
String clientId,
String authorizationServerEncodedUrl) {
String authorizationServerEncodedUrl)
{
this(
new Builder(
method,
Expand All @@ -140,7 +144,31 @@ public AuthorizationCodeFlow(
tokenServerUrl,
clientAuthentication,
clientId,
authorizationServerEncodedUrl));
authorizationServerEncodedUrl)
);
}

public AuthorizationCodeFlow(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets remove this overloaded method. If someone really wants to pass additional parameters, they can create builder, set additionalparam and call build() on the builder. Exactly like what we have done in the test.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I see. I have removed overloaded method.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets remove this overloaded method

AccessMethod method,
HttpTransport transport,
JsonFactory jsonFactory,
GenericUrl tokenServerUrl,
HttpExecuteInterceptor clientAuthentication,
String clientId,
String authorizationServerEncodedUrl,
Map<String, String> additionalParameters)
{
this(
new Builder(
method,
transport,
jsonFactory,
tokenServerUrl,
clientAuthentication,
clientId,
authorizationServerEncodedUrl,
additionalParameters)
);
}

/**
Expand All @@ -156,6 +184,7 @@ protected AuthorizationCodeFlow(Builder builder) {
clientId = Preconditions.checkNotNull(builder.clientId);
authorizationServerEncodedUrl =
Preconditions.checkNotNull(builder.authorizationServerEncodedUrl);
additionalParameters = Preconditions.checkNotNull(builder.additionalParameters);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need of null check here because it is an optional thing. You are checking for null before using it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! Removed precondition check.

requestInitializer = builder.requestInitializer;
credentialStore = builder.credentialStore;
credentialDataStore = builder.credentialDataStore;
Expand Down Expand Up @@ -192,6 +221,11 @@ public AuthorizationCodeRequestUrl newAuthorizationUrl() {
url.setCodeChallenge(pkce.getChallenge());
url.setCodeChallengeMethod(pkce.getChallengeMethod());
}
if (additionalParameters != null) {
for (Map.Entry<String, String> entry : additionalParameters.entrySet()) {
url.put(entry.getKey(), entry.getValue());
}
}
return url;
}

Expand Down Expand Up @@ -549,6 +583,9 @@ public static class Builder {
/** Refresh listeners provided by the client. */
Collection<CredentialRefreshListener> refreshListeners = Lists.newArrayList();

/** Additional authorization url parameters provided by the client **/
Map<String, String> additionalParameters;

/**
* @param method method of presenting the access token to the resource server (for example
* {@link BearerToken#authorizationHeaderAccessMethod})
Expand All @@ -560,21 +597,42 @@ public static class Builder {
* @param clientId client identifier
* @param authorizationServerEncodedUrl authorization server encoded URL
*/
public Builder(

public Builder(
AccessMethod method,
HttpTransport transport,
JsonFactory jsonFactory,
GenericUrl tokenServerUrl,
HttpExecuteInterceptor clientAuthentication,
String clientId,
String authorizationServerEncodedUrl) {
setMethod(method);
setTransport(transport);
setJsonFactory(jsonFactory);
setTokenServerUrl(tokenServerUrl);
setClientAuthentication(clientAuthentication);
setClientId(clientId);
setAuthorizationServerEncodedUrl(authorizationServerEncodedUrl);
setAdditionalParameters(Collections.<String, String>emptyMap());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is ok to just leave additional params as null. No need to set as empty map. It is ok to have it as well, no harm.

}

public Builder(
sai-sunder-s marked this conversation as resolved.
Show resolved Hide resolved
AccessMethod method,
HttpTransport transport,
JsonFactory jsonFactory,
GenericUrl tokenServerUrl,
HttpExecuteInterceptor clientAuthentication,
String clientId,
String authorizationServerEncodedUrl) {
String authorizationServerEncodedUrl,
Map<String, String> additionalParameters) {
setMethod(method);
setTransport(transport);
setJsonFactory(jsonFactory);
setTokenServerUrl(tokenServerUrl);
setClientAuthentication(clientAuthentication);
setClientId(clientId);
setAuthorizationServerEncodedUrl(authorizationServerEncodedUrl);
setAdditionalParameters(additionalParameters);
}

/** Returns a new instance of an authorization code flow based on this builder. */
Expand Down Expand Up @@ -717,6 +775,20 @@ public Builder setAuthorizationServerEncodedUrl(String authorizationServerEncode
return this;
}

/**
* Sets the additional url parameters.
*
* <p>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<String, String> additionalParameters) {
this.additionalParameters =
Preconditions.checkNotNull(additionalParameters);
return this;
}

/**
* {@link Beta} <br>
* Returns the credential persistence store or {@code null} for none.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
Expand Down Expand Up @@ -154,4 +156,24 @@ public void testPKCE() {
assertTrue(methods.contains(url.getCodeChallengeMethod().toLowerCase()));
assertTrue(url.getCodeChallenge().length() > 0);
}

public void testAdditionalParameters() {
Map<String, String> 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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package com.google.api.client.auth.oauth2;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import junit.framework.TestCase;

/**
Expand All @@ -41,4 +43,25 @@ public void testBuild() {
.setScopes(Arrays.asList("a", "b", "c"));
assertEquals(EXPECTED, url.build());
}

/*
public void testAdditionalParams() {
sai-sunder-s marked this conversation as resolved.
Show resolved Hide resolved

Map<String, String> testMap = new HashMap<>();
String expectedUrl =
"https://server.example.com/authorize?client_id=s6BhdRkqt3&"
+ "redirect_uri=https://client.example.com/rd&response_type=code"
+ "&scope=a%20b%20c&state=xyz&param1=value1";

testMap.put("param1", "value1");
AuthorizationRequestUrl url =
new AuthorizationCodeRequestUrl("https://server.example.com/authorize", "s6BhdRkqt3")
.setState("xyz")
.setRedirectUri("https://client.example.com/rd")
.setScopes(Arrays.asList("a", "b", "c"));

assertEquals(expectedUrl, url.build());
}
*/

}
Loading