Skip to content

Commit

Permalink
Strip resetToken and resetTokenExpiresAt from dbAuth forgotPassword h…
Browse files Browse the repository at this point in the history
…andler (#6778)

* Clear reset token with built-in function

* Remove any resetToken or resetTokenExpiresAt from forgotPassword handler response

* Updates test for forgotPassword return data
  • Loading branch information
cannikin authored and jtoar committed Nov 8, 2022
1 parent d792eff commit a335e59
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
16 changes: 13 additions & 3 deletions packages/api/src/functions/dbAuth/DbAuthHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,18 @@ export class DbAuthHandler<TUser extends Record<string | number, any>> {
this.options.forgotPassword as ForgotPasswordFlowOptions
).handler(this._sanitizeUser(user))

// remove resetToken and resetTokenExpiresAt if in the body of the
// forgotPassword handler response
let responseObj = response
if (typeof response === 'object') {
responseObj = Object.assign(response, {
[this.options.authFields.resetToken]: undefined,
[this.options.authFields.resetTokenExpiresAt]: undefined,
})
}

return [
response ? JSON.stringify(response) : '',
response ? JSON.stringify(responseObj) : '',
{
...this._deleteSessionHeader,
},
Expand Down Expand Up @@ -613,14 +623,14 @@ export class DbAuthHandler<TUser extends Record<string | number, any>> {
},
data: {
[this.options.authFields.hashedPassword]: hashedPassword,
[this.options.authFields.resetToken]: null,
[this.options.authFields.resetTokenExpiresAt]: null,
},
})
} catch (e) {
throw new DbAuthError.GenericError()
}

await this._clearResetToken(user)

// call the user-defined handler so they can decide what to do with this user
const response = await (
this.options.resetPassword as ResetPasswordFlowOptions
Expand Down
31 changes: 24 additions & 7 deletions packages/api/src/functions/dbAuth/__tests__/DbAuthHandler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -762,15 +762,16 @@ describe('dbAuth', () => {
// base64 characters only, except =
expect(resetUser.resetToken).toMatch(/^\w{16}$/)
expect(resetUser.resetTokenExpiresAt instanceof Date).toEqual(true)
// response contains the user data, minus `hashedPassword` and `salt`

// response contains data returned from the handler
expect(responseBody.id).toEqual(resetUser.id)
expect(responseBody.email).toEqual(resetUser.email)
expect(responseBody.resetToken).toEqual(resetUser.resetToken)
expect(responseBody.resetTokenExpiresAt).toEqual(
resetUser.resetTokenExpiresAt.toISOString()
)
expect(responseBody.hashedPassword).toEqual(undefined)
expect(responseBody.salt).toEqual(undefined)

// response data should not include sensitive info
expect(responseBody.resetToken).toBeUndefined()
expect(responseBody.resetTokenExpiresAt).toBeUndefined()
expect(responseBody.hashedPassword).toBeUndefined()
expect(responseBody.salt).toBeUndefined()
})

it('returns a logout session cookie', async () => {
Expand All @@ -797,6 +798,22 @@ describe('dbAuth', () => {
expect.assertions(1)
})

it('removes the token from the forgotPassword response', async () => {
const user = await createDbUser()
event.body = JSON.stringify({
username: user.email,
})
options.forgotPassword.handler = (handlerUser) => {
return handlerUser
}
const dbAuth = new DbAuthHandler(event, context, options)
const response = await dbAuth.forgotPassword()
const jsonResponse = JSON.parse(response[0])

expect(jsonResponse.resetToken).toBeUndefined()
expect(jsonResponse.resetTokenExpiresAt).toBeUndefined()
})

it('throws a generic error for an invalid client', async () => {
const user = await createDbUser()
event.body = JSON.stringify({
Expand Down

0 comments on commit a335e59

Please sign in to comment.