4444import java .io .ByteArrayInputStream ;
4545import java .io .IOException ;
4646import java .net .URI ;
47+ import java .util .Arrays ;
4748import java .util .Collection ;
4849import java .util .Date ;
4950import java .util .HashMap ;
5051import java .util .List ;
52+ import java .util .Locale ;
5153import java .util .Map ;
5254import javax .annotation .Nullable ;
5355import org .junit .Before ;
5961@ RunWith (JUnit4 .class )
6062public class ExternalAccountCredentialsTest {
6163
62- private static final String STS_URL = "https://www. sts.google .com" ;
64+ private static final String STS_URL = "https://sts.googleapis .com" ;
6365
6466 static class MockExternalAccountCredentialsTransportFactory implements HttpTransportFactory {
6567
@@ -176,7 +178,7 @@ public void fromJson_nullJson_throws() {
176178 @ Test
177179 public void fromJson_invalidServiceAccountImpersonationUrl_throws () {
178180 GenericJson json = buildJsonIdentityPoolCredential ();
179- json .put ("service_account_impersonation_url" , "invalid_url " );
181+ json .put ("service_account_impersonation_url" , "https://iamcredentials.googleapis.com " );
180182
181183 try {
182184 ExternalAccountCredentials .fromJson (json , OAuth2Utils .HTTP_TRANSPORT_FACTORY );
@@ -199,6 +201,48 @@ public void fromJson_nullTransport_throws() {
199201 }
200202 }
201203
204+ @ Test
205+ public void constructor_invalidTokenUrl () {
206+ try {
207+ new TestExternalAccountCredentials (
208+ transportFactory ,
209+ "audience" ,
210+ "subjectTokenType" ,
211+ "tokenUrl" ,
212+ new TestCredentialSource (new HashMap <String , Object >()),
213+ STS_URL ,
214+ /* serviceAccountImpersonationUrl= */ null ,
215+ "quotaProjectId" ,
216+ /* clientId= */ null ,
217+ /* clientSecret= */ null ,
218+ /* scopes= */ null );
219+ fail ("Should have failed since an invalid token URL was passed." );
220+ } catch (IllegalArgumentException e ) {
221+ assertEquals ("The provided token URL is invalid." , e .getMessage ());
222+ }
223+ }
224+
225+ @ Test
226+ public void constructor_invalidServiceAccountImpersonationUrl () {
227+ try {
228+ new TestExternalAccountCredentials (
229+ transportFactory ,
230+ "audience" ,
231+ "subjectTokenType" ,
232+ "tokenUrl" ,
233+ new TestCredentialSource (new HashMap <String , Object >()),
234+ /* tokenInfoUrl= */ null ,
235+ "serviceAccountImpersonationUrl" ,
236+ "quotaProjectId" ,
237+ /* clientId= */ null ,
238+ /* clientSecret= */ null ,
239+ /* scopes= */ null );
240+ fail ("Should have failed since an invalid token URL was passed." );
241+ } catch (IllegalArgumentException e ) {
242+ assertEquals ("The provided token URL is invalid." , e .getMessage ());
243+ }
244+ }
245+
202246 @ Test
203247 public void exchangeExternalCredentialForAccessToken () throws IOException {
204248 ExternalAccountCredentials credential =
@@ -267,7 +311,7 @@ public void getRequestMetadata_withQuotaProjectId() throws IOException {
267311 transportFactory ,
268312 "audience" ,
269313 "subjectTokenType" ,
270- "tokenUrl" ,
314+ STS_URL ,
271315 new TestCredentialSource (new HashMap <String , Object >()),
272316 "tokenInfoUrl" ,
273317 /* serviceAccountImpersonationUrl= */ null ,
@@ -282,6 +326,113 @@ public void getRequestMetadata_withQuotaProjectId() throws IOException {
282326 assertEquals ("quotaProjectId" , requestMetadata .get ("x-goog-user-project" ).get (0 ));
283327 }
284328
329+ @ Test
330+ public void validateTokenUrl_validUrls () {
331+ List <String > validUrls =
332+ Arrays .asList (
333+ "https://sts.googleapis.com" ,
334+ "https://us-east-1.sts.googleapis.com" ,
335+ "https://US-EAST-1.sts.googleapis.com" ,
336+ "https://sts.us-east-1.googleapis.com" ,
337+ "https://sts.US-WEST-1.googleapis.com" ,
338+ "https://us-east-1-sts.googleapis.com" ,
339+ "https://US-WEST-1-sts.googleapis.com" ,
340+ "https://us-west-1-sts.googleapis.com/path?query" );
341+
342+ for (String url : validUrls ) {
343+ ExternalAccountCredentials .validateTokenUrl (url );
344+ ExternalAccountCredentials .validateTokenUrl (url .toUpperCase (Locale .US ));
345+ }
346+ }
347+
348+ @ Test
349+ public void validateTokenUrl_invalidUrls () {
350+ List <String > invalidUrls =
351+ Arrays .asList (
352+ "https://iamcredentials.googleapis.com" ,
353+ "sts.googleapis.com" ,
354+ "https://" ,
355+ "http://sts.googleapis.com" ,
356+ "https://st.s.googleapis.com" ,
357+ "https://us-eas\\ t-1.sts.googleapis.com" ,
358+ "https:/us-east-1.sts.googleapis.com" ,
359+ "https://US-WE/ST-1-sts.googleapis.com" ,
360+ "https://sts-us-east-1.googleapis.com" ,
361+ "https://sts-US-WEST-1.googleapis.com" ,
362+ "testhttps://us-east-1.sts.googleapis.com" ,
363+ "https://us-east-1.sts.googleapis.comevil.com" ,
364+ "https://us-east-1.us-east-1.sts.googleapis.com" ,
365+ "https://us-ea.s.t.sts.googleapis.com" ,
366+ "https://sts.googleapis.comevil.com" ,
367+ "hhttps://us-east-1.sts.googleapis.com" ,
368+ "https://us- -1.sts.googleapis.com" ,
369+ "https://-sts.googleapis.com" ,
370+ "https://us-east-1.sts.googleapis.com.evil.com" );
371+
372+ for (String url : invalidUrls ) {
373+ try {
374+ ExternalAccountCredentials .validateTokenUrl (url );
375+ fail ("Should have failed since an invalid URL was passed." );
376+ } catch (IllegalArgumentException e ) {
377+ assertEquals ("The provided token URL is invalid." , e .getMessage ());
378+ }
379+ }
380+ }
381+
382+ @ Test
383+ public void validateServiceAccountImpersonationUrls_validUrls () {
384+ List <String > validUrls =
385+ Arrays .asList (
386+ "https://iamcredentials.googleapis.com" ,
387+ "https://us-east-1.iamcredentials.googleapis.com" ,
388+ "https://US-EAST-1.iamcredentials.googleapis.com" ,
389+ "https://iamcredentials.us-east-1.googleapis.com" ,
390+ "https://iamcredentials.US-WEST-1.googleapis.com" ,
391+ "https://us-east-1-iamcredentials.googleapis.com" ,
392+ "https://US-WEST-1-iamcredentials.googleapis.com" ,
393+ "https://us-west-1-iamcredentials.googleapis.com/path?query" );
394+
395+ for (String url : validUrls ) {
396+ ExternalAccountCredentials .validateServiceAccountImpersonationInfoUrl (url );
397+ ExternalAccountCredentials .validateServiceAccountImpersonationInfoUrl (
398+ url .toUpperCase (Locale .US ));
399+ }
400+ }
401+
402+ @ Test
403+ public void validateServiceAccountImpersonationUrls_invalidUrls () {
404+ List <String > invalidUrls =
405+ Arrays .asList (
406+ "https://sts.googleapis.com" ,
407+ "iamcredentials.googleapis.com" ,
408+ "https://" ,
409+ "http://iamcredentials.googleapis.com" ,
410+ "https://iamcre.dentials.googleapis.com" ,
411+ "https://us-eas\t -1.iamcredentials.googleapis.com" ,
412+ "https:/us-east-1.iamcredentials.googleapis.com" ,
413+ "https://US-WE/ST-1-iamcredentials.googleapis.com" ,
414+ "https://iamcredentials-us-east-1.googleapis.com" ,
415+ "https://iamcredentials-US-WEST-1.googleapis.com" ,
416+ "testhttps://us-east-1.iamcredentials.googleapis.com" ,
417+ "https://us-east-1.iamcredentials.googleapis.comevil.com" ,
418+ "https://us-east-1.us-east-1.iamcredentials.googleapis.com" ,
419+ "https://us-ea.s.t.iamcredentials.googleapis.com" ,
420+ "https://iamcredentials.googleapis.comevil.com" ,
421+ "hhttps://us-east-1.iamcredentials.googleapis.com" ,
422+ "https://us- -1.iamcredentials.googleapis.com" ,
423+ "https://-iamcredentials.googleapis.com" ,
424+ "https://us-east-1.iamcredentials.googleapis.com.evil.com" );
425+
426+ for (String url : invalidUrls ) {
427+ try {
428+ ExternalAccountCredentials .validateServiceAccountImpersonationInfoUrl (url );
429+ fail ("Should have failed since an invalid URL was passed." );
430+ } catch (IllegalArgumentException e ) {
431+ assertEquals ("The provided service account impersonation URL is invalid." , e .getMessage ());
432+ }
433+ }
434+ }
435+
285436 private GenericJson buildJsonIdentityPoolCredential () {
286437 GenericJson json = new GenericJson ();
287438 json .put ("audience" , "audience" );
0 commit comments