Skip to content

Commit 82780ae

Browse files
authored
Strip resetToken and resetTokenExpiresAt from dbAuth forgotPassword handler (#6778)
* Clear reset token with built-in function * Remove any resetToken or resetTokenExpiresAt from forgotPassword handler response * Updates test for forgotPassword return data
1 parent 8b90a78 commit 82780ae

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

packages/auth-providers-api/src/dbAuth/DbAuthHandler.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -506,8 +506,18 @@ export class DbAuthHandler<
506506
this.options.forgotPassword as ForgotPasswordFlowOptions
507507
).handler(this._sanitizeUser(user))
508508

509+
// remove resetToken and resetTokenExpiresAt if in the body of the
510+
// forgotPassword handler response
511+
let responseObj = response
512+
if (typeof response === 'object') {
513+
responseObj = Object.assign(response, {
514+
[this.options.authFields.resetToken]: undefined,
515+
[this.options.authFields.resetTokenExpiresAt]: undefined,
516+
})
517+
}
518+
509519
return [
510-
response ? JSON.stringify(response) : '',
520+
response ? JSON.stringify(responseObj) : '',
511521
{
512522
...this._deleteSessionHeader,
513523
},
@@ -612,14 +622,14 @@ export class DbAuthHandler<
612622
},
613623
data: {
614624
[this.options.authFields.hashedPassword]: hashedPassword,
615-
[this.options.authFields.resetToken]: null,
616-
[this.options.authFields.resetTokenExpiresAt]: null,
617625
},
618626
})
619627
} catch (e) {
620628
throw new DbAuthError.GenericError()
621629
}
622630

631+
await this._clearResetToken(user)
632+
623633
// call the user-defined handler so they can decide what to do with this user
624634
const response = await (
625635
this.options.resetPassword as ResetPasswordFlowOptions

packages/auth-providers-api/src/dbAuth/__tests__/DbAuthHandler.test.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -767,15 +767,16 @@ describe('dbAuth', () => {
767767
// base64 characters only, except =
768768
expect(resetUser.resetToken).toMatch(/^\w{16}$/)
769769
expect(resetUser.resetTokenExpiresAt instanceof Date).toEqual(true)
770-
// response contains the user data, minus `hashedPassword` and `salt`
770+
771+
// response contains data returned from the handler
771772
expect(responseBody.id).toEqual(resetUser.id)
772773
expect(responseBody.email).toEqual(resetUser.email)
773-
expect(responseBody.resetToken).toEqual(resetUser.resetToken)
774-
expect(responseBody.resetTokenExpiresAt).toEqual(
775-
resetUser.resetTokenExpiresAt.toISOString()
776-
)
777-
expect(responseBody.hashedPassword).toEqual(undefined)
778-
expect(responseBody.salt).toEqual(undefined)
774+
775+
// response data should not include sensitive info
776+
expect(responseBody.resetToken).toBeUndefined()
777+
expect(responseBody.resetTokenExpiresAt).toBeUndefined()
778+
expect(responseBody.hashedPassword).toBeUndefined()
779+
expect(responseBody.salt).toBeUndefined()
779780
})
780781

781782
it('returns a logout session cookie', async () => {
@@ -802,6 +803,22 @@ describe('dbAuth', () => {
802803
expect.assertions(1)
803804
})
804805

806+
it('removes the token from the forgotPassword response', async () => {
807+
const user = await createDbUser()
808+
event.body = JSON.stringify({
809+
username: user.email,
810+
})
811+
options.forgotPassword.handler = (handlerUser) => {
812+
return handlerUser
813+
}
814+
const dbAuth = new DbAuthHandler(event, context, options)
815+
const response = await dbAuth.forgotPassword()
816+
const jsonResponse = JSON.parse(response[0])
817+
818+
expect(jsonResponse.resetToken).toBeUndefined()
819+
expect(jsonResponse.resetTokenExpiresAt).toBeUndefined()
820+
})
821+
805822
it('throws a generic error for an invalid client', async () => {
806823
const user = await createDbUser()
807824
event.body = JSON.stringify({

0 commit comments

Comments
 (0)