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

Commit 8b0d4d6

Browse files
beta version
0 parents  commit 8b0d4d6

File tree

6 files changed

+120
-0
lines changed

6 files changed

+120
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Alessio Dionisi
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# HTTP Signature for Insomnia REST Client
2+
3+
This is a plugin for the [Insomnia REST Client](https://insomnia.rest/)

package-lock.json

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

package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "insomnia-plugin-http-signature",
3+
"version": "1.0.0-beta.1",
4+
"author": "Alessio Dionisi <hello@adns.io>",
5+
"description": "HTTP Signature for Insomnia REST Client",
6+
"repository": "github:adnsio/insomnia-plugin-http-signature",
7+
"bugs": "https://github.com/adnsio/insomnia-plugin-http-signature/issues",
8+
"homepage": "https://github.com/adnsio/insomnia-plugin-http-signature#readme",
9+
"license": "MIT",
10+
"main": "./src/plugin.js",
11+
"insomnia": {
12+
"name": "http-signature",
13+
"description": "HTTP Signature"
14+
},
15+
"dependencies": {
16+
"luxon": "^1.3.3"
17+
}
18+
}

src/plugin.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
const crypto = require('crypto')
2+
const { DateTime } = require('luxon')
3+
const { parse: parseUrl } = require('url')
4+
5+
module.exports.requestHooks = [
6+
async context => {
7+
const { store, request } = context
8+
const keyId = await store.getItem('keyId')
9+
let privateKey = await store.getItem('privateKey')
10+
privateKey = `-----BEGIN PRIVATE KEY-----\n${privateKey}\n-----END PRIVATE KEY-----`
11+
12+
const parsedUrl = parseUrl(request.getUrl())
13+
14+
const algorithmBits = 256
15+
const hashAlgorithm = `sha${algorithmBits}`
16+
const digestAlgorithm = `SHA-${algorithmBits}`
17+
const signAlgorithm = `RSA-SHA${algorithmBits}`
18+
19+
const date = DateTime.utc().toRFC2822()
20+
21+
const digestHash = crypto.createHash(hashAlgorithm)
22+
const digest = digestHash.update(request.getBodyText()).digest('base64')
23+
24+
const signatureString = []
25+
signatureString.push(`(request-target): ${request.getMethod().toLowerCase()} ${parsedUrl.path}`)
26+
signatureString.push(`host: ${parsedUrl.hostname}`)
27+
signatureString.push(`digest: ${digestAlgorithm}=${digest}`)
28+
signatureString.push(`date: ${date}`)
29+
if (request.hasHeader('Content-Type')) signatureString.push(`content-type: ${request.getHeader('Content-Type')}`)
30+
const signature = signatureString.join('\n')
31+
32+
const signatureSign = crypto.createSign(signAlgorithm)
33+
const signedSignature = signatureSign.update(signature).sign(privateKey, 'base64')
34+
35+
const authorization = `Signature keyId="${keyId}", algorithm="${signAlgorithm.toLowerCase()}", headers="(request-target) host digest date${request.hasHeader('Content-Type') ? ' content-type' : ''}", signature="${signedSignature}"`
36+
37+
request.setHeader('Digest', `${digestAlgorithm}=${digest}`)
38+
request.setHeader('Date', date)
39+
request.setHeader('Authorization', authorization)
40+
}
41+
]
42+
43+
module.exports.templateTags = [{
44+
name: 'httpsignature',
45+
displayName: 'HTTP Signature',
46+
description: 'sign http requests',
47+
48+
args: [
49+
{
50+
displayName: 'Key ID',
51+
type: 'string'
52+
},
53+
{
54+
displayName: 'Private Key',
55+
type: 'string'
56+
}
57+
],
58+
59+
async run (context, keyId, privateKey) {
60+
await context.store.setItem('keyId', keyId)
61+
await context.store.setItem('privateKey', privateKey)
62+
return ' '
63+
}
64+
}]

0 commit comments

Comments
 (0)