-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: forgotPassword set expiration time (#9871)
The logic for creating a timestamp for use in resetPassword was not correctly returning a valid date. --------- Co-authored-by: Patrik Kozak <[email protected]>
- Loading branch information
1 parent
ca52a50
commit 306b5d2
Showing
4 changed files
with
77 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -605,6 +605,43 @@ describe('Access Control', () => { | |
expect(res).toBeTruthy() | ||
}) | ||
}) | ||
|
||
describe('Auth - Local API', () => { | ||
it('should not allow reset password if forgotPassword expiration token is expired', async () => { | ||
// Mock Date.now() to simulate the forgotPassword call happening 1 hour ago (default is 1 hour) | ||
const originalDateNow = Date.now | ||
const mockDateNow = jest.spyOn(Date, 'now').mockImplementation(() => { | ||
// Move the current time back by 1 hour | ||
return originalDateNow() - 60 * 60 * 1000 | ||
}) | ||
|
||
let forgot | ||
try { | ||
// Call forgotPassword while the mocked Date.now() is active | ||
forgot = await payload.forgotPassword({ | ||
collection: 'users', | ||
data: { | ||
email: '[email protected]', | ||
}, | ||
}) | ||
} finally { | ||
// Restore the original Date.now() after the forgotPassword call | ||
mockDateNow.mockRestore() | ||
} | ||
|
||
// Attempt to reset password, which should fail because the token is expired | ||
await expect( | ||
payload.resetPassword({ | ||
collection: 'users', | ||
data: { | ||
password: 'test', | ||
token: forgot, | ||
}, | ||
overrideAccess: true, | ||
}), | ||
).rejects.toThrow('Token is either invalid or has expired.') | ||
}) | ||
}) | ||
}) | ||
|
||
async function createDoc<TSlug extends CollectionSlug = 'posts'>( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -932,5 +932,40 @@ describe('Auth', () => { | |
|
||
expect(reset.user.email).toStrictEqual('[email protected]') | ||
}) | ||
|
||
it('should not allow reset password if forgotPassword expiration token is expired', async () => { | ||
// Mock Date.now() to simulate the forgotPassword call happening 6 minutes ago (current expiration is set to 5 minutes) | ||
const originalDateNow = Date.now | ||
const mockDateNow = jest.spyOn(Date, 'now').mockImplementation(() => { | ||
// Move the current time back by 6 minutes (360,000 ms) | ||
return originalDateNow() - 6 * 60 * 1000 | ||
}) | ||
|
||
let forgot | ||
try { | ||
// Call forgotPassword while the mocked Date.now() is active | ||
forgot = await payload.forgotPassword({ | ||
collection: 'users', | ||
data: { | ||
email: '[email protected]', | ||
}, | ||
}) | ||
} finally { | ||
// Restore the original Date.now() after the forgotPassword call | ||
mockDateNow.mockRestore() | ||
} | ||
|
||
// Attempt to reset password, which should fail because the token is expired | ||
await expect( | ||
payload.resetPassword({ | ||
collection: 'users', | ||
data: { | ||
password: 'test', | ||
token: forgot, | ||
}, | ||
overrideAccess: true, | ||
}), | ||
).rejects.toThrow('Token is either invalid or has expired.') | ||
}) | ||
}) | ||
}) |