Skip to content

Commit 9f7b83b

Browse files
committed
remove opts from beforeemail & decode user record only for beforeCreate and beforeSignIn
1 parent 8a7808a commit 9f7b83b

File tree

4 files changed

+36
-35
lines changed

4 files changed

+36
-35
lines changed

spec/v2/providers/identity.spec.ts

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ import { onInit } from "../../../src/v2/core";
2626
import { MockRequest } from "../../fixtures/mockrequest";
2727
import { runHandler } from "../../helper";
2828

29+
const IDENTITY_TOOLKIT_API = "identitytoolkit.googleapis.com";
30+
const REGION = "us-west1";
31+
2932
const BEFORE_CREATE_TRIGGER = {
3033
eventType: "providers/cloud.auth/eventTypes/user.beforeCreate",
3134
options: {
@@ -46,18 +49,14 @@ const BEFORE_SIGN_IN_TRIGGER = {
4649

4750
const BEFORE_EMAIL_TRIGGER = {
4851
eventType: "providers/cloud.auth/eventTypes/user.beforeSendEmail",
49-
options: {
50-
accessToken: false,
51-
idToken: false,
52-
refreshToken: false,
53-
},
52+
options: {},
5453
};
5554

5655
const opts: identity.BlockingOptions = {
5756
accessToken: true,
5857
refreshToken: false,
5958
minInstances: 1,
60-
region: "us-west1",
59+
region: REGION,
6160
};
6261

6362
describe("identity", () => {
@@ -73,7 +72,7 @@ describe("identity", () => {
7372
});
7473
expect(fn.__requiredAPIs).to.deep.equal([
7574
{
76-
api: "identitytoolkit.googleapis.com",
75+
api: IDENTITY_TOOLKIT_API,
7776
reason: "Needed for auth blocking functions",
7877
},
7978
]);
@@ -87,7 +86,7 @@ describe("identity", () => {
8786
platform: "gcfv2",
8887
labels: {},
8988
minInstances: 1,
90-
region: ["us-west1"],
89+
region: [REGION],
9190
blockingTrigger: {
9291
...BEFORE_CREATE_TRIGGER,
9392
options: {
@@ -98,7 +97,7 @@ describe("identity", () => {
9897
});
9998
expect(fn.__requiredAPIs).to.deep.equal([
10099
{
101-
api: "identitytoolkit.googleapis.com",
100+
api: IDENTITY_TOOLKIT_API,
102101
reason: "Needed for auth blocking functions",
103102
},
104103
]);
@@ -138,7 +137,7 @@ describe("identity", () => {
138137
});
139138
expect(fn.__requiredAPIs).to.deep.equal([
140139
{
141-
api: "identitytoolkit.googleapis.com",
140+
api: IDENTITY_TOOLKIT_API,
142141
reason: "Needed for auth blocking functions",
143142
},
144143
]);
@@ -152,7 +151,7 @@ describe("identity", () => {
152151
platform: "gcfv2",
153152
labels: {},
154153
minInstances: 1,
155-
region: ["us-west1"],
154+
region: [REGION],
156155
blockingTrigger: {
157156
...BEFORE_SIGN_IN_TRIGGER,
158157
options: {
@@ -163,7 +162,7 @@ describe("identity", () => {
163162
});
164163
expect(fn.__requiredAPIs).to.deep.equal([
165164
{
166-
api: "identitytoolkit.googleapis.com",
165+
api: IDENTITY_TOOLKIT_API,
167166
reason: "Needed for auth blocking functions",
168167
},
169168
]);
@@ -203,7 +202,7 @@ describe("identity", () => {
203202
});
204203
expect(fn.__requiredAPIs).to.deep.equal([
205204
{
206-
api: "identitytoolkit.googleapis.com",
205+
api: IDENTITY_TOOLKIT_API,
207206
reason: "Needed for auth blocking functions",
208207
},
209208
]);
@@ -218,7 +217,7 @@ describe("identity", () => {
218217
platform: "gcfv2",
219218
labels: {},
220219
minInstances: 1,
221-
region: ["us-west1"],
220+
region: [REGION],
222221
blockingTrigger: {
223222
...BEFORE_EMAIL_TRIGGER,
224223
options: {
@@ -229,7 +228,7 @@ describe("identity", () => {
229228
});
230229
expect(fn.__requiredAPIs).to.deep.equal([
231230
{
232-
api: "identitytoolkit.googleapis.com",
231+
api: IDENTITY_TOOLKIT_API,
233232
reason: "Needed for auth blocking functions",
234233
},
235234
]);
@@ -247,7 +246,7 @@ describe("identity", () => {
247246
});
248247
expect(fn.__requiredAPIs).to.deep.equal([
249248
{
250-
api: "identitytoolkit.googleapis.com",
249+
api: IDENTITY_TOOLKIT_API,
251250
reason: "Needed for auth blocking functions",
252251
},
253252
]);
@@ -264,7 +263,7 @@ describe("identity", () => {
264263
});
265264
expect(fn.__requiredAPIs).to.deep.equal([
266265
{
267-
api: "identitytoolkit.googleapis.com",
266+
api: IDENTITY_TOOLKIT_API,
268267
reason: "Needed for auth blocking functions",
269268
},
270269
]);
@@ -281,7 +280,7 @@ describe("identity", () => {
281280
});
282281
expect(fn.__requiredAPIs).to.deep.equal([
283282
{
284-
api: "identitytoolkit.googleapis.com",
283+
api: IDENTITY_TOOLKIT_API,
285284
reason: "Needed for auth blocking functions",
286285
},
287286
]);
@@ -295,7 +294,7 @@ describe("identity", () => {
295294
platform: "gcfv2",
296295
labels: {},
297296
minInstances: 1,
298-
region: ["us-west1"],
297+
region: [REGION],
299298
blockingTrigger: {
300299
...BEFORE_CREATE_TRIGGER,
301300
options: {
@@ -306,7 +305,7 @@ describe("identity", () => {
306305
});
307306
expect(fn.__requiredAPIs).to.deep.equal([
308307
{
309-
api: "identitytoolkit.googleapis.com",
308+
api: IDENTITY_TOOLKIT_API,
310309
reason: "Needed for auth blocking functions",
311310
},
312311
]);
@@ -320,7 +319,7 @@ describe("identity", () => {
320319
platform: "gcfv2",
321320
labels: {},
322321
minInstances: 1,
323-
region: ["us-west1"],
322+
region: [REGION],
324323
blockingTrigger: {
325324
...BEFORE_SIGN_IN_TRIGGER,
326325
options: {
@@ -331,7 +330,7 @@ describe("identity", () => {
331330
});
332331
expect(fn.__requiredAPIs).to.deep.equal([
333332
{
334-
api: "identitytoolkit.googleapis.com",
333+
api: IDENTITY_TOOLKIT_API,
335334
reason: "Needed for auth blocking functions",
336335
},
337336
]);
@@ -345,7 +344,7 @@ describe("identity", () => {
345344
platform: "gcfv2",
346345
labels: {},
347346
minInstances: 1,
348-
region: ["us-west1"],
347+
region: [REGION],
349348
blockingTrigger: {
350349
...BEFORE_EMAIL_TRIGGER,
351350
options: {
@@ -356,7 +355,7 @@ describe("identity", () => {
356355
});
357356
expect(fn.__requiredAPIs).to.deep.equal([
358357
{
359-
api: "identitytoolkit.googleapis.com",
358+
api: IDENTITY_TOOLKIT_API,
360359
reason: "Needed for auth blocking functions",
361360
},
362361
]);

src/common/providers/identity.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ export interface AuthBlockingEvent extends AuthEventContext {
349349
/** The reCAPTCHA action options. */
350350
export type RecaptchaActionOptions = "ALLOW" | "BLOCK";
351351

352+
/** The handler response type for `beforeEmailSent` blocking events */
352353
export interface BeforeEmailResponse {
353354
recaptchaActionOverride?: RecaptchaActionOptions;
354355
}
@@ -399,7 +400,6 @@ interface DecodedPayloadUserRecordEnrolledFactors {
399400
export interface DecodedPayloadUserRecord {
400401
uid: string;
401402
email?: string;
402-
email_type?: string;
403403
email_verified?: boolean;
404404
phone_number?: string;
405405
display_name?: string;
@@ -476,8 +476,9 @@ export type HandlerV2 = (
476476
event: AuthBlockingEvent
477477
) => MaybeAsync<BeforeCreateResponse | BeforeSignInResponse | BeforeEmailResponse | void>;
478478

479-
export type AgnosticHandler = (HandlerV1 | HandlerV2) & {
480-
platform: string;
479+
export type AuthBlockingEventHandler = (HandlerV1 | HandlerV2) & {
480+
// Specify the GCF gen of the trigger that the auth blocking event handler was written for
481+
platform: "gcfv1" | "gcfv2";
481482
};
482483

483484
/**
@@ -846,7 +847,7 @@ export function getUpdateMask(authResponse?: BeforeCreateResponse | BeforeSignIn
846847
}
847848

848849
/** @internal */
849-
export function wrapHandler(eventType: AuthBlockingEventType, handler: AgnosticHandler) {
850+
export function wrapHandler(eventType: AuthBlockingEventType, handler: AuthBlockingEventHandler) {
850851
return async (req: express.Request, res: express.Response): Promise<void> => {
851852
try {
852853
const projectId = process.env.GCLOUD_PROJECT;
@@ -867,7 +868,10 @@ export function wrapHandler(eventType: AuthBlockingEventType, handler: AgnosticH
867868
? await auth.getAuth(getApp())._verifyAuthBlockingToken(req.body.data.jwt)
868869
: await auth.getAuth(getApp())._verifyAuthBlockingToken(req.body.data.jwt, "run.app");
869870
let authUserRecord: AuthUserRecord | undefined;
870-
if (decodedPayload.user_record) {
871+
if (
872+
decodedPayload.event_type === "beforeCreate" ||
873+
decodedPayload.event_type === "beforeSignIn"
874+
) {
871875
authUserRecord = parseAuthUserRecord(decodedPayload.user_record);
872876
}
873877
const authEventContext = parseAuthEventContext(decodedPayload, projectId);

src/v1/providers/auth.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import {
2727
BeforeCreateResponse,
2828
BeforeEmailResponse,
2929
BeforeSignInResponse,
30-
AgnosticHandler,
3130
HandlerV1,
3231
HttpsError,
3332
MaybeAsync,
@@ -204,7 +203,7 @@ export class UserBuilder {
204203
const idToken = this.userOptions?.blockingOptions?.idToken || false;
205204
const refreshToken = this.userOptions?.blockingOptions?.refreshToken || false;
206205

207-
const annotatedHandler: AgnosticHandler = Object.assign(handler, { platform: "gcfv1" });
206+
const annotatedHandler = Object.assign(handler, { platform: "gcfv1" as const });
208207
const func: any = wrapHandler(eventType, annotatedHandler);
209208

210209
const legacyEventType = `providers/cloud.auth/eventTypes/user.${eventType}`;

src/v2/providers/identity.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import {
3636
HttpsError,
3737
wrapHandler,
3838
MaybeAsync,
39-
AgnosticHandler,
4039
} from "../../common/providers/identity";
4140
import { BlockingFunction } from "../../v1/cloud-functions";
4241
import { wrapTraceContext } from "../trace";
@@ -243,7 +242,7 @@ export function beforeEmailSent(
243242
* @param handler - Event handler that is run before an email is sent to a user.
244243
*/
245244
export function beforeEmailSent(
246-
opts: BlockingOptions,
245+
opts: Omit<BlockingOptions, "idToken" | "accessToken" | "refreshToken">,
247246
handler: (event: AuthBlockingEvent) => MaybeAsync<BeforeEmailResponse | void>
248247
): BlockingFunction;
249248

@@ -254,7 +253,7 @@ export function beforeEmailSent(
254253
*/
255254
export function beforeEmailSent(
256255
optsOrHandler:
257-
| BlockingOptions
256+
| Omit<BlockingOptions, "idToken" | "accessToken" | "refreshToken">
258257
| ((event: AuthBlockingEvent) => MaybeAsync<BeforeEmailResponse | void>),
259258
handler?: (event: AuthBlockingEvent) => MaybeAsync<BeforeEmailResponse | void>
260259
): BlockingFunction {
@@ -283,7 +282,7 @@ export function beforeOperation(
283282
// Create our own function that just calls the provided function so we know for sure that
284283
// handler takes one argument. This is something common/providers/identity depends on.
285284
// const wrappedHandler = (event: AuthBlockingEvent) => handler(event);
286-
const annotatedHandler: AgnosticHandler = Object.assign(handler, { platform: "gcfv2" });
285+
const annotatedHandler = Object.assign(handler, { platform: "gcfv2" as const });
287286
const func: any = wrapTraceContext(withInit(wrapHandler(eventType, annotatedHandler)));
288287

289288
const legacyEventType = `providers/cloud.auth/eventTypes/user.${eventType}`;

0 commit comments

Comments
 (0)