-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
74 lines (67 loc) · 2.14 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const soap = require('soap')
const express = require('express')
const bearerToken = require('express-bearer-token')
const { XMLParser } = require('fast-xml-parser')
const app = express()
app.use(express.json())
if(process.env.API_KEY) {
app.use(bearerToken())
}
function findKeyRecursive(obj, key) {
if(typeof(obj) != 'object' || Array.isArray(obj))
return null
if(obj[key])
return obj[key]
for(let k of Object.keys(obj)) {
let v = findKeyRecursive(obj[k], key)
if(v)
return v
}
return null
}
function filterPassword(req, password) {
if(!password)
return req
return req.replace(password, '***')
}
app.post('/', async (req, res) => {
if(process.env.API_KEY && process.env.API_KEY != req.token) {
res.status(401).send({
'error': 'Authorization required!'
})
return
}
try {
let client = await soap.createClientAsync(req.body.wsdl)
if(req.body.username) {
client.setSecurity(new soap.WSSecurity(req.body.username, req.body.password))
}
const action = client[req.body.action + 'Async']
const [actionRes] = await action(req.body.args)
if(req.body.logging) {
res.set({
'X-Log-Request': filterPassword(client.lastRequest, req.body.password),
'X-Log-Response': client.lastResponse
})
}
res.send(actionRes)
} catch(err) {
if(err.response && err.response.status) {
const xml = (new XMLParser()).parse(err.body)
res.status(err.response.status != 200 ? err.response.status : 500)
if(req.body.logging) {
res.set({
'X-Log-Request': filterPassword(err.response.config.data, req.body.password),
'X-Log-Response': err.body
})
}
res.send({
error: findKeyRecursive(xml, 'faultstring') || xml
})
} else {
res.status(500).send({ error: 'Server Error' })
console.error(err)
}
}
})
app.listen(parseInt(process.env.HTTP_PORT || 80))