From 1b5b95e198fb6c9181d44369063387c490a608a2 Mon Sep 17 00:00:00 2001 From: kirrg001 Date: Fri, 18 Jan 2019 17:30:07 +0100 Subject: [PATCH] Renamed authenticateAdminApiKey to authenticate for admin api key auth refs #9865 - the outer authentication layer wants a consistent interface of each authentication package - admin.authenticate - session.authenticate - furthermore, there is no need to put the full feature into the exposed function name --- core/server/services/auth/api-key/admin.js | 6 ++---- .../unit/services/auth/api-key/admin_spec.js | 16 ++++++++-------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/core/server/services/auth/api-key/admin.js b/core/server/services/auth/api-key/admin.js index 24473a4fa3..886261536f 100644 --- a/core/server/services/auth/api-key/admin.js +++ b/core/server/services/auth/api-key/admin.js @@ -18,8 +18,6 @@ const _extractTokenFromHeader = function extractTokenFromHeader(header) { if (/^Ghost$/i.test(scheme)) { return token; } - - return; }; /** @@ -36,7 +34,7 @@ const _extractTokenFromHeader = function extractTokenFromHeader(header) { * - the "Audience" claim should match the requested API path * https://tools.ietf.org/html/rfc7519#section-4.1.3 */ -const authenticateAdminApiKey = function authenticateAdminApiKey(req, res, next) { +const authenticate = (req, res, next) => { // we don't have an Authorization header so allow fallthrough to other // auth middleware or final "ensure authenticated" check if (!req.headers || !req.headers.authorization) { @@ -109,5 +107,5 @@ const authenticateAdminApiKey = function authenticateAdminApiKey(req, res, next) }; module.exports = { - authenticateAdminApiKey + authenticate }; diff --git a/core/test/unit/services/auth/api-key/admin_spec.js b/core/test/unit/services/auth/api-key/admin_spec.js index 1192101649..91c7e8445f 100644 --- a/core/test/unit/services/auth/api-key/admin_spec.js +++ b/core/test/unit/services/auth/api-key/admin_spec.js @@ -2,7 +2,7 @@ const jwt = require('jsonwebtoken'); const should = require('should'); const sinon = require('sinon'); const Promise = require('bluebird'); -const {authenticateAdminApiKey} = require('../../../../../server/services/auth/api-key/admin'); +const apiKeyAuth = require('../../../../../server/services/auth/api-key'); const common = require('../../../../../server/lib/common'); const models = require('../../../../../server/models'); const testUtils = require('../../../../utils'); @@ -52,7 +52,7 @@ describe('Admin API Key Auth', function () { }; const res = {}; - authenticateAdminApiKey(req, res, (err) => { + apiKeyAuth.admin.authenticate(req, res, (err) => { should.not.exist(err); req.api_key.should.eql(this.fakeApiKey); done(); @@ -68,7 +68,7 @@ describe('Admin API Key Auth', function () { }; const res = {}; - authenticateAdminApiKey(req, res, function next(err) { + apiKeyAuth.admin.authenticate(req, res, function next(err) { should.exist(err); should.equal(err instanceof common.errors.UnauthorizedError, true); err.code.should.eql('INVALID_AUTH_HEADER'); @@ -86,7 +86,7 @@ describe('Admin API Key Auth', function () { }; const res = {}; - authenticateAdminApiKey(req, res, function next(err) { + apiKeyAuth.admin.authenticate(req, res, function next(err) { should.exist(err); should.equal(err instanceof common.errors.BadRequestError, true); err.code.should.eql('INVALID_JWT'); @@ -112,7 +112,7 @@ describe('Admin API Key Auth', function () { }; const res = {}; - authenticateAdminApiKey(req, res, function next(err) { + apiKeyAuth.admin.authenticate(req, res, function next(err) { should.exist(err); should.equal(err instanceof common.errors.UnauthorizedError, true); err.code.should.eql('UNKNOWN_ADMIN_API_KEY'); @@ -141,7 +141,7 @@ describe('Admin API Key Auth', function () { }; const res = {}; - authenticateAdminApiKey(req, res, function next(err) { + apiKeyAuth.admin.authenticate(req, res, function next(err) { should.exist(err); should.equal(err instanceof common.errors.UnauthorizedError, true); err.code.should.eql('INVALID_JWT'); @@ -171,7 +171,7 @@ describe('Admin API Key Auth', function () { }; const res = {}; - authenticateAdminApiKey(req, res, function next(err) { + apiKeyAuth.admin.authenticate(req, res, function next(err) { should.exist(err); should.equal(err instanceof common.errors.UnauthorizedError, true); err.code.should.eql('INVALID_JWT'); @@ -201,7 +201,7 @@ describe('Admin API Key Auth', function () { this.fakeApiKey.type = 'content'; - authenticateAdminApiKey(req, res, function next(err) { + apiKeyAuth.admin.authenticate(req, res, function next(err) { should.exist(err); should.equal(err instanceof common.errors.UnauthorizedError, true); err.code.should.eql('INVALID_API_KEY_TYPE');