Skip to content

Commit 6d35c68

Browse files
authored
fix: (WIF) remove erroneous check for the subject token field name for text credential source (googleapis#822)
* fix: remove erroneous check for the subject token field name for text format in IdentityPoolCredentialSource * fix: case insensitive * fix: null check * fix: imports
1 parent 0e54aee commit 6d35c68

File tree

2 files changed

+99
-9
lines changed

2 files changed

+99
-9
lines changed

oauth2_http/java/com/google/auth/oauth2/IdentityPoolCredentials.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import java.util.ArrayList;
5353
import java.util.Collection;
5454
import java.util.HashMap;
55+
import java.util.Locale;
5556
import java.util.Map;
5657
import javax.annotation.Nullable;
5758

@@ -132,18 +133,21 @@ enum CredentialFormatType {
132133
Map<String, String> formatMap = (Map<String, String>) credentialSourceMap.get("format");
133134
if (formatMap != null && formatMap.containsKey("type")) {
134135
String type = formatMap.get("type");
135-
if (!"text".equals(type) && !"json".equals(type)) {
136-
throw new IllegalArgumentException(
137-
String.format("Invalid credential source format type: %s.", type));
138-
}
139-
credentialFormatType =
140-
type.equals("text") ? CredentialFormatType.TEXT : CredentialFormatType.JSON;
141136

142-
if (!formatMap.containsKey("subject_token_field_name")) {
137+
if (type != null && "json".equals(type.toLowerCase(Locale.US))) {
138+
// For JSON, the subject_token field name must be provided.
139+
if (!formatMap.containsKey("subject_token_field_name")) {
140+
throw new IllegalArgumentException(
141+
"When specifying a JSON credential type, the subject_token_field_name must be set.");
142+
}
143+
credentialFormatType = CredentialFormatType.JSON;
144+
subjectTokenFieldName = formatMap.get("subject_token_field_name");
145+
} else if (type != null && "text".equals(type.toLowerCase(Locale.US))) {
146+
credentialFormatType = CredentialFormatType.TEXT;
147+
} else {
143148
throw new IllegalArgumentException(
144-
"When specifying a JSON credential type, the subject_token_field_name must be set.");
149+
String.format("Invalid credential source format type: %s.", type));
145150
}
146-
subjectTokenFieldName = formatMap.get("subject_token_field_name");
147151
}
148152
}
149153

oauth2_http/javatests/com/google/auth/oauth2/IdentityPoolCredentialsTest.java

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,92 @@ void refreshAccessToken_workforceWithServiceAccountImpersonation() throws IOExce
415415
assertEquals(expectedInternalOptions.toString(), query.get("options"));
416416
}
417417

418+
@Test
419+
void identityPoolCredentialSource_validFormats() {
420+
Map<String, Object> credentialSourceMapWithFileTextSource = new HashMap<>();
421+
Map<String, Object> credentialSourceMapWithFileJsonTextSource = new HashMap<>();
422+
Map<String, Object> credentialSourceMapWithUrlTextSource = new HashMap<>();
423+
Map<String, Object> credentialSourceMapWithUrlJsonTextSource = new HashMap<>();
424+
425+
credentialSourceMapWithFileTextSource.put("file", "/path/to/file");
426+
credentialSourceMapWithFileJsonTextSource.put("file", "/path/to/file");
427+
428+
credentialSourceMapWithUrlTextSource.put("url", "https://google.com");
429+
credentialSourceMapWithUrlJsonTextSource.put("url", "https://google.com");
430+
Map<String, String> headersMap = new HashMap<>();
431+
headersMap.put("header1", "value1");
432+
headersMap.put("header2", "value2");
433+
credentialSourceMapWithUrlTextSource.put("headers", headersMap);
434+
credentialSourceMapWithUrlJsonTextSource.put("headers", headersMap);
435+
436+
Map<String, String> textFormat = new HashMap<>();
437+
textFormat.put("type", "text");
438+
439+
Map<String, String> jsonTextFormat = new HashMap<>();
440+
jsonTextFormat.put("type", "json");
441+
jsonTextFormat.put("subject_token_field_name", "access_token");
442+
443+
credentialSourceMapWithFileTextSource.put("format", textFormat);
444+
credentialSourceMapWithFileJsonTextSource.put("format", jsonTextFormat);
445+
446+
credentialSourceMapWithUrlTextSource.put("format", textFormat);
447+
credentialSourceMapWithUrlJsonTextSource.put("format", jsonTextFormat);
448+
449+
List<Map<String, Object>> sources =
450+
Arrays.asList(
451+
credentialSourceMapWithFileTextSource,
452+
credentialSourceMapWithFileJsonTextSource,
453+
credentialSourceMapWithUrlTextSource,
454+
credentialSourceMapWithUrlJsonTextSource);
455+
for (Map<String, Object> source : sources) {
456+
// Should not throw.
457+
new IdentityPoolCredentialSource(source);
458+
}
459+
}
460+
461+
@Test
462+
void identityPoolCredentialSource_caseInsensitive() {
463+
Map<String, Object> credentialSourceMapWithFileTextSource = new HashMap<>();
464+
Map<String, Object> credentialSourceMapWithFileJsonTextSource = new HashMap<>();
465+
Map<String, Object> credentialSourceMapWithUrlTextSource = new HashMap<>();
466+
Map<String, Object> credentialSourceMapWithUrlJsonTextSource = new HashMap<>();
467+
468+
credentialSourceMapWithFileTextSource.put("file", "/path/to/file");
469+
credentialSourceMapWithFileJsonTextSource.put("file", "/path/to/file");
470+
471+
credentialSourceMapWithUrlTextSource.put("url", "https://google.com");
472+
credentialSourceMapWithUrlJsonTextSource.put("url", "https://google.com");
473+
Map<String, String> headersMap = new HashMap<>();
474+
headersMap.put("HeaDer1", "Value1");
475+
headersMap.put("HeaDer2", "Value2");
476+
credentialSourceMapWithUrlTextSource.put("headers", headersMap);
477+
credentialSourceMapWithUrlJsonTextSource.put("headers", headersMap);
478+
479+
Map<String, String> textFormat = new HashMap<>();
480+
textFormat.put("type", "TEXT");
481+
482+
Map<String, String> jsonTextFormat = new HashMap<>();
483+
jsonTextFormat.put("type", "JSON");
484+
jsonTextFormat.put("subject_token_field_name", "access_token");
485+
486+
credentialSourceMapWithFileTextSource.put("format", textFormat);
487+
credentialSourceMapWithFileJsonTextSource.put("format", jsonTextFormat);
488+
489+
credentialSourceMapWithUrlTextSource.put("format", textFormat);
490+
credentialSourceMapWithUrlJsonTextSource.put("format", jsonTextFormat);
491+
492+
List<Map<String, Object>> sources =
493+
Arrays.asList(
494+
credentialSourceMapWithFileTextSource,
495+
credentialSourceMapWithFileJsonTextSource,
496+
credentialSourceMapWithUrlTextSource,
497+
credentialSourceMapWithUrlJsonTextSource);
498+
for (Map<String, Object> source : sources) {
499+
// Should not throw.
500+
new IdentityPoolCredentialSource(source);
501+
}
502+
}
503+
418504
@Test
419505
void identityPoolCredentialSource_invalidSourceType() {
420506
IllegalArgumentException exception =

0 commit comments

Comments
 (0)