2020import static com .google .common .base .Preconditions .checkNotNull ;
2121
2222import com .google .api .client .http .GenericUrl ;
23+ import com .google .api .client .http .HttpContent ;
2324import com .google .api .client .http .HttpRequest ;
2425import com .google .api .client .http .HttpRequestFactory ;
2526import com .google .api .client .http .HttpResponse ;
3637import com .google .common .collect .ImmutableList ;
3738import com .google .common .collect .ImmutableMap ;
3839import com .google .firebase .FirebaseApp ;
40+ import com .google .firebase .ImplFirebaseTrampolines ;
3941import com .google .firebase .auth .UserRecord .CreateRequest ;
4042import com .google .firebase .auth .UserRecord .UpdateRequest ;
4143import com .google .firebase .auth .internal .DownloadAccountResponse ;
4547import com .google .firebase .auth .internal .UploadAccountResponse ;
4648import com .google .firebase .internal .FirebaseRequestInitializer ;
4749import com .google .firebase .internal .NonNull ;
50+ import com .google .firebase .internal .Nullable ;
4851import com .google .firebase .internal .SdkUtils ;
52+
4953import java .io .IOException ;
5054import java .util .List ;
5155import java .util .Map ;
@@ -91,9 +95,10 @@ class FirebaseUserManager {
9195 "iss" , "jti" , "nbf" , "nonce" , "sub" , "firebase" );
9296
9397 private static final String ID_TOOLKIT_URL =
94- "https://www .googleapis.com/identitytoolkit/v3/relyingparty/ " ;
98+ "https://identitytoolkit .googleapis.com/v1/projects/%s " ;
9599 private static final String CLIENT_VERSION_HEADER = "X-Client-Version" ;
96100
101+ private final String baseUrl ;
97102 private final JsonFactory jsonFactory ;
98103 private final HttpRequestFactory requestFactory ;
99104 private final String clientVersion = "Java/Admin/" + SdkUtils .getVersion ();
@@ -107,6 +112,12 @@ class FirebaseUserManager {
107112 */
108113 FirebaseUserManager (@ NonNull FirebaseApp app ) {
109114 checkNotNull (app , "FirebaseApp must not be null" );
115+ String projectId = ImplFirebaseTrampolines .getProjectId (app );
116+ checkArgument (!Strings .isNullOrEmpty (projectId ),
117+ "Project ID is required to access the auth service. Use a service account credential or "
118+ + "set the project ID explicitly via FirebaseOptions. Alternatively you can also "
119+ + "set the project ID via the GOOGLE_CLOUD_PROJECT environment variable." );
120+ this .baseUrl = String .format (ID_TOOLKIT_URL , projectId );
110121 this .jsonFactory = app .getOptions ().getJsonFactory ();
111122 HttpTransport transport = app .getOptions ().getHttpTransport ();
112123 this .requestFactory = transport .createRequestFactory (new FirebaseRequestInitializer (app ));
@@ -121,7 +132,7 @@ UserRecord getUserById(String uid) throws FirebaseAuthException {
121132 final Map <String , Object > payload = ImmutableMap .<String , Object >of (
122133 "localId" , ImmutableList .of (uid ));
123134 GetAccountInfoResponse response = post (
124- "getAccountInfo " , payload , GetAccountInfoResponse .class );
135+ "/accounts:lookup " , payload , GetAccountInfoResponse .class );
125136 if (response == null || response .getUsers () == null || response .getUsers ().isEmpty ()) {
126137 throw new FirebaseAuthException (USER_NOT_FOUND_ERROR ,
127138 "No user record found for the provided user ID: " + uid );
@@ -133,7 +144,7 @@ UserRecord getUserByEmail(String email) throws FirebaseAuthException {
133144 final Map <String , Object > payload = ImmutableMap .<String , Object >of (
134145 "email" , ImmutableList .of (email ));
135146 GetAccountInfoResponse response = post (
136- "getAccountInfo " , payload , GetAccountInfoResponse .class );
147+ "/accounts:lookup " , payload , GetAccountInfoResponse .class );
137148 if (response == null || response .getUsers () == null || response .getUsers ().isEmpty ()) {
138149 throw new FirebaseAuthException (USER_NOT_FOUND_ERROR ,
139150 "No user record found for the provided email: " + email );
@@ -145,7 +156,7 @@ UserRecord getUserByPhoneNumber(String phoneNumber) throws FirebaseAuthException
145156 final Map <String , Object > payload = ImmutableMap .<String , Object >of (
146157 "phoneNumber" , ImmutableList .of (phoneNumber ));
147158 GetAccountInfoResponse response = post (
148- "getAccountInfo " , payload , GetAccountInfoResponse .class );
159+ "/accounts:lookup " , payload , GetAccountInfoResponse .class );
149160 if (response == null || response .getUsers () == null || response .getUsers ().isEmpty ()) {
150161 throw new FirebaseAuthException (USER_NOT_FOUND_ERROR ,
151162 "No user record found for the provided phone number: " + phoneNumber );
@@ -155,7 +166,7 @@ UserRecord getUserByPhoneNumber(String phoneNumber) throws FirebaseAuthException
155166
156167 String createUser (CreateRequest request ) throws FirebaseAuthException {
157168 GenericJson response = post (
158- "signupNewUser " , request .getProperties (), GenericJson .class );
169+ "/accounts " , request .getProperties (), GenericJson .class );
159170 if (response != null ) {
160171 String uid = (String ) response .get ("localId" );
161172 if (!Strings .isNullOrEmpty (uid )) {
@@ -167,7 +178,7 @@ String createUser(CreateRequest request) throws FirebaseAuthException {
167178
168179 void updateUser (UpdateRequest request , JsonFactory jsonFactory ) throws FirebaseAuthException {
169180 GenericJson response = post (
170- "setAccountInfo " , request .getProperties (jsonFactory ), GenericJson .class );
181+ "/accounts:update " , request .getProperties (jsonFactory ), GenericJson .class );
171182 if (response == null || !request .getUid ().equals (response .get ("localId" ))) {
172183 throw new FirebaseAuthException (INTERNAL_ERROR , "Failed to update user: " + request .getUid ());
173184 }
@@ -176,7 +187,7 @@ void updateUser(UpdateRequest request, JsonFactory jsonFactory) throws FirebaseA
176187 void deleteUser (String uid ) throws FirebaseAuthException {
177188 final Map <String , Object > payload = ImmutableMap .<String , Object >of ("localId" , uid );
178189 GenericJson response = post (
179- "deleteAccount " , payload , GenericJson .class );
190+ "/accounts:delete " , payload , GenericJson .class );
180191 if (response == null || !response .containsKey ("kind" )) {
181192 throw new FirebaseAuthException (INTERNAL_ERROR , "Failed to delete user: " + uid );
182193 }
@@ -190,8 +201,10 @@ DownloadAccountResponse listUsers(int maxResults, String pageToken) throws Fireb
190201 builder .put ("nextPageToken" , pageToken );
191202 }
192203
193- DownloadAccountResponse response = post (
194- "downloadAccount" , builder .build (), DownloadAccountResponse .class );
204+ GenericUrl url = new GenericUrl (baseUrl + "/accounts:batchGet" );
205+ url .putAll (builder .build ());
206+ DownloadAccountResponse response = sendRequest (
207+ "GET" , url , null , DownloadAccountResponse .class );
195208 if (response == null ) {
196209 throw new FirebaseAuthException (INTERNAL_ERROR , "Failed to retrieve users." );
197210 }
@@ -200,7 +213,8 @@ DownloadAccountResponse listUsers(int maxResults, String pageToken) throws Fireb
200213
201214 UserImportResult importUsers (UserImportRequest request ) throws FirebaseAuthException {
202215 checkNotNull (request );
203- UploadAccountResponse response = post ("uploadAccount" , request , UploadAccountResponse .class );
216+ UploadAccountResponse response = post (
217+ "/accounts:batchCreate" , request , UploadAccountResponse .class );
204218 if (response == null ) {
205219 throw new FirebaseAuthException (INTERNAL_ERROR , "Failed to import users." );
206220 }
@@ -211,7 +225,7 @@ String createSessionCookie(String idToken,
211225 SessionCookieOptions options ) throws FirebaseAuthException {
212226 final Map <String , Object > payload = ImmutableMap .<String , Object >of (
213227 "idToken" , idToken , "validDuration" , options .getExpiresInSeconds ());
214- GenericJson response = post ("createSessionCookie" , payload , GenericJson .class );
228+ GenericJson response = post (": createSessionCookie" , payload , GenericJson .class );
215229 if (response != null ) {
216230 String cookie = (String ) response .get ("sessionCookie" );
217231 if (!Strings .isNullOrEmpty (cookie )) {
@@ -223,14 +237,22 @@ String createSessionCookie(String idToken,
223237
224238 private <T > T post (String path , Object content , Class <T > clazz ) throws FirebaseAuthException {
225239 checkArgument (!Strings .isNullOrEmpty (path ), "path must not be null or empty" );
226- checkNotNull (content , "content must not be null" );
227- checkNotNull (clazz , "response class must not be null" );
240+ checkNotNull (content , "content must not be null for POST requests" );
241+ GenericUrl url = new GenericUrl (baseUrl + path );
242+ return sendRequest ("POST" , url , content , clazz );
243+ }
228244
229- GenericUrl url = new GenericUrl (ID_TOOLKIT_URL + path );
245+ private <T > T sendRequest (
246+ String method , GenericUrl url ,
247+ @ Nullable Object content , Class <T > clazz ) throws FirebaseAuthException {
248+
249+ checkArgument (!Strings .isNullOrEmpty (method ), "method must not be null or empty" );
250+ checkNotNull (url , "url must not be null" );
251+ checkNotNull (clazz , "response class must not be null" );
230252 HttpResponse response = null ;
231253 try {
232- HttpRequest request = requestFactory . buildPostRequest ( url ,
233- new JsonHttpContent ( jsonFactory , content ) );
254+ HttpContent httpContent = content != null ? new JsonHttpContent ( jsonFactory , content ) : null ;
255+ HttpRequest request = requestFactory . buildRequest ( method , url , httpContent );
234256 request .setParser (new JsonObjectParser (jsonFactory ));
235257 request .getHeaders ().set (CLIENT_VERSION_HEADER , clientVersion );
236258 request .setResponseInterceptor (interceptor );
0 commit comments