4 * Copyright(c) 2010 LearnBoost <dev@learnboost.com>
12 var crypto = require('crypto')
13 , parse = require('url').parse;
37 * Return an "Authorization" header value with the given `options`
38 * in the form of "AWS <key>:<signature>"
40 * @param {Object} options
45 exports.authorization = function(options){
46 return 'AWS ' + options.key + ':' + exports.sign(options);
50 * Simple HMAC-SHA1 Wrapper
52 * @param {Object} options
57 exports.hmacSha1 = function(options){
58 return crypto.createHmac('sha1', options.secret).update(options.message).digest('base64');
62 * Create a base64 sha1 HMAC for `options`.
64 * @param {Object} options
69 exports.sign = function(options){
70 options.message = exports.stringToSign(options);
71 return exports.hmacSha1(options);
75 * Create a base64 sha1 HMAC for `options`.
77 * Specifically to be used with S3 presigned URLs
79 * @param {Object} options
84 exports.signQuery = function(options){
85 options.message = exports.queryStringToSign(options);
86 return exports.hmacSha1(options);
90 * Return a string for sign() with the given `options`.
101 * @param {Object} options
106 exports.stringToSign = function(options){
107 var headers = options.amazonHeaders || '';
108 if (headers) headers += '\n';
112 , options.contentType
113 , options.date.toUTCString()
114 , headers + options.resource
119 * Return a string for sign() with the given `options`, but is meant exclusively
120 * for S3 presigned URLs
127 * @param {Object} options
132 exports.queryStringToSign = function(options){
134 options.date + '\n' +
139 * Perform the following:
141 * - ignore non-amazon headers
143 * - sort lexicographically
144 * - trim whitespace between ":"
145 * - join with newline
147 * @param {Object} headers
152 exports.canonicalizeHeaders = function(headers){
154 , fields = Object.keys(headers);
155 for (var i = 0, len = fields.length; i < len; ++i) {
156 var field = fields[i]
157 , val = headers[field]
158 , field = field.toLowerCase();
159 if (0 !== field.indexOf('x-amz')) continue;
160 buf.push(field + ':' + val);
162 return buf.sort().join('\n');
166 * Perform the following:
168 * - ignore non sub-resources
169 * - sort lexicographically
171 * @param {String} resource
176 exports.canonicalizeResource = function(resource){
177 var url = parse(resource, true)
178 , path = url.pathname
181 Object.keys(url.query).forEach(function(key){
182 if (!~keys.indexOf(key)) return;
183 var val = '' == url.query[key] ? '' : '=' + encodeURIComponent(url.query[key]);
187 return path + (buf.length
188 ? '?' + buf.sort().join('&')