Skip to content

Commit 0aebe37

Browse files
authored
Add validation to provider config ID. (#410)
The OIDC Auth provider config ID must begin with "oidc.". This validation is being done in other APIs (e.g. Go), so we should do it here as well. I've moved assertValidUrl to the base class so that it can be reused for SamlProviderConfig, once that class is added.
1 parent 3a77897 commit 0aebe37

File tree

3 files changed

+28
-24
lines changed

3 files changed

+28
-24
lines changed

src/main/java/com/google/firebase/auth/OidcProviderConfig.java

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,8 @@
2020

2121
import com.google.api.client.util.Key;
2222
import com.google.common.base.Strings;
23-
import com.google.common.collect.ImmutableMap;
2423
import com.google.firebase.auth.ProviderConfig.AbstractCreateRequest;
2524
import com.google.firebase.auth.ProviderConfig.AbstractUpdateRequest;
26-
import java.net.MalformedURLException;
27-
import java.net.URL;
28-
import java.util.HashMap;
29-
import java.util.Map;
3025

3126
/**
3227
* Contains metadata associated with an OIDC Auth provider.
@@ -59,14 +54,6 @@ public UpdateRequest updateRequest() {
5954
return new UpdateRequest(getProviderId());
6055
}
6156

62-
private static void assertValidUrl(String url) throws IllegalArgumentException {
63-
try {
64-
new URL(url);
65-
} catch (MalformedURLException e) {
66-
throw new IllegalArgumentException(url + " is a malformed URL", e);
67-
}
68-
}
69-
7057
/**
7158
* A specification class for creating a new OIDC Auth provider.
7259
*
@@ -90,18 +77,18 @@ public CreateRequest() { }
9077
* @param clientId a non-null, non-empty client ID string.
9178
*/
9279
public CreateRequest setClientId(String clientId) {
93-
checkArgument(!Strings.isNullOrEmpty(clientId), "client ID must not be null or empty");
80+
checkArgument(!Strings.isNullOrEmpty(clientId), "Client ID must not be null or empty.");
9481
properties.put("clientId", clientId);
9582
return this;
9683
}
9784

9885
/**
9986
* Sets the issuer for the new provider.
10087
*
101-
* @param issuer a non-null, non-empty issuer string.
88+
* @param issuer a non-null, non-empty issuer URL string.
10289
*/
10390
public CreateRequest setIssuer(String issuer) {
104-
checkArgument(!Strings.isNullOrEmpty(issuer), "issuer must not be null or empty");
91+
checkArgument(!Strings.isNullOrEmpty(issuer), "Issuer must not be null or empty.");
10592
assertValidUrl(issuer);
10693
properties.put("issuer", issuer);
10794
return this;
@@ -130,10 +117,12 @@ public static final class UpdateRequest extends AbstractUpdateRequest<UpdateRequ
130117
* information persistently.
131118
*
132119
* @param tenantId a non-null, non-empty provider ID string.
133-
* @throws IllegalArgumentException If the provider ID is null or empty.
120+
* @throws IllegalArgumentException If the provider ID is null or empty, or if it's an invalid
121+
* format
134122
*/
135123
public UpdateRequest(String providerId) {
136124
super(providerId);
125+
checkArgument(providerId.startsWith("oidc."), "Invalid OIDC provider ID: " + providerId);
137126
}
138127

139128
/**
@@ -142,18 +131,18 @@ public UpdateRequest(String providerId) {
142131
* @param clientId a non-null, non-empty client ID string.
143132
*/
144133
public UpdateRequest setClientId(String clientId) {
145-
checkArgument(!Strings.isNullOrEmpty(clientId), "client ID must not be null or empty");
134+
checkArgument(!Strings.isNullOrEmpty(clientId), "Client ID must not be null or empty.");
146135
properties.put("clientId", clientId);
147136
return this;
148137
}
149138

150139
/**
151140
* Sets the issuer for the existing provider.
152141
*
153-
* @param issuer a non-null, non-empty issuer string.
142+
* @param issuer a non-null, non-empty issuer URL string.
154143
*/
155144
public UpdateRequest setIssuer(String issuer) {
156-
checkArgument(!Strings.isNullOrEmpty(issuer), "issuer must not be null or empty");
145+
checkArgument(!Strings.isNullOrEmpty(issuer), "Issuer must not be null or empty.");
157146
assertValidUrl(issuer);
158147
properties.put("issuer", issuer);
159148
return this;

src/main/java/com/google/firebase/auth/ProviderConfig.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import com.google.api.client.util.Key;
2222
import com.google.common.base.Strings;
2323
import com.google.common.collect.ImmutableMap;
24+
import java.net.MalformedURLException;
25+
import java.net.URL;
2426
import java.util.HashMap;
2527
import java.util.Map;
2628

@@ -50,6 +52,14 @@ public boolean isEnabled() {
5052
return enabled;
5153
}
5254

55+
static void assertValidUrl(String url) throws IllegalArgumentException {
56+
try {
57+
new URL(url);
58+
} catch (MalformedURLException e) {
59+
throw new IllegalArgumentException(url + " is a malformed URL.", e);
60+
}
61+
}
62+
5363
/**
5464
* A base specification class for creating a new provider.
5565
*
@@ -68,7 +78,7 @@ public abstract static class AbstractCreateRequest<T extends AbstractCreateReque
6878
*/
6979
public T setProviderId(String providerId) {
7080
checkArgument(
71-
!Strings.isNullOrEmpty(providerId), "provider ID name must not be null or empty");
81+
!Strings.isNullOrEmpty(providerId), "Provider ID name must not be null or empty.");
7282
this.providerId = providerId;
7383
return getThis();
7484
}
@@ -83,7 +93,7 @@ String getProviderId() {
8393
* @param displayName a non-null, non-empty display name string.
8494
*/
8595
public T setDisplayName(String displayName) {
86-
checkArgument(!Strings.isNullOrEmpty(displayName), "display name must not be null or empty");
96+
checkArgument(!Strings.isNullOrEmpty(displayName), "Display name must not be null or empty.");
8797
properties.put("displayName", displayName);
8898
return getThis();
8999
}
@@ -114,7 +124,7 @@ public abstract static class AbstractUpdateRequest<T extends AbstractUpdateReque
114124
final Map<String,Object> properties = new HashMap<>();
115125

116126
AbstractUpdateRequest(String providerId) {
117-
checkArgument(!Strings.isNullOrEmpty(providerId), "provider ID must not be null or empty");
127+
checkArgument(!Strings.isNullOrEmpty(providerId), "Provider ID must not be null or empty.");
118128
this.providerId = providerId;
119129
}
120130

@@ -128,7 +138,7 @@ String getProviderId() {
128138
* @param displayName a non-null, non-empty display name string.
129139
*/
130140
public T setDisplayName(String displayName) {
131-
checkArgument(!Strings.isNullOrEmpty(displayName), "display name must not be null or empty");
141+
checkArgument(!Strings.isNullOrEmpty(displayName), "Display name must not be null or empty.");
132142
properties.put("displayName", displayName);
133143
return getThis();
134144
}

src/test/java/com/google/firebase/auth/OidcProviderConfigTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ public void testUpdateRequestMissingProviderId() {
114114
new OidcProviderConfig.UpdateRequest(null);
115115
}
116116

117+
@Test(expected = IllegalArgumentException.class)
118+
public void testUpdateRequestInvalidProviderId() {
119+
new OidcProviderConfig.UpdateRequest("saml.provider-id");
120+
}
121+
117122
@Test(expected = IllegalArgumentException.class)
118123
public void testUpdateRequestMissingClientId() {
119124
new OidcProviderConfig.UpdateRequest("oidc.provider-id").setClientId(null);

0 commit comments

Comments
 (0)