Skip to content

Add operation to list SAML provider configs. #426

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
May 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
7 changes: 4 additions & 3 deletions checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@
<module name="FileTabCharacter">
<property name="eachLine" value="true"/>
</module>

<module name="SuppressionCommentFilter">
<property name="offCommentFormat" value="CSOFF\: ([\w\|]+)"/>
<property name="onCommentFormat" value="CSON\: ([\w\|]+)"/>
<property name="checkFormat" value="$1"/>
</module>

<module name="TreeWalker">
<module name="FileContentsHolder"/>
<module name="TreeWalker">
<module name="FileContentsHolder"/>
<module name="OuterTypeFilename"/>
<module name="IllegalTokenText">
<property name="tokens" value="STRING_LITERAL, CHAR_LITERAL"/>
Expand Down Expand Up @@ -229,6 +229,7 @@
<property name="allowedAnnotations" value="Override, Test"/>
<property name="allowThrowsTagsForSubclasses" value="true"/>
<property name="allowMissingJavadoc" value="true"/>
<property name="suppressLoadErrors" value="true"/>
</module>
<module name="MethodName">
<property name="format" value="^[a-z][a-z0-9][a-zA-Z0-9_]*$"/>
Expand Down
97 changes: 93 additions & 4 deletions src/main/java/com/google/firebase/auth/AbstractFirebaseAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.google.firebase.auth.FirebaseUserManager.UserImportRequest;
import com.google.firebase.auth.ListProviderConfigsPage;
import com.google.firebase.auth.ListProviderConfigsPage.DefaultOidcProviderConfigSource;
import com.google.firebase.auth.ListProviderConfigsPage.DefaultSamlProviderConfigSource;
import com.google.firebase.auth.ListUsersPage;
import com.google.firebase.auth.ListUsersPage.DefaultUserSource;
import com.google.firebase.auth.UserRecord;
Expand Down Expand Up @@ -1107,8 +1108,8 @@ public ListProviderConfigsPage<OidcProviderConfig> listOidcProviderConfigs(
}

/**
* Similar to {@link #listlistOidcProviderConfigs(String)} but performs the operation
* asynchronously. Page size will be limited to 100 provider configs.
* Similar to {@link #listOidcProviderConfigs(String)} but performs the operation asynchronously.
* Page size will be limited to 100 provider configs.
*
* @param pageToken A non-empty page token string, or null to retrieve the first page of provider
* configs.
Expand Down Expand Up @@ -1249,7 +1250,7 @@ protected SamlProviderConfig execute() throws FirebaseAuthException {

/**
* Updates an existing SAML Auth provider config with the attributes contained in the specified
* {@link OidcProviderConfig.UpdateRequest}.
* {@link SamlProviderConfig.UpdateRequest}.
*
* @param request A non-null {@link SamlProviderConfig.UpdateRequest} instance.
* @return A {@link SamlProviderConfig} instance corresponding to the updated provider config.
Expand Down Expand Up @@ -1296,7 +1297,7 @@ protected SamlProviderConfig execute() throws FirebaseAuthException {
* Gets the SAML provider Auth config corresponding to the specified provider ID.
*
* @param providerId A provider ID string.
* @return An {@link OidcProviderConfig} instance.
* @return An {@link SamlProviderConfig} instance.
* @throws IllegalArgumentException If the provider ID string is null or empty, or is not prefixed
* with 'saml'.
* @throws FirebaseAuthException If an error occurs while retrieving the provider config.
Expand Down Expand Up @@ -1335,6 +1336,94 @@ protected SamlProviderConfig execute() throws FirebaseAuthException {
};
}

/**
* Gets a page of SAML Auth provider configs starting from the specified {@code pageToken}. Page
* size will be limited to 100 provider configs.
*
* @param pageToken A non-empty page token string, or null to retrieve the first page of provider
* configs.
* @return A {@link ListProviderConfigsPage} instance.
* @throws IllegalArgumentException If the specified page token is empty.
* @throws FirebaseAuthException If an error occurs while retrieving provider config data.
*/
public ListProviderConfigsPage<SamlProviderConfig> listSamlProviderConfigs(
@Nullable String pageToken) throws FirebaseAuthException {
return listSamlProviderConfigs(
pageToken,
FirebaseUserManager.MAX_LIST_PROVIDER_CONFIGS_RESULTS);
}

/**
* Gets a page of SAML Auth provider configs starting from the specified {@code pageToken}.
*
* @param pageToken A non-empty page token string, or null to retrieve the first page of provider
* configs.
* @param maxResults Maximum number of provider configs to include in the returned page. This may
* not exceed 100.
* @return A {@link ListProviderConfigsPage} instance.
* @throws IllegalArgumentException If the specified page token is empty, or max results value is
* invalid.
* @throws FirebaseAuthException If an error occurs while retrieving provider config data.
*/
public ListProviderConfigsPage<SamlProviderConfig> listSamlProviderConfigs(
@Nullable String pageToken, int maxResults) throws FirebaseAuthException {
return listSamlProviderConfigsOp(pageToken, maxResults).call();
}

/**
* Similar to {@link #listSamlProviderConfigs(String)} but performs the operation asynchronously.
* Page size will be limited to 100 provider configs.
*
* @param pageToken A non-empty page token string, or null to retrieve the first page of provider
* configs.
* @return An {@code ApiFuture} which will complete successfully with a
* {@link ListProviderConfigsPage} instance. If an error occurs while retrieving provider
* config data, the future throws an exception.
* @throws IllegalArgumentException If the specified page token is empty.
*/
public ApiFuture<ListProviderConfigsPage<SamlProviderConfig>> listSamlProviderConfigsAsync(
@Nullable String pageToken) {
int maxResults = FirebaseUserManager.MAX_LIST_PROVIDER_CONFIGS_RESULTS;
return listSamlProviderConfigsAsync(pageToken, maxResults);
}

/**
* Similar to {@link #listSamlProviderConfigs(String, int)} but performs the operation
* asynchronously.
*
* @param pageToken A non-empty page token string, or null to retrieve the first page of provider
* configs.
* @param maxResults Maximum number of provider configs to include in the returned page. This may
* not exceed 100.
* @return An {@code ApiFuture} which will complete successfully with a
* {@link ListProviderConfigsPage} instance. If an error occurs while retrieving provider
* config data, the future throws an exception.
* @throws IllegalArgumentException If the specified page token is empty, or max results value is
* invalid.
*/
public ApiFuture<ListProviderConfigsPage<SamlProviderConfig>> listSamlProviderConfigsAsync(
@Nullable String pageToken,
int maxResults) {
return listSamlProviderConfigsOp(pageToken, maxResults).callAsync(firebaseApp);
}

private CallableOperation<ListProviderConfigsPage<SamlProviderConfig>, FirebaseAuthException>
listSamlProviderConfigsOp(@Nullable final String pageToken, final int maxResults) {
checkNotDestroyed();
final FirebaseUserManager userManager = getUserManager();
final DefaultSamlProviderConfigSource source = new DefaultSamlProviderConfigSource(userManager);
final ListProviderConfigsPage.Factory<SamlProviderConfig> factory =
new ListProviderConfigsPage.Factory<SamlProviderConfig>(source, maxResults, pageToken);
return
new CallableOperation<ListProviderConfigsPage<SamlProviderConfig>, FirebaseAuthException>() {
@Override
protected ListProviderConfigsPage<SamlProviderConfig> execute()
throws FirebaseAuthException {
return factory.create();
}
};
}

/**
* Deletes the SAML Auth provider config identified by the specified provider ID.
*
Expand Down
23 changes: 22 additions & 1 deletion src/main/java/com/google/firebase/auth/FirebaseUserManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import com.google.firebase.auth.internal.GetAccountInfoResponse;
import com.google.firebase.auth.internal.HttpErrorResponse;
import com.google.firebase.auth.internal.ListOidcProviderConfigsResponse;
import com.google.firebase.auth.internal.ListSamlProviderConfigsResponse;
import com.google.firebase.auth.internal.ListTenantsResponse;
import com.google.firebase.auth.internal.UploadAccountResponse;
import com.google.firebase.internal.ApiClientUtils;
Expand Down Expand Up @@ -366,7 +367,7 @@ ListOidcProviderConfigsResponse listOidcProviderConfigs(int maxResults, String p
ImmutableMap.<String, Object>builder().put("pageSize", maxResults);
if (pageToken != null) {
checkArgument(!pageToken.equals(
ListTenantsPage.END_OF_LIST), "Invalid end of list page token.");
ListProviderConfigsPage.END_OF_LIST), "Invalid end of list page token.");
builder.put("nextPageToken", pageToken);
}

Expand All @@ -380,6 +381,26 @@ ListOidcProviderConfigsResponse listOidcProviderConfigs(int maxResults, String p
return response;
}

ListSamlProviderConfigsResponse listSamlProviderConfigs(int maxResults, String pageToken)
throws FirebaseAuthException {
ImmutableMap.Builder<String, Object> builder =
ImmutableMap.<String, Object>builder().put("pageSize", maxResults);
if (pageToken != null) {
checkArgument(!pageToken.equals(
ListProviderConfigsPage.END_OF_LIST), "Invalid end of list page token.");
builder.put("nextPageToken", pageToken);
}

GenericUrl url = new GenericUrl(idpConfigMgtBaseUrl + "/inboundSamlConfigs");
url.putAll(builder.build());
ListSamlProviderConfigsResponse response =
sendRequest("GET", url, null, ListSamlProviderConfigsResponse.class);
if (response == null) {
throw new FirebaseAuthException(INTERNAL_ERROR, "Failed to retrieve provider configs.");
}
return response;
}

void deleteOidcProviderConfig(String providerId) throws FirebaseAuthException {
GenericUrl url = new GenericUrl(idpConfigMgtBaseUrl + getOidcUrlSuffix(providerId));
sendRequest("DELETE", url, null, GenericJson.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.firebase.auth.internal.DownloadAccountResponse;
import com.google.firebase.auth.internal.ListOidcProviderConfigsResponse;
import com.google.firebase.auth.internal.ListProviderConfigsResponse;
import com.google.firebase.auth.internal.ListSamlProviderConfigsResponse;
import com.google.firebase.internal.NonNull;
import com.google.firebase.internal.Nullable;
import java.util.Iterator;
Expand Down Expand Up @@ -205,7 +206,7 @@ static class DefaultOidcProviderConfigSource implements ProviderConfigSource<Oid
private final FirebaseUserManager userManager;

DefaultOidcProviderConfigSource(FirebaseUserManager userManager) {
this.userManager = checkNotNull(userManager, "user manager must not be null");
this.userManager = checkNotNull(userManager, "User manager must not be null.");
}

@Override
Expand All @@ -215,7 +216,20 @@ public ListOidcProviderConfigsResponse fetch(int maxResults, String pageToken)
}
}

// TODO(micahstairs): Add DefaultSamlProviderConfigSource class.
static class DefaultSamlProviderConfigSource implements ProviderConfigSource<SamlProviderConfig> {

private final FirebaseUserManager userManager;

DefaultSamlProviderConfigSource(FirebaseUserManager userManager) {
this.userManager = checkNotNull(userManager, "User manager must not be null.");
}

@Override
public ListSamlProviderConfigsResponse fetch(int maxResults, String pageToken)
throws FirebaseAuthException {
return userManager.listSamlProviderConfigs(maxResults, pageToken);
}
}

/**
* A simple factory class for {@link ProviderConfigsPage} instances.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public static final class UpdateRequest extends AbstractUpdateRequest<UpdateRequ
* {@link AbstractFirebaseAuth#updateOidcProviderConfig(CreateRequest)} to update the provider
* information persistently.
*
* @param tenantId A non-null, non-empty provider ID string.
* @param providerId A non-null, non-empty provider ID string.
* @throws IllegalArgumentException If the provider ID is null or empty, or is not prefixed with
* "oidc.".
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
package com.google.firebase.auth.internal;

import com.google.api.client.util.Key;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.firebase.auth.OidcProviderConfig;
import com.google.firebase.auth.Tenant;
import java.util.List;

/**
Expand All @@ -36,16 +35,6 @@ public final class ListOidcProviderConfigsResponse
@Key("nextPageToken")
private String pageToken;

@VisibleForTesting
public ListOidcProviderConfigsResponse(
List<OidcProviderConfig> providerConfigs,
String pageToken) {
this.providerConfigs = providerConfigs;
this.pageToken = pageToken;
}

public ListOidcProviderConfigsResponse() { }

@Override
public List<OidcProviderConfig> getProviderConfigs() {
return providerConfigs == null ? ImmutableList.<OidcProviderConfig>of() : providerConfigs;
Expand All @@ -58,6 +47,6 @@ public boolean hasProviderConfigs() {

@Override
public String getPageToken() {
return pageToken == null ? "" : pageToken;
return Strings.nullToEmpty(pageToken);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.firebase.auth.internal;

import com.google.api.client.util.Key;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.firebase.auth.SamlProviderConfig;
import java.util.List;

/**
* JSON data binding for ListInboundSamlConfigsResponse messages sent by Google identity toolkit
* service.
*/
public final class ListSamlProviderConfigsResponse
implements ListProviderConfigsResponse<SamlProviderConfig> {

@Key("inboundSamlConfigs")
private List<SamlProviderConfig> providerConfigs;

@Key("nextPageToken")
private String pageToken;

@Override
public List<SamlProviderConfig> getProviderConfigs() {
return providerConfigs == null ? ImmutableList.<SamlProviderConfig>of() : providerConfigs;
}

@Override
public boolean hasProviderConfigs() {
return providerConfigs != null && !providerConfigs.isEmpty();
}

@Override
public String getPageToken() {
return Strings.nullToEmpty(pageToken);
}
}
Loading