Skip to content

Commit

Permalink
PP-12395: API Keys landing page (#4385)
Browse files Browse the repository at this point in the history
* PP-12395: API keys landing page
  • Loading branch information
oswaldquek authored Dec 18, 2024
1 parent 6153ad0 commit 54b4cc8
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { response } = require('@utils/response')
const apiKeysService = require('@services/api-keys.service')

async function get (req, res) {
const activeKeys = await apiKeysService.getActiveKeys(req.account.id)
return response(req, res, 'simplified-account/settings/api-keys/index', {
accountType: req.account.type,
activeKeys,
createApiKeyLink: '#',
showRevokedKeysLink: '#'
})
}

module.exports = { get }
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const ControllerTestBuilder = require('@test/test-helpers/simplified-account/controllers/ControllerTestBuilder.class')
const sinon = require('sinon')
const { expect } = require('chai')
const ACCOUNT_TYPE = 'live'
const SERVICE_ID = 'service-id-123abc'

const mockResponse = sinon.spy()
const tokens = [{ description: 'my token', createdBy: 'system generated', issuedDate: '12 Dec 2024' }]
const apiKeysService = {
getActiveKeys: sinon.stub().resolves(tokens)
}

const {
req,
res,
call
} = new ControllerTestBuilder('@controllers/simplified-account/settings/api-keys/api-keys.controller')
.withServiceExternalId(SERVICE_ID)
.withAccountType(ACCOUNT_TYPE)
.withStubs({
'@utils/response': { response: mockResponse },
'@services/api-keys.service': apiKeysService
})
.build()

describe('Controller: settings/api-keys', () => {
describe('get', () => {
before(() => {
call('get')
})

it('should call the response method', () => {
expect(mockResponse).to.have.been.calledOnce // eslint-disable-line
})

it('should pass req, res and template path to the response method', () => {
expect(mockResponse).to.have.been.calledWith(req, res, 'simplified-account/settings/api-keys/index')
})

it('should pass context data to the response method', () => {
expect(mockResponse.args[0][3]).to.have.property('accountType').to.equal('live')
expect(mockResponse.args[0][3]).to.have.property('activeKeys').to.deep.equal(tokens)
})
})
})
1 change: 1 addition & 0 deletions app/controllers/simplified-account/settings/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ module.exports.teamMembers = require('./team-members/team-members.controller')
module.exports.organisationDetails = require('./organisation-details/organisation-details.controller')
module.exports.cardTypes = require('./card-types/card-types.controller')
module.exports.worldpayDetails = require('./worldpay-details/worldpay-details.controller')
module.exports.apiKeys = require('./api-keys/api-keys.controller')
34 changes: 34 additions & 0 deletions app/models/Token.class.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Token {
withDescription (description) {
this.description = description
return this
}

withCreatedBy (createdBy) {
this.createdBy = createdBy
return this
}

withIssuedDate (issuedDate) {
this.issuedDate = issuedDate
return this
}

withLastUsed (lastUsed) {
this.lastUsed = lastUsed
return this
}

static fromJson (data) {
if (!data) {
return undefined
}
return new Token()
.withDescription(data?.description)
.withCreatedBy(data?.created_by)
.withIssuedDate(data?.issued_date)
.withLastUsed(data?.last_used)
}
}

module.exports.Token = Token
18 changes: 18 additions & 0 deletions app/services/api-keys.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const publicAuthClient = require('@services/clients/public-auth.client')
const { Token } = require('@models/Token.class')

/**
* Gets the list of active api keys for a gateway account
* @param {string} gatewayAccountId
* @returns {[Token]}
*/
const getActiveKeys = async (gatewayAccountId) => {
const publicAuthData = await publicAuthClient.getActiveTokensForAccount({
accountId: gatewayAccountId
})
return publicAuthData.tokens.map(tokenData => Token.fromJson(tokenData))
}

module.exports = {
getActiveKeys
}
3 changes: 3 additions & 0 deletions app/simplified-account-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ simplifiedAccount.post(paths.simplifiedAccount.settings.worldpayDetails.credenti
// card types
simplifiedAccount.get(paths.simplifiedAccount.settings.cardTypes.index, permission('transactions:read'), serviceSettingsController.cardTypes.get)

// api keys
simplifiedAccount.get(paths.simplifiedAccount.settings.apiKeys.index, permission('tokens-active:read'), serviceSettingsController.apiKeys.get)

// stripe details
const stripeDetailsPath = paths.simplifiedAccount.settings.stripeDetails
const stripeDetailsRouter = new Router({ mergeParams: true })
Expand Down
3 changes: 1 addition & 2 deletions app/utils/simplified-account/settings/service-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ module.exports = (account, service, currentUrl, permissions) => {
id: 'api-keys',
name: 'API keys',
path: paths.simplifiedAccount.settings.apiKeys.index,
permission: 'tokens_update', // TODO find a better way of defining these
alwaysViewable: true // viewable on test and live accounts
permission: 'tokens_active_read'
})
.add({
id: 'webhooks',
Expand Down
97 changes: 97 additions & 0 deletions app/views/simplified-account/settings/api-keys/index.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
{% extends "../settings-layout.njk" %}

{% block settingsPageTitle %}
{% if accountType === 'test' %}Test{% else %}Live{% endif %} API keys
{% endblock %}

{% block settingsContent %}

<h1 class="govuk-heading-l page-title">
{% if accountType === 'test' %}Test{% else %}Live{% endif %} API keys
</h1>

{{ govukWarningText(
{
text: 'Do not use a test API key on your live account. It will not work properly.',
iconFallbackText: 'Warning'
} if accountType === 'test' else {
text: 'Do not use a live API key on your test account. It will not work properly.',
iconFallbackText: 'Warning'
}
) }}

<p class="govuk-body">Use these to connect your digital service to Pay and to access the reporting API.</p>
<p class="govuk-body">
You do not need API keys to use
<a class="govuk-link" href="https://www.payments.service.gov.uk/govuk-payment-pages/">payment links</a>.
</p>

{{ govukButton({
href: createApiKeyLink,
text: 'Create a new API key',
classes: 'govuk-!-margin-top-2'
}) }}

{% if activeKeys.length == 0 %}
<h2 class="govuk-heading-m govuk-!-margin-top-2">
There are no active {{ accountType }} API keys
</h2>
{% else %}
<h2 class="govuk-heading-m govuk-!-margin-top-2">
Active {{ accountType }} API keys ({{ activeKeys.length }})
</h2>
{% for key in activeKeys %}
{{ govukSummaryList({
card: {
title: {
text: key.description
},
actions: {
items: [
{
href: '#',
text: 'Change name'
},
{
href: '#',
text: 'Revoke'
}
]
}
},
rows: [
{
key: {
text: 'Created by'
},
value: {
text: key.createdBy
}
},
{
key: {
text: 'Date created'
},
value: {
text: key.issuedDate
}
},
{
key: {
text: 'Last used'
},
value: {
text: key.lastUsed
}
}
]
}) }}
{% endfor %}
{% endif %}

{{ govukButton({
href: showRevokedKeysLink,
text: 'Show revoked API keys',
classes: 'govuk-!-margin-top-2 govuk-button--secondary'
}) }}
{% endblock %}

0 comments on commit 54b4cc8

Please sign in to comment.