-
-
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 3 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,88 @@ 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(); | ||
}) | ||
.catch(error => { | ||
jfail(error); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should match codes with emailVerifyTokenReuseIfValid', done => { | ||
const user = new Parse.User(); | ||
let sendEmailOptions; | ||
let sendVerificationEmailCallCount = 0; | ||
let userBeforeRequest; | ||
const emailAdapter = { | ||
sendVerificationEmail: options => { | ||
sendEmailOptions = options; | ||
sendVerificationEmailCallCount++; | ||
}, | ||
sendPasswordResetEmail: () => Promise.resolve(), | ||
sendMail: () => {}, | ||
}; | ||
reconfigureServer({ | ||
appName: 'emailVerifyToken', | ||
verifyUserEmails: true, | ||
emailAdapter: emailAdapter, | ||
emailVerifyTokenValidityDuration: 5 * 60, // 5 minutes | ||
publicServerURL: 'http://localhost:8378/1', | ||
emailVerifyTokenReuseIfValid: true, | ||
}) | ||
.then(() => { | ||
user.setUsername('resends_verification_token'); | ||
user.setPassword('expiringToken'); | ||
user.set('email', '[email protected]'); | ||
dblythy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return user.signUp(); | ||
}) | ||
.then(() => { | ||
const config = Config.get('test'); | ||
return config.database | ||
.find('_User', { username: 'resends_verification_token' }) | ||
.then(results => { | ||
return results[0]; | ||
}); | ||
}) | ||
.then(newUser => { | ||
// store this user before we make our email request | ||
userBeforeRequest = newUser; | ||
expect(sendVerificationEmailCallCount).toBe(1); | ||
|
||
return 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', | ||
}, | ||
}); | ||
}) | ||
.then(response => { | ||
expect(response.status).toBe(200); | ||
expect(sendVerificationEmailCallCount).toBe(2); | ||
expect(sendEmailOptions).toBeDefined(); | ||
|
||
// query for this user again | ||
const config = Config.get('test'); | ||
return config.database | ||
.find('_User', { username: 'resends_verification_token' }) | ||
.then(results => { | ||
return results[0]; | ||
}); | ||
}) | ||
.then(userAfterRequest => { | ||
// 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(); | ||
}) | ||
|
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,103 @@ describe('Password Policy: ', () => { | |
}); | ||
}); | ||
|
||
it('should not keep reset token', done => { | ||
const user = new Parse.User(); | ||
const sendEmailOptions = []; | ||
const emailAdapter = { | ||
sendVerificationEmail: () => Promise.resolve(), | ||
sendPasswordResetEmail: options => { | ||
sendEmailOptions.push(options); | ||
}, | ||
sendMail: () => {}, | ||
}; | ||
reconfigureServer({ | ||
appName: 'passwordPolicy', | ||
emailAdapter: emailAdapter, | ||
passwordPolicy: { | ||
resetTokenValidityDuration: 5 * 60, // 5 minutes | ||
}, | ||
publicServerURL: 'http://localhost:8378/1', | ||
}) | ||
.then(() => { | ||
user.setUsername('testResetTokenValidity'); | ||
user.setPassword('original'); | ||
user.set('email', '[email protected]'); | ||
return user.signUp(); | ||
}) | ||
.then(() => { | ||
return Parse.User.requestPasswordReset('[email protected]').catch(err => { | ||
jfail(err); | ||
fail('Reset password request should not fail'); | ||
done(); | ||
}); | ||
}) | ||
.then(() => { | ||
return Parse.User.requestPasswordReset('[email protected]').catch(err => { | ||
jfail(err); | ||
fail('Reset password request should not fail'); | ||
done(); | ||
}); | ||
}) | ||
.then(() => { | ||
expect(sendEmailOptions[0].link).not.toBe(sendEmailOptions[1].link); | ||
done(); | ||
}) | ||
.catch(err => { | ||
jfail(err); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should keep reset token with resetTokenReuseIfValid', done => { | ||
const user = new Parse.User(); | ||
const sendEmailOptions = []; | ||
const emailAdapter = { | ||
sendVerificationEmail: () => Promise.resolve(), | ||
sendPasswordResetEmail: options => { | ||
sendEmailOptions.push(options); | ||
}, | ||
sendMail: () => {}, | ||
}; | ||
reconfigureServer({ | ||
appName: 'passwordPolicy', | ||
emailAdapter: emailAdapter, | ||
passwordPolicy: { | ||
resetTokenValidityDuration: 5 * 60, // 5 minutes | ||
resetTokenReuseIfValid: true, | ||
}, | ||
publicServerURL: 'http://localhost:8378/1', | ||
}) | ||
.then(() => { | ||
user.setUsername('testResetTokenValidity'); | ||
user.setPassword('original'); | ||
user.set('email', '[email protected]'); | ||
return user.signUp(); | ||
}) | ||
.then(() => { | ||
return Parse.User.requestPasswordReset('[email protected]').catch(err => { | ||
jfail(err); | ||
fail('Reset password request should not fail'); | ||
done(); | ||
}); | ||
}) | ||
.then(() => { | ||
return Parse.User.requestPasswordReset('[email protected]').catch(err => { | ||
jfail(err); | ||
fail('Reset password request should not fail'); | ||
done(); | ||
}); | ||
}) | ||
.then(() => { | ||
expect(sendEmailOptions[0].link).toBe(sendEmailOptions[1].link); | ||
done(); | ||
}) | ||
.catch(err => { | ||
jfail(err); | ||
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
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
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.