2015-12-14 23:05:11 +03:00
|
|
|
var sinon = require('sinon'),
|
2015-10-22 16:28:47 +03:00
|
|
|
should = require('should'),
|
|
|
|
passport = require('passport'),
|
|
|
|
rewire = require('rewire'),
|
2015-12-14 18:35:19 +03:00
|
|
|
errors = require('../../../server/errors'),
|
2015-10-22 16:28:47 +03:00
|
|
|
auth = rewire('../../../server/middleware/auth'),
|
|
|
|
BearerStrategy = require('passport-http-bearer').Strategy,
|
|
|
|
ClientPasswordStrategy = require('passport-oauth2-client-password').Strategy,
|
|
|
|
user = {id: 1},
|
|
|
|
info = {scope: '*'},
|
|
|
|
token = 'test_token',
|
|
|
|
testClient = 'test_client',
|
|
|
|
testSecret = 'not_available',
|
|
|
|
client = {
|
|
|
|
id: 2,
|
|
|
|
type: 'ua'
|
2015-12-14 18:35:19 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
sandbox = sinon.sandbox.create();
|
2015-07-15 19:01:23 +03:00
|
|
|
|
|
|
|
should.equal(true, true);
|
|
|
|
|
2015-08-09 13:50:05 +03:00
|
|
|
function registerSuccessfulBearerStrategy() {
|
|
|
|
// register fake BearerStrategy which always authenticates
|
|
|
|
passport.use(new BearerStrategy(
|
|
|
|
function strategy(accessToken, done) {
|
|
|
|
accessToken.should.eql(token);
|
|
|
|
return done(null, user, info);
|
|
|
|
}
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
function registerUnsuccessfulBearerStrategy() {
|
|
|
|
// register fake BearerStrategy which always authenticates
|
|
|
|
passport.use(new BearerStrategy(
|
|
|
|
function strategy(accessToken, done) {
|
|
|
|
accessToken.should.eql(token);
|
|
|
|
return done(null, false);
|
|
|
|
}
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2015-10-22 16:28:47 +03:00
|
|
|
function registerFaultyBearerStrategy() {
|
|
|
|
// register fake BearerStrategy which always authenticates
|
|
|
|
passport.use(new BearerStrategy(
|
|
|
|
function strategy(accessToken, done) {
|
|
|
|
accessToken.should.eql(token);
|
|
|
|
return done('error');
|
|
|
|
}
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
function registerSuccessfulClientPasswordStrategy() {
|
|
|
|
// register fake BearerStrategy which always authenticates
|
|
|
|
passport.use(new ClientPasswordStrategy(
|
|
|
|
function strategy(clientId, clientSecret, done) {
|
|
|
|
clientId.should.eql(testClient);
|
|
|
|
clientSecret.should.eql('not_available');
|
|
|
|
return done(null, client);
|
|
|
|
}
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
function registerUnsuccessfulClientPasswordStrategy() {
|
|
|
|
// register fake BearerStrategy which always authenticates
|
|
|
|
passport.use(new ClientPasswordStrategy(
|
|
|
|
function strategy(clientId, clientSecret, done) {
|
|
|
|
clientId.should.eql(testClient);
|
|
|
|
clientSecret.should.eql('not_available');
|
|
|
|
return done(null, false);
|
|
|
|
}
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
function registerFaultyClientPasswordStrategy() {
|
|
|
|
// register fake BearerStrategy which always authenticates
|
|
|
|
passport.use(new ClientPasswordStrategy(
|
|
|
|
function strategy(clientId, clientSecret, done) {
|
|
|
|
clientId.should.eql(testClient);
|
|
|
|
clientSecret.should.eql('not_available');
|
|
|
|
return done('error');
|
|
|
|
}
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('Auth', function () {
|
2015-12-14 18:35:19 +03:00
|
|
|
var res, req, next, errorStub;
|
2015-07-15 19:01:23 +03:00
|
|
|
|
|
|
|
beforeEach(function () {
|
2015-08-09 13:50:05 +03:00
|
|
|
req = {};
|
|
|
|
res = {};
|
|
|
|
next = sandbox.spy();
|
2015-12-14 18:35:19 +03:00
|
|
|
errorStub = sandbox.stub(errors, 'logError');
|
2015-07-15 19:01:23 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
sandbox.restore();
|
|
|
|
});
|
|
|
|
|
2015-10-22 16:28:47 +03:00
|
|
|
it('should require authorized user (user exists)', function (done) {
|
|
|
|
req.user = {id: 1};
|
2015-07-15 19:01:23 +03:00
|
|
|
|
2015-10-22 16:28:47 +03:00
|
|
|
auth.requiresAuthorizedUser(req, res, next);
|
2016-02-08 00:27:01 +03:00
|
|
|
next.called.should.be.true();
|
|
|
|
next.calledWith().should.be.true();
|
2015-08-09 13:50:05 +03:00
|
|
|
done();
|
2015-07-15 19:01:23 +03:00
|
|
|
});
|
|
|
|
|
2015-10-22 16:28:47 +03:00
|
|
|
it('should require authorized user (user is missing)', function (done) {
|
|
|
|
req.user = false;
|
|
|
|
res.status = {};
|
2015-08-09 13:50:05 +03:00
|
|
|
|
2015-10-22 16:28:47 +03:00
|
|
|
sandbox.stub(res, 'status', function (statusCode) {
|
|
|
|
statusCode.should.eql(403);
|
|
|
|
return {
|
|
|
|
json: function (err) {
|
|
|
|
err.errors[0].errorType.should.eql('NoPermissionError');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
2015-08-09 13:50:05 +03:00
|
|
|
|
2015-10-22 16:28:47 +03:00
|
|
|
auth.requiresAuthorizedUser(req, res, next);
|
2016-02-08 00:27:01 +03:00
|
|
|
next.called.should.be.false();
|
2015-08-09 13:50:05 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2015-10-22 16:28:47 +03:00
|
|
|
describe('User Authentication', function () {
|
|
|
|
it('should authenticate user', function (done) {
|
|
|
|
req.headers = {};
|
|
|
|
req.headers.authorization = 'Bearer ' + token;
|
2015-08-09 13:50:05 +03:00
|
|
|
|
2015-10-22 16:28:47 +03:00
|
|
|
registerSuccessfulBearerStrategy();
|
|
|
|
auth.authenticateUser(req, res, next);
|
2015-08-09 13:50:05 +03:00
|
|
|
|
2016-02-08 00:27:01 +03:00
|
|
|
next.called.should.be.true();
|
|
|
|
next.calledWith(null, user, info).should.be.true();
|
2015-10-22 16:28:47 +03:00
|
|
|
done();
|
|
|
|
});
|
2015-08-09 13:50:05 +03:00
|
|
|
|
2015-10-22 16:28:47 +03:00
|
|
|
it('shouldn\'t pass with client, no bearer token', function (done) {
|
|
|
|
req.headers = {};
|
|
|
|
req.client = {id: 1};
|
|
|
|
res.status = {};
|
2015-08-09 13:50:05 +03:00
|
|
|
|
2015-10-22 16:28:47 +03:00
|
|
|
auth.authenticateUser(req, res, next);
|
2015-08-09 13:50:05 +03:00
|
|
|
|
2016-02-08 00:27:01 +03:00
|
|
|
next.called.should.be.true();
|
|
|
|
next.calledWith().should.be.true();
|
2015-10-22 16:28:47 +03:00
|
|
|
done();
|
|
|
|
});
|
2015-08-09 13:50:05 +03:00
|
|
|
|
2015-10-22 16:28:47 +03:00
|
|
|
it('shouldn\'t authenticate user', function (done) {
|
|
|
|
req.headers = {};
|
|
|
|
req.headers.authorization = 'Bearer ' + token;
|
|
|
|
res.status = {};
|
|
|
|
|
|
|
|
sandbox.stub(res, 'status', function (statusCode) {
|
|
|
|
statusCode.should.eql(401);
|
|
|
|
return {
|
|
|
|
json: function (err) {
|
|
|
|
err.errors[0].errorType.should.eql('UnauthorizedError');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
registerUnsuccessfulBearerStrategy();
|
|
|
|
auth.authenticateUser(req, res, next);
|
|
|
|
|
2016-02-08 00:27:01 +03:00
|
|
|
next.called.should.be.false();
|
2015-10-22 16:28:47 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('shouldn\'t authenticate without bearer token', function (done) {
|
|
|
|
req.headers = {};
|
|
|
|
res.status = {};
|
|
|
|
|
|
|
|
sandbox.stub(res, 'status', function (statusCode) {
|
|
|
|
statusCode.should.eql(401);
|
|
|
|
return {
|
|
|
|
json: function (err) {
|
|
|
|
err.errors[0].errorType.should.eql('UnauthorizedError');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
registerUnsuccessfulBearerStrategy();
|
|
|
|
auth.authenticateUser(req, res, next);
|
|
|
|
|
2016-02-08 00:27:01 +03:00
|
|
|
next.called.should.be.false();
|
2015-10-22 16:28:47 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('shouldn\'t authenticate with bearer token and client', function (done) {
|
|
|
|
req.headers = {};
|
|
|
|
req.headers.authorization = 'Bearer ' + token;
|
|
|
|
req.client = {id: 1};
|
|
|
|
res.status = {};
|
|
|
|
|
|
|
|
sandbox.stub(res, 'status', function (statusCode) {
|
|
|
|
statusCode.should.eql(401);
|
|
|
|
return {
|
|
|
|
json: function (err) {
|
|
|
|
err.errors[0].errorType.should.eql('UnauthorizedError');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
registerUnsuccessfulBearerStrategy();
|
|
|
|
auth.authenticateUser(req, res, next);
|
|
|
|
|
2016-02-08 00:27:01 +03:00
|
|
|
next.called.should.be.false();
|
2015-10-22 16:28:47 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('shouldn\'t authenticate when error', function (done) {
|
|
|
|
req.headers = {};
|
|
|
|
req.headers.authorization = 'Bearer ' + token;
|
|
|
|
|
|
|
|
registerFaultyBearerStrategy();
|
|
|
|
auth.authenticateUser(req, res, next);
|
|
|
|
|
2016-02-08 00:27:01 +03:00
|
|
|
next.called.should.be.true();
|
|
|
|
next.calledWith('error').should.be.true();
|
2015-10-22 16:28:47 +03:00
|
|
|
done();
|
|
|
|
});
|
2015-08-09 13:50:05 +03:00
|
|
|
});
|
|
|
|
|
2015-10-22 16:28:47 +03:00
|
|
|
describe('Client Authentication', function () {
|
|
|
|
it('shouldn\'t require authorized client with bearer token', function (done) {
|
|
|
|
req.headers = {};
|
|
|
|
req.headers.authorization = 'Bearer ' + token;
|
2015-08-09 13:50:05 +03:00
|
|
|
|
2015-10-22 16:28:47 +03:00
|
|
|
auth.authenticateClient(req, res, next);
|
2016-02-08 00:27:01 +03:00
|
|
|
next.called.should.be.true();
|
|
|
|
next.calledWith().should.be.true();
|
2015-10-22 16:28:47 +03:00
|
|
|
done();
|
|
|
|
});
|
2015-07-15 19:01:23 +03:00
|
|
|
|
2015-10-22 16:28:47 +03:00
|
|
|
it('shouldn\'t authenticate client with broken bearer token', function (done) {
|
|
|
|
req.body = {};
|
|
|
|
req.headers = {};
|
|
|
|
req.headers.authorization = 'Bearer';
|
|
|
|
res.status = {};
|
|
|
|
|
|
|
|
sandbox.stub(res, 'status', function (statusCode) {
|
|
|
|
statusCode.should.eql(401);
|
|
|
|
return {
|
|
|
|
json: function (err) {
|
|
|
|
err.errors[0].errorType.should.eql('UnauthorizedError');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
auth.authenticateClient(req, res, next);
|
2016-02-08 00:27:01 +03:00
|
|
|
next.called.should.be.false();
|
2015-10-22 16:28:47 +03:00
|
|
|
done();
|
2015-07-15 19:01:23 +03:00
|
|
|
});
|
|
|
|
|
2015-10-22 16:28:47 +03:00
|
|
|
it('shouldn\'t authenticate client without client_id/client_secret', function (done) {
|
|
|
|
req.body = {};
|
|
|
|
res.status = {};
|
|
|
|
|
|
|
|
sandbox.stub(res, 'status', function (statusCode) {
|
|
|
|
statusCode.should.eql(401);
|
|
|
|
return {
|
|
|
|
json: function (err) {
|
|
|
|
err.errors[0].errorType.should.eql('UnauthorizedError');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
auth.authenticateClient(req, res, next);
|
2016-02-08 00:27:01 +03:00
|
|
|
next.called.should.be.false();
|
2015-10-22 16:28:47 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('shouldn\'t authenticate client without client_id', function (done) {
|
|
|
|
req.body = {};
|
|
|
|
req.body.client_secret = testSecret;
|
|
|
|
res.status = {};
|
|
|
|
|
|
|
|
sandbox.stub(res, 'status', function (statusCode) {
|
|
|
|
statusCode.should.eql(401);
|
|
|
|
return {
|
|
|
|
json: function (err) {
|
|
|
|
err.errors[0].errorType.should.eql('UnauthorizedError');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
auth.authenticateClient(req, res, next);
|
2016-02-08 00:27:01 +03:00
|
|
|
next.called.should.be.false();
|
2015-10-22 16:28:47 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('shouldn\'t authenticate client without client_secret', function (done) {
|
|
|
|
req.body = {};
|
|
|
|
req.body.client_id = testClient;
|
|
|
|
res.status = {};
|
|
|
|
|
|
|
|
sandbox.stub(res, 'status', function (statusCode) {
|
|
|
|
statusCode.should.eql(401);
|
|
|
|
return {
|
|
|
|
json: function (err) {
|
|
|
|
err.errors[0].errorType.should.eql('UnauthorizedError');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
auth.authenticateClient(req, res, next);
|
2016-02-08 00:27:01 +03:00
|
|
|
next.called.should.be.false();
|
2015-10-22 16:28:47 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2015-12-14 18:35:19 +03:00
|
|
|
it('shouldn\'t authenticate without full client credentials', function (done) {
|
2015-10-22 16:28:47 +03:00
|
|
|
req.body = {};
|
|
|
|
req.body.client_id = testClient;
|
|
|
|
res.status = {};
|
|
|
|
|
|
|
|
sandbox.stub(res, 'status', function (statusCode) {
|
|
|
|
statusCode.should.eql(401);
|
|
|
|
return {
|
|
|
|
json: function (err) {
|
|
|
|
err.errors[0].errorType.should.eql('UnauthorizedError');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
registerUnsuccessfulClientPasswordStrategy();
|
|
|
|
auth.authenticateClient(req, res, next);
|
2016-02-08 00:27:01 +03:00
|
|
|
next.called.should.be.false();
|
|
|
|
errorStub.calledTwice.should.be.true();
|
2015-12-14 18:35:19 +03:00
|
|
|
errorStub.getCall(0).args[1].should.eql('Client credentials were not provided');
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('shouldn\'t authenticate invalid/unknown client', function (done) {
|
|
|
|
req.body = {};
|
|
|
|
req.body.client_id = testClient;
|
|
|
|
req.body.client_secret = testSecret;
|
|
|
|
res.status = {};
|
|
|
|
|
|
|
|
sandbox.stub(res, 'status', function (statusCode) {
|
|
|
|
statusCode.should.eql(401);
|
|
|
|
return {
|
|
|
|
json: function (err) {
|
|
|
|
err.errors[0].errorType.should.eql('UnauthorizedError');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
registerUnsuccessfulClientPasswordStrategy();
|
|
|
|
auth.authenticateClient(req, res, next);
|
2016-02-08 00:27:01 +03:00
|
|
|
next.called.should.be.false();
|
|
|
|
errorStub.calledTwice.should.be.true();
|
2015-12-14 18:35:19 +03:00
|
|
|
errorStub.getCall(0).args[1].should.eql('Client credentials were not valid');
|
|
|
|
|
2015-10-22 16:28:47 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2015-12-14 18:35:19 +03:00
|
|
|
it('should authenticate valid/known client', function (done) {
|
2015-10-22 16:28:47 +03:00
|
|
|
req.body = {};
|
|
|
|
req.body.client_id = testClient;
|
|
|
|
req.body.client_secret = testSecret;
|
|
|
|
req.headers = {};
|
2015-11-23 20:21:19 +03:00
|
|
|
|
|
|
|
registerSuccessfulClientPasswordStrategy();
|
|
|
|
auth.authenticateClient(req, res, next);
|
2015-11-26 15:11:31 +03:00
|
|
|
|
2016-02-08 00:27:01 +03:00
|
|
|
next.called.should.be.true();
|
|
|
|
next.calledWith(null, client).should.be.true();
|
2015-11-23 20:21:19 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2015-10-22 16:28:47 +03:00
|
|
|
it('should authenticate client with id in query', function (done) {
|
|
|
|
req.body = {};
|
|
|
|
req.query = {};
|
|
|
|
req.query.client_id = testClient;
|
|
|
|
req.query.client_secret = testSecret;
|
|
|
|
req.headers = {};
|
|
|
|
|
|
|
|
registerSuccessfulClientPasswordStrategy();
|
|
|
|
auth.authenticateClient(req, res, next);
|
|
|
|
|
2016-02-08 00:27:01 +03:00
|
|
|
next.called.should.be.true();
|
|
|
|
next.calledWith(null, client).should.be.true();
|
2015-10-22 16:28:47 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should authenticate client with id + secret in query', function (done) {
|
|
|
|
req.body = {};
|
|
|
|
req.query = {};
|
|
|
|
req.query.client_id = testClient;
|
|
|
|
req.query.client_secret = testSecret;
|
|
|
|
req.headers = {};
|
|
|
|
|
|
|
|
registerSuccessfulClientPasswordStrategy();
|
|
|
|
auth.authenticateClient(req, res, next);
|
|
|
|
|
2016-02-08 00:27:01 +03:00
|
|
|
next.called.should.be.true();
|
|
|
|
next.calledWith(null, client).should.be.true();
|
2015-10-22 16:28:47 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('shouldn\'t authenticate when error', function (done) {
|
|
|
|
req.body = {};
|
|
|
|
req.body.client_id = testClient;
|
|
|
|
req.body.client_secret = testSecret;
|
|
|
|
res.status = {};
|
|
|
|
|
|
|
|
registerFaultyClientPasswordStrategy();
|
|
|
|
auth.authenticateClient(req, res, next);
|
|
|
|
|
2016-02-08 00:27:01 +03:00
|
|
|
next.called.should.be.true();
|
|
|
|
next.calledWith('error').should.be.true();
|
2015-10-22 16:28:47 +03:00
|
|
|
done();
|
|
|
|
});
|
2015-07-15 19:01:23 +03:00
|
|
|
});
|
|
|
|
});
|