@@ -207,6 +207,44 @@ public static void verifyIdToken(String idToken) throws InterruptedException, Ex
207
207
System .out .println ("Decoded ID token from user: " + uid );
208
208
}
209
209
210
+ public static void verifyIdTokenCheckRevoked (String idToken ) throws InterruptedException , ExecutionException {
211
+ // [START verify_id_token_check_revoked]
212
+ try {
213
+ // Verify the ID token while checking if the token is revoked by passing checkRevoked
214
+ // as true.
215
+ boolean checkRevoked = true ;
216
+ FirebaseToken decodedToken = FirebaseAuth .getInstance ().verifyIdTokenAsync (idToken , checkRevoked ).get ();
217
+ // Token is valid and not revoked.
218
+ String uid = decodedToken .getUid ();
219
+ }
220
+ catch (FirebaseAuthException e ) {
221
+ if ("id-token-revoked" .equals (e .getErrorCode ())) {
222
+ // Token is valid but has been revoked.
223
+ // When this occurs, inform the user to reauthenticate or signOut() the user.
224
+ } else {
225
+ // Error is other than "revoked" token is invalid.
226
+ }
227
+ }
228
+ // [END verify_id_token_check_revoked]
229
+ }
230
+
231
+ public static void revokeIdTokens (String idToken ) throws InterruptedException , ExecutionException {
232
+ String uid ="someUid" ;
233
+ // [START revoke_tokens]
234
+ FirebaseToken decodedToken = FirebaseAuth .getInstance ().revokeRefreshTokens (uid ).get ();
235
+ UserRecord user = FirebaseAuth .getInstance ().getUserAsync (uid ).get ();
236
+ // Convert to seconds as the auth_time in the token claims is in seconds too.
237
+ long revocationSecond = user .getTokensValidAfterTimestamp () / 1000 ;
238
+ System .err .println ("Tokens revoked at: " + revocationSecond );
239
+ // [END revoke_tokens]
240
+
241
+ // [START save_revocation_in_db]
242
+ DatabaseReference ref = FirebaseDatabase .getInstance ().getReference ("metadata/" + uid );
243
+ ref .setValueAsync (MapBuilder .of ("revokeTime" , revocationSecond )).get ();
244
+ // [END save_revocation_in_db]
245
+
246
+ }
247
+
210
248
public static void main (String [] args ) throws InterruptedException , ExecutionException {
211
249
System .out .println ("Hello, AuthSnippets!" );
212
250
0 commit comments