Skip to content

Commit 392bf67

Browse files
committed
Add OIDC Auth provider config class (#397)
Adds OIDC provider config class and base class. This is part of adding multi-tenancy support (see issue #332).
1 parent 6d1b15b commit 392bf67

File tree

3 files changed

+220
-0
lines changed

3 files changed

+220
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.firebase.auth;
18+
19+
import static com.google.common.base.Preconditions.checkArgument;
20+
21+
import com.google.api.client.util.Key;
22+
import com.google.common.base.Strings;
23+
import com.google.common.collect.ImmutableMap;
24+
import java.util.HashMap;
25+
import java.util.Map;
26+
27+
/**
28+
* The base class for Auth providers.
29+
*/
30+
public abstract class AuthProviderConfig {
31+
32+
@Key("name")
33+
private String providerId;
34+
35+
@Key("displayName")
36+
private String displayName;
37+
38+
@Key("enabled")
39+
private boolean enabled;
40+
41+
public String getProviderId() {
42+
return providerId;
43+
}
44+
45+
public String getDisplayName() {
46+
return displayName;
47+
}
48+
49+
public boolean isEnabled() {
50+
return enabled;
51+
}
52+
53+
/**
54+
* A base specification class for creating a new provider.
55+
*
56+
* <p>Set the initial attributes of the new provider by calling various setter methods available
57+
* in this class.
58+
*/
59+
public abstract static class CreateRequest {
60+
61+
final Map<String,Object> properties = new HashMap<>();
62+
63+
/**
64+
* Sets the ID for the new provider.
65+
*
66+
* @param providerId a non-null, non-empty provider ID string.
67+
*/
68+
public CreateRequest setProviderId(String providerId) {
69+
checkArgument(
70+
!Strings.isNullOrEmpty(providerId), "provider ID name must not be null or empty");
71+
properties.put("name", providerId);
72+
return this;
73+
}
74+
75+
/**
76+
* Sets the display name for the new provider.
77+
*
78+
* @param displayName a non-null, non-empty display name string.
79+
*/
80+
public CreateRequest setDisplayName(String displayName) {
81+
checkArgument(!Strings.isNullOrEmpty(displayName), "display name must not be null or empty");
82+
properties.put("displayName", displayName);
83+
return this;
84+
}
85+
86+
/**
87+
* Sets whether to allow the user to sign in with the provider.
88+
*
89+
* @param enabled a boolean indicating whether the user can sign in with the provider
90+
*/
91+
public CreateRequest setEnabled(boolean enabled) {
92+
properties.put("enabled", enabled);
93+
return this;
94+
}
95+
96+
Map<String, Object> getProperties() {
97+
return ImmutableMap.copyOf(properties);
98+
}
99+
}
100+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.firebase.auth;
18+
19+
import static com.google.common.base.Preconditions.checkArgument;
20+
21+
import com.google.api.client.util.Key;
22+
import com.google.common.base.Strings;
23+
import com.google.common.collect.ImmutableMap;
24+
import java.net.MalformedURLException;
25+
import java.net.URL;
26+
import java.util.HashMap;
27+
import java.util.Map;
28+
29+
/**
30+
* Contains metadata associated with an OIDC Auth provider.
31+
*
32+
* <p>Instances of this class are immutable and thread safe.
33+
*/
34+
public final class OidcProviderConfig extends AuthProviderConfig {
35+
36+
@Key("clientId")
37+
private String clientId;
38+
39+
@Key("issuer")
40+
private String issuer;
41+
42+
public String getClientId() {
43+
return clientId;
44+
}
45+
46+
public String getIssuer() {
47+
return issuer;
48+
}
49+
50+
/**
51+
* A specification class for creating a new OIDC Auth provider.
52+
*
53+
* <p>Set the initial attributes of the new provider by calling various setter methods available
54+
* in this class.
55+
*/
56+
public static final class CreateRequest extends AuthProviderConfig.CreateRequest {
57+
58+
/**
59+
* Creates a new {@link CreateRequest}, which can be used to create a new OIDC Auth provider.
60+
*
61+
* <p>The returned object should be passed to
62+
* {@link TenantAwareFirebaseAuth#createProviderConfig(CreateRequest)} to register the provider
63+
* information persistently.
64+
*/
65+
public CreateRequest() { }
66+
67+
/**
68+
* Sets the client ID for the new provider.
69+
*
70+
* @param clientId a non-null, non-empty client ID string.
71+
*/
72+
public CreateRequest setClientId(String clientId) {
73+
checkArgument(!Strings.isNullOrEmpty(clientId), "client ID must not be null or empty");
74+
properties.put("clientId", clientId);
75+
return this;
76+
}
77+
78+
/**
79+
* Sets the issuer for the new provider.
80+
*
81+
* @param issuer a non-null, non-empty issuer string.
82+
*/
83+
public CreateRequest setIssuer(String issuer) {
84+
checkArgument(!Strings.isNullOrEmpty(issuer), "issuer must not be null or empty");
85+
try {
86+
new URL(issuer);
87+
} catch (MalformedURLException e) {
88+
throw new IllegalArgumentException(issuer + " is a malformed URL", e);
89+
}
90+
properties.put("issuer", issuer);
91+
return this;
92+
}
93+
}
94+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.firebase.auth;
18+
19+
import org.junit.Test;
20+
21+
public class OidcProviderConfigTest {
22+
@Test(expected = IllegalArgumentException.class)
23+
public void testInvalidIssuerUrl() {
24+
new OidcProviderConfig.CreateRequest().setIssuer("not a valid url");
25+
}
26+
}

0 commit comments

Comments
 (0)