-
Notifications
You must be signed in to change notification settings - Fork 902
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
Fix auth_time in Auth Emulator. #3674
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
- Fixes Auth Emulator tokens having invalid auth_time field. (#3674) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1709,9 +1709,9 @@ function generateJwt( | |
} | ||
} | ||
|
||
const customAttributes = JSON.parse(user.customAttributes || "{}"); | ||
const customAttributes = JSON.parse(user.customAttributes || "{}") as Record<string, unknown>; | ||
/* eslint-disable camelcase */ | ||
const customPayloadFields: FirebaseJwtPayload = { | ||
const customPayloadFields: Partial<FirebaseJwtPayload> = { | ||
// Non-reserved fields (set before custom attributes): | ||
name: user.displayName, | ||
picture: user.photoUrl, | ||
|
@@ -1726,12 +1726,7 @@ function generateJwt( | |
// This field is only set for anonymous sign-in but not for any other | ||
// provider (such as email or Google) in production. Let's match that. | ||
provider_id: signInProvider === "anonymous" ? signInProvider : undefined, | ||
auth_time: | ||
user.lastLoginAt != null | ||
? toUnixTimestamp(new Date(user.lastLoginAt)) | ||
: user.lastRefreshAt != null | ||
? toUnixTimestamp(new Date(user.lastRefreshAt)) | ||
: toUnixTimestamp(new Date()), | ||
auth_time: toUnixTimestamp(getAuthTime(user)), | ||
user_id: user.localId, | ||
firebase: { | ||
identities, | ||
|
@@ -1755,6 +1750,27 @@ function generateJwt( | |
return jwtStr; | ||
} | ||
|
||
function getAuthTime(user: UserInfo): Date { | ||
if (user.lastLoginAt != null) { | ||
const millisSinceEpoch = parseInt(user.lastLoginAt, 10); | ||
const authTime = new Date(millisSinceEpoch); | ||
if (isNaN(authTime.getTime())) { | ||
throw new Error(`Internal assertion error: invalid user.lastLoginAt = ${user.lastLoginAt}`); | ||
} | ||
return authTime; | ||
} else if (user.lastRefreshAt != null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
const authTime = new Date(user.lastRefreshAt); // Parse from ISO date string. | ||
if (isNaN(authTime.getTime())) { | ||
throw new Error( | ||
`Internal assertion error: invalid user.lastRefreshAt = ${user.lastRefreshAt}` | ||
); | ||
} | ||
return authTime; | ||
} else { | ||
throw new Error(`Internal assertion error: Missing user.lastLoginAt and user.lastRefreshAt`); | ||
} | ||
} | ||
|
||
function verifyPhoneNumber(state: ProjectState, sessionInfo: string, code: string): string { | ||
const verification = state.getVerificationCodeBySessionInfo(sessionInfo); | ||
assert(verification, "INVALID_SESSION_INFO"); | ||
|
@@ -2127,12 +2143,17 @@ export interface FirebaseJwtPayload { | |
exp: number; // expiresAt (in seconds since epoch) | ||
iss: string; // issuer | ||
aud: string; // audience (=projectId) | ||
auth_time: number; // lastLoginAt (in seconds since epoch) | ||
// ...and other fields that we don't care for now. | ||
|
||
// Firebase-specific fields: | ||
|
||
// the last login time (in seconds since epoch), may be different from iat | ||
auth_time: number; | ||
email?: string; | ||
email_verified?: boolean; | ||
phone_number?: string; | ||
name?: string; | ||
picture?: string; | ||
user_id: string; | ||
provider_id?: string; | ||
firebase: { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
!==
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.
This is intentional since it can be
undefined
. TS won't let me do!== null