Skip to content

Commit 43bc253

Browse files
flovilmartRafael Santos
authored andcommitted
Creates a new sessionToken when updating password (parse-community#2266)
* Creates a new sessionToken when updating password * Adds test ensuring email is properly sent when upgrading from anon
1 parent e6a213c commit 43bc253

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

spec/ParseUser.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2203,6 +2203,26 @@ describe('Parse.User testing', () => {
22032203
});
22042204
});
22052205

2206+
it("invalid session tokens are rejected", (done) => {
2207+
Parse.User.signUp("asdf", "zxcv", null, {
2208+
success: function(user) {
2209+
request.get({
2210+
url: 'http://localhost:8378/1/classes/AClass',
2211+
json: true,
2212+
headers: {
2213+
'X-Parse-Application-Id': 'test',
2214+
'X-Parse-Rest-API-Key': 'rest',
2215+
'X-Parse-Session-Token': 'text'
2216+
},
2217+
}, (error, response, body) => {
2218+
expect(body.code).toBe(209);
2219+
expect(body.error).toBe('invalid session token');
2220+
done();
2221+
})
2222+
}
2223+
});
2224+
});
2225+
22062226
it_exclude_dbs(['postgres'])('should cleanup null authData keys (regression test for #935)', (done) => {
22072227
let database = new Config(Parse.applicationId).database;
22082228
database.create('_User', {

src/RestWrite.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ RestWrite.prototype.transformUser = function() {
367367
}
368368
if (this.query && !this.auth.isMaster ) {
369369
this.storage['clearSessions'] = true;
370+
this.storage['generateNewSession'] = true;
370371
}
371372
return passwordCrypto.hash(this.data.password).then((hashedPassword) => {
372373
this.data._hashed_password = hashedPassword;
@@ -428,6 +429,10 @@ RestWrite.prototype.createSessionTokenIfNeeded = function() {
428429
if (this.query) {
429430
return;
430431
}
432+
return this.createSessionToken();
433+
}
434+
435+
RestWrite.prototype.createSessionToken = function() {
431436
var token = 'r:' + cryptoUtils.newToken();
432437

433438
var expiresAt = this.config.generateSessionExpiresAt();
@@ -464,15 +469,21 @@ RestWrite.prototype.handleFollowup = function() {
464469
}
465470
};
466471
delete this.storage['clearSessions'];
467-
this.config.database.destroy('_Session', sessionQuery)
472+
return this.config.database.destroy('_Session', sessionQuery)
473+
.then(this.handleFollowup.bind(this));
474+
}
475+
476+
if (this.storage && this.storage['generateNewSession']) {
477+
delete this.storage['generateNewSession'];
478+
return this.createSessionToken()
468479
.then(this.handleFollowup.bind(this));
469480
}
470481

471482
if (this.storage && this.storage['sendVerificationEmail']) {
472483
delete this.storage['sendVerificationEmail'];
473484
// Fire and forget!
474485
this.config.userController.sendVerificationEmail(this.data);
475-
this.handleFollowup.bind(this);
486+
return this.handleFollowup.bind(this);
476487
}
477488
};
478489

0 commit comments

Comments
 (0)