Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit 4ddb72e

Browse files
beta 3
1 parent eb791cb commit 4ddb72e

File tree

4 files changed

+27
-67
lines changed

4 files changed

+27
-67
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
node_modules
1+
/node_modules

package-lock.json

Lines changed: 2 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "insomnia-plugin-http-signature",
3-
"version": "1.0.0-beta.2",
3+
"version": "1.0.0-beta.3",
44
"author": "Alessio Dionisi <hello@adns.io>",
55
"description": "HTTP Signature for Insomnia REST Client",
66
"repository": "github:adnsio/insomnia-plugin-http-signature",
@@ -11,8 +11,5 @@
1111
"insomnia": {
1212
"name": "http-signature",
1313
"description": "HTTP Signature"
14-
},
15-
"dependencies": {
16-
"luxon": "^1.3.3"
1714
}
1815
}

src/plugin.js

Lines changed: 23 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,6 @@
11
const crypto = require('crypto')
2-
const { DateTime } = require('luxon')
32
const { parse: parseUrl } = require('url')
43

5-
module.exports.requestHooks = [
6-
async context => {
7-
console.log(context)
8-
const { store, request } = context
9-
const keyId = await store.getItem('keyId')
10-
let privateKey = await store.getItem('privateKey')
11-
privateKey = `-----BEGIN PRIVATE KEY-----\n${privateKey}\n-----END PRIVATE KEY-----`
12-
const signRequest = await store.getItem('signRequest')
13-
14-
if (signRequest) {
15-
const parsedUrl = parseUrl(request.getUrl())
16-
17-
const algorithmBits = 256
18-
const hashAlgorithm = `sha${algorithmBits}`
19-
const digestAlgorithm = `SHA-${algorithmBits}`
20-
const signAlgorithm = `RSA-SHA${algorithmBits}`
21-
22-
const date = DateTime.utc().toRFC2822()
23-
24-
const digestHash = crypto.createHash(hashAlgorithm)
25-
const digest = digestHash.update(request.getBodyText()).digest('base64')
26-
27-
const signatureString = []
28-
signatureString.push(`(request-target): ${request.getMethod().toLowerCase()} ${parsedUrl.path}`)
29-
signatureString.push(`host: ${parsedUrl.hostname}`)
30-
signatureString.push(`digest: ${digestAlgorithm}=${digest}`)
31-
signatureString.push(`date: ${date}`)
32-
if (request.hasHeader('Content-Type')) signatureString.push(`content-type: ${request.getHeader('Content-Type')}`)
33-
const signature = signatureString.join('\n')
34-
35-
const signatureSign = crypto.createSign(signAlgorithm)
36-
const signedSignature = signatureSign.update(signature).sign(privateKey, 'base64')
37-
38-
const authorization = `Signature keyId="${keyId}", algorithm="${signAlgorithm.toLowerCase()}", headers="(request-target) host digest date${request.hasHeader('Content-Type') ? ' content-type' : ''}", signature="${signedSignature}"`
39-
40-
request.setHeader('Digest', `${digestAlgorithm}=${digest}`)
41-
request.setHeader('Date', date)
42-
request.setHeader('Authorization', authorization)
43-
}
44-
45-
await store.clear()
46-
}
47-
]
48-
494
module.exports.templateTags = [{
505
name: 'httpsignature',
516
displayName: 'HTTP Signature',
@@ -62,12 +17,28 @@ module.exports.templateTags = [{
6217
}
6318
],
6419

65-
async run (context, keyId, privateKey) {
66-
const { store } = context
67-
await store.clear()
68-
await store.setItem('keyId', keyId)
69-
await store.setItem('privateKey', privateKey)
70-
await store.setItem('signRequest', true)
71-
return ' '
20+
async run(context, keyId, privateKey) {
21+
console.log(context)
22+
const request = await context.util.models.request.getById(context.meta.requestId)
23+
const requestUrl = await context.util.render(request.url)
24+
25+
if (!keyId) throw new Error('missing keyId')
26+
if (!privateKey) throw new Error('missing privateKey')
27+
28+
const parsedUrl = parseUrl(requestUrl)
29+
30+
const algorithmBits = 256
31+
const signAlgorithm = `RSA-SHA${algorithmBits}`
32+
33+
const signatureString = []
34+
signatureString.push(`(request-target): ${request.method.toLowerCase()} ${parsedUrl.path}`)
35+
signatureString.push(`host: ${parsedUrl.hostname}`)
36+
const signature = signatureString.join('\n')
37+
38+
const signatureSign = crypto.createSign(signAlgorithm)
39+
const signedSignature = signatureSign.update(signature).sign(`-----BEGIN RSA PRIVATE KEY-----\n${privateKey}\n-----END RSA PRIVATE KEY-----`, 'base64')
40+
41+
const authorization = `keyId="${keyId}", algorithm="${signAlgorithm.toLowerCase()}", headers="(request-target) host", signature="${signedSignature}"`
42+
return authorization
7243
}
7344
}]

0 commit comments

Comments
 (0)