Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] PP-13119 fix correlation id #3901

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions app/controllers/charge.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const { CORRELATION_HEADER } = require('../../config/correlation-header')
const { createChargeIdSessionKey } = require('../utils/session')
const { getLoggingFields } = require('../utils/logging-fields-helper')
const { applePayEnabled, googlePayEnabled } = require('../utils/wallet-utils')
const crypto = require('crypto')
const { getRequestCorrelationIDField } = require('../services/clients/base/request-context')

const appendChargeForNewView = async (charge, req, chargeId) => {
const cardModel = Card(charge.gatewayAccount.cardTypes, charge.gatewayAccount.block_prepaid_cards, req.headers[CORRELATION_HEADER])
Expand Down Expand Up @@ -70,7 +72,7 @@ const appendChargeForNewView = async (charge, req, chargeId) => {
})
charge.googlePayRequestDetails = googlePayDetails

const correlationId = req.headers[CORRELATION_HEADER] || ''
const correlationId = req.headers[CORRELATION_HEADER] || getRequestCorrelationIDField() || crypto.randomBytes(16).toString('hex')
charge.worldpay3dsFlexDdcJwt = await worlpay3dsFlexService.getDdcJwt(charge, correlationId, getLoggingFields(req))

if (charge.gatewayAccount.type !== 'live') {
Expand Down Expand Up @@ -211,7 +213,7 @@ module.exports = {
return responseRouter.response(req, res, views.CHARGE_VIEW, data.validation)
}

const correlationId = req.headers[CORRELATION_HEADER] || ''
const correlationId = req.headers[CORRELATION_HEADER] || getRequestCorrelationIDField() || crypto.randomBytes(16).toString('hex')
const payload = normalise.apiPayload(req, card)
if (res.locals.collectBillingAddress === false) {
delete payload.address
Expand Down
4 changes: 3 additions & 1 deletion app/controllers/return.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ const { CORRELATION_HEADER } = require('../../config/correlation-header')
const { withAnalyticsError } = require('../utils/analytics')
const { createChargeIdSessionKey } = require('../utils/session')
const cookies = require('../utils/cookies')
const crypto = require('crypto')
const { getRequestCorrelationIDField } = require('../services/clients/base/request-context')

exports.return = (req, res) => {
const correlationId = req.headers[CORRELATION_HEADER] || ''
const correlationId = req.headers[CORRELATION_HEADER] || getRequestCorrelationIDField() || crypto.randomBytes(16).toString('hex')
const charge = Charge(correlationId)
const chargeKey = createChargeIdSessionKey(req.chargeId)
const chargeState = cookies.getSessionChargeState(req, chargeKey)
Expand Down
4 changes: 3 additions & 1 deletion app/controllers/secure.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ const withAnalyticsError = require('../utils/analytics').withAnalyticsError
const { resolveActionName } = require('../services/state.service')
const paths = require('../paths')
const { ChargeState } = require('../models/ChargeState')
const crypto = require('crypto')
const { getRequestCorrelationIDField } = require('../services/clients/base/request-context')

exports.new = async function (req, res) {
const chargeTokenId = req.params.chargeTokenId || req.body.chargeTokenId
const correlationId = req.headers[CORRELATION_HEADER] || ''
const correlationId = req.headers[CORRELATION_HEADER] || getRequestCorrelationIDField() || crypto.randomBytes(16).toString('hex')
try {
const tokenResponse = await Charge(correlationId).findByToken(chargeTokenId, getLoggingFields(req))

Expand Down
4 changes: 3 additions & 1 deletion app/controllers/three-d-secure.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ const normalise = require('../services/normalise-charge')
const paths = require('../paths')
const { withAnalytics } = require('../utils/analytics')
const connectorClient = require('../services/clients/connector.client')
const crypto = require('crypto')

// Constants
const { views, threeDsEPDQResults } = require('../../config/charge.controller')
const { CORRELATION_HEADER } = require('../../config/correlation-header')
const { getRequestCorrelationIDField } = require('../services/clients/base/request-context')

const routeFor = (resource, chargeId) => paths.generateRoute(`card.${resource}`, { chargeId: chargeId })

Expand Down Expand Up @@ -82,7 +84,7 @@ const handleThreeDsResponse = (req, res, charge) => response => {
module.exports = {
auth3dsHandler (req, res) {
const charge = normalise.charge(req.chargeData, req.chargeId)
const correlationId = req.headers[CORRELATION_HEADER] || ''
const correlationId = req.headers[CORRELATION_HEADER] || getRequestCorrelationIDField() || crypto.randomBytes(16).toString('hex')
const payload = build3dsPayload(charge.id, req)
connectorClient({ correlationId }).threeDs({ chargeId: charge.id, payload }, getLoggingFields(req))
.then(handleThreeDsResponse(req, res, charge))
Expand Down
4 changes: 3 additions & 1 deletion app/middleware/correlation-header.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
const { CORRELATION_ID } = require('@govuk-pay/pay-js-commons').logging.keys
const { CORRELATION_HEADER } = require('../../config/correlation-header')
const { setLoggingField } = require('../utils/logging-fields-helper')
const crypto = require('crypto')
const { getRequestCorrelationIDField } = require('../services/clients/base/request-context')

module.exports = (req, res, next) => {
req.correlationId = req.headers[CORRELATION_HEADER] || ''
req.correlationId = req.headers[CORRELATION_HEADER] || getRequestCorrelationIDField() || crypto.randomBytes(16).toString('hex')
setLoggingField(req, CORRELATION_ID, req.correlationId)
next()
}
3 changes: 2 additions & 1 deletion app/services/clients/base/request-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ const { CORRELATION_ID } = require('@govuk-pay/pay-js-commons').logging.keys

const { AsyncLocalStorage } = require('async_hooks')
const { CORRELATION_HEADER } = require('../../../../config/correlation-header')
const crypto = require('crypto')

const asyncLocalStorage = new AsyncLocalStorage()

function requestContextMiddleware (req, res, next) {
asyncLocalStorage.run({}, () => {
asyncLocalStorage.getStore()[CORRELATION_ID] = req.headers[CORRELATION_HEADER]
asyncLocalStorage.getStore()[CORRELATION_ID] = req.headers[CORRELATION_HEADER] || crypto.randomBytes(16).toString('hex')
next()
})
}
Expand Down
2 changes: 1 addition & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ function initialiseGlobalMiddleware (app) {
app.use(/\/((?!images|public|stylesheets|javascripts).)*/, loggingMiddleware())
app.use(favicon(path.join(__dirname, '/node_modules/govuk-frontend/govuk/assets/images', 'favicon.ico')))
app.use(staticify.middleware)
app.use(requestContextMiddleware)

app.use(function (req, res, next) {
app.use(requestContextMiddleware)
res.locals.asset_path = '/public/'
res.locals.googlePayMerchantID = GOOGLE_PAY_MERCHANT_ID
if (typeof ANALYTICS_TRACKING_ID === 'undefined') {
Expand Down
Loading