Skip to content
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

[ARRSOF-66] "requireSessionLogin" requires username and pass or sessi… #30

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions lib/lifecycle/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,32 @@ exports.login = function login(request, response, next) {
username = headers.user,
password = headers.pass;

// Remove the db from cache and from the connector variables
function deleteDb() {
self.db = null;

if(self.baseContext && self.baseContext.db) {
self.baseContext.db = null;
}

if(self.cache &&
request.headers &&
typeof request.headers.sessioncookiestring !== 'undefined' &&
request.headers.sessioncookiestring !== null) {
self.cache.set(request.headers.sessioncookiestring, null);
}
}

// login to get the user and make sure this session is valid
// but only do it once for a session
if (this.db && !username && !password && !request.cookies.arrowdbuid) {
if (this.db && !username && !password && !request.cookies.arrowdbuid && this.config.requireSessionLogin !== true) {
if (this.db.arrowdbuid) {
// we already have it
return next();
}
return this.db.usersShowMe(function (err, result) {
if (err) {
self.db = null;
deleteDb();
return next('Authentication is required. Invalid or expired sessionCookieString header passed.');
}
self.user = result && result.body && result.body.response && result.body.response.users && result.body.response.users[0];
Expand All @@ -33,7 +49,8 @@ exports.login = function login(request, response, next) {
}

if (!username || !password) {
if (this.config.requireSessionLogin) {
if (this.config.requireSessionLogin === true) {
deleteDb();
return next('Authentication is required. Please pass these headers: user, and pass; or sessionCookieString.');
}
else {
Expand All @@ -47,7 +64,7 @@ exports.login = function login(request, response, next) {
password: password
}, function loginCallback(err, result) {
if (err) {
self.db = self.baseContext.db = null;
deleteDb();
return next(err);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/lifecycle/loginRequired.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var ArrowDB = require('arrowdb');
* loginRequired checks to see if the current req for this connector requires the user to login.
*/
exports.loginRequired = function loginRequired(request, next) {
if (this.db || !request) {
if (this.config.requireSessionLogin !== true && (this.db || !request)) {
return next(null, false);
}
if (request.headers && request.headers.sessioncookiestring) {
Expand Down
8 changes: 4 additions & 4 deletions test/test-connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ describe('Connector', function () {
});
});

it('should return basedb', function (done) {
var basedb = this.connector.getDB();
should(basedb).be.an.Object;
done();
it('should throw error', function () {
should(function() {
this.connector.getDB();
}).throw();
});

it('should return db', function (done) {
Expand Down