diff --git a/packages/core/admin/server/config/api-token.js b/packages/core/admin/server/config/api-token.js index 9e9659ca77..e79ab9d97b 100644 --- a/packages/core/admin/server/config/api-token.js +++ b/packages/core/admin/server/config/api-token.js @@ -1,7 +1,5 @@ 'use strict'; -const { env } = require('../../../utils/lib'); - -module.exports = { +module.exports = ({ env }) => ({ salt: env('API_TOKEN_SALT'), -}; +}); diff --git a/packages/core/admin/server/controllers/api-token.js b/packages/core/admin/server/controllers/api-token.js index ed18ed1d19..888af53708 100644 --- a/packages/core/admin/server/controllers/api-token.js +++ b/packages/core/admin/server/controllers/api-token.js @@ -1,5 +1,6 @@ 'use strict'; +const { trim } = require('lodash/fp'); const { getService } = require('../utils'); const { validateApiTokenCreationInput } = require('../validation/api-tokens'); @@ -13,10 +14,8 @@ module.exports = { * - having a space at the end or start of the value. * - having only spaces as value; */ - attributes.name = attributes.name.trim(); - if (attributes.description) { - attributes.description = attributes.description.trim(); - } + attributes.name = trim(attributes.name); + attributes.description = trim(attributes.description); try { await validateApiTokenCreationInput(attributes); diff --git a/packages/core/admin/server/services/__tests__/api-token.test.js b/packages/core/admin/server/services/__tests__/api-token.test.js index 791cd0a5b7..7aa940671c 100644 --- a/packages/core/admin/server/services/__tests__/api-token.test.js +++ b/packages/core/admin/server/services/__tests__/api-token.test.js @@ -73,7 +73,14 @@ describe('API Token', () => { expect(mockedConfigSet).not.toHaveBeenCalled(); }); - test('It creates a new salt, appendit to the .env file and sets it in the configuration', () => { + test('It creates a new salt, appends it to the .env file and sets it in the configuration', () => { + const mockedApiToken = { + randomBytes: 'api-token_test-random-bytes', + hexedString: '6170692d746f6b656e5f746573742d72616e646f6d2d6279746573', + }; + + crypto.randomBytes = jest.fn(() => Buffer.from(mockedApiToken.randomBytes)); + const mockedAppendFile = jest.fn(); const mockedConfigSet = jest.fn(); @@ -88,7 +95,10 @@ describe('API Token', () => { apiTokenService.createSaltIfNotDefined(); expect(mockedAppendFile).toHaveBeenCalled(); - expect(mockedConfigSet).toHaveBeenCalled(); + expect(mockedConfigSet).toHaveBeenCalledWith( + 'server.admin.api-token.salt', + mockedApiToken.hexedString + ); }); test('It throws an error if the env variable used in the config file has been changed and is empty', () => {