Skip to content

Commit

Permalink
Switched to use new implementation of authorizeAdminApi
Browse files Browse the repository at this point in the history
refs #9865

- see code comments
  • Loading branch information
kirrg001 committed Jan 18, 2019
1 parent de7ba3c commit e90148e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 41 deletions.
10 changes: 7 additions & 3 deletions core/server/services/auth/authorize.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ const authorize = {
};
},

authorizeAdminApi: [session.ensureUser],

authorizeContentApi(req, res, next) {
const hasApiKey = req.api_key && req.api_key.id;
const hasMember = req.member;
Expand All @@ -59,7 +57,13 @@ const authorize = {
}));
},

requiresAuthorizedUserOrApiKey(req, res, next) {
/**
* @NOTE:
*
* We don't support admin api keys yet, but we can already use this authorization helper, because
* we have not connected authenticating with admin api keys yet. `req.api_key` will be always null.
*/
authorizeAdminApi(req, res, next) {
const hasUser = req.user && req.user.id;
const hasApiKey = req.api_key && req.api_key.id;

Expand Down
8 changes: 5 additions & 3 deletions core/server/services/auth/session/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@ module.exports = {
get getSession() {
return require('./middleware').getSession;
},

get cookieCsrfProtection() {
return require('./middleware').cookieCsrfProtection;
},

get safeGetSession() {
return require('./middleware').safeGetSession;
},

get createSession() {
return require('./middleware').createSession;
},

get destroySession() {
return require('./middleware').destroySession;
},

get getUser() {
return require('./middleware').getUser;
},
get ensureUser() {
return require('./middleware').ensureUser;
}
};
12 changes: 1 addition & 11 deletions core/server/services/auth/session/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,6 @@ const getUser = (req, res, next) => {
});
};

const ensureUser = (req, res, next) => {
if (req.user && req.user.id) {
return next();
}
next(new common.errors.UnauthorizedError({
message: common.i18n.t('errors.middleware.auth.accessDenied')
}));
};

const cookieCsrfProtection = (req, res, next) => {
// If there is no origin on the session object it means this is a *new*
// session, that hasn't been initialised yet. So we don't need CSRF protection
Expand All @@ -126,6 +117,5 @@ module.exports = exports = {
safeGetSession: [getSession, cookieCsrfProtection],
createSession,
destroySession,
getUser,
ensureUser
getUser
};
24 changes: 0 additions & 24 deletions core/test/unit/services/auth/session/index_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,30 +202,6 @@ describe('Session Service', function () {
});
});

describe('ensureUser', function () {
it('calls next with no error if req.user.id exists', function (done) {
const req = fakeReq();
const res = fakeRes();
const user = models.User.forge({id: 23});
req.user = user;

sessionService.ensureUser(req, res, function next(err) {
should.equal(err, null);
done();
});
});

it('calls next with UnauthorizedError if req.user.id does not exist', function (done) {
const req = fakeReq();
const res = fakeRes();

sessionService.ensureUser(req, res, function next(err) {
should.equal(err instanceof UnauthorizedError, true);
done();
});
});
});

describe('CSRF protection', function () {
it('calls next if the session is uninitialized', function (done) {
const req = fakeReq();
Expand Down

0 comments on commit e90148e

Please sign in to comment.