react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / request / node_modules / http-signature / lib / signer.js
81146 views// Copyright 2012 Joyent, Inc. All rights reserved.12var assert = require('assert-plus');3var crypto = require('crypto');4var http = require('http');56var sprintf = require('util').format;78910///--- Globals1112var Algorithms = {13'rsa-sha1': true,14'rsa-sha256': true,15'rsa-sha512': true,16'dsa-sha1': true,17'hmac-sha1': true,18'hmac-sha256': true,19'hmac-sha512': true20};2122var Authorization =23'Signature keyId="%s",algorithm="%s",headers="%s",signature="%s"';24252627///--- Specific Errors2829function MissingHeaderError(message) {30this.name = 'MissingHeaderError';31this.message = message;32this.stack = (new Error()).stack;33}34MissingHeaderError.prototype = new Error();353637function InvalidAlgorithmError(message) {38this.name = 'InvalidAlgorithmError';39this.message = message;40this.stack = (new Error()).stack;41}42InvalidAlgorithmError.prototype = new Error();43444546///--- Internal Functions4748function _pad(val) {49if (parseInt(val, 10) < 10) {50val = '0' + val;51}52return val;53}545556function _rfc1123() {57var date = new Date();5859var months = ['Jan',60'Feb',61'Mar',62'Apr',63'May',64'Jun',65'Jul',66'Aug',67'Sep',68'Oct',69'Nov',70'Dec'];71var days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];72return days[date.getUTCDay()] + ', ' +73_pad(date.getUTCDate()) + ' ' +74months[date.getUTCMonth()] + ' ' +75date.getUTCFullYear() + ' ' +76_pad(date.getUTCHours()) + ':' +77_pad(date.getUTCMinutes()) + ':' +78_pad(date.getUTCSeconds()) +79' GMT';80}81828384///--- Exported API8586module.exports = {8788/**89* Adds an 'Authorization' header to an http.ClientRequest object.90*91* Note that this API will add a Date header if it's not already set. Any92* other headers in the options.headers array MUST be present, or this93* will throw.94*95* You shouldn't need to check the return type; it's just there if you want96* to be pedantic.97*98* @param {Object} request an instance of http.ClientRequest.99* @param {Object} options signing parameters object:100* - {String} keyId required.101* - {String} key required (either a PEM or HMAC key).102* - {Array} headers optional; defaults to ['date'].103* - {String} algorithm optional; defaults to 'rsa-sha256'.104* - {String} httpVersion optional; defaults to '1.1'.105* @return {Boolean} true if Authorization (and optionally Date) were added.106* @throws {TypeError} on bad parameter types (input).107* @throws {InvalidAlgorithmError} if algorithm was bad.108* @throws {MissingHeaderError} if a header to be signed was specified but109* was not present.110*/111signRequest: function signRequest(request, options) {112assert.object(request, 'request');113assert.object(options, 'options');114assert.optionalString(options.algorithm, 'options.algorithm');115assert.string(options.keyId, 'options.keyId');116assert.optionalArrayOfString(options.headers, 'options.headers');117assert.optionalString(options.httpVersion, 'options.httpVersion');118119if (!request.getHeader('Date'))120request.setHeader('Date', _rfc1123());121if (!options.headers)122options.headers = ['date'];123if (!options.algorithm)124options.algorithm = 'rsa-sha256';125if (!options.httpVersion)126options.httpVersion = '1.1';127128options.algorithm = options.algorithm.toLowerCase();129130if (!Algorithms[options.algorithm])131throw new InvalidAlgorithmError(options.algorithm + ' is not supported');132133var i;134var stringToSign = '';135for (i = 0; i < options.headers.length; i++) {136if (typeof (options.headers[i]) !== 'string')137throw new TypeError('options.headers must be an array of Strings');138139var h = options.headers[i].toLowerCase();140141if (h !== 'request-line') {142var value = request.getHeader(h);143if (!value) {144throw new MissingHeaderError(h + ' was not in the request');145}146stringToSign += h + ': ' + value;147} else {148stringToSign +=149request.method + ' ' + request.path + ' HTTP/' + options.httpVersion;150}151152if ((i + 1) < options.headers.length)153stringToSign += '\n';154}155156var alg = options.algorithm.match(/(hmac|rsa)-(\w+)/);157var signature;158if (alg[1] === 'hmac') {159var hmac = crypto.createHmac(alg[2].toUpperCase(), options.key);160hmac.update(stringToSign);161signature = hmac.digest('base64');162} else {163var signer = crypto.createSign(options.algorithm.toUpperCase());164signer.update(stringToSign);165signature = signer.sign(options.key, 'base64');166}167168request.setHeader('Authorization', sprintf(Authorization,169options.keyId,170options.algorithm,171options.headers.join(' '),172signature));173174return true;175}176177};178179180