1 | # node-http-signature
|
2 |
|
3 | node-http-signature is a node.js library that has client and server components
|
4 | for Joyent's [HTTP Signature Scheme](http_signing.md).
|
5 |
|
6 | ## Usage
|
7 |
|
8 | Note the example below signs a request with the same key/cert used to start an
|
9 | HTTP server. This is almost certainly not what you actually want, but is just
|
10 | used to illustrate the API calls; you will need to provide your own key
|
11 | management in addition to this library.
|
12 |
|
13 | ### Client
|
14 |
|
15 | ```js
|
16 | var fs = require('fs');
|
17 | var https = require('https');
|
18 | var httpSignature = require('http-signature');
|
19 |
|
20 | var key = fs.readFileSync('./key.pem', 'ascii');
|
21 |
|
22 | var 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.
|
32 | var req = https.request(options, function(res) {
|
33 | console.log(res.statusCode);
|
34 | });
|
35 |
|
36 |
|
37 | httpSignature.sign(req, {
|
38 | key: key,
|
39 | keyId: './cert.pem',
|
40 | keyPassphrase: 'secret' // (optional)
|
41 | });
|
42 |
|
43 | req.end();
|
44 | ```
|
45 |
|
46 | ### Server
|
47 |
|
48 | ```js
|
49 | var fs = require('fs');
|
50 | var https = require('https');
|
51 | var httpSignature = require('http-signature');
|
52 |
|
53 | var options = {
|
54 | key: fs.readFileSync('./key.pem'),
|
55 | cert: fs.readFileSync('./cert.pem')
|
56 | };
|
57 |
|
58 | https.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 |
|
76 | MIT.
|
77 |
|
78 | ## Bugs
|
79 |
|
80 | See <https://github.com/joyent/node-http-signature/issues>.
|