Skip to content

Commit a1fd2ee

Browse files
authored
Add Auth emulator admin support in Functions emulator (#2727)
1 parent 3706133 commit a1fd2ee

File tree

5 files changed

+54
-1
lines changed

5 files changed

+54
-1
lines changed

src/emulator/constants.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,12 @@ export class Constants {
4646
// Environment variable to override SDK/CLI to point at the Firestore emulator.
4747
static FIRESTORE_EMULATOR_HOST = "FIRESTORE_EMULATOR_HOST";
4848

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

52+
// Environment variable to override SDK/CLI to point at the Firebase Auth emulator.
53+
static FIREBASE_AUTH_EMULATOR_HOST = "FIREBASE_AUTH_EMULATOR_HOST";
54+
5255
static SERVICE_FIRESTORE = "firestore.googleapis.com";
5356
static SERVICE_REALTIME_DATABASE = "firebaseio.com";
5457
static SERVICE_PUBSUB = "pubsub.googleapis.com";

src/emulator/functionsEmulator.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ export class FunctionsEmulator implements EmulatorInstance {
295295
firestore: this.getEmulatorInfo(Emulators.FIRESTORE),
296296
database: this.getEmulatorInfo(Emulators.DATABASE),
297297
pubsub: this.getEmulatorInfo(Emulators.PUBSUB),
298+
auth: this.getEmulatorInfo(Emulators.AUTH),
298299
},
299300
nodeMajorVersion: this.args.nodeMajorVersion,
300301
proto,
@@ -666,6 +667,7 @@ export class FunctionsEmulator implements EmulatorInstance {
666667
firestore: EmulatorRegistry.getInfo(Emulators.FIRESTORE),
667668
database: EmulatorRegistry.getInfo(Emulators.DATABASE),
668669
pubsub: EmulatorRegistry.getInfo(Emulators.PUBSUB),
670+
auth: EmulatorRegistry.getInfo(Emulators.AUTH),
669671
},
670672
disabled_features: this.args.disabledRuntimeFeatures,
671673
};

src/emulator/functionsEmulatorRuntime.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,18 @@ async function initializeFirebaseAdminStubs(frb: FunctionsRuntimeBundle): Promis
526526
// Tell the Firebase Functions SDK to use the proxied app so that things like "change.after.ref"
527527
// point to the right place.
528528
localFunctionsModule.app.setEmulatedAdminApp(defaultApp);
529+
530+
// When the auth emulator is running, try to disable JWT verification.
531+
if (frb.emulators.auth) {
532+
const auth = defaultApp.auth();
533+
if (typeof (auth as any).setJwtVerificationEnabled === "function") {
534+
logDebug("auth.setJwtVerificationEnabled(false)", {});
535+
(auth as any).setJwtVerificationEnabled(false);
536+
} else {
537+
logDebug("auth.setJwtVerificationEnabled not available", {});
538+
}
539+
}
540+
529541
return defaultApp;
530542
})
531543
.when("firestore", (target) => {
@@ -677,6 +689,13 @@ async function initializeEnvironmentalVariables(frb: FunctionsRuntimeBundle): Pr
677689
] = `${frb.emulators.database.host}:${frb.emulators.database.port}`;
678690
}
679691

692+
// Make firebase-admin point at the Auth emulator
693+
if (frb.emulators.auth) {
694+
process.env[
695+
Constants.FIREBASE_AUTH_EMULATOR_HOST
696+
] = `${frb.emulators.auth.host}:${frb.emulators.auth.port}`;
697+
}
698+
680699
if (frb.emulators.pubsub) {
681700
const pubsubHost = `${frb.emulators.pubsub.host}:${frb.emulators.pubsub.port}`;
682701
process.env.PUBSUB_EMULATOR_HOST = pubsubHost;

src/emulator/functionsEmulatorShared.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ export interface FunctionsRuntimeBundle {
6464
host: string;
6565
port: number;
6666
};
67+
auth?: {
68+
host: string;
69+
port: number;
70+
};
6771
};
6872
socketPath?: string;
6973
disabled_features?: FunctionsRuntimeFeatures;

src/test/emulators/functionsEmulatorRuntime.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,31 @@ describe("FunctionsEmulator-Runtime", () => {
434434
expect(info.databaseURL).to.eql(`https://${frb.projectId}.firebaseio.com`);
435435
}).timeout(TIMEOUT_MED);
436436
});
437+
438+
it("should set FIREBASE_AUTH_EMULATOR_HOST when the emulator is running", async () => {
439+
const frb = _.cloneDeep(FunctionRuntimeBundles.onRequest) as FunctionsRuntimeBundle;
440+
frb.emulators = {
441+
auth: {
442+
host: "localhost",
443+
port: 9099,
444+
},
445+
};
446+
447+
const worker = invokeRuntimeWithFunctions(frb, () => {
448+
return {
449+
function_id: require("firebase-functions").https.onRequest((req: any, res: any) => {
450+
res.json({
451+
var: process.env.FIREBASE_AUTH_EMULATOR_HOST,
452+
});
453+
}),
454+
};
455+
});
456+
457+
const data = await callHTTPSFunction(worker, frb);
458+
const res = JSON.parse(data);
459+
460+
expect(res.var).to.eql("localhost:9099");
461+
}).timeout(TIMEOUT_MED);
437462
});
438463

439464
describe("_InitializeFunctionsConfigHelper()", () => {

0 commit comments

Comments
 (0)