Skip to content

Commit a2d3f60

Browse files
authored
Make Javadoc comments consistent and validate provider ID in CreateRequest. (#418)
This makes the formatting of the Javadoc comments more consistent, and it's now more clear when/why IllegalArgumentExceptions get thrown. This also adds validation to the provider ID in the CreateRequest to ensure that it has the correct prefix.' Improvements were also made to the tests. The JSON string is now more readable, the parameters are now in the correct order for assertEquals calls, and the IllegalArgumentException tests have been properly fleshed out.
1 parent 21dbf78 commit a2d3f60

File tree

3 files changed

+71
-24
lines changed

3 files changed

+71
-24
lines changed

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

+18-8
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public String getIssuer() {
4848
* Returns a new {@link UpdateRequest}, which can be used to update the attributes of this
4949
* provider config.
5050
*
51-
* @return a non-null {@link UpdateRequest} instance.
51+
* @return A non-null {@link UpdateRequest} instance.
5252
*/
5353
public UpdateRequest updateRequest() {
5454
return new UpdateRequest(getProviderId());
@@ -74,7 +74,8 @@ public CreateRequest() { }
7474
/**
7575
* Sets the client ID for the new provider.
7676
*
77-
* @param clientId a non-null, non-empty client ID string.
77+
* @param clientId A non-null, non-empty client ID string.
78+
* @throws IllegalArgumentException If the client ID is null or empty.
7879
*/
7980
public CreateRequest setClientId(String clientId) {
8081
checkArgument(!Strings.isNullOrEmpty(clientId), "Client ID must not be null or empty.");
@@ -85,7 +86,9 @@ public CreateRequest setClientId(String clientId) {
8586
/**
8687
* Sets the issuer for the new provider.
8788
*
88-
* @param issuer a non-null, non-empty issuer URL string.
89+
* @param issuer A non-null, non-empty issuer URL string.
90+
* @throws IllegalArgumentException If the issuer URL is null or empty, or if the format is
91+
* invalid.
8992
*/
9093
public CreateRequest setIssuer(String issuer) {
9194
checkArgument(!Strings.isNullOrEmpty(issuer), "Issuer must not be null or empty.");
@@ -97,6 +100,10 @@ public CreateRequest setIssuer(String issuer) {
97100
CreateRequest getThis() {
98101
return this;
99102
}
103+
104+
void assertValidProviderIdFormat(String providerId) {
105+
checkArgument(providerId.startsWith("oidc."), "Invalid OIDC provider ID: " + providerId);
106+
}
100107
}
101108

102109
/**
@@ -116,9 +123,9 @@ public static final class UpdateRequest extends AbstractUpdateRequest<UpdateRequ
116123
* {@link AbstractFirebaseAuth#updateOidcProviderConfig(CreateRequest)} to update the provider
117124
* information persistently.
118125
*
119-
* @param tenantId a non-null, non-empty provider ID string.
120-
* @throws IllegalArgumentException If the provider ID is null or empty, or if it's an invalid
121-
* format
126+
* @param tenantId A non-null, non-empty provider ID string.
127+
* @throws IllegalArgumentException If the provider ID is null or empty, or is not prefixed with
128+
* "oidc.".
122129
*/
123130
public UpdateRequest(String providerId) {
124131
super(providerId);
@@ -128,7 +135,8 @@ public UpdateRequest(String providerId) {
128135
/**
129136
* Sets the client ID for the exsting provider.
130137
*
131-
* @param clientId a non-null, non-empty client ID string.
138+
* @param clientId A non-null, non-empty client ID string.
139+
* @throws IllegalArgumentException If the client ID is null or empty.
132140
*/
133141
public UpdateRequest setClientId(String clientId) {
134142
checkArgument(!Strings.isNullOrEmpty(clientId), "Client ID must not be null or empty.");
@@ -139,7 +147,9 @@ public UpdateRequest setClientId(String clientId) {
139147
/**
140148
* Sets the issuer for the existing provider.
141149
*
142-
* @param issuer a non-null, non-empty issuer URL string.
150+
* @param issuer A non-null, non-empty issuer URL string.
151+
* @throws IllegalArgumentException If the issuer URL is null or empty, or if the format is
152+
* invalid.
143153
*/
144154
public UpdateRequest setIssuer(String issuer) {
145155
checkArgument(!Strings.isNullOrEmpty(issuer), "Issuer must not be null or empty.");

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

+12-5
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,14 @@ public abstract static class AbstractCreateRequest<T extends AbstractCreateReque
7474
/**
7575
* Sets the ID for the new provider.
7676
*
77-
* @param providerId a non-null, non-empty provider ID string.
77+
* @param providerId A non-null, non-empty provider ID string.
78+
* @throws IllegalArgumentException If the provider ID is null or empty, or if the format is
79+
* invalid.
7880
*/
7981
public T setProviderId(String providerId) {
8082
checkArgument(
8183
!Strings.isNullOrEmpty(providerId), "Provider ID name must not be null or empty.");
84+
assertValidProviderIdFormat(providerId);
8285
this.providerId = providerId;
8386
return getThis();
8487
}
@@ -90,7 +93,8 @@ String getProviderId() {
9093
/**
9194
* Sets the display name for the new provider.
9295
*
93-
* @param displayName a non-null, non-empty display name string.
96+
* @param displayName A non-null, non-empty display name string.
97+
* @throws IllegalArgumentException If the display name is null or empty.
9498
*/
9599
public T setDisplayName(String displayName) {
96100
checkArgument(!Strings.isNullOrEmpty(displayName), "Display name must not be null or empty.");
@@ -101,7 +105,7 @@ public T setDisplayName(String displayName) {
101105
/**
102106
* Sets whether to allow the user to sign in with the provider.
103107
*
104-
* @param enabled a boolean indicating whether the user can sign in with the provider
108+
* @param enabled A boolean indicating whether the user can sign in with the provider.
105109
*/
106110
public T setEnabled(boolean enabled) {
107111
properties.put("enabled", enabled);
@@ -113,6 +117,8 @@ Map<String, Object> getProperties() {
113117
}
114118

115119
abstract T getThis();
120+
121+
abstract void assertValidProviderIdFormat(String providerId);
116122
}
117123

118124
/**
@@ -135,7 +141,8 @@ String getProviderId() {
135141
/**
136142
* Sets the display name for the existing provider.
137143
*
138-
* @param displayName a non-null, non-empty display name string.
144+
* @param displayName A non-null, non-empty display name string.
145+
* @throws IllegalArgumentException If the display name is null or empty.
139146
*/
140147
public T setDisplayName(String displayName) {
141148
checkArgument(!Strings.isNullOrEmpty(displayName), "Display name must not be null or empty.");
@@ -146,7 +153,7 @@ public T setDisplayName(String displayName) {
146153
/**
147154
* Sets whether to allow the user to sign in with the provider.
148155
*
149-
* @param enabled a boolean indicating whether the user can sign in with the provider
156+
* @param enabled A boolean indicating whether the user can sign in with the provider.
150157
*/
151158
public T setEnabled(boolean enabled) {
152159
properties.put("enabled", enabled);

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

+41-11
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,23 @@ public class OidcProviderConfigTest {
3232
private static final JsonFactory jsonFactory = Utils.getDefaultJsonFactory();
3333

3434
private static final String OIDC_JSON_STRING =
35-
"{"
36-
+ "\"name\":\"projects/projectId/oauthIdpConfigs/oidc.provider-id\","
37-
+ "\"displayName\":\"DISPLAY_NAME\","
38-
+ "\"enabled\":true,"
39-
+ "\"clientId\":\"CLIENT_ID\","
40-
+ "\"issuer\":\"https://oidc.com/issuer\""
41-
+ "}";
35+
("{"
36+
+ " 'name': 'projects/projectId/oauthIdpConfigs/oidc.provider-id',"
37+
+ " 'displayName': 'DISPLAY_NAME',"
38+
+ " 'enabled': true,"
39+
+ " 'clientId': 'CLIENT_ID',"
40+
+ " 'issuer': 'https://oidc.com/issuer'"
41+
+ "}").replace("'", "\"");
4242

4343
@Test
4444
public void testJsonSerialization() throws IOException {
4545
OidcProviderConfig config = jsonFactory.fromString(OIDC_JSON_STRING, OidcProviderConfig.class);
4646

47-
assertEquals(config.getProviderId(), "oidc.provider-id");
48-
assertEquals(config.getDisplayName(), "DISPLAY_NAME");
47+
assertEquals("oidc.provider-id", config.getProviderId());
48+
assertEquals("DISPLAY_NAME", config.getDisplayName());
4949
assertTrue(config.isEnabled());
50-
assertEquals(config.getClientId(), "CLIENT_ID");
51-
assertEquals(config.getIssuer(), "https://oidc.com/issuer");
50+
assertEquals("CLIENT_ID", config.getClientId());
51+
assertEquals("https://oidc.com/issuer", config.getIssuer());
5252
}
5353

5454
@Test
@@ -70,11 +70,31 @@ public void testCreateRequest() throws IOException {
7070
assertEquals("https://oidc.com/issuer", (String) properties.get("issuer"));
7171
}
7272

73+
@Test(expected = IllegalArgumentException.class)
74+
public void testCreateRequestMissingProviderId() {
75+
new OidcProviderConfig.CreateRequest().setProviderId(null);
76+
}
77+
78+
@Test(expected = IllegalArgumentException.class)
79+
public void testCreateRequestInvalidProviderId() {
80+
new OidcProviderConfig.CreateRequest().setProviderId("saml.provider-id");
81+
}
82+
83+
@Test(expected = IllegalArgumentException.class)
84+
public void testCreateRequestMissingDisplayName() {
85+
new OidcProviderConfig.CreateRequest().setDisplayName(null);
86+
}
87+
7388
@Test(expected = IllegalArgumentException.class)
7489
public void testCreateRequestMissingClientId() {
7590
new OidcProviderConfig.CreateRequest().setClientId(null);
7691
}
7792

93+
@Test(expected = IllegalArgumentException.class)
94+
public void testCreateRequestMissingIssuer() {
95+
new OidcProviderConfig.CreateRequest().setIssuer(null);
96+
}
97+
7898
@Test(expected = IllegalArgumentException.class)
7999
public void testCreateRequestInvalidIssuerUrl() {
80100
new OidcProviderConfig.CreateRequest().setIssuer("not a valid url");
@@ -119,11 +139,21 @@ public void testUpdateRequestInvalidProviderId() {
119139
new OidcProviderConfig.UpdateRequest("saml.provider-id");
120140
}
121141

142+
@Test(expected = IllegalArgumentException.class)
143+
public void testUpdateRequestMissingDisplayName() {
144+
new OidcProviderConfig.UpdateRequest("oidc.provider-id").setDisplayName(null);
145+
}
146+
122147
@Test(expected = IllegalArgumentException.class)
123148
public void testUpdateRequestMissingClientId() {
124149
new OidcProviderConfig.UpdateRequest("oidc.provider-id").setClientId(null);
125150
}
126151

152+
@Test(expected = IllegalArgumentException.class)
153+
public void testUpdateRequestMissingIssuer() {
154+
new OidcProviderConfig.UpdateRequest("oidc.provider-id").setIssuer(null);
155+
}
156+
127157
@Test(expected = IllegalArgumentException.class)
128158
public void testUpdateRequestInvalidIssuerUrl() {
129159
new OidcProviderConfig.UpdateRequest("oidc.provider-id").setIssuer("not a valid url");

0 commit comments

Comments
 (0)