UNPKG

1.64 kBMarkdownView Raw
1# node-http-signature
2
3node-http-signature is a node.js library that has client and server components
4for Joyent's [HTTP Signature Scheme](http_signing.md).
5
6## Usage
7
8Note the example below signs a request with the same key/cert used to start an
9HTTP server. This is almost certainly not what you actually want, but is just
10used to illustrate the API calls; you will need to provide your own key
11management in addition to this library.
12
13### Client
14
15```js
16var fs = require('fs');
17var https = require('https');
18var httpSignature = require('http-signature');
19
20var key = fs.readFileSync('./key.pem', 'ascii');
21
22var options = {
23 host: 'localhost',
24 port: 8443,
25 path: '/',
26 method: 'GET',
27 headers: {}
28};
29
30// Adds a 'Date' header in, signs it, and adds the
31// 'Authorization' header in.
32var req = https.request(options, function(res) {
33 console.log(res.statusCode);
34});
35
36
37httpSignature.sign(req, {
38 key: key,
39 keyId: './cert.pem',
40 keyPassphrase: 'secret' // (optional)
41});
42
43req.end();
44```
45
46### Server
47
48```js
49var fs = require('fs');
50var https = require('https');
51var httpSignature = require('http-signature');
52
53var options = {
54 key: fs.readFileSync('./key.pem'),
55 cert: fs.readFileSync('./cert.pem')
56};
57
58https.createServer(options, function (req, res) {
59 var rc = 200;
60 var parsed = httpSignature.parseRequest(req);
61 var pub = fs.readFileSync(parsed.keyId, 'ascii');
62 if (!httpSignature.verifySignature(parsed, pub))
63 rc = 401;
64
65 res.writeHead(rc);
66 res.end();
67}).listen(8443);
68```
69
70## Installation
71
72 npm install http-signature
73
74## License
75
76MIT.
77
78## Bugs
79
80See <https://github.com/joyent/node-http-signature/issues>.