Skip to content

Fix CreateRequest chaining and provider ID extraction. #399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/main/java/com/google/firebase/auth/OidcProviderConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.api.client.util.Key;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
import com.google.firebase.auth.ProviderConfig.AbstractCreateRequest;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
Expand All @@ -31,7 +32,7 @@
*
* <p>Instances of this class are immutable and thread safe.
*/
public final class OidcProviderConfig extends AuthProviderConfig {
public final class OidcProviderConfig extends ProviderConfig {

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

/**
* Creates a new {@link CreateRequest}, which can be used to create a new OIDC Auth provider.
*
* <p>The returned object should be passed to
* {@link TenantAwareFirebaseAuth#createProviderConfig(CreateRequest)} to register the provider
* {@link AbstractFirebaseAuth#createOidcProviderConfig(CreateRequest)} to register the provider
* information persistently.
*/
public CreateRequest() { }
Expand Down Expand Up @@ -90,5 +91,9 @@ public CreateRequest setIssuer(String issuer) {
properties.put("issuer", issuer);
return this;
}

CreateRequest getThis() {
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
/**
* The base class for Auth providers.
*/
public abstract class AuthProviderConfig {
public abstract class ProviderConfig {

@Key("name")
private String providerId;
private String resourceName;

@Key("displayName")
private String displayName;
Expand All @@ -39,7 +39,7 @@ public abstract class AuthProviderConfig {
private boolean enabled;

public String getProviderId() {
return providerId;
return resourceName.substring(resourceName.lastIndexOf("/") + 1);
}

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

final Map<String,Object> properties = new HashMap<>();
String providerId;

/**
* Sets the ID for the new provider.
*
* @param providerId a non-null, non-empty provider ID string.
*/
public CreateRequest setProviderId(String providerId) {
public T setProviderId(String providerId) {
checkArgument(
!Strings.isNullOrEmpty(providerId), "provider ID name must not be null or empty");
properties.put("name", providerId);
return this;
this.providerId = providerId;
return getThis();
}

String getProviderId() {
return providerId;
}

/**
* Sets the display name for the new provider.
*
* @param displayName a non-null, non-empty display name string.
*/
public CreateRequest setDisplayName(String displayName) {
public T setDisplayName(String displayName) {
checkArgument(!Strings.isNullOrEmpty(displayName), "display name must not be null or empty");
properties.put("displayName", displayName);
return this;
return getThis();
}

/**
* Sets whether to allow the user to sign in with the provider.
*
* @param enabled a boolean indicating whether the user can sign in with the provider
*/
public CreateRequest setEnabled(boolean enabled) {
public T setEnabled(boolean enabled) {
properties.put("enabled", enabled);
return this;
return getThis();
}

Map<String, Object> getProperties() {
return ImmutableMap.copyOf(properties);
}

abstract T getThis();
}
}
8 changes: 1 addition & 7 deletions src/main/java/com/google/firebase/auth/Tenant.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@
*/
public final class Tenant {

// Lazily initialized from 'resourceName'.
private String tenantId;

@Key("name")
private String resourceName;

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

public String getTenantId() {
if (tenantId == null) {
tenantId = resourceName.substring(resourceName.lastIndexOf("/") + 1);
}
return tenantId;
return resourceName.substring(resourceName.lastIndexOf("/") + 1);
}

public String getDisplayName() {
Expand Down
51 changes: 51 additions & 0 deletions src/test/java/com/google/firebase/auth/OidcProviderConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,60 @@

package com.google.firebase.auth;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import com.google.api.client.googleapis.util.Utils;
import com.google.api.client.json.JsonFactory;
import java.io.IOException;
import java.util.Map;
import org.junit.Test;

public class OidcProviderConfigTest {

private static final JsonFactory jsonFactory = Utils.getDefaultJsonFactory();

private static final String OIDC_JSON_STRING =
"{"
+ "\"name\":\"projects/projectId/oauthIdpConfigs/oidc.provider-id\","
+ "\"displayName\":\"DISPLAY_NAME\","
+ "\"enabled\":true,"
+ "\"clientId\":\"CLIENT_ID\","
+ "\"issuer\":\"https://oidc.com/issuer\""
+ "}";

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

assertEquals(config.getProviderId(), "oidc.provider-id");
assertEquals(config.getDisplayName(), "DISPLAY_NAME");
assertTrue(config.isEnabled());
assertEquals(config.getClientId(), "CLIENT_ID");
assertEquals(config.getIssuer(), "https://oidc.com/issuer");
}

@Test
public void testCreateRequest() throws IOException {
OidcProviderConfig.CreateRequest createRequest = new OidcProviderConfig.CreateRequest();
createRequest
.setProviderId("oidc.provider-id")
.setDisplayName("DISPLAY_NAME")
.setEnabled(false)
.setClientId("CLIENT_ID")
.setIssuer("https://oidc.com/issuer");

assertEquals("oidc.provider-id", createRequest.getProviderId());
Map<String,Object> properties = createRequest.getProperties();
assertEquals(properties.size(), 4);
assertEquals("DISPLAY_NAME", (String) properties.get("displayName"));
assertFalse((boolean) properties.get("enabled"));
assertEquals("CLIENT_ID", (String) properties.get("clientId"));
assertEquals("https://oidc.com/issuer", (String) properties.get("issuer"));
}

@Test(expected = IllegalArgumentException.class)
public void testInvalidIssuerUrl() {
new OidcProviderConfig.CreateRequest().setIssuer("not a valid url");
Expand Down