Skip to content

Commit 2f5080a

Browse files
authored
Merge pull request #21 from firebase/revoke_tokens
Revoke tokens and verify check revoked snippets
2 parents 457d0ac + 614e4de commit 2f5080a

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

auth/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ repositories {
1616

1717
dependencies {
1818
// Firebase Java SDK
19-
compile 'com.google.firebase:firebase-admin:5.6.0'
19+
compile 'com.google.firebase:firebase-admin:5.9.0'
2020
}

auth/src/main/java/com/google/firebase/quickstart/AuthSnippets.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,44 @@ public static void verifyIdToken(String idToken) throws InterruptedException, Ex
207207
System.out.println("Decoded ID token from user: " + uid);
208208
}
209209

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+
210248
public static void main(String[] args) throws InterruptedException, ExecutionException {
211249
System.out.println("Hello, AuthSnippets!");
212250

0 commit comments

Comments
 (0)