Skip to content

Adding Java samples for listUsersAsync() and setCustomUserClaimsAsync() #15

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 1 commit into from
Dec 7, 2017
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
2 changes: 1 addition & 1 deletion auth/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ repositories {

dependencies {
// Firebase Java SDK
compile 'com.google.firebase:firebase-admin:5.3.1'
compile 'com.google.firebase:firebase-admin:5.6.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.auth.ExportedUserRecord;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseToken;
import com.google.firebase.auth.ListUsersPage;
import com.google.firebase.auth.UserRecord;
import com.google.firebase.auth.UserRecord.CreateRequest;
import com.google.firebase.auth.UserRecord.UpdateRequest;
Expand Down Expand Up @@ -91,6 +93,81 @@ public static void updateUser(String uid) throws InterruptedException, Execution
// [END update_user]
}

public static void setCustomUserClaims(
String uid) throws InterruptedException, ExecutionException {
// [START set_custom_user_claims]
// Set admin privilege on the user corresponding to uid.
Map<String, Object> claims = new HashMap<>();
claims.put("admin", true);
FirebaseAuth.getInstance().setCustomClaimsAsync(uid, claims).get();
// The new custom claims will propagate to the user's ID token the
// next time a new one is issued.
// [END set_custom_user_claims]

String idToken = "id_token";
// [START verify_custom_claims]
// Verify the ID token first.
FirebaseToken decoded = FirebaseAuth.getInstance().verifyIdTokenAsync(idToken).get();
if (Boolean.TRUE.equals(decoded.getClaims().get("admin"))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Boolean.TRUE.equals seems a bit strange. Why not just if(decoded.getClaims().get("admin"))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getClaims() returns a Map<String, Object>. So when querying the map we need to deal with possible null values (if the key does not exist), and the returned value not being boolean. Boolean.TRUE.equals() provides a one-liner that deals with both cases.

// Allow access to requested admin resource.
}
// [END verify_custom_claims]

// [START read_custom_user_claims]
// Lookup the user associated with the specified uid.
UserRecord user = FirebaseAuth.getInstance().getUserAsync(uid).get();
System.out.println(user.getCustomClaims().get("admin"));
// [END read_custom_user_claims]
}

public static void setCustomUserClaimsScript() throws InterruptedException, ExecutionException {
// [START set_custom_user_claims_script]
UserRecord user = FirebaseAuth.getInstance()
.getUserByEmailAsync("[email protected]").get();
// Confirm user is verified.
if (user.isEmailVerified()) {
Map<String, Object> claims = new HashMap<>();
claims.put("admin", true);
FirebaseAuth.getInstance().setCustomClaimsAsync(user.getUid(), claims).get();
}
// [END set_custom_user_claims_script]
}

public static void setCustomUserClaimsInc() throws InterruptedException, ExecutionException {
// [START set_custom_user_claims_incremental]
UserRecord user = FirebaseAuth.getInstance()
.getUserByEmailAsync("[email protected]").get();
// Add incremental custom claim without overwriting the existing claims.
Map<String, Object> currentClaims = user.getCustomClaims();
if (Boolean.TRUE.equals(currentClaims.get("admin"))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

// Add level.
currentClaims.put("level", 10);
// Add custom claims for additional privileges.
FirebaseAuth.getInstance().setCustomClaimsAsync(user.getUid(), currentClaims).get();
}
// [END set_custom_user_claims_incremental]
}

public static void listAllUsers() throws InterruptedException, ExecutionException {
// [START list_all_users]
// Start listing users from the beginning, 1000 at a time.
ListUsersPage page = FirebaseAuth.getInstance().listUsersAsync(null).get();
while (page != null) {
for (ExportedUserRecord user : page.getValues()) {
System.out.println("User: " + user.getUid());
}
page = page.getNextPage();
}

// Iterate through all users. This will still retrieve users in batches,
// buffering no more than 1000 users in memory at a time.
page = FirebaseAuth.getInstance().listUsersAsync(null).get();
for (ExportedUserRecord user : page.iterateAll()) {
System.out.println("User: " + user.getUid());
}
// [END list_all_users]
}

public static void deleteUser(String uid) throws InterruptedException, ExecutionException {
// [START delete_user]
FirebaseAuth.getInstance().deleteUserAsync(uid).get();
Expand Down Expand Up @@ -155,6 +232,8 @@ public static void main(String[] args) throws InterruptedException, ExecutionExc
getUserByEmail("[email protected]");
getUserByPhoneNumber("+11234567890");
updateUser("some-uid");
//setCustomUserClaims("some-uid");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this commented out on purpose ?

listAllUsers();
deleteUser("some-uid");
createCustomToken();
createCustomTokenWithClaims();
Expand Down