diff --git a/app/controllers/api-keys/post-create.controller.js b/app/controllers/api-keys/post-create.controller.js index 003c740f03..966c55af25 100644 --- a/app/controllers/api-keys/post-create.controller.js +++ b/app/controllers/api-keys/post-create.controller.js @@ -4,7 +4,7 @@ const { response } = require('../../utils/response.js') const publicAuthClient = require('../../services/clients/public-auth.client') const { isADirectDebitAccount } = require('../../services/clients/direct-debit-connector.client.js') -module.exports = async function createAPIKey (req, res, next) { +module.exports = async function createApiKey (req, res, next) { const accountId = req.account.gateway_account_id const correlationId = req.correlationId const description = req.body.description diff --git a/app/controllers/api-keys/post-revoke.controller.js b/app/controllers/api-keys/post-revoke.controller.js index a8f05419c5..d0afde5b59 100644 --- a/app/controllers/api-keys/post-revoke.controller.js +++ b/app/controllers/api-keys/post-revoke.controller.js @@ -3,8 +3,11 @@ const paths = require('../../paths') const publicAuthClient = require('../../services/clients/public-auth.client') const logger = require('../../utils/logger')(__filename) +const formatAccountPathsFor = require('../../utils/format-account-paths-for') + +module.exports = async function revokeApiKey (req, res) { + const apiKeysPath = formatAccountPathsFor(paths.account.apiKeys.index, req.account.external_id) -module.exports = async (req, res) => { const accountId = req.account.gateway_account_id const payload = { token_link: req.body.token_link @@ -16,10 +19,10 @@ module.exports = async (req, res) => { }) req.flash('generic', 'The API key was successfully revoked') - return res.redirect(paths.apiKeys.index) + return res.redirect(apiKeysPath) } catch (error) { logger.error('Error revoking API key', { error: error.message }) req.flash('genericError', 'Something went wrong. Please try again or contact support.') - return res.redirect(paths.apiKeys.index) + return res.redirect(apiKeysPath) } } diff --git a/app/controllers/api-keys/post-update.controller.js b/app/controllers/api-keys/post-update.controller.js index cc42567839..52b2832ca0 100644 --- a/app/controllers/api-keys/post-update.controller.js +++ b/app/controllers/api-keys/post-update.controller.js @@ -3,8 +3,11 @@ const paths = require('../../paths') const publicAuthClient = require('../../services/clients/public-auth.client') const logger = require('../../utils/logger')(__filename) +const formatAccountPathsFor = require('../../utils/format-account-paths-for') + +module.exports = async function updateApiKey (req, res) { + const apiKeysPath = formatAccountPathsFor(paths.account.apiKeys.index, req.account.external_id) -module.exports = (req, res) => { // this does not need to be explicitly tied down to account_id // right now because the UUID space is big enough that no-one // will be able to discover other peoples' tokens to change them @@ -13,17 +16,17 @@ module.exports = (req, res) => { description: req.body.description } - publicAuthClient.updateToken({ - payload: payload, - correlationId: req.correlationId - }) - .then(() => { - req.flash('generic', 'The API key description was successfully updated') - res.redirect(paths.apiKeys.index) - }) - .catch(error => { - logger.error('Error updating API key description', { error }) - req.flash('genericError', 'Something went wrong. Please try again or contact support.') - res.redirect(paths.apiKeys.index) + try { + await publicAuthClient.updateToken({ + payload: payload, + correlationId: req.correlationId }) + + req.flash('generic', 'The API key description was successfully updated') + res.redirect(apiKeysPath) + } catch (error) { + logger.error('Error updating API key description', { error }) + req.flash('genericError', 'Something went wrong. Please try again or contact support.') + res.redirect(apiKeysPath) + } } diff --git a/app/paths.js b/app/paths.js index df1aa7b8ab..05a29ed119 100644 --- a/app/paths.js +++ b/app/paths.js @@ -40,6 +40,13 @@ module.exports = { toggleMotoMaskCardNumberAndSecurityCode: { cardNumber: '/moto-hide-card-number', securityCode: '/moto-hide-security-code' + }, + apiKeys: { + index: '/api-keys', + revoked: '/api-keys/revoked', + create: '/api-keys/create', + revoke: '/api-keys/revoke', + update: '/api-keys/update' } }, transactions: { @@ -91,13 +98,6 @@ module.exports = { dashboard: { index: '/' }, - apiKeys: { - index: '/api-keys', - revoked: '/api-keys/revoked', - create: '/api-keys/create', - revoke: '/api-keys/revoke', - update: '/api-keys/update' - }, serviceSwitcher: { index: '/my-services', switch: '/my-services/switch', diff --git a/app/routes.js b/app/routes.js index 6353fdc6c9..3ae8f1ca50 100644 --- a/app/routes.js +++ b/app/routes.js @@ -87,12 +87,13 @@ const stripeSetupDashboardRedirectController = require('./controllers/stripe-set // Assignments const { healthcheck, registerUser, user, dashboard, selfCreateService, transactions, credentials, - apiKeys, serviceSwitcher, teamMembers, staticPaths, inviteValidation, editServiceName, merchantDetails, + serviceSwitcher, teamMembers, staticPaths, inviteValidation, editServiceName, merchantDetails, notificationCredentials, prototyping, paymentLinks, requestToGoLive, policyPages, stripeSetup, stripe, settings, yourPsp, allServiceTransactions, payouts } = paths const { + apiKeys, digitalWallet, emailNotifications, paymentTypes, @@ -236,12 +237,12 @@ module.exports.bind = function (app) { app.post(merchantDetails.edit, permission('merchant-details:update'), merchantDetailsController.postEdit) // API KEYS - app.get(apiKeys.index, permission('tokens-active:read'), getAccount, apiKeysController.getIndex) - app.get(apiKeys.revoked, permission('tokens-revoked:read'), getAccount, apiKeysController.getRevoked) - app.get(apiKeys.create, permission('tokens:create'), getAccount, apiKeysController.getCreate) - app.post(apiKeys.create, permission('tokens:create'), getAccount, apiKeysController.postCreate) - app.post(apiKeys.revoke, permission('tokens:delete'), getAccount, apiKeysController.postRevoke) - app.post(apiKeys.update, permission('tokens:update'), getAccount, apiKeysController.postUpdate) + account.get(apiKeys.index, permission('tokens-active:read'), apiKeysController.getIndex) + account.get(apiKeys.revoked, permission('tokens-revoked:read'), apiKeysController.getRevoked) + account.get(apiKeys.create, permission('tokens:create'), apiKeysController.getCreate) + account.post(apiKeys.create, permission('tokens:create'), apiKeysController.postCreate) + account.post(apiKeys.revoke, permission('tokens:delete'), apiKeysController.postRevoke) + account.post(apiKeys.update, permission('tokens:update'), apiKeysController.postUpdate) account.get(paymentTypes.index, permission('payment-types:read'), paymentTypesController.getIndex) account.post(paymentTypes.index, permission('payment-types:update'), paymentTypesController.postIndex) diff --git a/app/utils/nav-builder.js b/app/utils/nav-builder.js index 1585bdc507..26825278eb 100644 --- a/app/utils/nav-builder.js +++ b/app/utils/nav-builder.js @@ -49,11 +49,11 @@ const serviceNavigationItems = (currentPath, permissions, type) => { navigationItems.push({ id: 'navigation-menu-settings', name: 'Settings', - url: type === 'card' ? paths.settings.index : paths.apiKeys.index, + url: paths.settings.index, current: currentPath !== '/' ? pathLookup(currentPath, [ ...mainSettingsPaths, ...yourPspPaths, - paths.apiKeys, + paths.account.apiKeys, paths.account.paymentTypes ]) : false, permissions: _.some([ @@ -69,6 +69,8 @@ const serviceNavigationItems = (currentPath, permissions, type) => { } const adminNavigationItems = (currentPath, permissions, type, paymentProvider, account = {}) => { + const apiKeysPath = formatAccountPathsFor(paths.account.apiKeys.index, account.external_id) + return [ { id: 'navigation-menu-settings-home', @@ -80,8 +82,8 @@ const adminNavigationItems = (currentPath, permissions, type, paymentProvider, a { id: 'navigation-menu-api-keys', name: 'API keys', - url: paths.apiKeys.index, - current: pathLookup(currentPath, paths.apiKeys.index), + url: apiKeysPath, + current: pathLookup(currentPath, paths.account.apiKeys.index), permissions: permissions.tokens_update }, { diff --git a/app/views/api-keys/_key.njk b/app/views/api-keys/_key.njk index a63f366039..a527ada72e 100644 --- a/app/views/api-keys/_key.njk +++ b/app/views/api-keys/_key.njk @@ -37,7 +37,7 @@ {% endif %} {% if permissions.tokens_delete %} -