mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-06 22:40:14 -05:00
💡Migrated session controllers for compatibility with "frame" (#11101)
no issue - Session controllers were using API v1 http method which bypassed "frame" introduced with API v2. - Changes here are just a long-awaited cleanup to allow completely remove v0.1 code
This commit is contained in:
parent
465ee0e609
commit
b8b0a5ea18
8 changed files with 43 additions and 33 deletions
|
@ -4,16 +4,18 @@ const models = require('../../models');
|
|||
const auth = require('../../services/auth');
|
||||
|
||||
const session = {
|
||||
read(options) {
|
||||
read(frame) {
|
||||
/*
|
||||
* TODO
|
||||
* Don't query db for user, when new api http wrapper is in we can
|
||||
* have direct access to req.user, we can also get access to some session
|
||||
* inofrmation too and send it back
|
||||
*/
|
||||
return models.User.findOne({id: options.context.user});
|
||||
return models.User.findOne({id: frame.options.context.user});
|
||||
},
|
||||
add(object) {
|
||||
add(frame) {
|
||||
const object = frame.data;
|
||||
|
||||
if (!object || !object.username || !object.password) {
|
||||
return Promise.reject(new common.errors.UnauthorizedError({
|
||||
message: common.i18n.t('errors.middleware.auth.accessDenied')
|
||||
|
|
|
@ -4,16 +4,18 @@ const models = require('../../models');
|
|||
const auth = require('../../services/auth');
|
||||
|
||||
const session = {
|
||||
read(options) {
|
||||
read(frame) {
|
||||
/*
|
||||
* TODO
|
||||
* Don't query db for user, when new api http wrapper is in we can
|
||||
* have direct access to req.user, we can also get access to some session
|
||||
* inofrmation too and send it back
|
||||
*/
|
||||
return models.User.findOne({id: options.context.user});
|
||||
return models.User.findOne({id: frame.options.context.user});
|
||||
},
|
||||
add(object) {
|
||||
add(frame) {
|
||||
const object = frame.data;
|
||||
|
||||
if (!object || !object.username || !object.password) {
|
||||
return Promise.reject(new common.errors.UnauthorizedError({
|
||||
message: common.i18n.t('errors.middleware.auth.accessDenied')
|
||||
|
|
|
@ -167,14 +167,14 @@ module.exports = function apiRoutes() {
|
|||
router.post('/slack/test', mw.authAdminApi, http(apiCanary.slack.sendTest));
|
||||
|
||||
// ## Sessions
|
||||
router.get('/session', mw.authAdminApi, api.http(apiCanary.session.read));
|
||||
router.get('/session', mw.authAdminApi, http(apiCanary.session.read));
|
||||
// We don't need auth when creating a new session (logging in)
|
||||
router.post('/session',
|
||||
shared.middlewares.brute.globalBlock,
|
||||
shared.middlewares.brute.userLogin,
|
||||
api.http(apiCanary.session.add)
|
||||
http(apiCanary.session.add)
|
||||
);
|
||||
router.del('/session', mw.authAdminApi, api.http(apiCanary.session.delete));
|
||||
router.del('/session', mw.authAdminApi, http(apiCanary.session.delete));
|
||||
|
||||
// ## Authentication
|
||||
router.post('/authentication/passwordreset',
|
||||
|
|
|
@ -167,14 +167,14 @@ module.exports = function apiRoutes() {
|
|||
router.post('/slack/test', mw.authAdminApi, http(apiv2.slack.sendTest));
|
||||
|
||||
// ## Sessions
|
||||
router.get('/session', mw.authAdminApi, api.http(apiv2.session.read));
|
||||
router.get('/session', mw.authAdminApi, http(apiv2.session.read));
|
||||
// We don't need auth when creating a new session (logging in)
|
||||
router.post('/session',
|
||||
shared.middlewares.brute.globalBlock,
|
||||
shared.middlewares.brute.userLogin,
|
||||
api.http(apiv2.session.add)
|
||||
http(apiv2.session.add)
|
||||
);
|
||||
router.del('/session', mw.authAdminApi, api.http(apiv2.session.delete));
|
||||
router.del('/session', mw.authAdminApi, http(apiv2.session.delete));
|
||||
|
||||
// ## Authentication
|
||||
router.post('/authentication/passwordreset',
|
||||
|
|
|
@ -60,10 +60,10 @@ describe('Session controller', function () {
|
|||
|
||||
const createSessionStub = sinon.stub(sessionServiceMiddleware, 'createSession');
|
||||
|
||||
return sessionController.add({
|
||||
return sessionController.add({data: {
|
||||
username: 'freddy@vodafone.com',
|
||||
password: 'qu33nRul35'
|
||||
}, {}).then((fn) => {
|
||||
}}).then((fn) => {
|
||||
fn(fakeReq, fakeRes, fakeNext);
|
||||
}).then(function () {
|
||||
should.equal(fakeReq.brute.reset.callCount, 1);
|
||||
|
@ -91,10 +91,10 @@ describe('Session controller', function () {
|
|||
|
||||
const createSessionStub = sinon.stub(sessionServiceMiddleware, 'createSession');
|
||||
|
||||
return sessionController.add({
|
||||
return sessionController.add({data: {
|
||||
username: 'freddy@vodafone.com',
|
||||
password: 'qu33nRul35'
|
||||
}, {}).then((fn) => {
|
||||
}}).then((fn) => {
|
||||
fn(fakeReq, fakeRes, fakeNext);
|
||||
}).then(function () {
|
||||
should.equal(fakeReq.brute.reset.callCount, 1);
|
||||
|
@ -129,8 +129,10 @@ describe('Session controller', function () {
|
|||
.returns(findOneReturnVal);
|
||||
|
||||
const result = sessionController.read({
|
||||
context: {
|
||||
user: 108
|
||||
options: {
|
||||
context: {
|
||||
user: 108
|
||||
}
|
||||
}
|
||||
});
|
||||
should.equal(result, findOneReturnVal);
|
||||
|
|
|
@ -60,10 +60,10 @@ describe('Session controller', function () {
|
|||
|
||||
const createSessionStub = sinon.stub(sessionServiceMiddleware, 'createSession');
|
||||
|
||||
return sessionController.add({
|
||||
return sessionController.add({data: {
|
||||
username: 'freddy@vodafone.com',
|
||||
password: 'qu33nRul35'
|
||||
}, {}).then((fn) => {
|
||||
}}).then((fn) => {
|
||||
fn(fakeReq, fakeRes, fakeNext);
|
||||
}).then(function () {
|
||||
should.equal(fakeReq.brute.reset.callCount, 1);
|
||||
|
@ -91,10 +91,10 @@ describe('Session controller', function () {
|
|||
|
||||
const createSessionStub = sinon.stub(sessionServiceMiddleware, 'createSession');
|
||||
|
||||
return sessionController.add({
|
||||
return sessionController.add({data: {
|
||||
username: 'freddy@vodafone.com',
|
||||
password: 'qu33nRul35'
|
||||
}, {}).then((fn) => {
|
||||
}}).then((fn) => {
|
||||
fn(fakeReq, fakeRes, fakeNext);
|
||||
}).then(function () {
|
||||
should.equal(fakeReq.brute.reset.callCount, 1);
|
||||
|
@ -129,8 +129,10 @@ describe('Session controller', function () {
|
|||
.returns(findOneReturnVal);
|
||||
|
||||
const result = sessionController.read({
|
||||
context: {
|
||||
user: 108
|
||||
options: {
|
||||
context: {
|
||||
user: 108
|
||||
}
|
||||
}
|
||||
});
|
||||
should.equal(result, findOneReturnVal);
|
||||
|
|
|
@ -36,10 +36,10 @@ describe('Session controller', function () {
|
|||
const userCheckStub = sinon.stub(models.User, 'check')
|
||||
.rejects(new Error());
|
||||
|
||||
return sessionController.add({
|
||||
return sessionController.add({data: {
|
||||
username: 'freddy@vodafone.com',
|
||||
password: 'qu33nRul35'
|
||||
}, {}).then(() => {
|
||||
}}).then(() => {
|
||||
should.fail('session.add did not throw');
|
||||
},(err) => {
|
||||
should.equal(err instanceof UnauthorizedError, true);
|
||||
|
@ -60,10 +60,10 @@ describe('Session controller', function () {
|
|||
|
||||
const createSessionStub = sinon.stub(sessionServiceMiddleware, 'createSession');
|
||||
|
||||
return sessionController.add({
|
||||
return sessionController.add({data: {
|
||||
username: 'freddy@vodafone.com',
|
||||
password: 'qu33nRul35'
|
||||
}, {}).then((fn) => {
|
||||
}}).then((fn) => {
|
||||
fn(fakeReq, fakeRes, fakeNext);
|
||||
}).then(function () {
|
||||
should.equal(fakeReq.brute.reset.callCount, 1);
|
||||
|
@ -91,10 +91,10 @@ describe('Session controller', function () {
|
|||
|
||||
const createSessionStub = sinon.stub(sessionServiceMiddleware, 'createSession');
|
||||
|
||||
return sessionController.add({
|
||||
return sessionController.add({data: {
|
||||
username: 'freddy@vodafone.com',
|
||||
password: 'qu33nRul35'
|
||||
}, {}).then((fn) => {
|
||||
}}).then((fn) => {
|
||||
fn(fakeReq, fakeRes, fakeNext);
|
||||
}).then(function () {
|
||||
should.equal(fakeReq.brute.reset.callCount, 1);
|
||||
|
@ -129,8 +129,10 @@ describe('Session controller', function () {
|
|||
.returns(findOneReturnVal);
|
||||
|
||||
const result = sessionController.read({
|
||||
context: {
|
||||
user: 108
|
||||
options: {
|
||||
context: {
|
||||
user: 108
|
||||
}
|
||||
}
|
||||
});
|
||||
should.equal(result, findOneReturnVal);
|
||||
|
|
|
@ -106,7 +106,7 @@ const login = (request, API_URL) => {
|
|||
}));
|
||||
}
|
||||
|
||||
resolve(res.headers['set-cookie'] || res.body.access_token);
|
||||
resolve(res.headers['set-cookie']);
|
||||
}, reject);
|
||||
});
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue