19
19
import static com .google .common .base .Preconditions .checkArgument ;
20
20
import static com .google .common .base .Preconditions .checkNotNull ;
21
21
22
- import com .google .api .client .http .GenericUrl ;
23
- import com .google .api .client .http .HttpMethods ;
24
22
import com .google .api .client .http .HttpRequestFactory ;
25
23
import com .google .api .client .http .HttpResponseInterceptor ;
26
24
import com .google .api .client .json .GenericJson ;
44
42
import com .google .firebase .auth .internal .ListSamlProviderConfigsResponse ;
45
43
import com .google .firebase .auth .internal .UploadAccountResponse ;
46
44
import com .google .firebase .internal .ApiClientUtils ;
45
+ import com .google .firebase .internal .HttpRequestInfo ;
47
46
import com .google .firebase .internal .NonNull ;
48
47
import com .google .firebase .internal .Nullable ;
49
-
50
48
import java .util .Collection ;
51
49
import java .util .HashSet ;
52
50
import java .util .List ;
60
58
* @see <a href="https://developers.google.com/identity/toolkit/web/reference/relyingparty">
61
59
* Google Identity Toolkit</a>
62
60
*/
63
- class FirebaseUserManager {
61
+ final class FirebaseUserManager {
64
62
65
63
static final int MAX_LIST_PROVIDER_CONFIGS_RESULTS = 100 ;
66
64
static final int MAX_GET_ACCOUNTS_BATCH_SIZE = 100 ;
@@ -81,13 +79,12 @@ class FirebaseUserManager {
81
79
private final AuthHttpClient httpClient ;
82
80
83
81
private FirebaseUserManager (Builder builder ) {
84
- FirebaseApp app = checkNotNull (builder .app , "FirebaseApp must not be null" );
85
- String projectId = ImplFirebaseTrampolines .getProjectId (app );
82
+ String projectId = builder .projectId ;
86
83
checkArgument (!Strings .isNullOrEmpty (projectId ),
87
84
"Project ID is required to access the auth service. Use a service account credential or "
88
85
+ "set the project ID explicitly via FirebaseOptions. Alternatively you can also "
89
86
+ "set the project ID via the GOOGLE_CLOUD_PROJECT environment variable." );
90
- this .jsonFactory = app . getOptions (). getJsonFactory ( );
87
+ this .jsonFactory = checkNotNull ( builder . jsonFactory , "JsonFactory must not be null" );
91
88
final String idToolkitUrlV1 = String .format (ID_TOOLKIT_URL , "v1" , projectId );
92
89
final String idToolkitUrlV2 = String .format (ID_TOOLKIT_URL , "v2" , projectId );
93
90
final String tenantId = builder .tenantId ;
@@ -100,9 +97,7 @@ private FirebaseUserManager(Builder builder) {
100
97
this .idpConfigMgtBaseUrl = idToolkitUrlV2 + "/tenants/" + tenantId ;
101
98
}
102
99
103
- HttpRequestFactory requestFactory = builder .requestFactory == null
104
- ? ApiClientUtils .newAuthorizedRequestFactory (app ) : builder .requestFactory ;
105
- this .httpClient = new AuthHttpClient (jsonFactory , requestFactory );
100
+ this .httpClient = new AuthHttpClient (jsonFactory , builder .requestFactory );
106
101
}
107
102
108
103
@ VisibleForTesting
@@ -182,9 +177,10 @@ DownloadAccountResponse listUsers(int maxResults, String pageToken) throws Fireb
182
177
builder .put ("nextPageToken" , pageToken );
183
178
}
184
179
185
- GenericUrl url = new GenericUrl (userMgtBaseUrl + "/accounts:batchGet" );
186
- url .putAll (builder .build ());
187
- return httpClient .sendRequest (HttpMethods .GET , url , null , DownloadAccountResponse .class );
180
+ String url = userMgtBaseUrl + "/accounts:batchGet" ;
181
+ HttpRequestInfo requestInfo = HttpRequestInfo .buildGetRequest (url )
182
+ .addAllParameters (builder .build ());
183
+ return httpClient .sendRequest (requestInfo , DownloadAccountResponse .class );
188
184
}
189
185
190
186
UserImportResult importUsers (UserImportRequest request ) throws FirebaseAuthException {
@@ -218,8 +214,9 @@ String getEmailActionLink(EmailLinkType type, String email,
218
214
219
215
private UserRecord lookupUserAccount (
220
216
Map <String , Object > payload , String identifier ) throws FirebaseAuthException {
221
- IncomingHttpResponse response = httpClient .sendRequest (
222
- HttpMethods .POST , new GenericUrl (userMgtBaseUrl + "/accounts:lookup" ), payload );
217
+ HttpRequestInfo requestInfo = HttpRequestInfo .buildJsonPostRequest (
218
+ userMgtBaseUrl + "/accounts:lookup" , payload );
219
+ IncomingHttpResponse response = httpClient .sendRequest (requestInfo );
223
220
GetAccountInfoResponse parsed = httpClient .parse (response , GetAccountInfoResponse .class );
224
221
if (parsed .getUsers () == null || parsed .getUsers ().isEmpty ()) {
225
222
throw new FirebaseAuthException (ErrorCode .NOT_FOUND ,
@@ -234,44 +231,46 @@ private UserRecord lookupUserAccount(
234
231
235
232
OidcProviderConfig createOidcProviderConfig (
236
233
OidcProviderConfig .CreateRequest request ) throws FirebaseAuthException {
237
- GenericUrl url = new GenericUrl (idpConfigMgtBaseUrl + "/oauthIdpConfigs" );
238
- url .set ("oauthIdpConfigId" , request .getProviderId ());
239
- return httpClient .sendRequest ("POST" , url , request .getProperties (), OidcProviderConfig .class );
234
+ String url = idpConfigMgtBaseUrl + "/oauthIdpConfigs" ;
235
+ HttpRequestInfo requestInfo = HttpRequestInfo .buildJsonPostRequest (url , request .getProperties ())
236
+ .addParameter ("oauthIdpConfigId" , request .getProviderId ());
237
+ return httpClient .sendRequest (requestInfo , OidcProviderConfig .class );
240
238
}
241
239
242
240
SamlProviderConfig createSamlProviderConfig (
243
241
SamlProviderConfig .CreateRequest request ) throws FirebaseAuthException {
244
- GenericUrl url = new GenericUrl (idpConfigMgtBaseUrl + "/inboundSamlConfigs" );
245
- url .set ("inboundSamlConfigId" , request .getProviderId ());
246
- return httpClient .sendRequest ("POST" , url , request .getProperties (), SamlProviderConfig .class );
242
+ String url = idpConfigMgtBaseUrl + "/inboundSamlConfigs" ;
243
+ HttpRequestInfo requestInfo = HttpRequestInfo .buildJsonPostRequest (url , request .getProperties ())
244
+ .addParameter ("inboundSamlConfigId" , request .getProviderId ());
245
+ return httpClient .sendRequest (requestInfo , SamlProviderConfig .class );
247
246
}
248
247
249
248
OidcProviderConfig updateOidcProviderConfig (OidcProviderConfig .UpdateRequest request )
250
249
throws FirebaseAuthException {
251
250
Map <String , Object > properties = request .getProperties ();
252
- GenericUrl url =
253
- new GenericUrl ( idpConfigMgtBaseUrl + getOidcUrlSuffix ( request . getProviderId ()));
254
- url . put ("updateMask" , Joiner .on ("," ).join (AuthHttpClient .generateMask (properties )));
255
- return httpClient .sendRequest ("PATCH" , url , properties , OidcProviderConfig .class );
251
+ String url = idpConfigMgtBaseUrl + getOidcUrlSuffix ( request . getProviderId ());
252
+ HttpRequestInfo requestInfo = HttpRequestInfo . buildJsonPatchRequest ( url , properties )
253
+ . addParameter ("updateMask" , Joiner .on ("," ).join (AuthHttpClient .generateMask (properties )));
254
+ return httpClient .sendRequest (requestInfo , OidcProviderConfig .class );
256
255
}
257
256
258
257
SamlProviderConfig updateSamlProviderConfig (SamlProviderConfig .UpdateRequest request )
259
258
throws FirebaseAuthException {
260
259
Map <String , Object > properties = request .getProperties ();
261
- GenericUrl url =
262
- new GenericUrl ( idpConfigMgtBaseUrl + getSamlUrlSuffix ( request . getProviderId ()));
263
- url . put ("updateMask" , Joiner .on ("," ).join (AuthHttpClient .generateMask (properties )));
264
- return httpClient .sendRequest ("PATCH" , url , properties , SamlProviderConfig .class );
260
+ String url = idpConfigMgtBaseUrl + getSamlUrlSuffix ( request . getProviderId ());
261
+ HttpRequestInfo requestInfo = HttpRequestInfo . buildJsonPatchRequest ( url , properties )
262
+ . addParameter ("updateMask" , Joiner .on ("," ).join (AuthHttpClient .generateMask (properties )));
263
+ return httpClient .sendRequest (requestInfo , SamlProviderConfig .class );
265
264
}
266
265
267
266
OidcProviderConfig getOidcProviderConfig (String providerId ) throws FirebaseAuthException {
268
- GenericUrl url = new GenericUrl ( idpConfigMgtBaseUrl + getOidcUrlSuffix (providerId ) );
269
- return httpClient .sendRequest ("GET" , url , null , OidcProviderConfig .class );
267
+ String url = idpConfigMgtBaseUrl + getOidcUrlSuffix (providerId );
268
+ return httpClient .sendRequest (HttpRequestInfo . buildGetRequest ( url ) , OidcProviderConfig .class );
270
269
}
271
270
272
271
SamlProviderConfig getSamlProviderConfig (String providerId ) throws FirebaseAuthException {
273
- GenericUrl url = new GenericUrl ( idpConfigMgtBaseUrl + getSamlUrlSuffix (providerId ) );
274
- return httpClient .sendRequest ("GET" , url , null , SamlProviderConfig .class );
272
+ String url = idpConfigMgtBaseUrl + getSamlUrlSuffix (providerId );
273
+ return httpClient .sendRequest (HttpRequestInfo . buildGetRequest ( url ) , SamlProviderConfig .class );
275
274
}
276
275
277
276
ListOidcProviderConfigsResponse listOidcProviderConfigs (int maxResults , String pageToken )
@@ -284,9 +283,10 @@ ListOidcProviderConfigsResponse listOidcProviderConfigs(int maxResults, String p
284
283
builder .put ("nextPageToken" , pageToken );
285
284
}
286
285
287
- GenericUrl url = new GenericUrl (idpConfigMgtBaseUrl + "/oauthIdpConfigs" );
288
- url .putAll (builder .build ());
289
- return httpClient .sendRequest ("GET" , url , null , ListOidcProviderConfigsResponse .class );
286
+ String url = idpConfigMgtBaseUrl + "/oauthIdpConfigs" ;
287
+ HttpRequestInfo requestInfo = HttpRequestInfo .buildGetRequest (url )
288
+ .addAllParameters (builder .build ());
289
+ return httpClient .sendRequest (requestInfo , ListOidcProviderConfigsResponse .class );
290
290
}
291
291
292
292
ListSamlProviderConfigsResponse listSamlProviderConfigs (int maxResults , String pageToken )
@@ -299,19 +299,20 @@ ListSamlProviderConfigsResponse listSamlProviderConfigs(int maxResults, String p
299
299
builder .put ("nextPageToken" , pageToken );
300
300
}
301
301
302
- GenericUrl url = new GenericUrl (idpConfigMgtBaseUrl + "/inboundSamlConfigs" );
303
- url .putAll (builder .build ());
304
- return httpClient .sendRequest ("GET" , url , null , ListSamlProviderConfigsResponse .class );
302
+ String url = idpConfigMgtBaseUrl + "/inboundSamlConfigs" ;
303
+ HttpRequestInfo requestInfo = HttpRequestInfo .buildGetRequest (url )
304
+ .addAllParameters (builder .build ());
305
+ return httpClient .sendRequest (requestInfo , ListSamlProviderConfigsResponse .class );
305
306
}
306
307
307
308
void deleteOidcProviderConfig (String providerId ) throws FirebaseAuthException {
308
- GenericUrl url = new GenericUrl ( idpConfigMgtBaseUrl + getOidcUrlSuffix (providerId ) );
309
- httpClient .sendRequest ("DELETE" , url , null , GenericJson . class );
309
+ String url = idpConfigMgtBaseUrl + getOidcUrlSuffix (providerId );
310
+ httpClient .sendRequest (HttpRequestInfo . buildDeleteRequest ( url ) );
310
311
}
311
312
312
313
void deleteSamlProviderConfig (String providerId ) throws FirebaseAuthException {
313
- GenericUrl url = new GenericUrl ( idpConfigMgtBaseUrl + getSamlUrlSuffix (providerId ) );
314
- httpClient .sendRequest ("DELETE" , url , null , GenericJson . class );
314
+ String url = idpConfigMgtBaseUrl + getSamlUrlSuffix (providerId );
315
+ httpClient .sendRequest (HttpRequestInfo . buildDeleteRequest ( url ) );
315
316
}
316
317
317
318
private static String getOidcUrlSuffix (String providerId ) {
@@ -327,8 +328,8 @@ private static String getSamlUrlSuffix(String providerId) {
327
328
private <T > T post (String path , Object content , Class <T > clazz ) throws FirebaseAuthException {
328
329
checkArgument (!Strings .isNullOrEmpty (path ), "path must not be null or empty" );
329
330
checkNotNull (content , "content must not be null for POST requests" );
330
- GenericUrl url = new GenericUrl ( userMgtBaseUrl + path ) ;
331
- return httpClient .sendRequest (HttpMethods . POST , url , content , clazz );
331
+ String url = userMgtBaseUrl + path ;
332
+ return httpClient .sendRequest (HttpRequestInfo . buildJsonPostRequest ( url , content ) , clazz );
332
333
}
333
334
334
335
static class UserImportRequest extends GenericJson {
@@ -371,18 +372,30 @@ enum EmailLinkType {
371
372
PASSWORD_RESET ,
372
373
}
373
374
375
+ static FirebaseUserManager createUserManager (FirebaseApp app , String tenantId ) {
376
+ return FirebaseUserManager .builder ()
377
+ .setProjectId (ImplFirebaseTrampolines .getProjectId (app ))
378
+ .setTenantId (tenantId )
379
+ .setHttpRequestFactory (ApiClientUtils .newAuthorizedRequestFactory (app ))
380
+ .setJsonFactory (app .getOptions ().getJsonFactory ())
381
+ .build ();
382
+ }
383
+
374
384
static Builder builder () {
375
385
return new Builder ();
376
386
}
377
387
378
388
static class Builder {
379
389
380
- private FirebaseApp app ;
390
+ private String projectId ;
381
391
private String tenantId ;
382
392
private HttpRequestFactory requestFactory ;
393
+ private JsonFactory jsonFactory ;
394
+
395
+ private Builder () { }
383
396
384
- Builder setFirebaseApp ( FirebaseApp app ) {
385
- this .app = app ;
397
+ public Builder setProjectId ( String projectId ) {
398
+ this .projectId = projectId ;
386
399
return this ;
387
400
}
388
401
@@ -396,6 +409,11 @@ Builder setHttpRequestFactory(HttpRequestFactory requestFactory) {
396
409
return this ;
397
410
}
398
411
412
+ public Builder setJsonFactory (JsonFactory jsonFactory ) {
413
+ this .jsonFactory = jsonFactory ;
414
+ return this ;
415
+ }
416
+
399
417
FirebaseUserManager build () {
400
418
return new FirebaseUserManager (this );
401
419
}
0 commit comments