Skip to content

Commit 22ba398

Browse files
cmmills91flovilmart
authored andcommitted
Add support for resending verification email in case of expired token (#3617)
* -Defines new public API route /apps/:appId/resend_verification_email that will generate a new email verification link and email for a user identified by username in POST body -Add template and url support for invalidVerificationLink, linkSendSuccess, and linkSendFail pages. The invalidVerificationLink pages includes a button that allows the user to generate a new verification email if their current token has expired, using the new public API route -All three pages have default html that will be functional out of the box, but they can be customized in the customPages object. The custom page for invalidVerificationLink needs to handle the extraction of the username and appId from the url and the POST to generate the new link (this requires javascript) -Clicking a link for an email that has already been verified now routes to the emailVerifySuccess page instead of the invalidLink page * Fix package.json repo url to be parse-server againwq * Fix js lint issues * Update unit tests * Use arrow functions, change html page comments, use qs and a string template to construct location for invalidVerificationLink page, syntax fixes * Remember to pass result when using arrow function
1 parent 7b9ebc4 commit 22ba398

9 files changed

+273
-13
lines changed

public_html/invalid_link.html

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
padding: 0 0 0 0;
3636
}
3737
</style>
38+
</head>
39+
3840
<body>
3941
<div class="container">
4042
<h1>Invalid Link</h1>
+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<!DOCTYPE html>
2+
<!-- This page is displayed when someone navigates to a verify email or reset password link
3+
but their security token is wrong. This can either mean the user has clicked on a
4+
stale link (i.e. re-click on a password reset link after resetting their password) or
5+
(rarely) this could be a sign of a malicious user trying to tamper with your app.
6+
-->
7+
<html>
8+
<head>
9+
<title>Invalid Link</title>
10+
<style type='text/css'>
11+
.container {
12+
border-width: 0px;
13+
display: block;
14+
font: inherit;
15+
font-family: 'Helvetica Neue', Helvetica;
16+
font-size: 16px;
17+
height: 30px;
18+
line-height: 16px;
19+
margin: 45px 0px 0px 45px;
20+
padding: 0px 8px 0px 8px;
21+
position: relative;
22+
vertical-align: baseline;
23+
}
24+
25+
h1, h2, h3, h4, h5 {
26+
color: #0067AB;
27+
display: block;
28+
font: inherit;
29+
font-family: 'Open Sans', 'Helvetica Neue', Helvetica;
30+
font-size: 30px;
31+
font-weight: 600;
32+
height: 30px;
33+
line-height: 30px;
34+
margin: 0 0 15px 0;
35+
padding: 0 0 0 0;
36+
}
37+
</style>
38+
</head>
39+
<script type="text/javascript">
40+
function getUrlParameter(name) {
41+
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
42+
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
43+
var results = regex.exec(location.search);
44+
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
45+
};
46+
47+
window.onload = addDataToForm;
48+
49+
function addDataToForm() {
50+
var username = getUrlParameter("username");
51+
document.getElementById("usernameField").value = username;
52+
53+
var appId = getUrlParameter("appId");
54+
document.getElementById("resendForm").action = '/apps/' + appId + '/resend_verification_email'
55+
}
56+
57+
</script>
58+
59+
<body>
60+
<div class="container">
61+
<h1>Invalid Verification Link</h1>
62+
<form id="resendForm" method="POST" action="/resend_verification_email">
63+
<input id="usernameField" class="form-control" name="username" type="hidden" value="">
64+
<button type="submit" class="btn btn-default">Resend Link</button>
65+
</form>
66+
</div>
67+
</body>
68+
</html>

public_html/link_send_fail.html

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<!DOCTYPE html>
2+
<!-- This page is displayed when someone navigates to a verify email link with an invalid
3+
security token and requests a link resend. This page is displayed when the username from
4+
the original link is invalid or if the email of that user has already been verfieid when
5+
the resend request is made
6+
-->
7+
<html>
8+
<head>
9+
<title>Invalid Link</title>
10+
<style type='text/css'>
11+
.container {
12+
border-width: 0px;
13+
display: block;
14+
font: inherit;
15+
font-family: 'Helvetica Neue', Helvetica;
16+
font-size: 16px;
17+
height: 30px;
18+
line-height: 16px;
19+
margin: 45px 0px 0px 45px;
20+
padding: 0px 8px 0px 8px;
21+
position: relative;
22+
vertical-align: baseline;
23+
}
24+
25+
h1, h2, h3, h4, h5 {
26+
color: #0067AB;
27+
display: block;
28+
font: inherit;
29+
font-family: 'Open Sans', 'Helvetica Neue', Helvetica;
30+
font-size: 30px;
31+
font-weight: 600;
32+
height: 30px;
33+
line-height: 30px;
34+
margin: 0 0 15px 0;
35+
padding: 0 0 0 0;
36+
}
37+
</style>
38+
</head>
39+
40+
<body>
41+
<div class="container">
42+
<h1>No link sent. User not found or email already verified</h1>
43+
</div>
44+
</body>
45+
</html>

public_html/link_send_success.html

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<!DOCTYPE html>
2+
<!-- This page is displayed when someone navigates to a verify email link with an invalid
3+
security token and requests a link resend. This page is displayed when the username
4+
from the original verification link has been found and a new verification link has
5+
been successfully sent to the corresponding stored email
6+
-->
7+
<html>
8+
<head>
9+
<title>Invalid Link</title>
10+
<style type='text/css'>
11+
.container {
12+
border-width: 0px;
13+
display: block;
14+
font: inherit;
15+
font-family: 'Helvetica Neue', Helvetica;
16+
font-size: 16px;
17+
height: 30px;
18+
line-height: 16px;
19+
margin: 45px 0px 0px 45px;
20+
padding: 0px 8px 0px 8px;
21+
position: relative;
22+
vertical-align: baseline;
23+
}
24+
25+
h1, h2, h3, h4, h5 {
26+
color: #0067AB;
27+
display: block;
28+
font: inherit;
29+
font-family: 'Open Sans', 'Helvetica Neue', Helvetica;
30+
font-size: 30px;
31+
font-weight: 600;
32+
height: 30px;
33+
line-height: 30px;
34+
margin: 0 0 15px 0;
35+
padding: 0 0 0 0;
36+
}
37+
</style>
38+
</head>
39+
40+
<body>
41+
<div class="container">
42+
<h1>Link Sent! Check your email.</h1>
43+
</div>
44+
</body>
45+
</html>

spec/EmailVerificationToken.spec.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const Config = require('../src/Config');
66

77
describe("Email Verification Token Expiration: ", () => {
88

9-
it('show the invalid link page, if the user clicks on the verify email link after the email verify token expires', done => {
9+
it('show the invalid verification link page, if the user clicks on the verify email link after the email verify token expires', done => {
1010
var user = new Parse.User();
1111
var sendEmailOptions;
1212
var emailAdapter = {
@@ -37,7 +37,7 @@ describe("Email Verification Token Expiration: ", () => {
3737
followRedirect: false,
3838
}, (error, response) => {
3939
expect(response.statusCode).toEqual(302);
40-
expect(response.body).toEqual('Found. Redirecting to http://localhost:8378/1/apps/invalid_link.html');
40+
expect(response.body).toEqual('Found. Redirecting to http://localhost:8378/1/apps/invalid_verification_link.html?username=testEmailVerifyTokenValidity&appId=test');
4141
done();
4242
});
4343
}, 1000);
@@ -313,7 +313,7 @@ describe("Email Verification Token Expiration: ", () => {
313313
});
314314
});
315315

316-
it('clicking on the email verify link by an email VERIFIED user that was setup before enabling the expire email verify token should show an invalid link', done => {
316+
it('clicking on the email verify link by an email VERIFIED user that was setup before enabling the expire email verify token should show email verify email success', done => {
317317
var user = new Parse.User();
318318
var sendEmailOptions;
319319
var emailAdapter = {
@@ -359,7 +359,7 @@ describe("Email Verification Token Expiration: ", () => {
359359
followRedirect: false,
360360
}, (error, response) => {
361361
expect(response.statusCode).toEqual(302);
362-
expect(response.body).toEqual('Found. Redirecting to http://localhost:8378/1/apps/invalid_link.html');
362+
expect(response.body).toEqual('Found. Redirecting to http://localhost:8378/1/apps/verify_email_success.html?username=testEmailVerifyTokenValidity');
363363
done();
364364
});
365365
})
@@ -369,7 +369,7 @@ describe("Email Verification Token Expiration: ", () => {
369369
});
370370
});
371371

372-
it('clicking on the email verify link by an email UNVERIFIED user that was setup before enabling the expire email verify token should show an invalid link', done => {
372+
it('clicking on the email verify link by an email UNVERIFIED user that was setup before enabling the expire email verify token should show invalid verficiation link page', done => {
373373
var user = new Parse.User();
374374
var sendEmailOptions;
375375
var emailAdapter = {
@@ -409,7 +409,7 @@ describe("Email Verification Token Expiration: ", () => {
409409
followRedirect: false,
410410
}, (error, response) => {
411411
expect(response.statusCode).toEqual(302);
412-
expect(response.body).toEqual('Found. Redirecting to http://localhost:8378/1/apps/invalid_link.html');
412+
expect(response.body).toEqual('Found. Redirecting to http://localhost:8378/1/apps/invalid_verification_link.html?username=testEmailVerifyTokenValidity&appId=test');
413413
done();
414414
});
415415
})

spec/ValidationAndPasswordsReset.spec.js

+28-3
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ describe("Custom Pages, Email Verification, Password Reset", () => {
655655
});
656656
});
657657

658-
it('redirects you to invalid link if you try to validate a nonexistant users email', done => {
658+
it('redirects you to invalid verification link page if you try to validate a nonexistant users email', done => {
659659
reconfigureServer({
660660
appName: 'emailing app',
661661
verifyUserEmails: true,
@@ -671,7 +671,32 @@ describe("Custom Pages, Email Verification, Password Reset", () => {
671671
followRedirect: false,
672672
}, (error, response) => {
673673
expect(response.statusCode).toEqual(302);
674-
expect(response.body).toEqual('Found. Redirecting to http://localhost:8378/1/apps/invalid_link.html');
674+
expect(response.body).toEqual('Found. Redirecting to http://localhost:8378/1/apps/invalid_verification_link.html?username=sadfasga&appId=test');
675+
done();
676+
});
677+
});
678+
});
679+
680+
it('redirects you to link send fail page if you try to resend a link for a nonexistant user', done => {
681+
reconfigureServer({
682+
appName: 'emailing app',
683+
verifyUserEmails: true,
684+
emailAdapter: {
685+
sendVerificationEmail: () => Promise.resolve(),
686+
sendPasswordResetEmail: () => Promise.resolve(),
687+
sendMail: () => {}
688+
},
689+
publicServerURL: "http://localhost:8378/1"
690+
})
691+
.then(() => {
692+
request.post('http://localhost:8378/1/apps/test/resend_verification_email', {
693+
followRedirect: false,
694+
form: {
695+
username: "sadfasga"
696+
}
697+
}, (error, response) => {
698+
expect(response.statusCode).toEqual(302);
699+
expect(response.body).toEqual('Found. Redirecting to http://localhost:8378/1/apps/link_send_fail.html');
675700
done();
676701
});
677702
});
@@ -685,7 +710,7 @@ describe("Custom Pages, Email Verification, Password Reset", () => {
685710
followRedirect: false,
686711
}, (error, response) => {
687712
expect(response.statusCode).toEqual(302);
688-
expect(response.body).toEqual('Found. Redirecting to http://localhost:8378/1/apps/invalid_link.html');
713+
expect(response.body).toEqual('Found. Redirecting to http://localhost:8378/1/apps/invalid_verification_link.html?username=zxcv&appId=test');
689714
user.fetch()
690715
.then(() => {
691716
expect(user.get('emailVerified')).toEqual(false);

src/Config.js

+12
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,18 @@ export class Config {
234234
return this.customPages.invalidLink || `${this.publicServerURL}/apps/invalid_link.html`;
235235
}
236236

237+
get invalidVerificationLinkURL() {
238+
return this.customPages.invalidVerificationLink || `${this.publicServerURL}/apps/invalid_verification_link.html`;
239+
}
240+
241+
get linkSendSuccessURL() {
242+
return this.customPages.linkSendSuccess || `${this.publicServerURL}/apps/link_send_success.html`
243+
}
244+
245+
get linkSendFailURL() {
246+
return this.customPages.linkSendFail || `${this.publicServerURL}/apps/link_send_fail.html`
247+
}
248+
237249
get verifyEmailSuccessURL() {
238250
return this.customPages.verifyEmailSuccess || `${this.publicServerURL}/apps/verify_email_success.html`;
239251
}

src/Controllers/UserController.js

+22-4
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,17 @@ export class UserController extends AdaptableController {
6060
updateFields._email_verify_token_expires_at = {__op: 'Delete'};
6161
}
6262

63-
return this.config.database.update('_User', query, updateFields).then((document) => {
64-
if (!document) {
65-
throw undefined;
63+
var checkIfAlreadyVerified = new RestQuery(this.config, Auth.master(this.config), '_User', {username: username, emailVerified: true});
64+
return checkIfAlreadyVerified.execute().then(result => {
65+
if (result.results.length) {
66+
return Promise.resolve(result.results.length[0]);
6667
}
67-
return Promise.resolve(document);
68+
return this.config.database.update('_User', query, updateFields).then((document) => {
69+
if (!document) {
70+
throw undefined
71+
}
72+
return Promise.resolve(document);
73+
})
6874
});
6975
}
7076

@@ -134,6 +140,18 @@ export class UserController extends AdaptableController {
134140
});
135141
}
136142

143+
resendVerificationEmail(username) {
144+
return this.getUserIfNeeded({username: username}).then((aUser) => {
145+
if (!aUser || aUser.emailVerified) {
146+
throw undefined;
147+
}
148+
this.setEmailVerifyToken(aUser);
149+
return this.config.database.update('_User', {username}, aUser).then(() => {
150+
this.sendVerificationEmail(aUser);
151+
});
152+
});
153+
}
154+
137155
setPasswordResetToken(email) {
138156
const token = { _perishable_token: randomString(25) };
139157

0 commit comments

Comments
 (0)