Skip to content

Commit c1c697d

Browse files
committed
Add class for SAML provider config.
1 parent a4236a4 commit c1c697d

File tree

3 files changed

+390
-0
lines changed

3 files changed

+390
-0
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
import com.google.common.collect.ImmutableMap;
2424
import java.net.MalformedURLException;
2525
import java.net.URL;
26+
import java.util.ArrayList;
2627
import java.util.HashMap;
28+
import java.util.List;
2729
import java.util.Map;
2830

2931
/**
@@ -60,6 +62,24 @@ static void assertValidUrl(String url) throws IllegalArgumentException {
6062
}
6163
}
6264

65+
static List<Object> getNestedList(Map<String, Object> outerMap, String id) {
66+
List<Object> list = (List<Object>) outerMap.get(id);
67+
if (list == null) {
68+
list = new ArrayList<Object>();
69+
outerMap.put(id, list);
70+
}
71+
return list;
72+
}
73+
74+
static Map<String, Object> getNestedMap(Map<String, Object> outerMap, String id) {
75+
Map<String, Object> map = (Map<String, Object>) outerMap.get(id);
76+
if (map == null) {
77+
map = new HashMap<String, Object>();
78+
outerMap.put(id, map);
79+
}
80+
return map;
81+
}
82+
6383
/**
6484
* A base specification class for creating a new provider.
6585
*
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
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.ImmutableList;
24+
import com.google.common.collect.ImmutableMap;
25+
import com.google.firebase.auth.ProviderConfig.AbstractCreateRequest;
26+
import com.google.firebase.auth.ProviderConfig.AbstractUpdateRequest;
27+
import java.util.List;
28+
import java.util.Map;
29+
30+
/**
31+
* Contains metadata associated with a SAML Auth provider.
32+
*
33+
* <p>Instances of this class are immutable and thread safe.
34+
*/
35+
public final class SamlProviderConfig extends ProviderConfig {
36+
37+
@Key("idpConfig")
38+
private IdpConfig idpConfig;
39+
40+
@Key("spConfig")
41+
private SpConfig spConfig;
42+
43+
public String getIdpEntityId() {
44+
return idpConfig.getIdpEntityId();
45+
}
46+
47+
public String getSsoUrl() {
48+
return idpConfig.getSsoUrl();
49+
}
50+
51+
public List<String> getX509Certificates() {
52+
ImmutableList.Builder<String> certificates = ImmutableList.<String>builder();
53+
for (IdpCertificate idpCertificate : idpConfig.getIdpCertificates()) {
54+
certificates.add(idpCertificate.getX509Certificate());
55+
}
56+
return certificates.build();
57+
}
58+
59+
public String getRpEntityId() {
60+
return spConfig.getRpEntityId();
61+
}
62+
63+
public String getCallbackUrl() {
64+
return spConfig.getCallbackUrl();
65+
}
66+
67+
/**
68+
* A specification class for creating a new SAML Auth provider.
69+
*
70+
* <p>Set the initial attributes of the new provider by calling various setter methods available
71+
* in this class.
72+
*/
73+
public static final class CreateRequest extends AbstractCreateRequest<CreateRequest> {
74+
75+
/**
76+
* Creates a new {@link CreateRequest}, which can be used to create a new SAML Auth provider.
77+
*
78+
* <p>The returned object should be passed to
79+
* {@link AbstractFirebaseAuth#createSamlProviderConfig(CreateRequest)} to register the provider
80+
* information persistently.
81+
*/
82+
public CreateRequest() { }
83+
84+
/**
85+
* Sets the IDP entity ID for the new provider.
86+
*
87+
* @param idpEntityId A non-null, non-empty IDP entity ID string.
88+
* @throws IllegalArgumentException If the IDP entity ID is null or empty.
89+
*/
90+
public CreateRequest setIdpEntityId(String idpEntityId) {
91+
checkArgument(!Strings.isNullOrEmpty(idpEntityId),
92+
"IDP entity ID must not be null or empty.");
93+
getNestedMap(properties, "idpConfig").put("idpEntityId", idpEntityId);
94+
return this;
95+
}
96+
97+
/**
98+
* Sets the SSO URL for the new provider.
99+
*
100+
* @param ssoUrl A non-null, non-empty SSO URL string.
101+
* @throws IllegalArgumentException If the SSO URL is null or empty, or if the format is
102+
* invalid.
103+
*/
104+
public CreateRequest setSsoUrl(String ssoUrl) {
105+
checkArgument(!Strings.isNullOrEmpty(ssoUrl), "SSO URL must not be null or empty.");
106+
assertValidUrl(ssoUrl);
107+
getNestedMap(properties, "idpConfig").put("ssoUrl", ssoUrl);
108+
return this;
109+
}
110+
111+
/**
112+
* Sets the RP entity ID for the new provider.
113+
*
114+
* @param rpEntityId A non-null, non-empty RP entity ID string.
115+
* @throws IllegalArgumentException If the RP entity ID is null or empty.
116+
*/
117+
public CreateRequest setRpEntityId(String rpEntityId) {
118+
checkArgument(!Strings.isNullOrEmpty(rpEntityId), "RP entity ID must not be null or empty.");
119+
getNestedMap(properties, "spConfig").put("spEntityId", rpEntityId);
120+
return this;
121+
}
122+
123+
/**
124+
* Adds a x509 certificate to the new provider.
125+
*
126+
* @param x509Certificate A non-null, non-empty x509 certificate string.
127+
* @throws IllegalArgumentException If the x509 certificate is null or empty.
128+
*/
129+
public CreateRequest addX509Certificate(String x509Certificate) {
130+
checkArgument(!Strings.isNullOrEmpty(x509Certificate),
131+
"The x509 certificate must not be null or empty.");
132+
Map<String, Object> idpConfigProperties = getNestedMap(properties, "idpConfig");
133+
List<Object> x509Certificates = getNestedList(idpConfigProperties, "idpCertificates");
134+
x509Certificates.add(ImmutableMap.<String, Object>of("x509Certificate", x509Certificate));
135+
return this;
136+
}
137+
138+
// TODO(micahstairs): Add 'addAllX509Certificates' method.
139+
140+
/**
141+
* Sets the callback URL for the new provider.
142+
*
143+
* @param callbackUrl A non-null, non-empty callback URL string.
144+
* @throws IllegalArgumentException If the callback URL is null or empty, or if the format is
145+
* invalid.
146+
*/
147+
public CreateRequest setCallbackUrl(String callbackUrl) {
148+
checkArgument(!Strings.isNullOrEmpty(callbackUrl), "Callback URL must not be null or empty.");
149+
assertValidUrl(callbackUrl);
150+
getNestedMap(properties, "spConfig").put("callbackUri", callbackUrl);
151+
return this;
152+
}
153+
154+
// TODO(micahstairs): Add 'setRequestSigningEnabled' method.
155+
156+
CreateRequest getThis() {
157+
return this;
158+
}
159+
160+
void assertValidProviderIdFormat(String providerId) {
161+
checkArgument(providerId.startsWith("saml."), "Invalid SAML provider ID: " + providerId);
162+
}
163+
}
164+
165+
public static class IdpCertificate {
166+
@Key("x509Certificate")
167+
private String x509Certificate;
168+
169+
public String getX509Certificate() {
170+
return x509Certificate;
171+
}
172+
}
173+
174+
public static class IdpConfig {
175+
@Key("idpEntityId")
176+
private String idpEntityId;
177+
178+
@Key("ssoUrl")
179+
private String ssoUrl;
180+
181+
@Key("idpCertificates")
182+
private List<IdpCertificate> idpCertificates;
183+
184+
public String getIdpEntityId() {
185+
return idpEntityId;
186+
}
187+
188+
public String getSsoUrl() {
189+
return ssoUrl;
190+
}
191+
192+
public List<IdpCertificate> getIdpCertificates() {
193+
return idpCertificates;
194+
}
195+
}
196+
197+
public static class SpConfig {
198+
@Key("spEntityId")
199+
private String rpEntityId;
200+
201+
@Key("callbackUri")
202+
private String callbackUrl;
203+
204+
public String getRpEntityId() {
205+
return rpEntityId;
206+
}
207+
208+
public String getCallbackUrl() {
209+
return callbackUrl;
210+
}
211+
}
212+
}

0 commit comments

Comments
 (0)