Skip to content
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

Blocking-fn-sms-final #1589

Open
wants to merge 6 commits into
base: blidd.blocking-fn-email-final
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
- Add support for beforeEmailSent auth blocking triggers. (#1492)
- Add support for beforeSmsSent auth blocking triggers. (#1589)
54 changes: 54 additions & 0 deletions spec/common/providers/identity.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ describe("identity", () => {
eventId: "EVENT_ID",
eventType: EVENT,
emailType: undefined,
smsType: undefined,
authType: "UNAUTHENTICATED",
resource: {
service: "identitytoolkit.googleapis.com",
Expand All @@ -542,6 +543,7 @@ describe("identity", () => {
isNewUser: false,
recaptchaScore: TEST_RECAPTCHA_SCORE,
email: undefined,
phoneNumber: undefined,
},
credential: null,
params: {},
Expand Down Expand Up @@ -580,6 +582,7 @@ describe("identity", () => {
eventId: "EVENT_ID",
eventType: "providers/cloud.auth/eventTypes/user.beforeSignIn:password",
emailType: undefined,
smsType: undefined,
authType: "UNAUTHENTICATED",
resource: {
service: "identitytoolkit.googleapis.com",
Expand All @@ -593,6 +596,7 @@ describe("identity", () => {
isNewUser: false,
recaptchaScore: TEST_RECAPTCHA_SCORE,
email: undefined,
phoneNumber: undefined,
},
credential: {
claims: undefined,
Expand Down Expand Up @@ -668,6 +672,7 @@ describe("identity", () => {
eventId: "EVENT_ID",
eventType: "providers/cloud.auth/eventTypes/user.beforeCreate:oidc.provider",
emailType: undefined,
smsType: undefined,
authType: "USER",
resource: {
service: "identitytoolkit.googleapis.com",
Expand All @@ -681,6 +686,7 @@ describe("identity", () => {
isNewUser: true,
recaptchaScore: TEST_RECAPTCHA_SCORE,
email: undefined,
phoneNumber: undefined,
},
credential: {
claims: undefined,
Expand Down Expand Up @@ -721,6 +727,7 @@ describe("identity", () => {
eventId: "EVENT_ID",
eventType: "providers/cloud.auth/eventTypes/user.beforeSendEmail",
emailType: "RESET_PASSWORD",
smsType: undefined,
authType: "UNAUTHENTICATED",
resource: {
service: "identitytoolkit.googleapis.com",
Expand All @@ -734,6 +741,53 @@ describe("identity", () => {
username: undefined,
recaptchaScore: TEST_RECAPTCHA_SCORE,
email: "[email protected]",
phoneNumber: undefined,
},
credential: null,
params: {},
};

expect(identity.parseAuthEventContext(decodedJwt, "project-id", time)).to.deep.equal(context);
});

it("should parse a beforeSendSms event", () => {
const time = now.getTime();
const decodedJwt = {
iss: "https://securetoken.google.com/project_id",
aud: "https://us-east1-project_id.cloudfunctions.net/function-1",
iat: 1,
exp: 60 * 60 + 1,
event_id: "EVENT_ID",
event_type: "beforeSendSms",
user_agent: "USER_AGENT",
ip_address: "1.2.3.4",
locale: "en",
recaptcha_score: TEST_RECAPTCHA_SCORE,
sms_type: "SIGN_IN_OR_SIGN_UP",
phone_number: "+11234567890",
};
const context = {
locale: "en",
ipAddress: "1.2.3.4",
userAgent: "USER_AGENT",
eventId: "EVENT_ID",
eventType: "providers/cloud.auth/eventTypes/user.beforeSendSms",
emailType: undefined,
smsType: "SIGN_IN_OR_SIGN_UP",
authType: "UNAUTHENTICATED",
resource: {
service: "identitytoolkit.googleapis.com",
name: "projects/project-id",
},
timestamp: new Date(1000).toUTCString(),
additionalUserInfo: {
isNewUser: false,
profile: undefined,
providerId: undefined,
username: undefined,
recaptchaScore: TEST_RECAPTCHA_SCORE,
email: undefined,
phoneNumber: "+11234567890",
},
credential: null,
params: {},
Expand Down
90 changes: 90 additions & 0 deletions spec/v1/providers/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,96 @@ describe("Auth Functions", () => {
});
});

describe("beforeSms", () => {
it("should create function without options", () => {
const fn = auth.user().beforeSms(() => Promise.resolve());

expect(fn.__trigger).to.deep.equal({
labels: {},
blockingTrigger: {
eventType: "providers/cloud.auth/eventTypes/user.beforeSendSms",
options: {
accessToken: false,
idToken: false,
refreshToken: false,
},
},
});
expect(fn.__endpoint).to.deep.equal({
...MINIMAL_V1_ENDPOINT,
platform: "gcfv1",
labels: {},
blockingTrigger: {
eventType: "providers/cloud.auth/eventTypes/user.beforeSendSms",
options: {
accessToken: false,
idToken: false,
refreshToken: false,
},
},
});
expect(fn.__requiredAPIs).to.deep.equal([
{
api: "identitytoolkit.googleapis.com",
reason: "Needed for auth blocking functions",
},
]);
});

it("should create the function with options", () => {
const fn = functions
.region("us-east1")
.runWith({
timeoutSeconds: 90,
memory: "256MB",
})
.auth.user({
blockingOptions: {
accessToken: true,
refreshToken: false,
},
})
.beforeSms(() => Promise.resolve());

expect(fn.__trigger).to.deep.equal({
labels: {},
regions: ["us-east1"],
availableMemoryMb: 256,
timeout: "90s",
blockingTrigger: {
eventType: "providers/cloud.auth/eventTypes/user.beforeSendSms",
options: {
accessToken: true,
idToken: false,
refreshToken: false,
},
},
});
expect(fn.__endpoint).to.deep.equal({
...MINIMAL_V1_ENDPOINT,
platform: "gcfv1",
labels: {},
region: ["us-east1"],
availableMemoryMb: 256,
timeoutSeconds: 90,
blockingTrigger: {
eventType: "providers/cloud.auth/eventTypes/user.beforeSendSms",
options: {
accessToken: true,
idToken: false,
refreshToken: false,
},
},
});
expect(fn.__requiredAPIs).to.deep.equal([
{
api: "identitytoolkit.googleapis.com",
reason: "Needed for auth blocking functions",
},
]);
});
});

describe("#_dataConstructor", () => {
let cloudFunctionDelete: CloudFunction<UserRecord>;

Expand Down
86 changes: 86 additions & 0 deletions spec/v2/providers/identity.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ const BEFORE_EMAIL_TRIGGER = {
options: {},
};

const BEFORE_SMS_TRIGGER = {
eventType: "providers/cloud.auth/eventTypes/user.beforeSendSms",
options: {},
};

const opts: identity.BlockingOptions = {
accessToken: true,
refreshToken: false,
Expand Down Expand Up @@ -233,6 +238,49 @@ describe("identity", () => {
});
});

describe("beforeSmsSent", () => {
it("should accept a handler", () => {
const fn = identity.beforeSmsSent(() => Promise.resolve());

expect(fn.__endpoint).to.deep.equal({
...MINIMAL_V2_ENDPOINT,
platform: "gcfv2",
labels: {},
blockingTrigger: BEFORE_SMS_TRIGGER,
});
expect(fn.__requiredAPIs).to.deep.equal([
{
api: IDENTITY_TOOLKIT_API,
reason: "Needed for auth blocking functions",
},
]);
});

it("should accept options and a handler", () => {
const fn = identity.beforeSmsSent(
{ region: opts.region, minInstances: opts.minInstances },
() => Promise.resolve()
);

expect(fn.__endpoint).to.deep.equal({
...MINIMAL_V2_ENDPOINT,
platform: "gcfv2",
labels: {},
minInstances: 1,
region: [REGION],
blockingTrigger: {
...BEFORE_SMS_TRIGGER,
},
});
expect(fn.__requiredAPIs).to.deep.equal([
{
api: IDENTITY_TOOLKIT_API,
reason: "Needed for auth blocking functions",
},
]);
});
});

describe("beforeOperation", () => {
it("should handle eventType and handler for before create events", () => {
const fn = identity.beforeOperation("beforeCreate", () => Promise.resolve(), undefined);
Expand Down Expand Up @@ -285,6 +333,23 @@ describe("identity", () => {
]);
});

it("should handle eventType and handler for before SMS events", () => {
const fn = identity.beforeOperation("beforeSendSms", () => Promise.resolve(), undefined);

expect(fn.__endpoint).to.deep.equal({
...MINIMAL_V2_ENDPOINT,
platform: "gcfv2",
labels: {},
blockingTrigger: BEFORE_SMS_TRIGGER,
});
expect(fn.__requiredAPIs).to.deep.equal([
{
api: IDENTITY_TOOLKIT_API,
reason: "Needed for auth blocking functions",
},
]);
});

it("should handle eventType, options, and handler for before create events", () => {
const fn = identity.beforeOperation("beforeCreate", opts, () => Promise.resolve());

Expand Down Expand Up @@ -355,6 +420,27 @@ describe("identity", () => {
},
]);
});

it("should handle eventType, options, and handler for before send SMS events", () => {
const fn = identity.beforeOperation("beforeSendSms", opts, () => Promise.resolve());

expect(fn.__endpoint).to.deep.equal({
...MINIMAL_V2_ENDPOINT,
platform: "gcfv2",
labels: {},
minInstances: 1,
region: [REGION],
blockingTrigger: {
...BEFORE_SMS_TRIGGER,
},
});
expect(fn.__requiredAPIs).to.deep.equal([
{
api: IDENTITY_TOOLKIT_API,
reason: "Needed for auth blocking functions",
},
]);
});
});

describe("getOpts", () => {
Expand Down
Loading
Loading