node-http-signature
node-http-signature is a node.js library that has client and server components for Joyent's HTTP Signature Scheme.
Usage
Note the example below signs a request with the same key/cert used to start an HTTP server. This is almost certainly not what you actually want, but is just used to illustrate the API calls; you will need to provide your own key management in addition to this library.
Client
var fs = require('fs');
var https = require('https');
var httpSignature = require('http-signature');
var key = fs.readFileSync('./key.pem', 'ascii');
var options = {
host: 'localhost',
port: 8443,
path: '/',
method: 'GET',
headers: {}
};
// Adds a 'Date' header in, signs it, and adds the
// 'Authorization' header in.
var req = https.request(options, function(res) {
console.log(res.statusCode);
});
httpSignature.sign(req, {
key: key,
keyId: './cert.pem'
});
req.end();
Server
var fs = require('fs');
var https = require('https');
var httpSignature = require('http-signature');
var options = {
key: fs.readFileSync('./key.pem'),
cert: fs.readFileSync('./cert.pem')
};
https.createServer(options, function (req, res) {
var rc = 200;
var parsed = httpSignature.parseRequest(req);
var pub = fs.readFileSync(parsed.keyId, 'ascii');
if (!httpSignature.verifySignature(parsed, pub))
rc = 401;
res.writeHead(rc);
res.end();
}).listen(8443);
Installation
npm install http-signature
License
MIT.
Bugs
See <https://github.com/joyent/node-http-signature/issues>.
Source
| 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 | }); |
| 41 | |
| 42 | req.end(); |
| 43 | ``` |
| 44 | |
| 45 | ### Server |
| 46 | |
| 47 | ```js |
| 48 | var fs = require('fs'); |
| 49 | var https = require('https'); |
| 50 | var httpSignature = require('http-signature'); |
| 51 | |
| 52 | var options = { |
| 53 | key: fs.readFileSync('./key.pem'), |
| 54 | cert: fs.readFileSync('./cert.pem') |
| 55 | }; |
| 56 | |
| 57 | https.createServer(options, function (req, res) { |
| 58 | var rc = 200; |
| 59 | var parsed = httpSignature.parseRequest(req); |
| 60 | var pub = fs.readFileSync(parsed.keyId, 'ascii'); |
| 61 | if (!httpSignature.verifySignature(parsed, pub)) |
| 62 | rc = 401; |
| 63 | |
| 64 | res.writeHead(rc); |
| 65 | res.end(); |
| 66 | }).listen(8443); |
| 67 | ``` |
| 68 | |
| 69 | ## Installation |
| 70 | |
| 71 | npm install http-signature |
| 72 | |
| 73 | ## License |
| 74 | |
| 75 | MIT. |
| 76 | |
| 77 | ## Bugs |
| 78 | |
| 79 | See <https://github.com/joyent/node-http-signature/issues>. |