Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Server Handling, Secure Bug Fix, Passing custom Data around #53

Merged
merged 11 commits into from
May 20, 2016
Prev Previous commit
Next Next commit
passing data around instead of class properties
sumit0k committed May 18, 2016
commit 2a94216411c6898e2e179aceac1c22f3afc45d51
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -220,7 +220,7 @@ These fields are not needed if ownServer is false
You can register the following events:

* `error` This event is emitted when an error occured and receives one argument that contains the error message.
* `authed` Emitted when the OAuth was completed successfully. Receives one argument which represents the API key that can be passed on to other API functionality.
* `authed` Emitted when the OAuth was completed successfully. Receives one argument which represents the API key in custom object with metadata that can be passed on to other API functionality.

Example:

@@ -231,22 +231,21 @@ var MailChimpAPI = require('mailchimp').MailChimpAPI;
var options = {
clientId: 'Your MailChimp client id',
clientSecret: 'Your MailChimp client secret',
serverUri: 'http://www.example.com',
redirectUri: 'http://www.example.com/successfulLogin.html'
redirectUri: 'http://www.example.com/redirect'
};

var oauth = new MailChimpOAuth(options);

console.log(oauth.getAuthorizeUri()); // The MailChimp login URI the user needs to be sent to

oauth.on('error', function (error) {
console.log(error.message);
oauth.on('error', function (error, customData) {
console.log(error.message, customData);
});

oauth.on('authed', function (apiKey) {
oauth.on('authed', function (data) {

try {
var api = new MailChimpAPI(apiKey, { version : '1.3', secure : false });
var api = new MailChimpAPI(data.apiKey, { version : '1.3', secure : false });
} catch (error) {
console.log(error.message);
}
49 changes: 20 additions & 29 deletions lib/mailchimp/MailChimpOAuth.js
Original file line number Diff line number Diff line change
@@ -49,14 +49,7 @@ var http = require('http'),
function MailChimpOAuth(options) {

var self = this;

this.code;
this.accessToken;
this.metadata;
this.apiKey;

if (!options)
var options = {};
options = options || {};

EventEmitter.call(this);

@@ -99,7 +92,7 @@ function MailChimpOAuth(options) {
});

this.on('receivedMetadata', function (customParams) {
self.emit('authed', self.apiKey, customParams);
self.emit('authed', customParams);
});

if(this.spawnServerFlag)
@@ -132,31 +125,23 @@ MailChimpOAuth.prototype.spawnServer = function () {
}

var requestUrl = url.parse(request.url);
var query = querystring.parse(requestUrl.query);
var query = querystring.parse(requestUrl.query) || {};

if (typeof query.code !== 'undefined') {

self.code = query.code;
self.emit('receivedCode');

self.emit('receivedCode', query);
if (self.finalUri) {
response.writeHead(302, {'Location': self.finalUri});
response.end();
} else {
response.writeHead(204, {'Content-Type': 'text/plain'});
response.end();
}

} else {

self.emit('error', new Error('Received a request without a code.'));

self.emit('error', new Error('Received a request without a code.'), query);
response.writeHead(500, {'Content-Type': 'text/plain'});
response.end();

}

}
};
var server;


@@ -186,8 +171,8 @@ MailChimpOAuth.prototype.spawnServer = function () {
*/
MailChimpOAuth.prototype.handleOAuthResponse = function (query) {
var self = this;
query = query || {};
if (typeof query.code !== 'undefined') {
self.code = query.code;
self.emit('receivedCode', query);
} else {
self.emit('error', new Error('Received a request without a code.'), query);
@@ -226,12 +211,16 @@ MailChimpOAuth.prototype.getAuthorizeUri = function () {
MailChimpOAuth.prototype.getAccessToken = function (customParams) {

var self = this;

customParams = customParams || {};
if(!customParams.code){
self.emit('error', new Error('Code is required in Params'), customParams);
return;
}
var params = {
grant_type: 'authorization_code',
client_id: this.clientId,
client_secret: this.clientSecret,
code: this.code,
code: customParams.code,
redirect_uri: this.addPort?this.redirectUri + ':' + this.httpPort:this.redirectUri
};

@@ -249,7 +238,6 @@ MailChimpOAuth.prototype.getAccessToken = function (customParams) {

if (error) {
self.emit('error', new Error('Unable to connect to the MailChimp OAuth service.'), customParams);
return;
} else {

try {
@@ -261,7 +249,7 @@ MailChimpOAuth.prototype.getAccessToken = function (customParams) {
}

if (typeof parsedResponse.access_token !== 'undefined') {
self.accessToken = parsedResponse.access_token;
customParams.accessToken = parsedResponse.access_token;
self.emit('receivedAccessToken', customParams);
} else {
self.emit('error', new Error('Answer from MailChimp API does not contain an access token.'), customParams);
@@ -285,11 +273,15 @@ MailChimpOAuth.prototype.getMetadata = function (customParams) {

var self = this;
customParams = customParams || {};
if(!customParams.accessToken){
self.emit('error', new Error('accessToken is required in Params'), customParams);
return;
}
request({
uri: 'https://login.mailchimp.com/oauth2/metadata',
headers: {
'User-Agent': 'node-mailchimp/' + this.packageInfo['version'],
'Authorization': 'OAuth ' + this.accessToken
'Authorization': 'OAuth ' + customParams.accessToken
}
}, function (error, response, body) {

@@ -309,9 +301,8 @@ MailChimpOAuth.prototype.getMetadata = function (customParams) {
}

if (typeof parsedResponse.dc !== 'undefined') {
self.metadata = parsedResponse;
self.apiKey = self.accessToken + '-' + self.metadata.dc;
customParams.metadata = parsedResponse;
customParams.apiKey = self.accessToken + '-' + self.metadata.dc;
self.emit('receivedMetadata', customParams);
} else {
self.emit('error', new Error('Answer from MailChimp API does not contain a datacenter pointer.'), customParams);