This repository has been archived by the owner on Jun 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.js
60 lines (51 loc) · 1.72 KB
/
plugin.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
const crypto = require('crypto')
const { URL } = require('url')
module.exports.templateTags = [
{
name: 'httpsignature',
displayName: 'HTTP Signature',
description: 'sign http requests',
args: [
{
displayName: 'Key ID',
type: 'string',
},
{
displayName: 'Private Key',
type: 'string',
},
],
async run(context, keyId, privateKey) {
const request = await context.util.models.request.getById(
context.meta.requestId
)
const requestUrl = await context.util.render(request.url)
if (!keyId) throw new Error('missing keyId')
if (!privateKey) throw new Error('missing privateKey')
const parsedUrl = new URL(requestUrl)
for (const parameter of request.parameters) {
if (!parameter.disabled)
parsedUrl.searchParams.append(parameter.name, parameter.value)
}
const algorithmBits = 256
const signAlgorithm = `RSA-SHA${algorithmBits}`
const signatureString = []
signatureString.push(
`(request-target): ${request.method.toLowerCase()} ${
parsedUrl.pathname
}${parsedUrl.search}`
)
signatureString.push(`host: ${parsedUrl.host}`)
const signature = signatureString.join('\n')
const signatureSign = crypto.createSign(signAlgorithm)
const signedSignature = signatureSign
.update(signature)
.sign(
`-----BEGIN RSA PRIVATE KEY-----\n${privateKey}\n-----END RSA PRIVATE KEY-----`,
'base64'
)
const authorization = `keyId="${keyId}", algorithm="${signAlgorithm.toLowerCase()}", headers="(request-target) host", signature="${signedSignature}"`
return authorization
},
},
]