Skip to content

Commit

Permalink
feat(lambda): support for CustomMessage_Authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
jagregory committed Nov 25, 2021
1 parent 79f43ef commit dfb6fdf
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 5 deletions.
111 changes: 110 additions & 1 deletion src/services/messages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,96 @@ describe("messages service", () => {

const user = TDB.user();

it.todo("authentication");
describe("authentication", () => {
describe("CustomMessage lambda is configured", () => {
describe("lambda returns a custom message", () => {
it("returns the custom message and code", async () => {
mockTriggers.enabled.mockImplementation((name) => {
return name === "CustomMessage";
});
mockTriggers.customMessage.mockResolvedValue({
smsMessage: "sms",
emailSubject: "email subject",
emailMessage: "email",
});

const messages = new MessagesService(mockTriggers);
const message = await messages.authentication(
"clientId",
"userPoolId",
user,
"1234"
);

expect(message).toMatchObject({
__code: "1234",
smsMessage: "sms",
emailSubject: "email subject",
emailMessage: "email",
});

expect(mockTriggers.customMessage).toHaveBeenCalledWith({
clientId: "clientId",
code: "1234",
source: "CustomMessage_Authentication",
userAttributes: user.Attributes,
userPoolId: "userPoolId",
username: user.Username,
});
});
});

describe("lambda does not return a custom message", () => {
it("returns just the code", async () => {
mockTriggers.enabled.mockImplementation((name) => {
return name === "CustomMessage";
});
mockTriggers.customMessage.mockResolvedValue(null);

const messages = new MessagesService(mockTriggers);
const message = await messages.authentication(
"clientId",
"userPoolId",
user,
"1234"
);

expect(message).toMatchObject({
__code: "1234",
});

expect(mockTriggers.customMessage).toHaveBeenCalledWith({
clientId: "clientId",
code: "1234",
source: "CustomMessage_Authentication",
userAttributes: user.Attributes,
userPoolId: "userPoolId",
username: user.Username,
});
});
});
});

describe("CustomMessage lambda is not configured", () => {
it("returns just the code", async () => {
mockTriggers.enabled.mockReturnValue(false);

const messages = new MessagesService(mockTriggers);
const message = await messages.authentication(
"clientId",
"userPoolId",
user,
"1234"
);

expect(message).toMatchObject({
__code: "1234",
});

expect(mockTriggers.customMessage).not.toHaveBeenCalled();
});
});
});

describe("forgotPassword", () => {
describe("CustomMessage lambda is configured", () => {
Expand Down Expand Up @@ -41,6 +130,15 @@ describe("messages service", () => {
emailSubject: "email subject",
emailMessage: "email",
});

expect(mockTriggers.customMessage).toHaveBeenCalledWith({
clientId: "clientId",
code: "1234",
source: "CustomMessage_ForgotPassword",
userAttributes: user.Attributes,
userPoolId: "userPoolId",
username: user.Username,
});
});
});

Expand All @@ -62,6 +160,15 @@ describe("messages service", () => {
expect(message).toMatchObject({
__code: "1234",
});

expect(mockTriggers.customMessage).toHaveBeenCalledWith({
clientId: "clientId",
code: "1234",
source: "CustomMessage_ForgotPassword",
userAttributes: user.Attributes,
userPoolId: "userPoolId",
username: user.Username,
});
});
});
});
Expand All @@ -81,6 +188,8 @@ describe("messages service", () => {
expect(message).toMatchObject({
__code: "1234",
});

expect(mockTriggers.customMessage).not.toHaveBeenCalled();
});
});
});
Expand Down
35 changes: 32 additions & 3 deletions src/services/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ export interface Message {
}

export interface Messages {
authentication(code: string): Promise<Message>;
authentication(
clientId: string,
userPoolId: string,
user: User,
code: string
): Promise<Message>;
forgotPassword(
clientId: string,
userPoolId: string,
Expand All @@ -31,8 +36,32 @@ export class MessagesService implements Messages {
this.triggers = triggers;
}

public authentication(code: string): Promise<Message> {
return stubMessage(code);
public async authentication(
clientId: string,
userPoolId: string,
user: User,
code: string
): Promise<Message> {
if (this.triggers.enabled("CustomMessage")) {
const message = await this.triggers.customMessage({
clientId,
code,
source: "CustomMessage_Authentication",
userAttributes: user.Attributes,
username: user.Username,
userPoolId,
});

return {
__code: code,
...message,
};
}

// TODO: What should the default message be?
return {
__code: code,
};
}

public async forgotPassword(
Expand Down
6 changes: 6 additions & 0 deletions src/targets/initiateAuth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,12 @@ describe("InitiateAuth target", () => {

expect(output).toBeDefined();

expect(mockMessages.authentication).toHaveBeenCalledWith(
"clientId",
"test",
user,
"1234"
);
expect(mockMessageDelivery.deliver).toHaveBeenCalledWith(
user,
{
Expand Down
7 changes: 6 additions & 1 deletion src/targets/initiateAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ const verifyMfaChallenge = async (
}

const code = otp();
const message = await messages.authentication(code);
const message = await messages.authentication(
req.ClientId,
userPool.config.Id,
user,
code
);
await messageDelivery.deliver(
user,
{
Expand Down

0 comments on commit dfb6fdf

Please sign in to comment.