Skip to content

Commit 0751674

Browse files
committed
Add operations to create and delete SAML provider configs. (#420)
This adds operations to create and delete SAML provider configs.
1 parent 537b7a7 commit 0751674

File tree

10 files changed

+567
-46
lines changed

10 files changed

+567
-46
lines changed

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

Lines changed: 104 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,13 +1076,15 @@ protected String execute() throws FirebaseAuthException {
10761076
}
10771077

10781078
/**
1079-
* Creates a new provider OIDC Auth config with the attributes contained in the specified {@link
1080-
* OidcProviderConfig.CreateRequest}.
1079+
* Creates a new OIDC Auth provider config with the attributes contained in the specified
1080+
* {@link OidcProviderConfig.CreateRequest}.
10811081
*
10821082
* @param request A non-null {@link OidcProviderConfig.CreateRequest} instance.
10831083
* @return An {@link OidcProviderConfig} instance corresponding to the newly created provider
10841084
* config.
10851085
* @throws NullPointerException if the provided request is null.
1086+
* @throws IllegalArgumentException If the provider ID string is null or empty, or is not
1087+
* prefixed with 'oidc.'.
10861088
* @throws FirebaseAuthException if an error occurs while creating the provider config.
10871089
*/
10881090
public OidcProviderConfig createOidcProviderConfig(
@@ -1098,6 +1100,8 @@ public OidcProviderConfig createOidcProviderConfig(
10981100
* instance corresponding to the newly created provider config. If an error occurs while
10991101
* creating the provider config, the future throws a {@link FirebaseAuthException}.
11001102
* @throws NullPointerException if the provided request is null.
1103+
* @throws IllegalArgumentException If the provider ID string is null or empty, or is not
1104+
* prefixed with 'oidc.'.
11011105
*/
11021106
public ApiFuture<OidcProviderConfig> createOidcProviderConfigAsync(
11031107
@NonNull OidcProviderConfig.CreateRequest request) {
@@ -1108,6 +1112,7 @@ public ApiFuture<OidcProviderConfig> createOidcProviderConfigAsync(
11081112
createOidcProviderConfigOp(final OidcProviderConfig.CreateRequest request) {
11091113
checkNotDestroyed();
11101114
checkNotNull(request, "Create request must not be null.");
1115+
OidcProviderConfig.checkOidcProviderId(request.getProviderId());
11111116
final FirebaseUserManager userManager = getUserManager();
11121117
return new CallableOperation<OidcProviderConfig, FirebaseAuthException>() {
11131118
@Override
@@ -1162,7 +1167,8 @@ protected OidcProviderConfig execute() throws FirebaseAuthException {
11621167
*
11631168
* @param providerId A provider ID string.
11641169
* @return An {@link OidcProviderConfig} instance.
1165-
* @throws IllegalArgumentException If the provider ID string is null or empty.
1170+
* @throws IllegalArgumentException If the provider ID string is null or empty, or is not prefixed
1171+
* with 'oidc'.
11661172
* @throws FirebaseAuthException If an error occurs while retrieving the provider config.
11671173
*/
11681174
public OidcProviderConfig getOidcProviderConfig(@NonNull String providerId)
@@ -1179,7 +1185,8 @@ public OidcProviderConfig getOidcProviderConfig(@NonNull String providerId)
11791185
* {@link OidcProviderConfig} instance. If an error occurs while retrieving the provider
11801186
* config or if the specified provider ID does not exist, the future throws a
11811187
* {@link FirebaseAuthException}.
1182-
* @throws IllegalArgumentException If the provider ID string is null or empty.
1188+
* @throws IllegalArgumentException If the provider ID string is null or empty, or is not
1189+
* prefixed with 'oidc.'.
11831190
*/
11841191
public ApiFuture<OidcProviderConfig> getOidcProviderConfigAsync(@NonNull String providerId) {
11851192
return getOidcProviderConfigOp(providerId).callAsync(firebaseApp);
@@ -1188,7 +1195,7 @@ public ApiFuture<OidcProviderConfig> getOidcProviderConfigAsync(@NonNull String
11881195
private CallableOperation<OidcProviderConfig, FirebaseAuthException>
11891196
getOidcProviderConfigOp(final String providerId) {
11901197
checkNotDestroyed();
1191-
checkArgument(!Strings.isNullOrEmpty(providerId), "Provider ID must not be null or empty.");
1198+
OidcProviderConfig.checkOidcProviderId(providerId);
11921199
final FirebaseUserManager userManager = getUserManager();
11931200
return new CallableOperation<OidcProviderConfig, FirebaseAuthException>() {
11941201
@Override
@@ -1289,7 +1296,8 @@ protected ListProviderConfigsPage<OidcProviderConfig> execute()
12891296
* Deletes the OIDC Auth provider config identified by the specified provider ID.
12901297
*
12911298
* @param providerId A provider ID string.
1292-
* @throws IllegalArgumentException If the provider ID string is null or empty.
1299+
* @throws IllegalArgumentException If the provider ID string is null or empty, or is not prefixed
1300+
* with 'oidc'.
12931301
* @throws FirebaseAuthException If an error occurs while deleting the provider config.
12941302
*/
12951303
public void deleteOidcProviderConfig(@NonNull String providerId) throws FirebaseAuthException {
@@ -1303,7 +1311,8 @@ public void deleteOidcProviderConfig(@NonNull String providerId) throws Firebase
13031311
* @return An {@code ApiFuture} which will complete successfully when the specified provider
13041312
* config has been deleted. If an error occurs while deleting the provider config, the future
13051313
* throws a {@link FirebaseAuthException}.
1306-
* @throws IllegalArgumentException If the provider ID string is null or empty.
1314+
* @throws IllegalArgumentException If the provider ID string is null or empty, or is not prefixed
1315+
* with "oidc.".
13071316
*/
13081317
public ApiFuture<Void> deleteOidcProviderConfigAsync(String providerId) {
13091318
return deleteOidcProviderConfigOp(providerId).callAsync(firebaseApp);
@@ -1312,7 +1321,7 @@ public ApiFuture<Void> deleteOidcProviderConfigAsync(String providerId) {
13121321
private CallableOperation<Void, FirebaseAuthException> deleteOidcProviderConfigOp(
13131322
final String providerId) {
13141323
checkNotDestroyed();
1315-
checkArgument(!Strings.isNullOrEmpty(providerId), "Provider ID must not be null or empty.");
1324+
OidcProviderConfig.checkOidcProviderId(providerId);
13161325
final FirebaseUserManager userManager = getUserManager();
13171326
return new CallableOperation<Void, FirebaseAuthException>() {
13181327
@Override
@@ -1323,6 +1332,93 @@ protected Void execute() throws FirebaseAuthException {
13231332
};
13241333
}
13251334

1335+
/**
1336+
* Creates a new SAML Auth provider config with the attributes contained in the specified
1337+
* {@link SamlProviderConfig.CreateRequest}.
1338+
*
1339+
* @param request A non-null {@link SamlProviderConfig.CreateRequest} instance.
1340+
* @return An {@link SamlProviderConfig} instance corresponding to the newly created provider
1341+
* config.
1342+
* @throws NullPointerException if the provided request is null.
1343+
* @throws IllegalArgumentException If the provider ID string is null or empty, or is not prefixed
1344+
* with 'saml'.
1345+
* @throws FirebaseAuthException if an error occurs while creating the provider config.
1346+
*/
1347+
public SamlProviderConfig createSamlProviderConfig(
1348+
@NonNull SamlProviderConfig.CreateRequest request) throws FirebaseAuthException {
1349+
return createSamlProviderConfigOp(request).call();
1350+
}
1351+
1352+
/**
1353+
* Similar to {@link #createSamlProviderConfig} but performs the operation asynchronously.
1354+
*
1355+
* @param request A non-null {@link SamlProviderConfig.CreateRequest} instance.
1356+
* @return An {@code ApiFuture} which will complete successfully with a {@link SamlProviderConfig}
1357+
* instance corresponding to the newly created provider config. If an error occurs while
1358+
* creating the provider config, the future throws a {@link FirebaseAuthException}.
1359+
* @throws NullPointerException if the provided request is null.
1360+
* @throws IllegalArgumentException If the provider ID string is null or empty, or is not prefixed
1361+
* with 'saml'.
1362+
*/
1363+
public ApiFuture<SamlProviderConfig> createSamlProviderConfigAsync(
1364+
@NonNull SamlProviderConfig.CreateRequest request) {
1365+
return createSamlProviderConfigOp(request).callAsync(firebaseApp);
1366+
}
1367+
1368+
private CallableOperation<SamlProviderConfig, FirebaseAuthException>
1369+
createSamlProviderConfigOp(final SamlProviderConfig.CreateRequest request) {
1370+
checkNotDestroyed();
1371+
checkNotNull(request, "Create request must not be null.");
1372+
SamlProviderConfig.checkSamlProviderId(request.getProviderId());
1373+
final FirebaseUserManager userManager = getUserManager();
1374+
return new CallableOperation<SamlProviderConfig, FirebaseAuthException>() {
1375+
@Override
1376+
protected SamlProviderConfig execute() throws FirebaseAuthException {
1377+
return userManager.createSamlProviderConfig(request);
1378+
}
1379+
};
1380+
}
1381+
1382+
/**
1383+
* Deletes the SAML Auth provider config identified by the specified provider ID.
1384+
*
1385+
* @param providerId A provider ID string.
1386+
* @throws IllegalArgumentException If the provider ID string is null or empty, or is not prefixed
1387+
* with "saml.".
1388+
* @throws FirebaseAuthException If an error occurs while deleting the provider config.
1389+
*/
1390+
public void deleteSamlProviderConfig(@NonNull String providerId) throws FirebaseAuthException {
1391+
deleteSamlProviderConfigOp(providerId).call();
1392+
}
1393+
1394+
/**
1395+
* Similar to {@link #deleteSamlProviderConfig} but performs the operation asynchronously.
1396+
*
1397+
* @param providerId A provider ID string.
1398+
* @return An {@code ApiFuture} which will complete successfully when the specified provider
1399+
* config has been deleted. If an error occurs while deleting the provider config, the future
1400+
* throws a {@link FirebaseAuthException}.
1401+
* @throws IllegalArgumentException If the provider ID string is null or empty, or is not prefixed
1402+
* with "saml.".
1403+
*/
1404+
public ApiFuture<Void> deleteSamlProviderConfigAsync(String providerId) {
1405+
return deleteSamlProviderConfigOp(providerId).callAsync(firebaseApp);
1406+
}
1407+
1408+
private CallableOperation<Void, FirebaseAuthException> deleteSamlProviderConfigOp(
1409+
final String providerId) {
1410+
checkNotDestroyed();
1411+
SamlProviderConfig.checkSamlProviderId(providerId);
1412+
final FirebaseUserManager userManager = getUserManager();
1413+
return new CallableOperation<Void, FirebaseAuthException>() {
1414+
@Override
1415+
protected Void execute() throws FirebaseAuthException {
1416+
userManager.deleteSamlProviderConfig(providerId);
1417+
return null;
1418+
}
1419+
};
1420+
}
1421+
13261422
FirebaseApp getFirebaseApp() {
13271423
return this.firebaseApp;
13281424
}

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,8 @@ Tenant createTenant(Tenant.CreateRequest request) throws FirebaseAuthException {
302302

303303
Tenant updateTenant(Tenant.UpdateRequest request) throws FirebaseAuthException {
304304
Map<String, Object> properties = request.getProperties();
305+
// TODO(micahstairs): Move this check so that argument validation happens outside the
306+
// CallableOperation.
305307
checkArgument(!properties.isEmpty(), "Tenant update must have at least one property set");
306308
GenericUrl url = new GenericUrl(tenantMgtBaseUrl + getTenantUrlSuffix(request.getTenantId()));
307309
url.put("updateMask", generateMask(properties));
@@ -368,15 +370,22 @@ String getEmailActionLink(EmailLinkType type, String email,
368370
OidcProviderConfig createOidcProviderConfig(
369371
OidcProviderConfig.CreateRequest request) throws FirebaseAuthException {
370372
GenericUrl url = new GenericUrl(idpConfigMgtBaseUrl + "/oauthIdpConfigs");
371-
String providerId = request.getProviderId();
372-
checkArgument(!Strings.isNullOrEmpty(providerId), "Provider ID must not be null or empty.");
373-
url.set("oauthIdpConfigId", providerId);
373+
url.set("oauthIdpConfigId", request.getProviderId());
374374
return sendRequest("POST", url, request.getProperties(), OidcProviderConfig.class);
375375
}
376376

377+
SamlProviderConfig createSamlProviderConfig(
378+
SamlProviderConfig.CreateRequest request) throws FirebaseAuthException {
379+
GenericUrl url = new GenericUrl(idpConfigMgtBaseUrl + "/inboundSamlConfigs");
380+
url.set("inboundSamlConfigId", request.getProviderId());
381+
return sendRequest("POST", url, request.getProperties(), SamlProviderConfig.class);
382+
}
383+
377384
OidcProviderConfig updateOidcProviderConfig(OidcProviderConfig.UpdateRequest request)
378385
throws FirebaseAuthException {
379386
Map<String, Object> properties = request.getProperties();
387+
// TODO(micahstairs): Move this check so that argument validation happens outside the
388+
// CallableOperation.
380389
checkArgument(!properties.isEmpty(),
381390
"Provider config update must have at least one property set.");
382391
GenericUrl url =
@@ -415,6 +424,11 @@ void deleteOidcProviderConfig(String providerId) throws FirebaseAuthException {
415424
sendRequest("DELETE", url, null, GenericJson.class);
416425
}
417426

427+
void deleteSamlProviderConfig(String providerId) throws FirebaseAuthException {
428+
GenericUrl url = new GenericUrl(idpConfigMgtBaseUrl + getSamlUrlSuffix(providerId));
429+
sendRequest("DELETE", url, null, GenericJson.class);
430+
}
431+
418432
private static String generateMask(Map<String, Object> properties) {
419433
// This implementation does not currently handle the case of nested properties. This is fine
420434
// since we do not currently generate masks for any properties with nested values. When it
@@ -433,6 +447,11 @@ private static String getOidcUrlSuffix(String providerId) {
433447
return "/oauthIdpConfigs/" + providerId;
434448
}
435449

450+
private static String getSamlUrlSuffix(String providerId) {
451+
checkArgument(!Strings.isNullOrEmpty(providerId), "Provider ID must not be null or empty.");
452+
return "/inboundSamlConfigs/" + providerId;
453+
}
454+
436455
private <T> T post(String path, Object content, Class<T> clazz) throws FirebaseAuthException {
437456
checkArgument(!Strings.isNullOrEmpty(path), "path must not be null or empty");
438457
checkNotNull(content, "content must not be null for POST requests");

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ public UpdateRequest updateRequest() {
5454
return new UpdateRequest(getProviderId());
5555
}
5656

57+
static void checkOidcProviderId(String providerId) {
58+
checkArgument(!Strings.isNullOrEmpty(providerId), "Provider ID must not be null or empty.");
59+
checkArgument(providerId.startsWith("oidc."),
60+
"Invalid OIDC provider ID (must be prefixed with 'oidc.'): " + providerId);
61+
}
62+
5763
/**
5864
* A specification class for creating a new OIDC Auth provider.
5965
*
@@ -71,6 +77,19 @@ public static final class CreateRequest extends AbstractCreateRequest<CreateRequ
7177
*/
7278
public CreateRequest() { }
7379

80+
/**
81+
* Sets the ID for the new provider.
82+
*
83+
* @param providerId A non-null, non-empty provider ID string.
84+
* @throws IllegalArgumentException If the provider ID is null or empty, or is not prefixed with
85+
* 'oidc.'.
86+
*/
87+
@Override
88+
public CreateRequest setProviderId(String providerId) {
89+
checkOidcProviderId(providerId);
90+
return super.setProviderId(providerId);
91+
}
92+
7493
/**
7594
* Sets the client ID for the new provider.
7695
*
@@ -100,10 +119,6 @@ public CreateRequest setIssuer(String issuer) {
100119
CreateRequest getThis() {
101120
return this;
102121
}
103-
104-
void assertValidProviderIdFormat(String providerId) {
105-
checkArgument(providerId.startsWith("oidc."), "Invalid OIDC provider ID: " + providerId);
106-
}
107122
}
108123

109124
/**
@@ -129,7 +144,7 @@ public static final class UpdateRequest extends AbstractUpdateRequest<UpdateRequ
129144
*/
130145
public UpdateRequest(String providerId) {
131146
super(providerId);
132-
checkArgument(providerId.startsWith("oidc."), "Invalid OIDC provider ID: " + providerId);
147+
checkOidcProviderId(providerId);
133148
}
134149

135150
/**

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

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,7 @@ public abstract static class AbstractCreateRequest<T extends AbstractCreateReque
7171
final Map<String,Object> properties = new HashMap<>();
7272
String providerId;
7373

74-
/**
75-
* Sets the ID for the new provider.
76-
*
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.
80-
*/
81-
public T setProviderId(String providerId) {
82-
checkArgument(
83-
!Strings.isNullOrEmpty(providerId), "Provider ID name must not be null or empty.");
84-
assertValidProviderIdFormat(providerId);
74+
T setProviderId(String providerId) {
8575
this.providerId = providerId;
8676
return getThis();
8777
}
@@ -117,8 +107,6 @@ Map<String, Object> getProperties() {
117107
}
118108

119109
abstract T getThis();
120-
121-
abstract void assertValidProviderIdFormat(String providerId);
122110
}
123111

124112
/**

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ public String getCallbackUrl() {
7171
return (String) spConfig.get("callbackUri");
7272
}
7373

74+
static void checkSamlProviderId(String providerId) {
75+
checkArgument(!Strings.isNullOrEmpty(providerId), "Provider ID must not be null or empty.");
76+
checkArgument(providerId.startsWith("saml."),
77+
"Invalid SAML provider ID (must be prefixed with 'saml.'): " + providerId);
78+
}
79+
7480
private static List<Object> ensureNestedList(Map<String, Object> outerMap, String id) {
7581
List<Object> list = (List<Object>) outerMap.get(id);
7682
if (list == null) {
@@ -106,6 +112,19 @@ public static final class CreateRequest extends AbstractCreateRequest<CreateRequ
106112
*/
107113
public CreateRequest() { }
108114

115+
/**
116+
* Sets the ID for the new provider.
117+
*
118+
* @param providerId A non-null, non-empty provider ID string.
119+
* @throws IllegalArgumentException If the provider ID is null or empty, or is not prefixed with
120+
* 'saml.'.
121+
*/
122+
@Override
123+
public CreateRequest setProviderId(String providerId) {
124+
checkSamlProviderId(providerId);
125+
return super.setProviderId(providerId);
126+
}
127+
109128
/**
110129
* Sets the IDP entity ID for the new provider.
111130
*
@@ -181,9 +200,5 @@ public CreateRequest setCallbackUrl(String callbackUrl) {
181200
CreateRequest getThis() {
182201
return this;
183202
}
184-
185-
void assertValidProviderIdFormat(String providerId) {
186-
checkArgument(providerId.startsWith("saml."), "Invalid SAML provider ID: " + providerId);
187-
}
188203
}
189204
}

0 commit comments

Comments
 (0)