Skip to content

Commit a65cd9a

Browse files
abeisgoatyuchenshi
andauthored
Move from manual JWT creation to jsonwebtoken (#5002)
* Move from manual JWT creation to jsonwebtoken * Adds jsonwebtoken types * Another type fix attempt * Replace missing token headers * Revert jwt dep * Add manual trimming * Fix trimming padding. Co-authored-by: Yuchen Shi <[email protected]>
1 parent 35865ee commit a65cd9a

File tree

1 file changed

+8
-3
lines changed
  • packages/rules-unit-testing/src/api

1 file changed

+8
-3
lines changed

packages/rules-unit-testing/src/api/index.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import * as request from 'request';
2727
import { base64 } from '@firebase/util';
2828
import { setLogLevel, LogLevel } from '@firebase/logger';
2929
import { Component, ComponentType } from '@firebase/component';
30+
import { base64Encode } from '@firebase/util';
3031

3132
const { firestore, database, storage } = firebase;
3233
export { firestore, database, storage };
@@ -157,6 +158,11 @@ export type FirebaseEmulatorOptions = {
157158
};
158159
};
159160

161+
function trimmedBase64Encode(val: string): string {
162+
// Use base64url encoding and remove padding in the end (dot characters).
163+
return base64Encode(val).replace(/\./g, "");
164+
}
165+
160166
function createUnsecuredJwt(token: TokenOptions, projectId?: string): string {
161167
// Unsecured JWTs use "none" as the algorithm.
162168
const header = {
@@ -198,12 +204,11 @@ function createUnsecuredJwt(token: TokenOptions, projectId?: string): string {
198204
// Unsecured JWTs use the empty string as a signature.
199205
const signature = '';
200206
return [
201-
base64.encodeString(JSON.stringify(header), /*webSafe=*/ false),
202-
base64.encodeString(JSON.stringify(payload), /*webSafe=*/ false),
207+
trimmedBase64Encode(JSON.stringify(header)),
208+
trimmedBase64Encode(JSON.stringify(payload)),
203209
signature
204210
].join('.');
205211
}
206-
207212
export function apps(): firebase.app.App[] {
208213
return firebase.apps;
209214
}

0 commit comments

Comments
 (0)