Skip to content

Revoke tokens and verify check revoked snippets #21

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 14 commits into from
Feb 13, 2018
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.6.0'
compile 'com.google.firebase:firebase-admin:5.9.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,44 @@ public static void verifyIdToken(String idToken) throws InterruptedException, Ex
System.out.println("Decoded ID token from user: " + uid);
}

public static void verifyIdTokenCheckRevoked(String idToken) throws InterruptedException, ExecutionException {
// [START verify_id_token_check_revoked]
try {
// Verify the ID token while checking if the token is revoked by passing checkRevoked
// as true.
boolean checkRevoked = true;
FirebaseToken decodedToken = FirebaseAuth.getInstance().verifyIdTokenAsync(idToken, checkRevoked).get();
// Token is valid and not revoked.
String uid = decodedToken.getUid();
}
catch (FirebaseAuthException e) {
if ("id-token-revoked".equals(e.getErrorCode())) {
// Token is valid but has been revoked.
// When this occurs, inform the user to reauthenticate or signOut() the user.
} else {
// Error is other than "revoked" token is invalid.
}
}
// [END verify_id_token_check_revoked]
}

public static void revokeIdTokens(String idToken) throws InterruptedException, ExecutionException {
String uid="someUid";
// [START revoke_tokens]
FirebaseToken decodedToken = FirebaseAuth.getInstance().revokeRefreshTokens(uid).get();
UserRecord user = FirebaseAuth.getInstance().getUserAsync(uid).get();
// Convert to seconds as the auth_time in the token claims is in seconds too.
long revocationSecond = user.getTokensValidAfterTimestamp() / 1000;
System.err.println("Tokens revoked at: " + revocationSecond);
// [END revoke_tokens]

// [START save_revocation_in_db]
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("metadata/" + uid);
ref.setValueAsync(MapBuilder.of("revokeTime", revocationSecond)).get();
// [END save_revocation_in_db]

}

public static void main(String[] args) throws InterruptedException, ExecutionException {
System.out.println("Hello, AuthSnippets!");

Expand Down