Skip to content

chore: Support for specifying query parameters in HttpRequestInfo #463

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 2 commits into from
Jul 31, 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
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,7 @@ public FirebaseTokenVerifier get() {
new Supplier<FirebaseUserManager>() {
@Override
public FirebaseUserManager get() {
return FirebaseUserManager
.builder()
.setFirebaseApp(app)
.setTenantId(tenantId)
.build();
return FirebaseUserManager.createUserManager(app, tenantId);
}
});
}
Expand Down
34 changes: 1 addition & 33 deletions src/main/java/com/google/firebase/auth/FirebaseAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.api.client.util.Clock;
import com.google.api.core.ApiFuture;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import com.google.common.base.Supplier;
import com.google.firebase.FirebaseApp;
import com.google.firebase.ImplFirebaseTrampolines;
import com.google.firebase.auth.internal.FirebaseTokenFactory;
import com.google.firebase.auth.multitenancy.TenantManager;
import com.google.firebase.internal.CallableOperation;
import com.google.firebase.internal.FirebaseService;
Expand Down Expand Up @@ -214,37 +212,7 @@ FirebaseTokenVerifier getSessionCookieVerifier(boolean checkRevoked) {
protected void doDestroy() { }

private static FirebaseAuth fromApp(final FirebaseApp app) {
return new FirebaseAuth(
AbstractFirebaseAuth.builder()
.setFirebaseApp(app)
.setTokenFactory(
new Supplier<FirebaseTokenFactory>() {
@Override
public FirebaseTokenFactory get() {
return FirebaseTokenUtils.createTokenFactory(app, Clock.SYSTEM);
}
})
.setIdTokenVerifier(
new Supplier<FirebaseTokenVerifier>() {
@Override
public FirebaseTokenVerifier get() {
return FirebaseTokenUtils.createIdTokenVerifier(app, Clock.SYSTEM);
}
})
.setCookieVerifier(
new Supplier<FirebaseTokenVerifier>() {
@Override
public FirebaseTokenVerifier get() {
return FirebaseTokenUtils.createSessionCookieVerifier(app, Clock.SYSTEM);
}
})
.setUserManager(
new Supplier<FirebaseUserManager>() {
@Override
public FirebaseUserManager get() {
return FirebaseUserManager.builder().setFirebaseApp(app).build();
}
}));
return new FirebaseAuth(AbstractFirebaseAuth.builderFromAppAndTenantId(app, null));
}

private static class FirebaseAuthService extends FirebaseService<FirebaseAuth> {
Expand Down
114 changes: 66 additions & 48 deletions src/main/java/com/google/firebase/auth/FirebaseUserManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpMethods;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpResponseInterceptor;
import com.google.api.client.json.GenericJson;
Expand All @@ -44,9 +42,9 @@
import com.google.firebase.auth.internal.ListSamlProviderConfigsResponse;
import com.google.firebase.auth.internal.UploadAccountResponse;
import com.google.firebase.internal.ApiClientUtils;
import com.google.firebase.internal.HttpRequestInfo;
import com.google.firebase.internal.NonNull;
import com.google.firebase.internal.Nullable;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
Expand All @@ -60,7 +58,7 @@
* @see <a href="https://developers.google.com/identity/toolkit/web/reference/relyingparty">
* Google Identity Toolkit</a>
*/
class FirebaseUserManager {
final class FirebaseUserManager {

static final int MAX_LIST_PROVIDER_CONFIGS_RESULTS = 100;
static final int MAX_GET_ACCOUNTS_BATCH_SIZE = 100;
Expand All @@ -81,13 +79,12 @@ class FirebaseUserManager {
private final AuthHttpClient httpClient;

private FirebaseUserManager(Builder builder) {
FirebaseApp app = checkNotNull(builder.app, "FirebaseApp must not be null");
String projectId = ImplFirebaseTrampolines.getProjectId(app);
String projectId = builder.projectId;
checkArgument(!Strings.isNullOrEmpty(projectId),
"Project ID is required to access the auth service. Use a service account credential or "
+ "set the project ID explicitly via FirebaseOptions. Alternatively you can also "
+ "set the project ID via the GOOGLE_CLOUD_PROJECT environment variable.");
this.jsonFactory = app.getOptions().getJsonFactory();
this.jsonFactory = checkNotNull(builder.jsonFactory, "JsonFactory must not be null");
final String idToolkitUrlV1 = String.format(ID_TOOLKIT_URL, "v1", projectId);
final String idToolkitUrlV2 = String.format(ID_TOOLKIT_URL, "v2", projectId);
final String tenantId = builder.tenantId;
Expand All @@ -100,9 +97,7 @@ private FirebaseUserManager(Builder builder) {
this.idpConfigMgtBaseUrl = idToolkitUrlV2 + "/tenants/" + tenantId;
}

HttpRequestFactory requestFactory = builder.requestFactory == null
? ApiClientUtils.newAuthorizedRequestFactory(app) : builder.requestFactory;
this.httpClient = new AuthHttpClient(jsonFactory, requestFactory);
this.httpClient = new AuthHttpClient(jsonFactory, builder.requestFactory);
}

@VisibleForTesting
Expand Down Expand Up @@ -182,9 +177,10 @@ DownloadAccountResponse listUsers(int maxResults, String pageToken) throws Fireb
builder.put("nextPageToken", pageToken);
}

GenericUrl url = new GenericUrl(userMgtBaseUrl + "/accounts:batchGet");
url.putAll(builder.build());
return httpClient.sendRequest(HttpMethods.GET, url, null, DownloadAccountResponse.class);
String url = userMgtBaseUrl + "/accounts:batchGet";
HttpRequestInfo requestInfo = HttpRequestInfo.buildGetRequest(url)
.addAllParameters(builder.build());
return httpClient.sendRequest(requestInfo, DownloadAccountResponse.class);
}

UserImportResult importUsers(UserImportRequest request) throws FirebaseAuthException {
Expand Down Expand Up @@ -218,8 +214,9 @@ String getEmailActionLink(EmailLinkType type, String email,

private UserRecord lookupUserAccount(
Map<String, Object> payload, String identifier) throws FirebaseAuthException {
IncomingHttpResponse response = httpClient.sendRequest(
HttpMethods.POST, new GenericUrl(userMgtBaseUrl + "/accounts:lookup"), payload);
HttpRequestInfo requestInfo = HttpRequestInfo.buildJsonPostRequest(
userMgtBaseUrl + "/accounts:lookup", payload);
IncomingHttpResponse response = httpClient.sendRequest(requestInfo);
GetAccountInfoResponse parsed = httpClient.parse(response, GetAccountInfoResponse.class);
if (parsed.getUsers() == null || parsed.getUsers().isEmpty()) {
throw new FirebaseAuthException(ErrorCode.NOT_FOUND,
Expand All @@ -234,44 +231,46 @@ private UserRecord lookupUserAccount(

OidcProviderConfig createOidcProviderConfig(
OidcProviderConfig.CreateRequest request) throws FirebaseAuthException {
GenericUrl url = new GenericUrl(idpConfigMgtBaseUrl + "/oauthIdpConfigs");
url.set("oauthIdpConfigId", request.getProviderId());
return httpClient.sendRequest("POST", url, request.getProperties(), OidcProviderConfig.class);
String url = idpConfigMgtBaseUrl + "/oauthIdpConfigs";
HttpRequestInfo requestInfo = HttpRequestInfo.buildJsonPostRequest(url, request.getProperties())
.addParameter("oauthIdpConfigId", request.getProviderId());
return httpClient.sendRequest(requestInfo, OidcProviderConfig.class);
}

SamlProviderConfig createSamlProviderConfig(
SamlProviderConfig.CreateRequest request) throws FirebaseAuthException {
GenericUrl url = new GenericUrl(idpConfigMgtBaseUrl + "/inboundSamlConfigs");
url.set("inboundSamlConfigId", request.getProviderId());
return httpClient.sendRequest("POST", url, request.getProperties(), SamlProviderConfig.class);
String url = idpConfigMgtBaseUrl + "/inboundSamlConfigs";
HttpRequestInfo requestInfo = HttpRequestInfo.buildJsonPostRequest(url, request.getProperties())
.addParameter("inboundSamlConfigId", request.getProviderId());
return httpClient.sendRequest(requestInfo, SamlProviderConfig.class);
}

OidcProviderConfig updateOidcProviderConfig(OidcProviderConfig.UpdateRequest request)
throws FirebaseAuthException {
Map<String, Object> properties = request.getProperties();
GenericUrl url =
new GenericUrl(idpConfigMgtBaseUrl + getOidcUrlSuffix(request.getProviderId()));
url.put("updateMask", Joiner.on(",").join(AuthHttpClient.generateMask(properties)));
return httpClient.sendRequest("PATCH", url, properties, OidcProviderConfig.class);
String url = idpConfigMgtBaseUrl + getOidcUrlSuffix(request.getProviderId());
HttpRequestInfo requestInfo = HttpRequestInfo.buildJsonPatchRequest(url, properties)
.addParameter("updateMask", Joiner.on(",").join(AuthHttpClient.generateMask(properties)));
return httpClient.sendRequest(requestInfo, OidcProviderConfig.class);
}

SamlProviderConfig updateSamlProviderConfig(SamlProviderConfig.UpdateRequest request)
throws FirebaseAuthException {
Map<String, Object> properties = request.getProperties();
GenericUrl url =
new GenericUrl(idpConfigMgtBaseUrl + getSamlUrlSuffix(request.getProviderId()));
url.put("updateMask", Joiner.on(",").join(AuthHttpClient.generateMask(properties)));
return httpClient.sendRequest("PATCH", url, properties, SamlProviderConfig.class);
String url = idpConfigMgtBaseUrl + getSamlUrlSuffix(request.getProviderId());
HttpRequestInfo requestInfo = HttpRequestInfo.buildJsonPatchRequest(url, properties)
.addParameter("updateMask", Joiner.on(",").join(AuthHttpClient.generateMask(properties)));
return httpClient.sendRequest(requestInfo, SamlProviderConfig.class);
}

OidcProviderConfig getOidcProviderConfig(String providerId) throws FirebaseAuthException {
GenericUrl url = new GenericUrl(idpConfigMgtBaseUrl + getOidcUrlSuffix(providerId));
return httpClient.sendRequest("GET", url, null, OidcProviderConfig.class);
String url = idpConfigMgtBaseUrl + getOidcUrlSuffix(providerId);
return httpClient.sendRequest(HttpRequestInfo.buildGetRequest(url), OidcProviderConfig.class);
}

SamlProviderConfig getSamlProviderConfig(String providerId) throws FirebaseAuthException {
GenericUrl url = new GenericUrl(idpConfigMgtBaseUrl + getSamlUrlSuffix(providerId));
return httpClient.sendRequest("GET", url, null, SamlProviderConfig.class);
String url = idpConfigMgtBaseUrl + getSamlUrlSuffix(providerId);
return httpClient.sendRequest(HttpRequestInfo.buildGetRequest(url), SamlProviderConfig.class);
}

ListOidcProviderConfigsResponse listOidcProviderConfigs(int maxResults, String pageToken)
Expand All @@ -284,9 +283,10 @@ ListOidcProviderConfigsResponse listOidcProviderConfigs(int maxResults, String p
builder.put("nextPageToken", pageToken);
}

GenericUrl url = new GenericUrl(idpConfigMgtBaseUrl + "/oauthIdpConfigs");
url.putAll(builder.build());
return httpClient.sendRequest("GET", url, null, ListOidcProviderConfigsResponse.class);
String url = idpConfigMgtBaseUrl + "/oauthIdpConfigs";
HttpRequestInfo requestInfo = HttpRequestInfo.buildGetRequest(url)
.addAllParameters(builder.build());
return httpClient.sendRequest(requestInfo, ListOidcProviderConfigsResponse.class);
}

ListSamlProviderConfigsResponse listSamlProviderConfigs(int maxResults, String pageToken)
Expand All @@ -299,19 +299,20 @@ ListSamlProviderConfigsResponse listSamlProviderConfigs(int maxResults, String p
builder.put("nextPageToken", pageToken);
}

GenericUrl url = new GenericUrl(idpConfigMgtBaseUrl + "/inboundSamlConfigs");
url.putAll(builder.build());
return httpClient.sendRequest("GET", url, null, ListSamlProviderConfigsResponse.class);
String url = idpConfigMgtBaseUrl + "/inboundSamlConfigs";
HttpRequestInfo requestInfo = HttpRequestInfo.buildGetRequest(url)
.addAllParameters(builder.build());
return httpClient.sendRequest(requestInfo, ListSamlProviderConfigsResponse.class);
}

void deleteOidcProviderConfig(String providerId) throws FirebaseAuthException {
GenericUrl url = new GenericUrl(idpConfigMgtBaseUrl + getOidcUrlSuffix(providerId));
httpClient.sendRequest("DELETE", url, null, GenericJson.class);
String url = idpConfigMgtBaseUrl + getOidcUrlSuffix(providerId);
httpClient.sendRequest(HttpRequestInfo.buildDeleteRequest(url));
}

void deleteSamlProviderConfig(String providerId) throws FirebaseAuthException {
GenericUrl url = new GenericUrl(idpConfigMgtBaseUrl + getSamlUrlSuffix(providerId));
httpClient.sendRequest("DELETE", url, null, GenericJson.class);
String url = idpConfigMgtBaseUrl + getSamlUrlSuffix(providerId);
httpClient.sendRequest(HttpRequestInfo.buildDeleteRequest(url));
}

private static String getOidcUrlSuffix(String providerId) {
Expand All @@ -327,8 +328,8 @@ private static String getSamlUrlSuffix(String providerId) {
private <T> T post(String path, Object content, Class<T> clazz) throws FirebaseAuthException {
checkArgument(!Strings.isNullOrEmpty(path), "path must not be null or empty");
checkNotNull(content, "content must not be null for POST requests");
GenericUrl url = new GenericUrl(userMgtBaseUrl + path);
return httpClient.sendRequest(HttpMethods.POST, url, content, clazz);
String url = userMgtBaseUrl + path;
return httpClient.sendRequest(HttpRequestInfo.buildJsonPostRequest(url, content), clazz);
}

static class UserImportRequest extends GenericJson {
Expand Down Expand Up @@ -371,18 +372,30 @@ enum EmailLinkType {
PASSWORD_RESET,
}

static FirebaseUserManager createUserManager(FirebaseApp app, String tenantId) {
return FirebaseUserManager.builder()
.setProjectId(ImplFirebaseTrampolines.getProjectId(app))
.setTenantId(tenantId)
.setHttpRequestFactory(ApiClientUtils.newAuthorizedRequestFactory(app))
.setJsonFactory(app.getOptions().getJsonFactory())
.build();
}

static Builder builder() {
return new Builder();
}

static class Builder {

private FirebaseApp app;
private String projectId;
private String tenantId;
private HttpRequestFactory requestFactory;
private JsonFactory jsonFactory;

private Builder() { }

Builder setFirebaseApp(FirebaseApp app) {
this.app = app;
public Builder setProjectId(String projectId) {
this.projectId = projectId;
return this;
}

Expand All @@ -396,6 +409,11 @@ Builder setHttpRequestFactory(HttpRequestFactory requestFactory) {
return this;
}

public Builder setJsonFactory(JsonFactory jsonFactory) {
this.jsonFactory = jsonFactory;
return this;
}

FirebaseUserManager build() {
return new FirebaseUserManager(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.google.firebase.auth.internal;

import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpResponseInterceptor;
import com.google.api.client.json.JsonFactory;
Expand All @@ -25,7 +24,6 @@
import com.google.firebase.auth.FirebaseAuthException;
import com.google.firebase.internal.ErrorHandlingHttpClient;
import com.google.firebase.internal.HttpRequestInfo;
import com.google.firebase.internal.Nullable;
import com.google.firebase.internal.SdkUtils;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -65,21 +63,14 @@ public void setInterceptor(HttpResponseInterceptor interceptor) {
this.httpClient.setInterceptor(interceptor);
}

public IncomingHttpResponse sendRequest(
String method, GenericUrl url, @Nullable Object content) throws FirebaseAuthException {
HttpRequestInfo request = HttpRequestInfo.buildJsonRequest(method, url, content)
.addHeader(CLIENT_VERSION_HEADER, CLIENT_VERSION);
return httpClient.send(request);
public <T> T sendRequest(HttpRequestInfo request, Class<T> clazz) throws FirebaseAuthException {
IncomingHttpResponse response = this.sendRequest(request);
return this.parse(response, clazz);
}

public <T> T sendRequest(
String method,
GenericUrl url,
@Nullable Object content,
Class<T> clazz) throws FirebaseAuthException {

IncomingHttpResponse response = this.sendRequest(method, url, content);
return this.parse(response, clazz);
public IncomingHttpResponse sendRequest(HttpRequestInfo request) throws FirebaseAuthException {
request.addHeader(CLIENT_VERSION_HEADER, CLIENT_VERSION);
return httpClient.send(request);
}

public <T> T parse(IncomingHttpResponse response, Class<T> clazz) throws FirebaseAuthException {
Expand Down
Loading