Skip to content

Commit 37c502b

Browse files
steven-supersoliddrew-gross
authored andcommitted
Add config.expireInactiveSession to add support for non-expiring inactive sessions (#1536)
* Create non-expiring session when sessionLength is zero * Introduce expireInactiveSessions setting
1 parent b00572d commit 37c502b

File tree

4 files changed

+66
-8
lines changed

4 files changed

+66
-8
lines changed

spec/RestCreate.spec.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,4 +352,31 @@ describe('rest create', () => {
352352
done();
353353
});
354354
});
355+
356+
it("can create a session with no expiration", (done) => {
357+
var user = {
358+
username: 'asdf',
359+
password: 'zxcv',
360+
foo: 'bar'
361+
};
362+
config.expireInactiveSessions = false;
363+
364+
rest.create(config, auth.nobody(config), '_User', user)
365+
.then((r) => {
366+
expect(Object.keys(r.response).length).toEqual(3);
367+
expect(typeof r.response.objectId).toEqual('string');
368+
expect(typeof r.response.createdAt).toEqual('string');
369+
expect(typeof r.response.sessionToken).toEqual('string');
370+
return rest.find(config, auth.master(config),
371+
'_Session', {sessionToken: r.response.sessionToken});
372+
})
373+
.then((r) => {
374+
expect(r.results.length).toEqual(1);
375+
376+
var session = r.results[0];
377+
expect(session.expiresAt).toBeUndefined();
378+
379+
done();
380+
});
381+
});
355382
});

spec/index.spec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,29 @@ describe('server', () => {
332332
sessionLength: '0'
333333
})).toThrow('Session length must be a value greater than 0.');
334334
done();
335+
});
336+
337+
it('ignores the session length when expireInactiveSessions set to false', (done) => {
338+
expect(() => setServerConfiguration({
339+
serverURL: 'http://localhost:8378/1',
340+
appId: 'test',
341+
appName: 'unused',
342+
javascriptKey: 'test',
343+
masterKey: 'test',
344+
sessionLength: '-33',
345+
expireInactiveSessions: false
346+
})).not.toThrow();
347+
348+
expect(() => setServerConfiguration({
349+
serverURL: 'http://localhost:8378/1',
350+
appId: 'test',
351+
appName: 'unused',
352+
javascriptKey: 'test',
353+
masterKey: 'test',
354+
sessionLength: '0',
355+
expireInactiveSessions: false
356+
})).not.toThrow();
357+
done();
335358
})
336359

337360
it('fails if you try to set revokeSessionOnPasswordReset to non-boolean', done => {

src/Config.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export class Config {
4848
this.mount = removeTrailingSlash(mount);
4949
this.liveQueryController = cacheInfo.liveQueryController;
5050
this.sessionLength = cacheInfo.sessionLength;
51+
this.expireInactiveSessions = cacheInfo.expireInactiveSessions;
5152
this.generateSessionExpiresAt = this.generateSessionExpiresAt.bind(this);
5253
this.revokeSessionOnPasswordReset = cacheInfo.revokeSessionOnPasswordReset;
5354
}
@@ -69,7 +70,7 @@ export class Config {
6970
}
7071
}
7172

72-
this.validateSessionLength(options.sessionLength);
73+
this.validateSessionConfiguration(options.sessionLength, options.expireInactiveSessions);
7374
}
7475

7576
static validateEmailConfiguration({verifyUserEmails, appName, publicServerURL}) {
@@ -95,16 +96,21 @@ export class Config {
9596
this._mount = newValue;
9697
}
9798

98-
static validateSessionLength(sessionLength) {
99-
if(isNaN(sessionLength)) {
100-
throw 'Session length must be a valid number.';
101-
}
102-
else if(sessionLength <= 0) {
103-
throw 'Session length must be a value greater than 0.'
99+
static validateSessionConfiguration(sessionLength, expireInactiveSessions) {
100+
if (expireInactiveSessions) {
101+
if (isNaN(sessionLength)) {
102+
throw 'Session length must be a valid number.';
103+
}
104+
else if (sessionLength <= 0) {
105+
throw 'Session length must be a value greater than 0.'
106+
}
104107
}
105108
}
106109

107110
generateSessionExpiresAt() {
111+
if (!this.expireInactiveSessions) {
112+
return undefined;
113+
}
108114
var now = new Date();
109115
return new Date(now.getTime() + (this.sessionLength*1000));
110116
}
@@ -132,7 +138,7 @@ export class Config {
132138
get verifyEmailURL() {
133139
return `${this.publicServerURL}/apps/${this.applicationId}/verify_email`;
134140
}
135-
};
141+
}
136142

137143
export default Config;
138144
module.exports = Config;

src/ParseServer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ class ParseServer {
114114
},
115115
liveQuery = {},
116116
sessionLength = 31536000, // 1 Year in seconds
117+
expireInactiveSessions = true,
117118
verbose = false,
118119
revokeSessionOnPasswordReset = true,
119120
}) {
@@ -188,6 +189,7 @@ class ParseServer {
188189
maxUploadSize: maxUploadSize,
189190
liveQueryController: liveQueryController,
190191
sessionLength: Number(sessionLength),
192+
expireInactiveSessions: expireInactiveSessions,
191193
revokeSessionOnPasswordReset
192194
});
193195

0 commit comments

Comments
 (0)