-
Notifications
You must be signed in to change notification settings - Fork 936
Add mockUserToken support for database emulator. #4792
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
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
4f7f840
Add mockUserToken support for database emulator.
yuchenshi 21374c2
Add compact API.
yuchenshi 5fd82c7
Add generated API file.
yuchenshi 4d8b954
Change default project ID to demo-project.
yuchenshi e5dae20
Merge remote-tracking branch 'origin/master' into ys/mockUserToken-da…
yuchenshi 6bdd4e8
Merge remote-tracking branch 'origin/master' into ys/mockUserToken-da…
yuchenshi 9f0b203
Fix db.useEmulator
schmidt-sebastian a829398
Create sweet-monkeys-warn.md
schmidt-sebastian efe6d17
Update packages/database/src/exp/Database.ts
yuchenshi f0b11f0
Update packages/util/test/emulator.test.ts
yuchenshi 82cd88e
Merge remote-tracking branch 'origin/mrschmidt/emulator' into ys/mock…
yuchenshi ff24362
Fix sub field name.
yuchenshi 05c383e
Remove optional in jsdocs.
yuchenshi 9d99437
Create loud-feet-jump.md
yuchenshi f3aebe0
Update loud-feet-jump.md
yuchenshi a684219
Make sub/user_id required in typing.
yuchenshi 6238b8b
Update error messages to contain mockUserToken.
yuchenshi 2059715
Merge branch 'master' into ys/mockUserToken-database
yuchenshi 3a1cd98
Add API changes in md.
yuchenshi ea53f0c
Update error message for uid field.
yuchenshi c0b51f8
Update loud-feet-jump.md
yuchenshi dc72577
Change custom claim typing to unknown.
yuchenshi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,7 @@ | ||
--- | ||
"@firebase/database": minor | ||
"firebase": minor | ||
"@firebase/util": minor | ||
--- | ||
|
||
Add mockUserToken support for database emulator. |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,142 @@ | ||
/** | ||
* @license | ||
* Copyright 2021 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { base64 } from './crypt'; | ||
|
||
// Firebase Auth tokens contain snake_case claims following the JWT standard / convention. | ||
/* eslint-disable camelcase */ | ||
|
||
export type FirebaseSignInProvider = | ||
| 'custom' | ||
| 'email' | ||
| 'password' | ||
| 'phone' | ||
| 'anonymous' | ||
| 'google.com' | ||
| 'facebook.com' | ||
| 'github.com' | ||
| 'twitter.com' | ||
| 'microsoft.com' | ||
| 'apple.com'; | ||
|
||
interface FirebaseIdToken { | ||
// Always set to https://securetoken.google.com/PROJECT_ID | ||
iss: string; | ||
|
||
// Always set to PROJECT_ID | ||
aud: string; | ||
|
||
// The user's unique id | ||
sub: string; | ||
|
||
// The token issue time, in seconds since epoch | ||
iat: number; | ||
|
||
// The token expiry time, normally 'iat' + 3600 | ||
exp: number; | ||
|
||
// The user's unique id, must be equal to 'sub' | ||
user_id: string; | ||
|
||
// The time the user authenticated, normally 'iat' | ||
auth_time: number; | ||
|
||
// The sign in provider, only set when the provider is 'anonymous' | ||
provider_id?: 'anonymous'; | ||
|
||
// The user's primary email | ||
email?: string; | ||
|
||
// The user's email verification status | ||
email_verified?: boolean; | ||
|
||
// The user's primary phone number | ||
phone_number?: string; | ||
|
||
// The user's display name | ||
name?: string; | ||
|
||
// The user's profile photo URL | ||
picture?: string; | ||
|
||
// Information on all identities linked to this user | ||
firebase: { | ||
// The primary sign-in provider | ||
sign_in_provider: FirebaseSignInProvider; | ||
|
||
// A map of providers to the user's list of unique identifiers from | ||
// each provider | ||
identities?: { [provider in FirebaseSignInProvider]?: string[] }; | ||
}; | ||
|
||
// Custom claims set by the developer | ||
[claim: string]: unknown; | ||
|
||
uid?: never; // Try to catch a common mistake of "uid" (should be "sub" instead). | ||
} | ||
|
||
export type EmulatorMockTokenOptions = ({ user_id: string } | { sub: string }) & | ||
Partial<FirebaseIdToken>; | ||
|
||
export function createMockUserToken( | ||
token: EmulatorMockTokenOptions, | ||
projectId?: string | ||
): string { | ||
if (token.uid) { | ||
yuchenshi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
throw new Error( | ||
'The "uid" field is no longer supported by mockUserToken. Please use "sub" instead for Firebase Auth User ID.' | ||
); | ||
} | ||
// Unsecured JWTs use "none" as the algorithm. | ||
const header = { | ||
alg: 'none', | ||
type: 'JWT' | ||
}; | ||
|
||
const project = projectId || 'demo-project'; | ||
const iat = token.iat || 0; | ||
const sub = token.sub || token.user_id; | ||
if (!sub) { | ||
throw new Error("mockUserToken must contain 'sub' or 'user_id' field!"); | ||
} | ||
|
||
const payload: FirebaseIdToken = { | ||
// Set all required fields to decent defaults | ||
iss: `https://securetoken.google.com/${project}`, | ||
aud: project, | ||
iat, | ||
exp: iat + 3600, | ||
auth_time: iat, | ||
sub, | ||
user_id: sub, | ||
firebase: { | ||
sign_in_provider: 'custom', | ||
identities: {} | ||
}, | ||
|
||
// Override with user options | ||
...token | ||
}; | ||
|
||
// Unsecured JWTs use the empty string as a signature. | ||
const signature = ''; | ||
return [ | ||
base64.encodeString(JSON.stringify(header), /*webSafe=*/ false), | ||
base64.encodeString(JSON.stringify(payload), /*webSafe=*/ false), | ||
signature | ||
].join('.'); | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.