Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'master' of github.com:bnoguchi/everyauth

Conflicts:
	example/server.js
	test/creds.dummy.js
  • Loading branch information...
commit 3fa0f8ef5805501c989f46bbbdcfb33ac9fa8952 2 parents 505af47 + 533344d
Brian Noguchi authored
30 README.md
View
@@ -47,6 +47,8 @@ So far, `everyauth` enables you to login via:
<tr> <td> <img src="https://github.com/bnoguchi/everyauth/raw/master/media/soundcloud.ico" style="vertical-align:middle"> SoundCloud <td> <a href="https://github.com/chrisleishman">Chris Leishman</a>
<tr> <td> <img src="https://github.com/bnoguchi/everyauth/raw/master/media/mixi.ico" style="vertical-align:middle"> mixi
<td> <a href="https://github.com/ufssf">ufssf</a>
+ <tr> <td> <img src="http://static.mailchimp.com/www/downloads/brand-assets/Freddie_Light_Background.png" style="vertical-align:middle" width="16px"> Mailchimp
+ <td> <a href="http://github.com/wnadeau">Winfred Nadeau</a>
</tbody>
<tbody id=misc>
<tr> <td> <img src="https://github.com/bnoguchi/everyauth/raw/master/media/box.ico" style="vertical-align:middle"> Box.net <td>
@@ -1927,6 +1929,34 @@ var routes = function (app) {
};
```
+### Mailchimp OAuth2
+
+First, register an app [in Mailchimp](http://login.mailchimp.com).
+
+```javascript
+var everyauth = require('everyauth')
+ , connect = require('connect');
+
+everyauth.mailchimp
+ .appId('YOUR CLIENT KEY HERE')
+ .appSecret('YOUR CLIENT SECRET HERE')
+ .myHostname(process.env.HOSTNAME || "http://127.0.0.1:3000")//MC requires 127.0.0.1 for dev
+ .findOrCreateUser( function (session, accessToken, accessTokenExtra, mailchimpUserData) {
+ // find or create user logic goes here
+ // Return a user or Promise that promises a user
+ // Promises are created via
+ // var promise = this.Promise();
+ // The mailchimpUserData object contains everything from the API method getAccountDetails and an apikey.
+ // You'll want to work with mailchimpUserData.user_id for queries
+ // and mailchimpUserData.apikey for your API wrapper
+ })
+ .redirectPath('/');
+
+var routes = function (app) {
+ // Define your routes here
+};
+```
+
### OpenID protocol
OpenID protocol allows you to use an openid auth request. You can read more information about it here http://openid.net/
4 example/conf.js
View
@@ -104,4 +104,8 @@ module.exports = {
, consumerSecret: 'Enter your consumer secret here'
, scope: 'r_profile'
}
+ , mailchimp: {
+ appId: 'Enter your client/app id here'
+ , appSecret: 'Enter your client/app secret here'
+ }
};
12 example/server.js
View
@@ -48,6 +48,7 @@ var usersByAzureAcs = {};
var usersByTripIt = {};
var usersBy500pxId = {};
var usersBySoundCloudId = {};
+var usersByMailchimpId = {};
var usersByLogin = {
'brian@example.com': addUser({ login: 'brian@example.com', password: 'password'})
};
@@ -379,6 +380,17 @@ everyauth
(usersByFbId[mixiUserMetadata.id] = addUser('mixi', mixiUserMetadata));
})
.redirectPath('/');
+
+everyauth
+ .mailchimp
+ .appId(conf.mailchimp.appId)
+ .appSecret(conf.mailchimp.appSecret)
+ .myHostname(process.env.HOSTNAME || "http://127.0.0.1:3000")//MC requires 127.0.0.1 for dev
+ .findOrCreateUser( function (session, accessToken, accessTokenExtra, mailchimpUser){
+ return usersByMailchimpId[mailchimpUser.id] ||
+ (usersByMailchimpId[mailchimpUser.user_id] = addUser('mailchimp', mailchimpUser));
+ })
+ .redirectPath("/");
var app = express.createServer(
express.bodyParser()
5 example/views/home.jade
View
@@ -81,6 +81,8 @@
#mixi-login
a(href='/auth/mixi', style='border: 0px')
img(src='http://mixi.co.jp/press/images/thumb_press_def.gif', title='Login with mixi')
+ #mailchimp-login
+ a(href='/auth/mailchimp',style='border: 0px') Login with Mailchimp
#openid-login
form#openid(action='/auth/openid')
label(for='openid_identifier') OpenID Identifier: &nbsp;
@@ -166,5 +168,8 @@
- if (everyauth.mixi)
h3 mixi User Data
p= JSON.stringify(everyauth.mixi.user)
+ - if (everyauth.mailchimp)
+ h3 mailchimp User Data
+ p= JSON.stringify(everyauth.mailchimp.user)
h3
a(href='/logout') Logout
43 lib/modules/mailchimp.js
View
@@ -0,0 +1,43 @@
+var oauthModule = require('./oauth2'),
+ rest = require('../restler');
+
+var mailchimp = module.exports =
+oauthModule.submodule('mailchimp')
+ .configurable({
+ metadataPath: "Although this shouldn't be changed, this is where we get the datacenter for building the API key."
+ })
+ .oauthHost('https://login.mailchimp.com')
+ .authPath('/oauth2/authorize')
+ .accessTokenPath('/oauth2/token')
+ .metadataPath('/oauth2/metadata')
+
+ .entryPath('/auth/mailchimp')
+ .callbackPath('/auth/mailchimp/callback')
+ .fetchOAuthUser( function (accessToken) {
+ var p = this.Promise();
+ this.oauth._request("GET", this.oauthHost() + this.metadataPath(), {
+ Authorization: 'OAuth ' + accessToken
+ },"","",function(error,data){
+ if (error) return p.fail(error);
+
+ var metadata = JSON.parse(data);
+ var apikey = accessToken + "-"+ metadata.dc;
+
+ rest.post(metadata.api_endpoint + '/1.3/?method=getAccountDetails',
+ {data:{apikey: apikey}})
+
+ .on('success',function(user,response){
+ user.apikey = apikey;
+ p.fulfill(user);
+ })
+
+ .on('error',function(err){
+ p.fail(err);
+ });
+ });
+
+ return p;
+ })
+ .postAccessTokenParamsVia("data")
+ .authQueryParam('response_type','code')
+ .accessTokenParam('grant_type','authorization_code')
4 test/creds.dummy.js
View
@@ -11,4 +11,8 @@ module.exports = {
login: ''
, password: ''
}
+ , mailchimp: {
+ login: ''
+ , password: ''
+ }
};
38 test/mailchimp.tobi.js
View
@@ -0,0 +1,38 @@
+var tobi = require('tobi')
+ , expect = require('expect.js')
+ , creds = require('./creds.js');
+
+require('./util/expect.js');
+
+describe('Mailchimp', function () {
+ var app, browser;
+
+ beforeEach( function () {
+ delete require.cache[require.resolve('./app')]
+ app = require('./app')
+ tobi.Browser.browsers = {};
+ var everyauth = require('../index');
+ everyauth.debug = false;
+ browser = tobi.createBrowser(3000, '127.0.0.1'); //mailchimp requires 127.0.0.1 instead of localhost in dev/test.
+ browser.userAgent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.100 Safari/534.30';
+ });
+
+ afterEach( function () {
+ app.close();
+ });
+
+ it('should successfully login with the right username, password', function (done) {
+ this.timeout(10000);
+ browser.get('/auth/mailchimp', function (res, $) {
+ $('#login-form')
+ .fill({
+ username: creds.mailchimp.login
+ , password: creds.mailchimp.password })
+ .submit(function (res, $) {
+ expect($('h2')).to.have.text('Authenticated');
+ expect($('h2')).to.not.have.text('Not Authenticated');
+ done();
+ });
+ });
+ });
+});
Please sign in to comment.
Something went wrong with that request. Please try again.