Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion src/emulator/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@ export class Constants {
// Environment variable to override SDK/CLI to point at the Firestore emulator.
static FIRESTORE_EMULATOR_HOST = "FIRESTORE_EMULATOR_HOST";

// Environment variable tok override SDK/CLI to point at the Realtime Database emulator.
// Environment variable to override SDK/CLI to point at the Realtime Database emulator.
static FIREBASE_DATABASE_EMULATOR_HOST = "FIREBASE_DATABASE_EMULATOR_HOST";

// Environment variable to override SDK/CLI to point at the Firebase Auth emulator.
static FIREBASE_AUTH_EMULATOR_HOST = "FIREBASE_AUTH_EMULATOR_HOST";

static SERVICE_FIRESTORE = "firestore.googleapis.com";
static SERVICE_REALTIME_DATABASE = "firebaseio.com";
static SERVICE_PUBSUB = "pubsub.googleapis.com";
Expand Down
2 changes: 2 additions & 0 deletions src/emulator/functionsEmulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ export class FunctionsEmulator implements EmulatorInstance {
firestore: this.getEmulatorInfo(Emulators.FIRESTORE),
database: this.getEmulatorInfo(Emulators.DATABASE),
pubsub: this.getEmulatorInfo(Emulators.PUBSUB),
auth: this.getEmulatorInfo(Emulators.AUTH),
},
nodeMajorVersion: this.args.nodeMajorVersion,
proto,
Expand Down Expand Up @@ -666,6 +667,7 @@ export class FunctionsEmulator implements EmulatorInstance {
firestore: EmulatorRegistry.getInfo(Emulators.FIRESTORE),
database: EmulatorRegistry.getInfo(Emulators.DATABASE),
pubsub: EmulatorRegistry.getInfo(Emulators.PUBSUB),
auth: EmulatorRegistry.getInfo(Emulators.AUTH),
},
disabled_features: this.args.disabledRuntimeFeatures,
};
Expand Down
19 changes: 19 additions & 0 deletions src/emulator/functionsEmulatorRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,18 @@ async function initializeFirebaseAdminStubs(frb: FunctionsRuntimeBundle): Promis
// Tell the Firebase Functions SDK to use the proxied app so that things like "change.after.ref"
// point to the right place.
localFunctionsModule.app.setEmulatedAdminApp(defaultApp);

// When the auth emulator is running, try to disable JWT verification.
if (frb.emulators.auth) {
const auth = defaultApp.auth();
if (typeof (auth as any).setJwtVerificationEnabled === "function") {
logDebug("auth.setJwtVerificationEnabled(false)", {});
(auth as any).setJwtVerificationEnabled(false);
} else {
logDebug("auth.setJwtVerificationEnabled not available", {});
}
}

return defaultApp;
})
.when("firestore", (target) => {
Expand Down Expand Up @@ -677,6 +689,13 @@ async function initializeEnvironmentalVariables(frb: FunctionsRuntimeBundle): Pr
] = `${frb.emulators.database.host}:${frb.emulators.database.port}`;
}

// Make firebase-admin point at the Auth emulator
if (frb.emulators.auth) {
process.env[
Constants.FIREBASE_AUTH_EMULATOR_HOST
] = `${frb.emulators.auth.host}:${frb.emulators.auth.port}`;
}

if (frb.emulators.pubsub) {
const pubsubHost = `${frb.emulators.pubsub.host}:${frb.emulators.pubsub.port}`;
process.env.PUBSUB_EMULATOR_HOST = pubsubHost;
Expand Down
4 changes: 4 additions & 0 deletions src/emulator/functionsEmulatorShared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ export interface FunctionsRuntimeBundle {
host: string;
port: number;
};
auth?: {
host: string;
port: number;
};
};
socketPath?: string;
disabled_features?: FunctionsRuntimeFeatures;
Expand Down
25 changes: 25 additions & 0 deletions src/test/emulators/functionsEmulatorRuntime.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,31 @@ describe("FunctionsEmulator-Runtime", () => {
expect(info.databaseURL).to.eql(`https://${frb.projectId}.firebaseio.com`);
}).timeout(TIMEOUT_MED);
});

it("should set FIREBASE_AUTH_EMULATOR_HOST when the emulator is running", async () => {
const frb = _.cloneDeep(FunctionRuntimeBundles.onRequest) as FunctionsRuntimeBundle;
frb.emulators = {
auth: {
host: "localhost",
port: 9099,
},
};

const worker = invokeRuntimeWithFunctions(frb, () => {
return {
function_id: require("firebase-functions").https.onRequest((req: any, res: any) => {
res.json({
var: process.env.FIREBASE_AUTH_EMULATOR_HOST,
});
}),
};
});

const data = await callHTTPSFunction(worker, frb);
const res = JSON.parse(data);

expect(res.var).to.eql("localhost:9099");
}).timeout(TIMEOUT_MED);
});

describe("_InitializeFunctionsConfigHelper()", () => {
Expand Down