-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Feature: Reuse tokens if they haven't expired #7017
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
davimacedo
merged 6 commits into
parse-community:master
from
dblythy:resendExistingCode
Nov 25, 2020
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7f14cad
Reuse tokens if they haven't expired
dblythy 9aa489a
Fix failing tests
dblythy 4a6eb57
Update UserController.js
dblythy bfb96e2
Update tests
dblythy f65046c
Tests for invalid config
dblythy 28da90b
restart tests
dblythy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -510,7 +510,7 @@ describe('Email Verification Token Expiration: ', () => { | |
userAfterEmailReset._email_verify_token | ||
); | ||
expect(userBeforeEmailReset._email_verify_token_expires_at).not.toEqual( | ||
userAfterEmailReset.__email_verify_token_expires_at | ||
userAfterEmailReset._email_verify_token_expires_at | ||
); | ||
expect(sendEmailOptions).toBeDefined(); | ||
done(); | ||
|
@@ -594,7 +594,7 @@ describe('Email Verification Token Expiration: ', () => { | |
userAfterRequest._email_verify_token | ||
); | ||
expect(userBeforeRequest._email_verify_token_expires_at).not.toEqual( | ||
userAfterRequest.__email_verify_token_expires_at | ||
userAfterRequest._email_verify_token_expires_at | ||
); | ||
done(); | ||
}) | ||
|
@@ -604,6 +604,110 @@ describe('Email Verification Token Expiration: ', () => { | |
}); | ||
}); | ||
|
||
it('should throw with invalid emailVerifyTokenReuseIfValid', async done => { | ||
const sendEmailOptions = []; | ||
const emailAdapter = { | ||
sendVerificationEmail: () => Promise.resolve(), | ||
sendPasswordResetEmail: options => { | ||
sendEmailOptions.push(options); | ||
}, | ||
sendMail: () => {}, | ||
}; | ||
try { | ||
await reconfigureServer({ | ||
appName: 'passwordPolicy', | ||
verifyUserEmails: true, | ||
emailAdapter: emailAdapter, | ||
emailVerifyTokenValidityDuration: 5 * 60, // 5 minutes | ||
emailVerifyTokenReuseIfValid: [], | ||
publicServerURL: 'http://localhost:8378/1', | ||
}); | ||
fail('should have thrown.'); | ||
} catch (e) { | ||
expect(e).toBe('emailVerifyTokenReuseIfValid must be a boolean value'); | ||
} | ||
try { | ||
await reconfigureServer({ | ||
appName: 'passwordPolicy', | ||
verifyUserEmails: true, | ||
emailAdapter: emailAdapter, | ||
emailVerifyTokenReuseIfValid: true, | ||
publicServerURL: 'http://localhost:8378/1', | ||
}); | ||
fail('should have thrown.'); | ||
} catch (e) { | ||
expect(e).toBe( | ||
'You cannot use emailVerifyTokenReuseIfValid without emailVerifyTokenValidityDuration' | ||
); | ||
} | ||
done(); | ||
}); | ||
|
||
it('should match codes with emailVerifyTokenReuseIfValid', async done => { | ||
let sendEmailOptions; | ||
let sendVerificationEmailCallCount = 0; | ||
const emailAdapter = { | ||
sendVerificationEmail: options => { | ||
sendEmailOptions = options; | ||
sendVerificationEmailCallCount++; | ||
}, | ||
sendPasswordResetEmail: () => Promise.resolve(), | ||
sendMail: () => {}, | ||
}; | ||
await reconfigureServer({ | ||
appName: 'emailVerifyToken', | ||
verifyUserEmails: true, | ||
emailAdapter: emailAdapter, | ||
emailVerifyTokenValidityDuration: 5 * 60, // 5 minutes | ||
publicServerURL: 'http://localhost:8378/1', | ||
emailVerifyTokenReuseIfValid: true, | ||
}); | ||
const user = new Parse.User(); | ||
user.setUsername('resends_verification_token'); | ||
user.setPassword('expiringToken'); | ||
user.set('email', '[email protected]'); | ||
await user.signUp(); | ||
|
||
const config = Config.get('test'); | ||
const [userBeforeRequest] = await config.database.find('_User', { | ||
username: 'resends_verification_token', | ||
}); | ||
// store this user before we make our email request | ||
expect(sendVerificationEmailCallCount).toBe(1); | ||
await new Promise(resolve => { | ||
setTimeout(() => { | ||
resolve(); | ||
}, 1000); | ||
}); | ||
const response = await request({ | ||
url: 'http://localhost:8378/1/verificationEmailRequest', | ||
method: 'POST', | ||
body: { | ||
email: '[email protected]', | ||
}, | ||
headers: { | ||
'X-Parse-Application-Id': Parse.applicationId, | ||
'X-Parse-REST-API-Key': 'rest', | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
expect(response.status).toBe(200); | ||
expect(sendVerificationEmailCallCount).toBe(2); | ||
expect(sendEmailOptions).toBeDefined(); | ||
|
||
const [userAfterRequest] = await config.database.find('_User', { | ||
username: 'resends_verification_token', | ||
}); | ||
|
||
// verify that our token & expiration has been changed for this new request | ||
expect(typeof userAfterRequest).toBe('object'); | ||
expect(userBeforeRequest._email_verify_token).toEqual(userAfterRequest._email_verify_token); | ||
expect(userBeforeRequest._email_verify_token_expires_at).toEqual( | ||
userAfterRequest._email_verify_token_expires_at | ||
); | ||
done(); | ||
}); | ||
|
||
it('should not send a new verification email when a resend is requested and the user is VERIFIED', done => { | ||
const user = new Parse.User(); | ||
let sendEmailOptions; | ||
|
This file contains hidden or 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 |
---|---|---|
|
@@ -122,6 +122,102 @@ describe('Password Policy: ', () => { | |
}); | ||
}); | ||
|
||
it('should not keep reset token by default', async done => { | ||
const sendEmailOptions = []; | ||
const emailAdapter = { | ||
sendVerificationEmail: () => Promise.resolve(), | ||
sendPasswordResetEmail: options => { | ||
sendEmailOptions.push(options); | ||
}, | ||
sendMail: () => {}, | ||
}; | ||
await reconfigureServer({ | ||
appName: 'passwordPolicy', | ||
emailAdapter: emailAdapter, | ||
passwordPolicy: { | ||
resetTokenValidityDuration: 5 * 60, // 5 minutes | ||
}, | ||
publicServerURL: 'http://localhost:8378/1', | ||
}); | ||
const user = new Parse.User(); | ||
user.setUsername('testResetTokenValidity'); | ||
user.setPassword('original'); | ||
user.set('email', '[email protected]'); | ||
await user.signUp(); | ||
await Parse.User.requestPasswordReset('[email protected]'); | ||
await Parse.User.requestPasswordReset('[email protected]'); | ||
expect(sendEmailOptions[0].link).not.toBe(sendEmailOptions[1].link); | ||
done(); | ||
}); | ||
|
||
it('should keep reset token with resetTokenReuseIfValid', async done => { | ||
const sendEmailOptions = []; | ||
const emailAdapter = { | ||
sendVerificationEmail: () => Promise.resolve(), | ||
sendPasswordResetEmail: options => { | ||
sendEmailOptions.push(options); | ||
}, | ||
sendMail: () => {}, | ||
}; | ||
await reconfigureServer({ | ||
appName: 'passwordPolicy', | ||
emailAdapter: emailAdapter, | ||
passwordPolicy: { | ||
resetTokenValidityDuration: 5 * 60, // 5 minutes | ||
resetTokenReuseIfValid: true, | ||
}, | ||
publicServerURL: 'http://localhost:8378/1', | ||
}); | ||
const user = new Parse.User(); | ||
user.setUsername('testResetTokenValidity'); | ||
user.setPassword('original'); | ||
user.set('email', '[email protected]'); | ||
await user.signUp(); | ||
await Parse.User.requestPasswordReset('[email protected]'); | ||
await Parse.User.requestPasswordReset('[email protected]'); | ||
expect(sendEmailOptions[0].link).toBe(sendEmailOptions[1].link); | ||
done(); | ||
}); | ||
|
||
it('should throw with invalid resetTokenReuseIfValid', async done => { | ||
const sendEmailOptions = []; | ||
const emailAdapter = { | ||
sendVerificationEmail: () => Promise.resolve(), | ||
sendPasswordResetEmail: options => { | ||
sendEmailOptions.push(options); | ||
}, | ||
sendMail: () => {}, | ||
}; | ||
try { | ||
await reconfigureServer({ | ||
appName: 'passwordPolicy', | ||
emailAdapter: emailAdapter, | ||
passwordPolicy: { | ||
resetTokenValidityDuration: 5 * 60, // 5 minutes | ||
resetTokenReuseIfValid: [], | ||
}, | ||
publicServerURL: 'http://localhost:8378/1', | ||
}); | ||
fail('should have thrown.'); | ||
} catch (e) { | ||
expect(e).toBe('resetTokenReuseIfValid must be a boolean value'); | ||
} | ||
try { | ||
await reconfigureServer({ | ||
appName: 'passwordPolicy', | ||
emailAdapter: emailAdapter, | ||
passwordPolicy: { | ||
resetTokenReuseIfValid: true, | ||
}, | ||
publicServerURL: 'http://localhost:8378/1', | ||
}); | ||
fail('should have thrown.'); | ||
} catch (e) { | ||
expect(e).toBe('You cannot use resetTokenReuseIfValid without resetTokenValidityDuration'); | ||
} | ||
done(); | ||
}); | ||
|
||
it('should fail if passwordPolicy.resetTokenValidityDuration is not a number', done => { | ||
reconfigureServer({ | ||
appName: 'passwordPolicy', | ||
|
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.