Skip to content

Fix JWT format and interop with Storage Emulator. Fixes #3442. #5002

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 8 commits into from
Aug 9, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs use base64url instead of base64.

export const base64Encode = function (str: string): string {
is the utility for that. I think we still need to trim padding ourselves though.

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