Skip to content

Add iat to fake access token payload #1022

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 7 commits into from
Jul 20, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
81 changes: 49 additions & 32 deletions packages/testing/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,25 @@ import * as fs from 'fs';
import { FirebaseApp, FirebaseOptions } from '@firebase/app-types';
import { base64 } from '@firebase/util';

/** The default url for the local database emulator. */
const DBURL = 'http://localhost:9000';

class FakeCredentials {
getAccessToken() {
return Promise.resolve({
expires_in: 1000000,
access_token: 'owner'
});
}
getCertificate() {
return null;
}
/** Passing this in tells the emulator to treat you as an admin. */
const ADMIN_TOKEN = 'owner';
/** Create an unsecured JWT for the given auth payload. See https://tools.ietf.org/html/rfc7519#section-6. */
function createUnsecuredJwt(auth: object): string {
// Unsecured JWTs use "none" as the algorithm.
const header = {
alg: 'none',
kid: 'fakekid'
};
(auth as any).iat = 0;
// Unsecured JWTs use the empty string as a signature.
const signature = '';
return [
base64.encodeString(JSON.stringify(header), /*webSafe=*/ false),
base64.encodeString(JSON.stringify(auth), /*webSafe=*/ false),
signature
].join('.');
}

export function apps(): (FirebaseApp | null)[] {
Expand All @@ -43,39 +50,49 @@ export type AppOptions = {
projectId?: string;
auth: object;
};
/** Construct a FirebaseApp authenticated with options.auth. */
export function initializeTestApp(options: AppOptions): FirebaseApp {
let appOptions: FirebaseOptions;
if ('databaseName' in options) {
const app = initializeApp(options.databaseName, options.projectId);
// hijacking INTERNAL.getToken to bypass FirebaseAuth and allows specifying of auth headers
const unsecuredJwt = createUnsecuredJwt(options.auth);
(app as any).INTERNAL.getToken = () =>
Promise.resolve({ accessToken: unsecuredJwt });
return app;
}

export type AdminAppOptions = {
databaseName?: string;
projectId?: string;
};
/** Construct a FirebaseApp authenticated as an admin user. */
export function initializeAdminApp(options: AdminAppOptions): FirebaseApp {
const app = initializeApp(options.databaseName, options.projectId);
// hijacking INTERNAL.getToken to bypass FirebaseAuth and allows specifying of auth headers
(app as any).INTERNAL.getToken = () =>
Promise.resolve({ accessToken: ADMIN_TOKEN });
return app;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

can you combine the logic for initializeTestApp and initializeAdminApp since they're essentially the same thing with a different accessToken


function initializeApp(databaseName?: string, projectId?: string): FirebaseApp {
let appOptions: FirebaseOptions = {};
if (databaseName) {
appOptions = {
databaseURL: DBURL + '?ns=' + options.databaseName
databaseURL: DBURL + '?ns=' + databaseName
};
} else if ('projectId' in options) {
} else if (projectId) {
appOptions = {
projectId: options.projectId
projectId: projectId
};
} else {
throw new Error('neither databaseName or projectId were specified');
}
const header = {
alg: 'RS256',
kid: 'fakekid'
};
const fakeToken = [
base64.encodeString(JSON.stringify(header), /*webSafe=*/ false),
base64.encodeString(JSON.stringify(options.auth), /*webSafe=*/ false),
'fakesignature'
].join('.');
const appName = 'app-' + new Date().getTime() + '-' + Math.random();
const app = firebase.initializeApp(appOptions, appName);
// hijacking INTERNAL.getToken to bypass FirebaseAuth and allows specifying of auth headers
(app as any).INTERNAL.getToken = () =>
Promise.resolve({ accessToken: fakeToken });
return app;
return firebase.initializeApp(appOptions, appName);
}

export type LoadDatabaseRulesOptions = {
databaseName: String;
rules: String;
databaseName: string;
rules: string;
rulesPath: fs.PathLike;
};
export function loadDatabaseRules(options: LoadDatabaseRulesOptions): void {
Expand Down
2 changes: 1 addition & 1 deletion packages/testing/test/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ describe('Testing Module Tests', function() {
expect(claims).to.equal('{"uid":"alice"}');
});

it('initializeTestApp() with FirestoreAppOptions uses specified auth.', async function() {
it('initializeTestApp() uses specified auth.', async function() {
let app = firebase.initializeTestApp({
projectId: 'foo',
auth: {}
Expand Down