Skip to content

Commit

Permalink
Move from manual JWT creation to jsonwebtoken (#5002)
Browse files Browse the repository at this point in the history
* 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 <yuchenshi@google.com>
  • Loading branch information
abeisgoat and yuchenshi authored Aug 9, 2021
1 parent 35865ee commit a65cd9a
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions packages/rules-unit-testing/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import * as request from 'request';
import { base64 } from '@firebase/util';
import { setLogLevel, LogLevel } from '@firebase/logger';
import { Component, ComponentType } from '@firebase/component';
import { base64Encode } from '@firebase/util';

const { firestore, database, storage } = firebase;
export { firestore, database, storage };
Expand Down Expand Up @@ -157,6 +158,11 @@ export type FirebaseEmulatorOptions = {
};
};

function trimmedBase64Encode(val: string): string {
// Use base64url encoding and remove padding in the end (dot characters).
return base64Encode(val).replace(/\./g, "");
}

function createUnsecuredJwt(token: TokenOptions, projectId?: string): string {
// Unsecured JWTs use "none" as the algorithm.
const header = {
Expand Down Expand Up @@ -198,12 +204,11 @@ function createUnsecuredJwt(token: TokenOptions, projectId?: string): string {
// 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),
trimmedBase64Encode(JSON.stringify(header)),
trimmedBase64Encode(JSON.stringify(payload)),
signature
].join('.');
}

export function apps(): firebase.app.App[] {
return firebase.apps;
}
Expand Down

0 comments on commit a65cd9a

Please sign in to comment.