Skip to content

Commit a2fa782

Browse files
committed
Fix CreateRequest chaining and provider ID extraction. (#399)
This makes the base `CreateRequest` setters return the proper instance type, so that methods can be chained. This also makes it so that the provider ID can be parsed from the resource name. A package private `getProviderId()` method was also added, which will be needed by the `FirebaseUserManager` class when the provider config operations are added there. Also renamed `AuthProviderConfig` to `ProviderConfig` since "Auth" is redundant with the package name. This work is part of adding multi-tenancy support (see issue #332).
1 parent 392bf67 commit a2fa782

File tree

4 files changed

+78
-21
lines changed

4 files changed

+78
-21
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.google.api.client.util.Key;
2222
import com.google.common.base.Strings;
2323
import com.google.common.collect.ImmutableMap;
24+
import com.google.firebase.auth.ProviderConfig.AbstractCreateRequest;
2425
import java.net.MalformedURLException;
2526
import java.net.URL;
2627
import java.util.HashMap;
@@ -31,7 +32,7 @@
3132
*
3233
* <p>Instances of this class are immutable and thread safe.
3334
*/
34-
public final class OidcProviderConfig extends AuthProviderConfig {
35+
public final class OidcProviderConfig extends ProviderConfig {
3536

3637
@Key("clientId")
3738
private String clientId;
@@ -53,13 +54,13 @@ public String getIssuer() {
5354
* <p>Set the initial attributes of the new provider by calling various setter methods available
5455
* in this class.
5556
*/
56-
public static final class CreateRequest extends AuthProviderConfig.CreateRequest {
57+
public static final class CreateRequest extends AbstractCreateRequest<CreateRequest> {
5758

5859
/**
5960
* Creates a new {@link CreateRequest}, which can be used to create a new OIDC Auth provider.
6061
*
6162
* <p>The returned object should be passed to
62-
* {@link TenantAwareFirebaseAuth#createProviderConfig(CreateRequest)} to register the provider
63+
* {@link AbstractFirebaseAuth#createOidcProviderConfig(CreateRequest)} to register the provider
6364
* information persistently.
6465
*/
6566
public CreateRequest() { }
@@ -90,5 +91,9 @@ public CreateRequest setIssuer(String issuer) {
9091
properties.put("issuer", issuer);
9192
return this;
9293
}
94+
95+
CreateRequest getThis() {
96+
return this;
97+
}
9398
}
9499
}

src/main/java/com/google/firebase/auth/AuthProviderConfig.java renamed to src/main/java/com/google/firebase/auth/ProviderConfig.java

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
/**
2828
* The base class for Auth providers.
2929
*/
30-
public abstract class AuthProviderConfig {
30+
public abstract class ProviderConfig {
3131

3232
@Key("name")
33-
private String providerId;
33+
private String resourceName;
3434

3535
@Key("displayName")
3636
private String displayName;
@@ -39,7 +39,7 @@ public abstract class AuthProviderConfig {
3939
private boolean enabled;
4040

4141
public String getProviderId() {
42-
return providerId;
42+
return resourceName.substring(resourceName.lastIndexOf("/") + 1);
4343
}
4444

4545
public String getDisplayName() {
@@ -56,45 +56,52 @@ public boolean isEnabled() {
5656
* <p>Set the initial attributes of the new provider by calling various setter methods available
5757
* in this class.
5858
*/
59-
public abstract static class CreateRequest {
59+
public abstract static class AbstractCreateRequest<T extends AbstractCreateRequest<T>> {
6060

6161
final Map<String,Object> properties = new HashMap<>();
62+
String providerId;
6263

6364
/**
6465
* Sets the ID for the new provider.
6566
*
6667
* @param providerId a non-null, non-empty provider ID string.
6768
*/
68-
public CreateRequest setProviderId(String providerId) {
69+
public T setProviderId(String providerId) {
6970
checkArgument(
7071
!Strings.isNullOrEmpty(providerId), "provider ID name must not be null or empty");
71-
properties.put("name", providerId);
72-
return this;
72+
this.providerId = providerId;
73+
return getThis();
74+
}
75+
76+
String getProviderId() {
77+
return providerId;
7378
}
7479

7580
/**
7681
* Sets the display name for the new provider.
7782
*
7883
* @param displayName a non-null, non-empty display name string.
7984
*/
80-
public CreateRequest setDisplayName(String displayName) {
85+
public T setDisplayName(String displayName) {
8186
checkArgument(!Strings.isNullOrEmpty(displayName), "display name must not be null or empty");
8287
properties.put("displayName", displayName);
83-
return this;
88+
return getThis();
8489
}
8590

8691
/**
8792
* Sets whether to allow the user to sign in with the provider.
8893
*
8994
* @param enabled a boolean indicating whether the user can sign in with the provider
9095
*/
91-
public CreateRequest setEnabled(boolean enabled) {
96+
public T setEnabled(boolean enabled) {
9297
properties.put("enabled", enabled);
93-
return this;
98+
return getThis();
9499
}
95100

96101
Map<String, Object> getProperties() {
97102
return ImmutableMap.copyOf(properties);
98103
}
104+
105+
abstract T getThis();
99106
}
100107
}

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@
3131
*/
3232
public final class Tenant {
3333

34-
// Lazily initialized from 'resourceName'.
35-
private String tenantId;
36-
3734
@Key("name")
3835
private String resourceName;
3936

@@ -47,10 +44,7 @@ public final class Tenant {
4744
private boolean emailLinkSignInEnabled;
4845

4946
public String getTenantId() {
50-
if (tenantId == null) {
51-
tenantId = resourceName.substring(resourceName.lastIndexOf("/") + 1);
52-
}
53-
return tenantId;
47+
return resourceName.substring(resourceName.lastIndexOf("/") + 1);
5448
}
5549

5650
public String getDisplayName() {

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,60 @@
1616

1717
package com.google.firebase.auth;
1818

19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertFalse;
21+
import static org.junit.Assert.assertNull;
22+
import static org.junit.Assert.assertTrue;
23+
24+
import com.google.api.client.googleapis.util.Utils;
25+
import com.google.api.client.json.JsonFactory;
26+
import java.io.IOException;
27+
import java.util.Map;
1928
import org.junit.Test;
2029

2130
public class OidcProviderConfigTest {
31+
32+
private static final JsonFactory jsonFactory = Utils.getDefaultJsonFactory();
33+
34+
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+
+ "}";
42+
43+
@Test
44+
public void testJsonSerialization() throws IOException {
45+
OidcProviderConfig config = jsonFactory.fromString(OIDC_JSON_STRING, OidcProviderConfig.class);
46+
47+
assertEquals(config.getProviderId(), "oidc.provider-id");
48+
assertEquals(config.getDisplayName(), "DISPLAY_NAME");
49+
assertTrue(config.isEnabled());
50+
assertEquals(config.getClientId(), "CLIENT_ID");
51+
assertEquals(config.getIssuer(), "https://oidc.com/issuer");
52+
}
53+
54+
@Test
55+
public void testCreateRequest() throws IOException {
56+
OidcProviderConfig.CreateRequest createRequest = new OidcProviderConfig.CreateRequest();
57+
createRequest
58+
.setProviderId("oidc.provider-id")
59+
.setDisplayName("DISPLAY_NAME")
60+
.setEnabled(false)
61+
.setClientId("CLIENT_ID")
62+
.setIssuer("https://oidc.com/issuer");
63+
64+
assertEquals("oidc.provider-id", createRequest.getProviderId());
65+
Map<String,Object> properties = createRequest.getProperties();
66+
assertEquals(properties.size(), 4);
67+
assertEquals("DISPLAY_NAME", (String) properties.get("displayName"));
68+
assertFalse((boolean) properties.get("enabled"));
69+
assertEquals("CLIENT_ID", (String) properties.get("clientId"));
70+
assertEquals("https://oidc.com/issuer", (String) properties.get("issuer"));
71+
}
72+
2273
@Test(expected = IllegalArgumentException.class)
2374
public void testInvalidIssuerUrl() {
2475
new OidcProviderConfig.CreateRequest().setIssuer("not a valid url");

0 commit comments

Comments
 (0)