Permalink
Browse files

Migrate the Amazon base plugin from appsattic/awssum to awssum/awssum…

…-amazon
  • Loading branch information...
chilts committed Feb 24, 2013
0 parents commit 0176d282ffa27febe7ec07839c2cc94a95c6355c
Showing with 725 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +29 −0 LICENSE
  3. 0 README.md
  4. +160 −0 awssum-amazon.js
  5. +118 −0 lib/aws-signature-v2.js
  6. +213 −0 lib/aws-signature-v4.js
  7. +47 −0 package.json
  8. +156 −0 test/amazon.js
@@ -0,0 +1,2 @@
+node_modules/
+*~
29 LICENSE
@@ -0,0 +1,29 @@
+-------------------------------------------------------------------------------
+
+This software is published under the MIT license as published here:
+
+* http://opensource.org/licenses/MIT
+
+-------------------------------------------------------------------------------
+
+Copyright 2011-2013 Apps Attic Ltd. All rights reserved.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+of the Software, and to permit persons to whom the Software is furnished to do
+so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+
+-------------------------------------------------------------------------------
No changes.
@@ -0,0 +1,160 @@
+// --------------------------------------------------------------------------------------------------------------------
+//
+// amazon.js - the base class for all Amazon Web Services
+//
+// Copyright (c) 2011 AppsAttic Ltd - http://www.appsattic.com/
+// Written by Andrew Chilton <chilts@appsattic.com>
+//
+// License: http://opensource.org/licenses/MIT
+//
+// --------------------------------------------------------------------------------------------------------------------
+
+// --------------------------------------------------------------------------------------------------------------------
+// requires
+
+var util = require("util");
+
+// our own library
+var awssum = require("awssum");
+var awsSignatureV2 = require('./lib/aws-signature-v2.js');
+
+// --------------------------------------------------------------------------------------------------------------------
+// constants
+
+var MARK = 'amazon: ';
+
+var US_EAST_1 = 'us-east-1';
+var US_WEST_1 = 'us-west-1';
+var US_WEST_2 = 'us-west-2';
+var EU_WEST_1 = 'eu-west-1';
+var AP_SOUTHEAST_1 = 'ap-southeast-1';
+var AP_SOUTHEAST_2 = 'ap-southeast-2';
+var AP_NORTHEAST_1 = 'ap-northeast-1';
+var SA_EAST_1 = 'sa-east-1';
+var US_GOV_WEST_1 = 'us-gov-west-1'; // See : http://aws.amazon.com/about-aws/globalinfrastructure/
+
+var Region = {
+ US_EAST_1 : true,
+ US_WEST_1 : true,
+ US_WEST_2 : true,
+ EU_WEST_1 : true,
+ AP_SOUTHEAST_1 : true,
+ AP_SOUTHEAST_2 : true,
+ AP_NORTHEAST_1 : true,
+ SA_EAST_1 : true,
+ US_GOV_WEST_1 : true,
+};
+
+// --------------------------------------------------------------------------------------------------------------------
+// constructor
+
+var Amazon = function(opts) {
+ var self = this;
+ var accessKeyId, secretAccessKey, awsAccountId, _awsAccountId, region, token;
+
+ // call the superclass for initialisation
+ Amazon.super_.call(this, opts);
+
+ // check that we have each of these values
+ if ( ! opts.accessKeyId ) {
+ throw MARK + 'accessKeyID is required';
+ }
+ if ( ! opts.secretAccessKey ) {
+ throw MARK + 'secretAccessKey is required';
+ }
+ if ( ! opts.region ) {
+ throw MARK + 'region is required';
+ }
+
+ // set the local vars so the functions below can close over them
+ accessKeyId = opts.accessKeyId;
+ secretAccessKey = opts.secretAccessKey;
+ region = opts.region;
+
+ // for services which can use the Simple Token Service (STS)
+ if ( opts.token ) {
+ token = opts.token;
+ }
+
+ // getters and setters
+ self.setAccessKeyId = function(newStr) { accessKeyId = newStr; };
+ self.setSecretAccessKey = function(newStr) { secretAccessKey = newStr; };
+ self.setAwsAccountId = function(newStr) {
+ var m;
+ if ( m = newStr.match(/^(\d{4})(\d{4})(\d{4})$/) ) {
+ awsAccountId = newStr;
+ _awsAccountId = m[1] + '-' + m[2] + '-' + m[3];
+ }
+ else if ( m = newStr.match(/^\d{4}-\d{4}-\d{4}$/) ) {
+ _awsAccountId = newStr;
+ awsAccountId = newStr.replace(/-/g, '');
+ }
+ else {
+ throw MARK + "invalid awsAccountId, must be '111122223333' or '1111-2222-3333'";
+ }
+ };
+
+ self.accessKeyId = function() { return accessKeyId; };
+ self.secretAccessKey = function() { return secretAccessKey; };
+ self.region = function() { return region; };
+ self.awsAccountId = function() { return awsAccountId; };
+ self._awsAccountId = function() { return _awsAccountId; };
+ self.token = function() { return token; };
+
+ // use the setAwsAccountId setter (which contains extra logic)
+ if ( opts.awsAccountId ) {
+ self.setAwsAccountId(opts.awsAccountId);
+ }
+
+ return self;
+};
+
+// inherit from AwsSum
+util.inherits(Amazon, awssum.AwsSum);
+
+// --------------------------------------------------------------------------------------------------------------------
+// functions to be overriden by inheriting class
+
+// see ../awssum.js for more details
+
+Amazon.prototype.extractBody = function() {
+ // most amazon services return XML, so override in inheriting classes if needed
+ return 'xml';
+};
+
+// --------------------------------------------------------------------------------------------------------------------
+// functions to be overriden by inheriting (Amazon) class
+
+// function version() -> string (the version of this service)
+// function signatureVersion() -> string (the signature version used)
+// function signatureMethod() -> string (the signature method used)
+// function strToSign(options) -> string (the string that needs to be signed)
+// function signature(strToSign) -> string (the signature itself)
+// function addSignature(options, signature) -> side effect, adds the signature to the 'options'
+
+// This service uses (defaults to) the AWS Signature v2.
+Amazon.prototype.signatureVersion = awsSignatureV2.signatureVersion;
+Amazon.prototype.signatureMethod = awsSignatureV2.signatureMethod;
+Amazon.prototype.strToSign = awsSignatureV2.strToSign;
+Amazon.prototype.signature = awsSignatureV2.signature;
+Amazon.prototype.addSignature = awsSignatureV2.addSignature;
+Amazon.prototype.addCommonOptions = awsSignatureV2.addCommonOptions;
+
+// --------------------------------------------------------------------------------------------------------------------
+// exports
+
+// constants
+exports.US_EAST_1 = US_EAST_1;
+exports.US_WEST_1 = US_WEST_1;
+exports.US_WEST_2 = US_WEST_2;
+exports.EU_WEST_1 = EU_WEST_1;
+exports.AP_SOUTHEAST_1 = AP_SOUTHEAST_1;
+exports.AP_SOUTHEAST_2 = AP_SOUTHEAST_2;
+exports.AP_NORTHEAST_1 = AP_NORTHEAST_1;
+exports.US_GOV_WEST_1 = US_GOV_WEST_1;
+exports.SA_EAST_1 = SA_EAST_1;
+
+// object constructor
+exports.Amazon = Amazon;
+
+// --------------------------------------------------------------------------------------------------------------------
@@ -0,0 +1,118 @@
+// --------------------------------------------------------------------------------------------------------------------
+//
+// aws-signature-v2.js - helper functions for AWS Signature v2
+//
+// Copyright (c) 2012 AppsAttic Ltd - http://www.appsattic.com/
+// Written by Andrew Chilton <chilts@appsattic.com>
+//
+// License: http://opensource.org/licenses/MIT
+//
+// --------------------------------------------------------------------------------------------------------------------
+
+// --------------------------------------------------------------------------------------------------------------------
+// requires
+
+// built-ins
+var crypto = require('crypto');
+
+// dependencies
+var _ = require('underscore');
+var esc = require('awssum').esc;
+
+// --------------------------------------------------------------------------------------------------------------------
+// constants
+
+var debug = false;
+
+// --------------------------------------------------------------------------------------------------------------------
+
+// Some example services and examples:
+//
+// * https://payments.amazon.com/sdui/sdui/helpTab/Amazon-Flexible-Payments-Service/Technical-Resources/Signature-V2
+// * http://docs.amazonwebservices.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/Query_QueryAuth.html
+// * http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/using-query-api.html
+
+function signatureVersion() {
+ return 2;
+}
+
+function signatureMethod() {
+ return 'HmacSHA256';
+}
+
+// Creates the strToSign for this request.
+function strToSign(options, args) {
+ var self = this;
+
+ // create the strToSign for this request
+ var toSign = options.method + "\n" + options.host.toLowerCase() + "\n" + options.path + "\n";
+
+ // now add on all of the params (after being sorted)
+ var pvPairs = _(options.params)
+ .chain()
+ .sortBy(function(p) { return p.name; })
+ .map(function(v, i) { return '' + esc(v.name) + '=' + esc(v.value); })
+ .join('&')
+ .value()
+ ;
+ toSign += pvPairs;
+
+ // console.log('toSign:', toSign);
+
+ return toSign;
+}
+
+// Creates the signature for this request.
+function signature(strToSign, options) {
+ var self = this;
+
+ // sign the request string
+ var sig = crypto
+ .createHmac('sha256', self.secretAccessKey())
+ .update(strToSign)
+ .digest('base64');
+
+ // console.log('Signature :', sig);
+
+ return sig;
+}
+
+// Adds the signature to the request.
+function addSignature(options, signature) {
+ var self = this;
+ options.params.push({ 'name' : 'Signature', 'value' : signature });
+}
+
+// Called by AwsSum, and this calls the rest of the Amazon Signature things (above).
+function addCommonOptions(options, args) {
+ var self = this;
+
+ // get the date in UTC : %Y-%m-%dT%H:%M:%SZ
+ var date = (new Date()).toISOString();
+
+ // add in the common params
+ options.params.push({ 'name' : 'AWSAccessKeyId', 'value' : self.accessKeyId() });
+ if( self.token() ) {
+ options.params.push({ 'name' : 'SecurityToken', 'value' : self.token() });
+ }
+ options.params.push({ 'name' : 'SignatureVersion', 'value' : self.signatureVersion() });
+ options.params.push({ 'name' : 'SignatureMethod', 'value' : self.signatureMethod() });
+ options.params.push({ 'name' : 'Timestamp', 'value' : date });
+ options.params.push({ 'name' : 'Version', 'value' : self.version() });
+
+ // make the strToSign, create the signature and sign it
+ var toSign = self.strToSign(options);
+ var signature = self.signature(toSign);
+ self.addSignature(options, signature);
+}
+
+// --------------------------------------------------------------------------------------------------------------------
+
+exports.signatureVersion = signatureVersion;
+exports.signatureMethod = signatureMethod;
+exports.strToSign = strToSign;
+exports.signature = signature;
+exports.addSignature = addSignature;
+exports.addCommonOptions = addCommonOptions;
+
+// --------------------------------------------------------------------------------------------------------------------
Oops, something went wrong.

0 comments on commit 0176d28

Please sign in to comment.