This repository has been archived by the owner on Jun 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathawssum-amazon-ses.js
121 lines (90 loc) · 3.92 KB
/
awssum-amazon-ses.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//-------------------------------------------------------------------------------------------------------------------
//
// ses.js - class for AWS Simple Email Service
//
// Copyright (c) 2011 AppsAttic Ltd - http://www.appsattic.com/
// Written by Andrew Chilton <[email protected]>
//
// License: http://opensource.org/licenses/MIT
//
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// requires
// built-ins
var util = require('util');
// dependencies
var _ = require('underscore');
var dateFormat = require('dateformat');
// our own
var awssum = require('awssum');
var amazon = require('awssum-amazon');
var operations = require('./config.js');
// --------------------------------------------------------------------------------------------------------------------
// package variables
var MARK = 'ses: ';
// From: http://aws.amazon.com/releasenotes/Amazon-SES
var version = '2012-07-17';
// --------------------------------------------------------------------------------------------------------------------
// constructor
var Ses = function(opts) {
var self = this;
// we only have one region for this service, so default it here
opts.region = amazon.US_EAST_1;
// call the superclass for initialisation
Ses.super_.call(this, opts);
return self;
};
// inherit from Amazon
util.inherits(Ses, amazon.AmazonSignatureV2);
// --------------------------------------------------------------------------------------------------------------------
// methods we need to implement from awssum.js/amazon.js
Ses.prototype.method = function() {
return 'POST';
};
// From: http://docs.amazonwebservices.com/general/latest/gr/rande.html
Ses.prototype.host = function() {
return 'email.us-east-1.amazonaws.com';
};
Ses.prototype.version = function() {
return version;
};
Ses.prototype.addCommonOptions = function(options) {
var self = this;
// get the date in both %Y-%m-%dT%H:%M:%SZ and regular
var date = new Date();
var dateHeader = dateFormat(new Date(), "UTC:ddd, dd mmm yyyy HH:MM:ss Z");
var timestamp = date.toISOString();
// add the date header
options.headers.Date = dateHeader;
options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
// add in the common form fields
options.form = options.form || [];
options.form.push({ 'name' : 'AWSAccessKeyId', 'value' : this.accessKeyId() });
options.form.push({ 'name' : 'Timestamp', 'value' : timestamp });
options.form.push({ 'name' : 'Version', 'value' : self.version() });
// make the strToSign, create the signature and sign it
var strToSign = self.strToSign(options);
var signature = self.signature(strToSign);
self.addSignature(options, signature);
};
// From: http://docs.amazonwebservices.com/ses/latest/DeveloperGuide/index.html?HMACShaSignatures.html
Ses.prototype.strToSign = function(options) {
var self = this;
return options.headers.Date;
};
Ses.prototype.addSignature = function(options, signature) {
var self = this;
// do the extra headers (including the signature)
options.headers['X-Amzn-Authorization'] = 'AWS3-HTTPS AWSAccessKeyId=' + self.accessKeyId()
+ ', Signature=' + signature
+ ', Algorithm=' + self.signatureMethod();
};
// --------------------------------------------------------------------------------------------------------------------
// operations on the service
_.each(operations, function(operation, operationName) {
Ses.prototype[operationName] = awssum.makeOperation(operation);
});
// --------------------------------------------------------------------------------------------------------------------
// exports
exports.Ses = Ses;
// --------------------------------------------------------------------------------------------------------------------