Skip to content
This repository was archived by the owner on Jan 5, 2022. It is now read-only.

Commit c123574

Browse files
Refactored change password API endpoint (#1764)
* rebase with master * Update README * add new action AdminSetUserPassword to lambda policy * update dist files * Remove aws-sdk from package * update package* files * update dist files
1 parent a329829 commit c123574

File tree

20 files changed

+275
-342
lines changed

20 files changed

+275
-342
lines changed

README.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,6 @@ authentication flow - specifically multi-part emails are not supported; welcome
289289
to the 21st century. To work around these restrictions registration and password
290290
reset were decoupled using the Cognito Identity Service Provider admin APIs.
291291
Verification is implemented with custom verification codes and email delivery.
292-
However, as Cognito currently does not support setting the password for a user
293-
through the admin API, the user is deleted and recreated with the exact same
294-
identifier (a UUID). This is heavily tested with acceptance tests and just
295-
works, but it's not ideal. Hopefully AWS will address these issues in the
296-
future.
297292

298293
## License
299294

modules/api/iam/policies/lambda.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"cognito-idp:AdminConfirmSignUp",
1717
"cognito-idp:AdminDeleteUser",
1818
"cognito-idp:AdminGetUser",
19-
"cognito-idp:AdminUpdateUserAttributes"
19+
"cognito-idp:AdminUpdateUserAttributes",
20+
"cognito-idp:AdminSetUserPassword"
2021
],
2122
"Resource": "${cognito_user_pool_arn}"
2223
},

modules/api/lambda/dist.zip

642 Bytes
Binary file not shown.

modules/api/lambda/package-lock.json

Lines changed: 62 additions & 62 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

modules/api/lambda/package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
"lint": "make lint",
2424
"start": "make start",
2525
"test": "make test",
26-
"test:acceptance": "make test-acceptance",
2726
"watch": "make watch"
2827
},
2928
"dependencies": {
@@ -34,18 +33,19 @@
3433
"uuid": "^8.3.2"
3534
},
3635
"devDependencies": {
37-
"@types/cookie": "^0.4.0",
38-
"@types/express": "^4.17.11",
39-
"@types/glob": "^7.1.3",
40-
"@types/morgan": "^1.9.2",
36+
"@types/cookie": "^0.4.1",
37+
"@types/express": "^4.17.13",
38+
"@types/glob": "^7.1.4",
39+
"@types/morgan": "^1.9.3",
40+
"@types/node": "^16.3.2",
4141
"@types/supertest": "^2.0.11",
42-
"@types/uuid": "^8.3.0",
42+
"@types/uuid": "^8.3.1",
4343
"express": "^4.17.1",
4444
"generate-password": "^1.6.0",
4545
"glob": "^7.1.7",
4646
"header-case-normalizer": "^1.0.3",
4747
"morgan": "^1.10.0",
4848
"supertest": "^6.1.3",
49-
"typescript-json-schema": "^0.50.0"
49+
"typescript-json-schema": "^0.50.1"
5050
}
5151
}

modules/api/lambda/src/clients/management/index.ts

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,7 @@ export class ManagementClient extends Client {
7676
}
7777

7878
/**
79-
* Reset password for user
80-
*
81-
* Hack: Cognito doesn't allow us to change the password of a user without
82-
* his consent, so we have to work around this in order to implement our own
83-
* customized authentication flow by deleting and re-creating the user.
84-
* Hopefully, this issue will be adressed, see https://amzn.to/2snEPMm
85-
*
86-
* We must sign out the user to force re-authentication, but we don't need to
87-
* do it explicitly as we delete the old and create a new user.
79+
* Change password for user
8880
*
8981
* @param username - Username or email address
9082
* @param password - Password
@@ -94,27 +86,11 @@ export class ManagementClient extends Client {
9486
public async changePassword(
9587
username: string, password: string
9688
): Promise<void> {
97-
// this.cognito.adminSetUserPassword
98-
const { Username: id, UserAttributes } =
99-
await this.cognito.adminGetUser({
100-
UserPoolId: process.env.COGNITO_USER_POOL_ID!,
101-
Username: username
102-
}).promise()
103-
104-
/* Delete user */
105-
await this.deleteUser(id)
106-
107-
/* Re-create user */
108-
await this.cognito.signUp({
109-
ClientId: process.env.COGNITO_USER_POOL_CLIENT_ID!,
110-
Username: id,
89+
await this.cognito.adminSetUserPassword({
90+
UserPoolId: process.env.COGNITO_USER_POOL_ID!,
91+
Username: username,
11192
Password: password,
112-
UserAttributes: UserAttributes!.filter(attr => {
113-
return ["sub", "email_verified"].indexOf(attr.Name) === -1
114-
})
93+
Permanent: true
11594
}).promise()
116-
117-
/* Auto-verify user */
118-
await this.verifyUser(id)
11995
}
12096
}

0 commit comments

Comments
 (0)