Skip to content
Open
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
3 changes: 2 additions & 1 deletion apis/enterprise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const config = require('config')
const db = require('../models/mongodb')

const { addFileToXinfinIpfs } = require('../helpers/ipfs')
const kycLogger = require('../middlewares/kycLogger')

function unauthorized (res, reason) {
return res.status(401).json({
Expand Down Expand Up @@ -51,7 +52,7 @@ router.post('/keys', async (req, res) => {
})

// Enterprise addKYC endpoint
router.post('/addKYC', async (req, res) => {
router.post('/addKYC', kycLogger, async (req, res) => {
try {
const apiKey = req.headers['x-api-key']
const apiTimestamp = req.headers['x-api-timestamp']
Expand Down
3 changes: 2 additions & 1 deletion apis/ipfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const web3 = require('../models/blockchain/web3rpc').Web3RpcInternal()
const { recoverPersonalSignAddress, isValidEIP1271Signature } = require('../helpers/personalSign')

const { addFileToXinfinIpfs } = require('../helpers/ipfs')
const kycLogger = require('../middlewares/kycLogger')

function toHexAddress (address) {
if (!address || typeof address !== 'string') return ''
Expand Down Expand Up @@ -42,7 +43,7 @@ if (!fs.existsSync(path.join(__dirname, '../tmp/'))) {
fs.mkdirSync(path.join(__dirname, '../tmp/'))
}

router.post('/addKYC', async function (req, res, next) {
router.post('/addKYC', kycLogger, async function (req, res, next) {
const account = normalizeValue(
req.body.account ||
req.headers['x-kyc-account'] ||
Expand Down
51 changes: 51 additions & 0 deletions middlewares/kycLogger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict'

const db = require('../models/mongodb')

async function kycLogger (req, res, next) {
const originalJson = res.json
const originalSend = res.send
let responseSent = false

const logEntry = new db.KycLog({
endpoint: req.originalUrl,
method: req.method,
headers: req.headers,
body: req.body,
query: req.query,
ip: req.ip
})

if (req.files) {
logEntry.files = Object.keys(req.files).map(k => ({
fieldname: k,
name: req.files[k].name,
size: req.files[k].size,
mimetype: req.files[k].mimetype
}))
}

const saveLog = (data, statusCode) => {
if (!responseSent) {
responseSent = true
logEntry.responseStatus = statusCode || res.statusCode
logEntry.responseData = data
logEntry.success = logEntry.responseStatus >= 200 && logEntry.responseStatus < 300
logEntry.save().catch(err => console.error('KycLog save error:', err))
}
}

res.json = function (data) {
saveLog(data, res.statusCode)
return originalJson.call(this, data)
}

res.send = function (data) {
saveLog(typeof data === 'string' ? data : data ? data.toString() : data, res.statusCode)
return originalSend.call(this, data)
}

next()
}

module.exports = kycLogger
21 changes: 21 additions & 0 deletions models/mongodb/kycLog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict'

const mongoose = require('mongoose')
const Schema = mongoose.Schema

const KycLogSchema = new Schema({
endpoint: String,
method: String,
headers: Schema.Types.Mixed,
body: Schema.Types.Mixed,
query: Schema.Types.Mixed,
ip: String,
files: [Schema.Types.Mixed],
responseStatus: Number,
responseData: Schema.Types.Mixed,
success: Boolean
}, {
timestamps: true
})

module.exports = mongoose.model('KycLog', KycLogSchema)