|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2021 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import { base64 } from './crypt'; |
| 19 | + |
| 20 | +// Firebase Auth tokens contain snake_case claims following the JWT standard / convention. |
| 21 | +/* eslint-disable camelcase */ |
| 22 | + |
| 23 | +export type FirebaseSignInProvider = |
| 24 | + | 'custom' |
| 25 | + | 'email' |
| 26 | + | 'password' |
| 27 | + | 'phone' |
| 28 | + | 'anonymous' |
| 29 | + | 'google.com' |
| 30 | + | 'facebook.com' |
| 31 | + | 'github.com' |
| 32 | + | 'twitter.com' |
| 33 | + | 'microsoft.com' |
| 34 | + | 'apple.com'; |
| 35 | + |
| 36 | +interface FirebaseIdToken { |
| 37 | + // Always set to https://securetoken.google.com/PROJECT_ID |
| 38 | + iss: string; |
| 39 | + |
| 40 | + // Always set to PROJECT_ID |
| 41 | + aud: string; |
| 42 | + |
| 43 | + // The user's unique id |
| 44 | + sub: string; |
| 45 | + |
| 46 | + // The token issue time, in seconds since epoch |
| 47 | + iat: number; |
| 48 | + |
| 49 | + // The token expiry time, normally 'iat' + 3600 |
| 50 | + exp: number; |
| 51 | + |
| 52 | + // The user's unique id, must be equal to 'sub' |
| 53 | + user_id: string; |
| 54 | + |
| 55 | + // The time the user authenticated, normally 'iat' |
| 56 | + auth_time: number; |
| 57 | + |
| 58 | + // The sign in provider, only set when the provider is 'anonymous' |
| 59 | + provider_id?: 'anonymous'; |
| 60 | + |
| 61 | + // The user's primary email |
| 62 | + email?: string; |
| 63 | + |
| 64 | + // The user's email verification status |
| 65 | + email_verified?: boolean; |
| 66 | + |
| 67 | + // The user's primary phone number |
| 68 | + phone_number?: string; |
| 69 | + |
| 70 | + // The user's display name |
| 71 | + name?: string; |
| 72 | + |
| 73 | + // The user's profile photo URL |
| 74 | + picture?: string; |
| 75 | + |
| 76 | + // Information on all identities linked to this user |
| 77 | + firebase: { |
| 78 | + // The primary sign-in provider |
| 79 | + sign_in_provider: FirebaseSignInProvider; |
| 80 | + |
| 81 | + // A map of providers to the user's list of unique identifiers from |
| 82 | + // each provider |
| 83 | + identities?: { [provider in FirebaseSignInProvider]?: string[] }; |
| 84 | + }; |
| 85 | + |
| 86 | + // Custom claims set by the developer |
| 87 | + [claim: string]: unknown; |
| 88 | + |
| 89 | + uid?: never; // Try to catch a common mistake of "uid" (should be "sub" instead). |
| 90 | +} |
| 91 | + |
| 92 | +export type EmulatorMockTokenOptions = ({ user_id: string } | { sub: string }) & |
| 93 | + Partial<FirebaseIdToken>; |
| 94 | + |
| 95 | +export function createMockUserToken( |
| 96 | + token: EmulatorMockTokenOptions, |
| 97 | + projectId?: string |
| 98 | +): string { |
| 99 | + if (token.uid) { |
| 100 | + throw new Error( |
| 101 | + 'The "uid" field is no longer supported by mockUserToken. Please use "sub" instead for Firebase Auth User ID.' |
| 102 | + ); |
| 103 | + } |
| 104 | + // Unsecured JWTs use "none" as the algorithm. |
| 105 | + const header = { |
| 106 | + alg: 'none', |
| 107 | + type: 'JWT' |
| 108 | + }; |
| 109 | + |
| 110 | + const project = projectId || 'demo-project'; |
| 111 | + const iat = token.iat || 0; |
| 112 | + const sub = token.sub || token.user_id; |
| 113 | + if (!sub) { |
| 114 | + throw new Error("mockUserToken must contain 'sub' or 'user_id' field!"); |
| 115 | + } |
| 116 | + |
| 117 | + const payload: FirebaseIdToken = { |
| 118 | + // Set all required fields to decent defaults |
| 119 | + iss: `https://securetoken.google.com/${project}`, |
| 120 | + aud: project, |
| 121 | + iat, |
| 122 | + exp: iat + 3600, |
| 123 | + auth_time: iat, |
| 124 | + sub, |
| 125 | + user_id: sub, |
| 126 | + firebase: { |
| 127 | + sign_in_provider: 'custom', |
| 128 | + identities: {} |
| 129 | + }, |
| 130 | + |
| 131 | + // Override with user options |
| 132 | + ...token |
| 133 | + }; |
| 134 | + |
| 135 | + // Unsecured JWTs use the empty string as a signature. |
| 136 | + const signature = ''; |
| 137 | + return [ |
| 138 | + base64.encodeString(JSON.stringify(header), /*webSafe=*/ false), |
| 139 | + base64.encodeString(JSON.stringify(payload), /*webSafe=*/ false), |
| 140 | + signature |
| 141 | + ].join('.'); |
| 142 | +} |
0 commit comments