mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-24 11:22:19 +03:00
1882278b5b
- 🛠 add bunyan and prettyjson, remove morgan - ✨ add logging module - GhostLogger class that handles setup of bunyan - PrettyStream for stdout - ✨ config for logging - @TODO: testing level fatal? - ✨ log each request via GhostLogger (express middleware) - @TODO: add errors to output - 🔥 remove errors.updateActiveTheme - we can read the value from config - 🔥 remove 15 helper functions in core/server/errors/index.js - all these functions get replaced by modules: 1. logging 2. error middleware handling for html/json 3. error creation (which will be part of PR #7477) - ✨ add express error handler for html/json - one true error handler for express responses - contains still some TODO's, but they are not high priority for first implementation/integration - this middleware only takes responsibility of either rendering html responses or return json error responses - 🎨 use new express error handler in middleware/index - 404 and 500 handling - 🎨 return error instead of error message in permissions/index.js - the rule for error handling should be: if you call a unit, this unit should return a custom Ghost error - 🎨 wrap serve static module - rule: if you call a module/unit, you should always wrap this error - it's always the same rule - so the caller never has to worry about what comes back - it's always a clear error instance - in this case: we return our notfounderror if serve static does not find the resource - this avoid having checks everywhere - 🎨 replace usages of errors/index.js functions and adapt tests - use logging.error, logging.warn - make tests green - remove some usages of logging and throwing api errors -> because when a request is involved, logging happens automatically - 🐛 return errorDetails to Ghost-Admin - errorDetails is used for Theme error handling - 🎨 use 500er error for theme is missing error in theme-handler - 🎨 extend file rotation to 1w
363 lines
12 KiB
JavaScript
363 lines
12 KiB
JavaScript
var sinon = require('sinon'),
|
|
should = require('should'),
|
|
passport = require('passport'),
|
|
rewire = require('rewire'),
|
|
errors = require('../../../server/errors'),
|
|
auth = rewire('../../../server/auth'),
|
|
logging = require('../../../server/logging'),
|
|
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'
|
|
},
|
|
|
|
sandbox = sinon.sandbox.create();
|
|
|
|
should.equal(true, true);
|
|
|
|
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);
|
|
}
|
|
));
|
|
}
|
|
|
|
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 () {
|
|
var res, req, next, loggingStub;
|
|
|
|
beforeEach(function () {
|
|
req = {};
|
|
res = {};
|
|
next = sandbox.spy();
|
|
loggingStub = sandbox.stub(logging, 'error');
|
|
});
|
|
|
|
afterEach(function () {
|
|
sandbox.restore();
|
|
});
|
|
|
|
it('should require authorized user (user exists)', function (done) {
|
|
req.user = {id: 1};
|
|
|
|
auth.authorize.requiresAuthorizedUser(req, res, next);
|
|
next.called.should.be.true();
|
|
next.calledWith().should.be.true();
|
|
done();
|
|
});
|
|
|
|
it('should require authorized user (user is missing)', function (done) {
|
|
req.user = false;
|
|
res.status = {};
|
|
|
|
var next = function next(err) {
|
|
err.statusCode.should.eql(403);
|
|
(err instanceof errors.NoPermissionError).should.eql(true);
|
|
done();
|
|
};
|
|
|
|
auth.authorize.requiresAuthorizedUser(req, res, next);
|
|
});
|
|
|
|
describe('User Authentication', function () {
|
|
it('should authenticate user', function (done) {
|
|
req.headers = {};
|
|
req.headers.authorization = 'Bearer ' + token;
|
|
|
|
registerSuccessfulBearerStrategy();
|
|
auth.authenticate.authenticateUser(req, res, next);
|
|
|
|
next.called.should.be.true();
|
|
next.calledWith(null, user, info).should.be.true();
|
|
done();
|
|
});
|
|
|
|
it('shouldn\'t pass with client, no bearer token', function (done) {
|
|
req.headers = {};
|
|
req.client = {id: 1};
|
|
res.status = {};
|
|
|
|
auth.authenticate.authenticateUser(req, res, next);
|
|
|
|
next.called.should.be.true();
|
|
next.calledWith().should.be.true();
|
|
done();
|
|
});
|
|
|
|
it('shouldn\'t authenticate user', function (done) {
|
|
req.headers = {};
|
|
req.headers.authorization = 'Bearer ' + token;
|
|
res.status = {};
|
|
|
|
var next = function next(err) {
|
|
err.statusCode.should.eql(401);
|
|
(err instanceof errors.UnauthorizedError).should.eql(true);
|
|
done();
|
|
};
|
|
|
|
registerUnsuccessfulBearerStrategy();
|
|
auth.authenticate.authenticateUser(req, res, next);
|
|
});
|
|
|
|
it('shouldn\'t authenticate without bearer token', function (done) {
|
|
req.headers = {};
|
|
res.status = {};
|
|
|
|
var next = function next(err) {
|
|
err.statusCode.should.eql(401);
|
|
(err instanceof errors.UnauthorizedError).should.eql(true);
|
|
done();
|
|
};
|
|
|
|
registerUnsuccessfulBearerStrategy();
|
|
auth.authenticate.authenticateUser(req, res, next);
|
|
});
|
|
|
|
it('shouldn\'t authenticate with bearer token and client', function (done) {
|
|
req.headers = {};
|
|
req.headers.authorization = 'Bearer ' + token;
|
|
req.client = {id: 1};
|
|
res.status = {};
|
|
|
|
var next = function next(err) {
|
|
err.statusCode.should.eql(401);
|
|
(err instanceof errors.UnauthorizedError).should.eql(true);
|
|
done();
|
|
};
|
|
|
|
registerUnsuccessfulBearerStrategy();
|
|
auth.authenticate.authenticateUser(req, res, next);
|
|
});
|
|
|
|
it('shouldn\'t authenticate when error', function (done) {
|
|
req.headers = {};
|
|
req.headers.authorization = 'Bearer ' + token;
|
|
|
|
registerFaultyBearerStrategy();
|
|
auth.authenticate.authenticateUser(req, res, next);
|
|
|
|
next.called.should.be.true();
|
|
next.calledWith('error').should.be.true();
|
|
done();
|
|
});
|
|
});
|
|
|
|
describe('Client Authentication', function () {
|
|
it('shouldn\'t require authorized client with bearer token', function (done) {
|
|
req.headers = {};
|
|
req.headers.authorization = 'Bearer ' + token;
|
|
|
|
auth.authenticate.authenticateClient(req, res, next);
|
|
next.called.should.be.true();
|
|
next.calledWith().should.be.true();
|
|
done();
|
|
});
|
|
|
|
it('shouldn\'t authenticate client with broken bearer token', function (done) {
|
|
req.body = {};
|
|
req.headers = {};
|
|
req.headers.authorization = 'Bearer';
|
|
res.status = {};
|
|
|
|
var next = function next(err) {
|
|
err.statusCode.should.eql(401);
|
|
(err instanceof errors.UnauthorizedError).should.eql(true);
|
|
done();
|
|
};
|
|
|
|
auth.authenticate.authenticateClient(req, res, next);
|
|
});
|
|
|
|
it('shouldn\'t authenticate client without client_id/client_secret', function (done) {
|
|
req.body = {};
|
|
res.status = {};
|
|
|
|
var next = function next(err) {
|
|
err.statusCode.should.eql(401);
|
|
(err instanceof errors.UnauthorizedError).should.eql(true);
|
|
done();
|
|
};
|
|
|
|
auth.authenticate.authenticateClient(req, res, next);
|
|
});
|
|
|
|
it('shouldn\'t authenticate client without client_id', function (done) {
|
|
req.body = {};
|
|
req.body.client_secret = testSecret;
|
|
res.status = {};
|
|
|
|
var next = function next(err) {
|
|
err.statusCode.should.eql(401);
|
|
(err instanceof errors.UnauthorizedError).should.eql(true);
|
|
done();
|
|
};
|
|
|
|
auth.authenticate.authenticateClient(req, res, next);
|
|
});
|
|
|
|
it('shouldn\'t authenticate client without client_secret', function (done) {
|
|
req.body = {};
|
|
req.body.client_id = testClient;
|
|
res.status = {};
|
|
|
|
var next = function next(err) {
|
|
err.statusCode.should.eql(401);
|
|
(err instanceof errors.UnauthorizedError).should.eql(true);
|
|
done();
|
|
};
|
|
|
|
auth.authenticate.authenticateClient(req, res, next);
|
|
});
|
|
|
|
it('shouldn\'t authenticate without full client credentials', function (done) {
|
|
req.body = {};
|
|
req.body.client_id = testClient;
|
|
res.status = {};
|
|
|
|
var next = function next(err) {
|
|
err.statusCode.should.eql(401);
|
|
(err instanceof errors.UnauthorizedError).should.eql(true);
|
|
done();
|
|
};
|
|
|
|
registerUnsuccessfulClientPasswordStrategy();
|
|
auth.authenticate.authenticateClient(req, res, next);
|
|
});
|
|
|
|
it('shouldn\'t authenticate invalid/unknown client', function (done) {
|
|
req.body = {};
|
|
req.body.client_id = testClient;
|
|
req.body.client_secret = testSecret;
|
|
res.status = {};
|
|
|
|
var next = function next(err) {
|
|
err.statusCode.should.eql(401);
|
|
(err instanceof errors.UnauthorizedError).should.eql(true);
|
|
done();
|
|
};
|
|
|
|
registerUnsuccessfulClientPasswordStrategy();
|
|
auth.authenticate.authenticateClient(req, res, next);
|
|
});
|
|
|
|
it('should authenticate valid/known client', function (done) {
|
|
req.body = {};
|
|
req.body.client_id = testClient;
|
|
req.body.client_secret = testSecret;
|
|
req.headers = {};
|
|
|
|
registerSuccessfulClientPasswordStrategy();
|
|
auth.authenticate.authenticateClient(req, res, next);
|
|
|
|
next.called.should.be.true();
|
|
next.calledWith(null, client).should.be.true();
|
|
done();
|
|
});
|
|
|
|
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.authenticate.authenticateClient(req, res, next);
|
|
|
|
next.called.should.be.true();
|
|
next.calledWith(null, client).should.be.true();
|
|
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.authenticate.authenticateClient(req, res, next);
|
|
|
|
next.called.should.be.true();
|
|
next.calledWith(null, client).should.be.true();
|
|
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.authenticate.authenticateClient(req, res, next);
|
|
|
|
next.called.should.be.true();
|
|
next.calledWith('error').should.be.true();
|
|
done();
|
|
});
|
|
});
|
|
});
|