-
Notifications
You must be signed in to change notification settings - Fork 299
Revoke Refresh Tokens #133
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
Conversation
hiranya911
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mostly good. Few changes needed.
| String uid = firebaseToken.getUid(); | ||
| UserRecord user = userManager.getUserById(uid); | ||
| if (user.getTokensValidAfterTimestamp() | ||
| > ((long)firebaseToken.getClaims().get("iat")) * 1000) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extract to a separate line for clarity
| String uid = firebaseToken.getUid(); | ||
| UserRecord user = userManager.getUserById(uid); | ||
| if (user.getTokensValidAfterTimestamp() | ||
| > ((long)firebaseToken.getClaims().get("iat")) * 1000) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Add space between (long) and firebaseToken
| UserRecord user = userManager.getUserById(uid); | ||
| if (user.getTokensValidAfterTimestamp() | ||
| > ((long)firebaseToken.getClaims().get("iat")) * 1000) { | ||
| throw new FirebaseAuthException("id-token-revoked", "Firebase auth token revoked"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add package-level constant for the error code
| * Revokes all refresh tokens for the specified user. | ||
| * | ||
| * <p>In addition to revoking all refresh tokens for a user, all ID tokens issued | ||
| * before revocation will also be revoked at the Auth backend. Any request with an |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this is incorrect. Please align with the documentation of Node.js SDK.
| * associated with this FirebaseAuth instance (which by default is extracted from your service | ||
| * account) | ||
| * | ||
| * <p>If a request was made to check revoked, the issued-at property of the token (like all |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Way too much implementation details here. Just mention something like: "If checkRevoked is true, additionally checks if the token has been revoked."
| } | ||
|
|
||
| @Test | ||
| public void testVerifyIDToken() throws Exception { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
testVerifyIdToken
| assertEquals("user_ver", decoded.getUid()); | ||
| decoded = auth.verifyIdTokenAsync(idToken, true).get(); | ||
| assertEquals("user_ver", decoded.getUid()); | ||
| Thread.sleep(1100); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why 1100?
| fail("expecting exception"); | ||
| } catch (ExecutionException e) { | ||
| assertTrue(e.getCause() instanceof FirebaseAuthException); | ||
| assertEquals("id-token-revoked", ((FirebaseAuthException) e.getCause()).getErrorCode()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test against the package constant when you have it.
| import com.google.api.client.json.JsonFactory; | ||
| import com.google.common.collect.ImmutableList; | ||
| import com.google.common.collect.ImmutableMap; | ||
| import com.google.firebase.auth.UserRecord.UpdateRequest; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes in this file seem unnecessary (just imports)?
On the other hand, why don't we have unit tests for the change in UserRecord class?
|
|
||
| @Test | ||
| public void testVerifyIDToken() throws Exception { | ||
| String customToken = auth.createCustomTokenAsync("user_ver").get(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use "user2" or something to be consistent with other tests.
hiranya911
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with some nits. Please address them prior to merging.
| * <p>In addition to revoking all refresh tokens for a user, all ID tokens issued | ||
| * before revocation will also be revoked at the Auth backend. Any request with an | ||
| * ID token generated before revocation will be rejected with a token expired error. | ||
| * <p>Updates the user's tokensValidAfterTimestamp to the current UTC second expressed in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/second/time/
| * parsed version of the token from which the UID and other claims in the token can be inspected. | ||
| * If the token is invalid, the future throws an exception indicating the failure. | ||
| * | ||
| * <p>This does not check whether a token has been revoked, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
has been revoked. See....
|
|
||
| /** | ||
| * Returns the timestamp beginning with which tokens are valid in seconds since the epoch. | ||
| * Returns the timestamp beginning with which tokens are valid in milliseconds since the epoch. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Returns a timestamp in milliseconds since epoch, truncated down to the closest second. Tokens minted before this timestamp are considered invalid.
| private static void checkValidSince(long epochSeconds) { | ||
| checkArgument(epochSeconds > 0, | ||
| "validSince must be greater than 0 in seconds since the epoch: " | ||
| + Long.toString(epochSeconds)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unnecessary wrapping.
|
|
||
| private static void checkValidSince(long epochSeconds) { | ||
| checkArgument(epochSeconds > 0, | ||
| "validSince must be greater than 0 in seconds since the epoch: " |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Formatting/indentation messed up here.
| // expected | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add new line at eof
| UserRecord user = userManager.getUserById(uid); | ||
| long issuedAt = (long) firebaseToken.getClaims().get("iat"); | ||
| if (user.getTokensValidAfterTimestamp() > issuedAt * 1000) { | ||
| throw new FirebaseAuthException(FirebaseUserManager.ID_TOKEN_REVOKED_ERROR, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrong indentation?
|
|
||
| private Task<Void> revokeRefreshTokens(String uid) { | ||
| checkNotDestroyed(); | ||
| final UpdateRequest request = new UpdateRequest(uid).setValidSince( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit:
int currentTimeSeconds = (int) (System.currentTimeMillis() / 1000);
initial PR, adding comments.