|
3 | 3 | import com.google.auth.oauth2.GoogleCredentials;
|
4 | 4 | import com.google.firebase.FirebaseApp;
|
5 | 5 | import com.google.firebase.FirebaseOptions;
|
| 6 | +import com.google.firebase.auth.ExportedUserRecord; |
6 | 7 | import com.google.firebase.auth.FirebaseAuth;
|
7 | 8 | import com.google.firebase.auth.FirebaseToken;
|
| 9 | +import com.google.firebase.auth.ListUsersPage; |
8 | 10 | import com.google.firebase.auth.UserRecord;
|
9 | 11 | import com.google.firebase.auth.UserRecord.CreateRequest;
|
10 | 12 | import com.google.firebase.auth.UserRecord.UpdateRequest;
|
@@ -91,6 +93,81 @@ public static void updateUser(String uid) throws InterruptedException, Execution
|
91 | 93 | // [END update_user]
|
92 | 94 | }
|
93 | 95 |
|
| 96 | + public static void setCustomUserClaims( |
| 97 | + String uid) throws InterruptedException, ExecutionException { |
| 98 | + // [START set_custom_user_claims] |
| 99 | + // Set admin privilege on the user corresponding to uid. |
| 100 | + Map<String, Object> claims = new HashMap<>(); |
| 101 | + claims.put("admin", true); |
| 102 | + FirebaseAuth.getInstance().setCustomClaimsAsync(uid, claims).get(); |
| 103 | + // The new custom claims will propagate to the user's ID token the |
| 104 | + // next time a new one is issued. |
| 105 | + // [END set_custom_user_claims] |
| 106 | + |
| 107 | + String idToken = "id_token"; |
| 108 | + // [START verify_custom_claims] |
| 109 | + // Verify the ID token first. |
| 110 | + FirebaseToken decoded = FirebaseAuth.getInstance().verifyIdTokenAsync(idToken).get(); |
| 111 | + if (Boolean.TRUE.equals(decoded.getClaims().get("admin"))) { |
| 112 | + // Allow access to requested admin resource. |
| 113 | + } |
| 114 | + // [END verify_custom_claims] |
| 115 | + |
| 116 | + // [START read_custom_user_claims] |
| 117 | + // Lookup the user associated with the specified uid. |
| 118 | + UserRecord user = FirebaseAuth.getInstance().getUserAsync(uid).get(); |
| 119 | + System.out.println(user.getCustomClaims().get("admin")); |
| 120 | + // [END read_custom_user_claims] |
| 121 | + } |
| 122 | + |
| 123 | + public static void setCustomUserClaimsScript() throws InterruptedException, ExecutionException { |
| 124 | + // [START set_custom_user_claims_script] |
| 125 | + UserRecord user = FirebaseAuth.getInstance() |
| 126 | + . getUserByEmailAsync( "[email protected]"). get(); |
| 127 | + // Confirm user is verified. |
| 128 | + if (user.isEmailVerified()) { |
| 129 | + Map<String, Object> claims = new HashMap<>(); |
| 130 | + claims.put("admin", true); |
| 131 | + FirebaseAuth.getInstance().setCustomClaimsAsync(user.getUid(), claims).get(); |
| 132 | + } |
| 133 | + // [END set_custom_user_claims_script] |
| 134 | + } |
| 135 | + |
| 136 | + public static void setCustomUserClaimsInc() throws InterruptedException, ExecutionException { |
| 137 | + // [START set_custom_user_claims_incremental] |
| 138 | + UserRecord user = FirebaseAuth.getInstance() |
| 139 | + . getUserByEmailAsync( "[email protected]"). get(); |
| 140 | + // Add incremental custom claim without overwriting the existing claims. |
| 141 | + Map<String, Object> currentClaims = user.getCustomClaims(); |
| 142 | + if (Boolean.TRUE.equals(currentClaims.get("admin"))) { |
| 143 | + // Add level. |
| 144 | + currentClaims.put("level", 10); |
| 145 | + // Add custom claims for additional privileges. |
| 146 | + FirebaseAuth.getInstance().setCustomClaimsAsync(user.getUid(), currentClaims).get(); |
| 147 | + } |
| 148 | + // [END set_custom_user_claims_incremental] |
| 149 | + } |
| 150 | + |
| 151 | + public static void listAllUsers() throws InterruptedException, ExecutionException { |
| 152 | + // [START list_all_users] |
| 153 | + // Start listing users from the beginning, 1000 at a time. |
| 154 | + ListUsersPage page = FirebaseAuth.getInstance().listUsersAsync(null).get(); |
| 155 | + while (page != null) { |
| 156 | + for (ExportedUserRecord user : page.getValues()) { |
| 157 | + System.out.println("User: " + user.getUid()); |
| 158 | + } |
| 159 | + page = page.getNextPage(); |
| 160 | + } |
| 161 | + |
| 162 | + // Iterate through all users. This will still retrieve users in batches, |
| 163 | + // buffering no more than 1000 users in memory at a time. |
| 164 | + page = FirebaseAuth.getInstance().listUsersAsync(null).get(); |
| 165 | + for (ExportedUserRecord user : page.iterateAll()) { |
| 166 | + System.out.println("User: " + user.getUid()); |
| 167 | + } |
| 168 | + // [END list_all_users] |
| 169 | + } |
| 170 | + |
94 | 171 | public static void deleteUser(String uid) throws InterruptedException, ExecutionException {
|
95 | 172 | // [START delete_user]
|
96 | 173 | FirebaseAuth.getInstance().deleteUserAsync(uid).get();
|
@@ -155,6 +232,8 @@ public static void main(String[] args) throws InterruptedException, ExecutionExc
|
155 | 232 | getUserByEmail( "[email protected]");
|
156 | 233 | getUserByPhoneNumber("+11234567890");
|
157 | 234 | updateUser("some-uid");
|
| 235 | + //setCustomUserClaims("some-uid"); |
| 236 | + listAllUsers(); |
158 | 237 | deleteUser("some-uid");
|
159 | 238 | createCustomToken();
|
160 | 239 | createCustomTokenWithClaims();
|
|
0 commit comments