Add config.expireInactiveSession to add support for non-expiring inactive sessions (#1536)

* Create non-expiring session when sessionLength is zero

* Introduce expireInactiveSessions setting
This commit is contained in:
Steven Shipton
2016-05-06 20:50:45 +01:00
committed by Drew
parent b00572de65
commit 37c502bed0
4 changed files with 66 additions and 8 deletions

View File

@@ -352,4 +352,31 @@ describe('rest create', () => {
done(); done();
}); });
}); });
it("can create a session with no expiration", (done) => {
var user = {
username: 'asdf',
password: 'zxcv',
foo: 'bar'
};
config.expireInactiveSessions = false;
rest.create(config, auth.nobody(config), '_User', user)
.then((r) => {
expect(Object.keys(r.response).length).toEqual(3);
expect(typeof r.response.objectId).toEqual('string');
expect(typeof r.response.createdAt).toEqual('string');
expect(typeof r.response.sessionToken).toEqual('string');
return rest.find(config, auth.master(config),
'_Session', {sessionToken: r.response.sessionToken});
})
.then((r) => {
expect(r.results.length).toEqual(1);
var session = r.results[0];
expect(session.expiresAt).toBeUndefined();
done();
});
});
}); });

View File

@@ -332,6 +332,29 @@ describe('server', () => {
sessionLength: '0' sessionLength: '0'
})).toThrow('Session length must be a value greater than 0.'); })).toThrow('Session length must be a value greater than 0.');
done(); done();
});
it('ignores the session length when expireInactiveSessions set to false', (done) => {
expect(() => setServerConfiguration({
serverURL: 'http://localhost:8378/1',
appId: 'test',
appName: 'unused',
javascriptKey: 'test',
masterKey: 'test',
sessionLength: '-33',
expireInactiveSessions: false
})).not.toThrow();
expect(() => setServerConfiguration({
serverURL: 'http://localhost:8378/1',
appId: 'test',
appName: 'unused',
javascriptKey: 'test',
masterKey: 'test',
sessionLength: '0',
expireInactiveSessions: false
})).not.toThrow();
done();
}) })
it('fails if you try to set revokeSessionOnPasswordReset to non-boolean', done => { it('fails if you try to set revokeSessionOnPasswordReset to non-boolean', done => {

View File

@@ -48,6 +48,7 @@ export class Config {
this.mount = removeTrailingSlash(mount); this.mount = removeTrailingSlash(mount);
this.liveQueryController = cacheInfo.liveQueryController; this.liveQueryController = cacheInfo.liveQueryController;
this.sessionLength = cacheInfo.sessionLength; this.sessionLength = cacheInfo.sessionLength;
this.expireInactiveSessions = cacheInfo.expireInactiveSessions;
this.generateSessionExpiresAt = this.generateSessionExpiresAt.bind(this); this.generateSessionExpiresAt = this.generateSessionExpiresAt.bind(this);
this.revokeSessionOnPasswordReset = cacheInfo.revokeSessionOnPasswordReset; this.revokeSessionOnPasswordReset = cacheInfo.revokeSessionOnPasswordReset;
} }
@@ -69,7 +70,7 @@ export class Config {
} }
} }
this.validateSessionLength(options.sessionLength); this.validateSessionConfiguration(options.sessionLength, options.expireInactiveSessions);
} }
static validateEmailConfiguration({verifyUserEmails, appName, publicServerURL}) { static validateEmailConfiguration({verifyUserEmails, appName, publicServerURL}) {
@@ -95,16 +96,21 @@ export class Config {
this._mount = newValue; this._mount = newValue;
} }
static validateSessionLength(sessionLength) { static validateSessionConfiguration(sessionLength, expireInactiveSessions) {
if(isNaN(sessionLength)) { if (expireInactiveSessions) {
throw 'Session length must be a valid number.'; if (isNaN(sessionLength)) {
} throw 'Session length must be a valid number.';
else if(sessionLength <= 0) { }
throw 'Session length must be a value greater than 0.' else if (sessionLength <= 0) {
throw 'Session length must be a value greater than 0.'
}
} }
} }
generateSessionExpiresAt() { generateSessionExpiresAt() {
if (!this.expireInactiveSessions) {
return undefined;
}
var now = new Date(); var now = new Date();
return new Date(now.getTime() + (this.sessionLength*1000)); return new Date(now.getTime() + (this.sessionLength*1000));
} }
@@ -132,7 +138,7 @@ export class Config {
get verifyEmailURL() { get verifyEmailURL() {
return `${this.publicServerURL}/apps/${this.applicationId}/verify_email`; return `${this.publicServerURL}/apps/${this.applicationId}/verify_email`;
} }
}; }
export default Config; export default Config;
module.exports = Config; module.exports = Config;

View File

@@ -114,6 +114,7 @@ class ParseServer {
}, },
liveQuery = {}, liveQuery = {},
sessionLength = 31536000, // 1 Year in seconds sessionLength = 31536000, // 1 Year in seconds
expireInactiveSessions = true,
verbose = false, verbose = false,
revokeSessionOnPasswordReset = true, revokeSessionOnPasswordReset = true,
}) { }) {
@@ -188,6 +189,7 @@ class ParseServer {
maxUploadSize: maxUploadSize, maxUploadSize: maxUploadSize,
liveQueryController: liveQueryController, liveQueryController: liveQueryController,
sessionLength: Number(sessionLength), sessionLength: Number(sessionLength),
expireInactiveSessions: expireInactiveSessions,
revokeSessionOnPasswordReset revokeSessionOnPasswordReset
}); });