Skip to content

Commit e2975f0

Browse files
committed
fix(api): confirm user apis return errors for invalid states
1 parent f33c4e3 commit e2975f0

10 files changed

+95
-16
lines changed

integration-tests/aws-sdk/adminConfirmSignUp.test.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,19 @@ describe(
66
it("confirms a user", async () => {
77
const client = Cognito();
88

9+
const upc = await client
10+
.createUserPoolClient({
11+
UserPoolId: "test",
12+
ClientName: "test",
13+
})
14+
.promise();
15+
916
await client
10-
.adminCreateUser({
17+
.signUp({
1118
UserAttributes: [{ Name: "phone_number", Value: "0400000000" }],
1219
Username: "abc",
13-
UserPoolId: "test",
20+
ClientId: upc.UserPoolClient?.ClientId!,
21+
Password: "def",
1422
})
1523
.promise();
1624

@@ -21,7 +29,7 @@ describe(
2129
})
2230
.promise();
2331

24-
expect(user.UserStatus).toEqual("FORCE_CHANGE_PASSWORD");
32+
expect(user.UserStatus).toEqual("UNCONFIRMED");
2533

2634
await client
2735
.adminConfirmSignUp({

integration-tests/aws-sdk/deleteUserAttributes.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ describe(
3636
.promise();
3737

3838
await client
39-
.adminConfirmSignUp({
39+
.adminSetUserPassword({
4040
UserPoolId: userPoolId,
4141
Username: "abc",
42+
Password: "def",
43+
Permanent: true,
4244
})
4345
.promise();
4446

integration-tests/aws-sdk/getUserAttributeVerificationCode.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ describe(
3535
.promise();
3636

3737
await client
38-
.adminConfirmSignUp({
38+
.adminSetUserPassword({
3939
UserPoolId: userPoolId,
4040
Username: "abc",
41+
Password: "def",
42+
Permanent: true,
4143
})
4244
.promise();
4345

integration-tests/aws-sdk/initiateAuth.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,11 @@ describe(
103103
);
104104

105105
await client
106-
.adminConfirmSignUp({
106+
.adminSetUserPassword({
107107
UserPoolId: "test",
108108
Username: "abc",
109+
Password: "def",
110+
Permanent: true,
109111
})
110112
.promise();
111113

@@ -190,9 +192,11 @@ describe(
190192
);
191193

192194
await client
193-
.adminConfirmSignUp({
195+
.adminSetUserPassword({
194196
UserPoolId: "test",
195197
Username: "abc",
198+
Password: "def",
199+
Permanent: true,
196200
})
197201
.promise();
198202

integration-tests/aws-sdk/updateUserAttributes.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ describe(
3535
.promise();
3636

3737
await client
38-
.adminConfirmSignUp({
38+
.adminSetUserPassword({
3939
UserPoolId: userPoolId,
4040
Username: "abc",
41+
Password: "def",
42+
Permanent: true,
4143
})
4244
.promise();
4345

integration-tests/aws-sdk/verifyUserAttribute.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ describe(
3535
.promise();
3636

3737
await client
38-
.adminConfirmSignUp({
39-
UserPoolId: userPoolId,
38+
.adminSetUserPassword({
4039
Username: "abc",
40+
UserPoolId: userPoolId,
41+
Password: "def",
42+
Permanent: true,
4143
})
4244
.promise();
4345

src/errors.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ export class CognitoError extends Error {
1010
}
1111

1212
export class NotAuthorizedError extends CognitoError {
13-
public constructor() {
14-
super("NotAuthorizedException", "User not authorized");
13+
public constructor(message = "User not authorized") {
14+
super("NotAuthorizedException", message);
1515
}
1616
}
1717

@@ -33,6 +33,15 @@ export class CodeMismatchError extends CognitoError {
3333
}
3434
}
3535

36+
export class ExpiredCodeError extends CognitoError {
37+
public constructor() {
38+
super(
39+
"ExpiredCodeException",
40+
"Invalid code provided, please request a code again."
41+
);
42+
}
43+
}
44+
3645
export class InvalidPasswordError extends CognitoError {
3746
public constructor() {
3847
super("InvalidPasswordException", "Invalid password");

src/targets/adminConfirmSignUp.test.ts

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,40 @@ describe("AdminConfirmSignUp target", () => {
4545
).rejects.toEqual(new NotAuthorizedError());
4646
});
4747

48+
it.each([
49+
"CONFIRMED",
50+
"ARCHIVED",
51+
"COMPROMISED",
52+
"UNKNOWN",
53+
"RESET_REQUIRED",
54+
"FORCE_CHANGE_PASSWORD",
55+
"something else",
56+
])("throws if the user has status %s", async (status) => {
57+
const user = TDB.user({
58+
UserStatus: status,
59+
});
60+
61+
mockUserPoolService.getUserByUsername.mockResolvedValue(user);
62+
63+
await expect(
64+
adminConfirmSignUp(TestContext, {
65+
ClientMetadata: {
66+
client: "metadata",
67+
},
68+
Username: user.Username,
69+
UserPoolId: "test",
70+
})
71+
).rejects.toEqual(
72+
new NotAuthorizedError(
73+
`User cannot be confirmed. Current status is ${status}`
74+
)
75+
);
76+
});
77+
4878
it("updates the user's status", async () => {
49-
const user = TDB.user();
79+
const user = TDB.user({
80+
UserStatus: "UNCONFIRMED",
81+
});
5082

5183
mockUserPoolService.getUserByUsername.mockResolvedValue(user);
5284

@@ -71,7 +103,9 @@ describe("AdminConfirmSignUp target", () => {
71103
(trigger) => trigger === "PostConfirmation"
72104
);
73105

74-
const user = TDB.user();
106+
const user = TDB.user({
107+
UserStatus: "UNCONFIRMED",
108+
});
75109

76110
mockUserPoolService.getUserByUsername.mockResolvedValue(user);
77111

@@ -103,7 +137,9 @@ describe("AdminConfirmSignUp target", () => {
103137
it("invokes the trigger", async () => {
104138
mockTriggers.enabled.mockReturnValue(false);
105139

106-
const user = TDB.user();
140+
const user = TDB.user({
141+
UserStatus: "UNCONFIRMED",
142+
});
107143

108144
mockUserPoolService.getUserByUsername.mockResolvedValue(user);
109145

src/targets/adminConfirmSignUp.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ export const AdminConfirmSignUp =
3030
throw new NotAuthorizedError();
3131
}
3232

33+
if (user.UserStatus !== "UNCONFIRMED") {
34+
throw new NotAuthorizedError(
35+
`User cannot be confirmed. Current status is ${user.UserStatus}`
36+
);
37+
}
38+
3339
const updatedUser = {
3440
...user,
3541
UserLastModifiedDate: clock.get(),

src/targets/confirmSignUp.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import {
22
ConfirmSignUpRequest,
33
ConfirmSignUpResponse,
44
} from "aws-sdk/clients/cognitoidentityserviceprovider";
5-
import { CodeMismatchError, NotAuthorizedError } from "../errors";
5+
import {
6+
CodeMismatchError,
7+
ExpiredCodeError,
8+
NotAuthorizedError,
9+
} from "../errors";
610
import { Services } from "../services";
711
import { attribute, attributesAppend } from "../services/userPoolService";
812
import { Target } from "./Target";
@@ -25,6 +29,10 @@ export const ConfirmSignUp =
2529
throw new NotAuthorizedError();
2630
}
2731

32+
if (!user.ConfirmationCode) {
33+
throw new ExpiredCodeError();
34+
}
35+
2836
if (user.ConfirmationCode !== req.ConfirmationCode) {
2937
throw new CodeMismatchError();
3038
}

0 commit comments

Comments
 (0)