Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
updated with API proposal guidence
  • Loading branch information
Xiaoshouzi-gh committed Nov 2, 2023
commit 0de4e7ecd6fd54e74e47c04bb07a67f24efec419
28 changes: 15 additions & 13 deletions spec/common/providers/identity.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import * as identity from "../../../src/common/providers/identity";
const EVENT = "EVENT_TYPE";
const now = new Date();
const TEST_NAME = "John Doe";
const ALLOW = "ALLOW";
const BLOCK = "BLOCK";

describe("identity", () => {
describe("userRecordConstructor", () => {
Expand Down Expand Up @@ -771,24 +773,24 @@ describe("identity", () => {
});
});

describe("generateRequestPayload", () => {
describe("generateResponsePayload", () => {
const DISPLAY_NAME_FIELD = "displayName";
const TEST_RESPONSE = {
displayName: TEST_NAME,
recaptchaPassed: false,
recaptchaActionOverride: BLOCK,
} as identity.BeforeCreateResponse;

const EXPECT_PAYLOAD = {
userRecord: { displayName: TEST_NAME, updateMask: DISPLAY_NAME_FIELD },
recaptchaPassed: false,
recaptchaActionOverride: BLOCK,
};

const TEST_RESPONSE_RECAPTCHA_TRUE = {
recaptchaPassed: true,
const TEST_RESPONSE_RECAPTCHA_ALLOW = {
recaptchaActionOverride: ALLOW,
} as identity.BeforeCreateResponse;

const EXPECT_PAYLOAD_RECAPTCHA_TRUE = {
recaptchaPassed: true,
const EXPECT_PAYLOAD_RECAPTCHA_ALLOW = {
recaptchaActionOverride: ALLOW,
};

const TEST_RESPONSE_RECAPTCHA_UNDEFINED = {
Expand All @@ -802,19 +804,19 @@ describe("identity", () => {
expect(identity.generateResponsePayload()).to.eql({});
});

it("should exclude recaptchaPass field from updateMask", () => {
it("should exclude recaptchaActionOverride field from updateMask", () => {
expect(identity.generateResponsePayload(TEST_RESPONSE)).to.deep.equal(EXPECT_PAYLOAD);
});

it("should return recaptchaPass when it is true on response", () => {
expect(identity.generateResponsePayload(TEST_RESPONSE_RECAPTCHA_TRUE)).to.deep.equal(
EXPECT_PAYLOAD_RECAPTCHA_TRUE
it("should return recaptchaActionOverride when it is true on response", () => {
expect(identity.generateResponsePayload(TEST_RESPONSE_RECAPTCHA_ALLOW)).to.deep.equal(
EXPECT_PAYLOAD_RECAPTCHA_ALLOW
);
});

it("should not return recaptchaPass if undefined", () => {
it("should not return recaptchaActionOverride if undefined", () => {
const payload = identity.generateResponsePayload(TEST_RESPONSE_RECAPTCHA_UNDEFINED);
expect(payload.hasOwnProperty("recaptchaPassed")).to.be.false;
expect(payload.hasOwnProperty("recaptchaActionOverride")).to.be.false;
expect(payload).to.deep.equal(EXPECT_PAYLOAD_UNDEFINED);
});
});
Expand Down
28 changes: 17 additions & 11 deletions src/common/providers/identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,18 +339,19 @@ export interface AuthBlockingEvent extends AuthEventContext {
data: AuthUserRecord;
}

/** The base handler response type for beforeCreate and beforeSignIn blocking events*/
export interface BlockingFunctionResponse {
recaptchaPassed?: boolean;
}
/**
* The reCACPTCHA action options.
*/
export type RecatpchaActionOptions = "ALLOW" | "BLOCK";

Choose a reason for hiding this comment

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

nit: "Recaptcha" typo. Also in the comment.


/** The handler response type for beforeCreate blocking events */
export interface BeforeCreateResponse extends BlockingFunctionResponse {
export interface BeforeCreateResponse {
displayName?: string;
disabled?: boolean;
emailVerified?: boolean;
photoURL?: string;
customClaims?: object;
recaptchaActionOverride?: RecatpchaActionOptions;
}

/** The handler response type for beforeSignIn blocking events */
Expand Down Expand Up @@ -433,14 +434,18 @@ export interface DecodedPayload {
[key: string]: any;
}

/** @internal */
/**
* This interface defines the payload to send back to GCIP.
* The nesting structure different than what customers returned.
* @internal */
export interface ResponsePayload {
userRecord?: UserRecordResponsePayload;
recaptchaPassed?: boolean;
recaptchaActionOverride?: RecatpchaActionOptions;
}

/** @internal */
export interface UserRecordResponsePayload extends Omit<BeforeSignInResponse, "recaptchaPassed"> {
export interface UserRecordResponsePayload
extends Omit<BeforeSignInResponse, "recaptchaActionOverride"> {
updateMask?: string;
}

Expand Down Expand Up @@ -673,7 +678,8 @@ export function generateResponsePayload(
return {};
}

const { recaptchaPassed, ...formattedAuthResponse } = authResponse;
const { recaptchaActionOverride: recaptchaActionOverride, ...formattedAuthResponse } =
Copy link
Contributor

Choose a reason for hiding this comment

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

Am I missing something with the destructuring assignment for recaptchaActionOverride, or can we just do const { recaptchaActionOverride, ...formatedAuthResponse } = authResponse; instead?

authResponse;
const result = {} as ResponsePayload;
const updateMask = getUpdateMask(formattedAuthResponse);

Expand All @@ -684,8 +690,8 @@ export function generateResponsePayload(
};
}

if (recaptchaPassed !== undefined) {
result.recaptchaPassed = recaptchaPassed;
if (recaptchaActionOverride !== undefined) {
result.recaptchaActionOverride = recaptchaActionOverride;
}

return result;
Expand Down