3232package com .google .auth .oauth2 ;
3333
3434import static org .junit .Assert .assertEquals ;
35+ import static org .junit .Assert .assertFalse ;
3536import static org .junit .Assert .assertNull ;
3637import static org .junit .Assert .assertSame ;
3738import static org .junit .Assert .fail ;
4344import java .net .URL ;
4445import java .util .Arrays ;
4546import java .util .Date ;
47+ import java .util .HashMap ;
4648import java .util .List ;
4749import java .util .Map ;
4850import org .junit .Test ;
@@ -170,6 +172,50 @@ public void getAuthorizationUrl() throws IOException {
170172 assertEquals (pkce .getCodeChallengeMethod (), parameters .get ("code_challenge_method" ));
171173 }
172174
175+ @ Test
176+ public void getAuthorizationUrl_additionalParameters () throws IOException {
177+ final String CUSTOM_STATE = "custom_state" ;
178+ final String PROTOCOL = "https" ;
179+ final String HOST = "accounts.test.com" ;
180+ final String PATH = "/o/o/oauth2/auth" ;
181+ final URI AUTH_URI = URI .create (PROTOCOL + "://" + HOST + PATH );
182+ final String EXPECTED_CALLBACK = "http://example.com" + CALLBACK_URI .toString ();
183+ UserAuthorizer authorizer =
184+ UserAuthorizer .newBuilder ()
185+ .setClientId (CLIENT_ID )
186+ .setScopes (DUMMY_SCOPES )
187+ .setCallbackUri (CALLBACK_URI )
188+ .setUserAuthUri (AUTH_URI )
189+ .build ();
190+ Map <String , String > additionalParameters = new HashMap <String , String >();
191+ additionalParameters .put ("param1" , "value1" );
192+ additionalParameters .put ("param2" , "value2" );
193+
194+ // Verify that the authorization URL doesn't include the additional parameters if they are not
195+ // passed in.
196+ URL authorizationUrl = authorizer .getAuthorizationUrl (USER_ID , CUSTOM_STATE , BASE_URI );
197+ String query = authorizationUrl .getQuery ();
198+ Map <String , String > parameters = TestUtils .parseQuery (query );
199+ assertFalse (parameters .containsKey ("param1" ));
200+ assertFalse (parameters .containsKey ("param2" ));
201+
202+ // Verify that the authorization URL includes the additional parameters if they are passed in.
203+ authorizationUrl =
204+ authorizer .getAuthorizationUrl (USER_ID , CUSTOM_STATE , BASE_URI , additionalParameters );
205+ query = authorizationUrl .getQuery ();
206+ parameters = TestUtils .parseQuery (query );
207+ assertEquals ("value1" , parameters .get ("param1" ));
208+ assertEquals ("value2" , parameters .get ("param2" ));
209+
210+ // Verify that the authorization URL doesn't include the additional parameters passed in the
211+ // previous call to the authorizer
212+ authorizationUrl = authorizer .getAuthorizationUrl (USER_ID , CUSTOM_STATE , BASE_URI );
213+ query = authorizationUrl .getQuery ();
214+ parameters = TestUtils .parseQuery (query );
215+ assertFalse (parameters .containsKey ("param1" ));
216+ assertFalse (parameters .containsKey ("param2" ));
217+ }
218+
173219 @ Test
174220 public void getCredentials_noCredentials_returnsNull () throws IOException {
175221 UserAuthorizer authorizer =
@@ -340,7 +386,41 @@ public void getCredentialsFromCode_conevertsCodeToTokens() throws IOException {
340386 MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory ();
341387 transportFactory .transport .addClient (CLIENT_ID_VALUE , CLIENT_SECRET );
342388 transportFactory .transport .addAuthorizationCode (
343- CODE , REFRESH_TOKEN , ACCESS_TOKEN_VALUE , GRANTED_SCOPES_STRING );
389+ CODE , REFRESH_TOKEN , ACCESS_TOKEN_VALUE , GRANTED_SCOPES_STRING , null );
390+ TokenStore tokenStore = new MemoryTokensStorage ();
391+ UserAuthorizer authorizer =
392+ UserAuthorizer .newBuilder ()
393+ .setClientId (CLIENT_ID )
394+ .setScopes (DUMMY_SCOPES )
395+ .setTokenStore (tokenStore )
396+ .setHttpTransportFactory (transportFactory )
397+ .build ();
398+
399+ UserCredentials credentials = authorizer .getCredentialsFromCode (CODE , BASE_URI );
400+
401+ assertEquals (REFRESH_TOKEN , credentials .getRefreshToken ());
402+ assertEquals (ACCESS_TOKEN_VALUE , credentials .getAccessToken ().getTokenValue ());
403+ assertEquals (GRANTED_SCOPES , credentials .getAccessToken ().getScopes ());
404+ }
405+
406+ @ Test
407+ public void getCredentialsFromCode_additionalParameters () throws IOException {
408+ MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory ();
409+ transportFactory .transport .addClient (CLIENT_ID_VALUE , CLIENT_SECRET );
410+
411+ Map <String , String > additionalParameters = new HashMap <String , String >();
412+ additionalParameters .put ("param1" , "value1" );
413+ additionalParameters .put ("param2" , "value2" );
414+
415+ String code2 = "code2" ;
416+ String refreshToken2 = "refreshToken2" ;
417+ String accessTokenValue2 = "accessTokenValue2" ;
418+
419+ transportFactory .transport .addAuthorizationCode (
420+ CODE , REFRESH_TOKEN , ACCESS_TOKEN_VALUE , GRANTED_SCOPES_STRING , null );
421+ transportFactory .transport .addAuthorizationCode (
422+ code2 , refreshToken2 , accessTokenValue2 , GRANTED_SCOPES_STRING , additionalParameters );
423+
344424 TokenStore tokenStore = new MemoryTokensStorage ();
345425 UserAuthorizer authorizer =
346426 UserAuthorizer .newBuilder ()
@@ -350,8 +430,20 @@ public void getCredentialsFromCode_conevertsCodeToTokens() throws IOException {
350430 .setHttpTransportFactory (transportFactory )
351431 .build ();
352432
433+ // Verify that the additional parameters are not attached to the post body when not specified
353434 UserCredentials credentials = authorizer .getCredentialsFromCode (CODE , BASE_URI );
435+ assertEquals (REFRESH_TOKEN , credentials .getRefreshToken ());
436+ assertEquals (ACCESS_TOKEN_VALUE , credentials .getAccessToken ().getTokenValue ());
437+ assertEquals (GRANTED_SCOPES , credentials .getAccessToken ().getScopes ());
438+
439+ // Verify that the additional parameters are attached to the post body when specified
440+ credentials = authorizer .getCredentialsFromCode (code2 , BASE_URI , additionalParameters );
441+ assertEquals (refreshToken2 , credentials .getRefreshToken ());
442+ assertEquals (accessTokenValue2 , credentials .getAccessToken ().getTokenValue ());
443+ assertEquals (GRANTED_SCOPES , credentials .getAccessToken ().getScopes ());
354444
445+ // Verify that the additional parameters from previous request are not attached to the post body
446+ credentials = authorizer .getCredentialsFromCode (CODE , BASE_URI );
355447 assertEquals (REFRESH_TOKEN , credentials .getRefreshToken ());
356448 assertEquals (ACCESS_TOKEN_VALUE , credentials .getAccessToken ().getTokenValue ());
357449 assertEquals (GRANTED_SCOPES , credentials .getAccessToken ().getScopes ());
@@ -376,7 +468,7 @@ public void getAndStoreCredentialsFromCode_getAndStoresCredentials() throws IOEx
376468 MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory ();
377469 transportFactory .transport .addClient (CLIENT_ID_VALUE , CLIENT_SECRET );
378470 transportFactory .transport .addAuthorizationCode (
379- CODE , REFRESH_TOKEN , accessTokenValue1 , GRANTED_SCOPES_STRING );
471+ CODE , REFRESH_TOKEN , accessTokenValue1 , GRANTED_SCOPES_STRING , null );
380472 TokenStore tokenStore = new MemoryTokensStorage ();
381473 UserAuthorizer authorizer =
382474 UserAuthorizer .newBuilder ()
0 commit comments