Permalink
Browse files

Merge pull request bnoguchi#311 from justan/master

Added provider for weibo.com
  • Loading branch information...
bnoguchi committed Aug 8, 2012
2 parents c5ee5fe + 20d643b commit 8c117106929f3c6230f22357661b5081ee8f37d1
Showing with 95 additions and 1 deletion.
  1. +1 −0 README.md
  2. +5 −1 example/conf.js
  3. +11 −0 example/server.js
  4. +4 −0 example/views/home.jade
  5. +74 −0 lib/modules/weibo.js
View
@@ -61,6 +61,7 @@ So far, `everyauth` enables you to login via:
<tr> <td> LDAP (experimental; not production-tested) <td>
<tr> <td> Windows Azure Access Control Service (ACS)<td> <a href="https://github.com/darrenzully">Dario Renzulli</a>, <a href="https://github.com/jpgarcia">Juan Pablo Garcia</a>, <a href="https://github.com/woloski">Matias Woloski</a> from <a href="http://blogs.southworks.net/">Southworks</a>
<tr><td><img src='https://www.dailycred.com/public/img/favicon.ico' style="vertical-align:middle">Dailycred <td> <a href='https://github.com/hstove'>Hank Stoever</a> at <a href='https://dailycred.com'>Dailycred.com</a>
<tr><td><img src='http://www.sinaimg.cn/blog/developer/wiki/LOGO_16x16.png' style="vertical-align:middle">Sina Weibo<td> <a href='https://github.com/justan'>justan</a>
</tbody>
</table>
View
@@ -119,5 +119,9 @@ module.exports = {
, mendeley: {
consumerKey: 'Enter your consumer key here'
, consumerSecret: 'Enter your consumer secret here'
}
}
, weibo: {
appId: '3350967939'
, appSecret: 'ef7f0a836d0ef315dca53e8d73816cc0'
}
};
View
@@ -52,6 +52,7 @@ var usersByMailchimpId = {};
var usersMailruId = {};
var usersByMendeleyId = {};
var usersByDcId = {};
var usersByWeiboId = {};
var usersByLogin = {
'brian@example.com': addUser({ login: 'brian@example.com', password: 'password'})
};
@@ -419,6 +420,16 @@ everyauth
(usersByMailchimpId[mailchimpUser.user_id] = addUser('mailchimp', mailchimpUser));
})
.redirectPath("/");
everyauth
.weibo
.appId(conf.weibo.appId)
.appSecret(conf.weibo.appSecret)
.findOrCreateUser( function (session, accessToken, accessTokenExtra, weiboUser){
return usersByWeiboId[weiboUser.uid] ||
(usersByWeiboId[weiboUser.uid] = addUser('weibo', weiboUser));
})
.redirectPath("/");
var app = express.createServer(
express.bodyParser()
View
@@ -93,6 +93,8 @@
input(type='submit') Login
#dailycred-login
a(href='/auth/dailycred') Login with Dailycred
#weibo-login
a(href='/auth/weibo') Login with sina weibo
- else
h2 Authenticated
- if (everyauth.facebook)
@@ -181,5 +183,7 @@
p= JSON.stringify(everyauth.mendeley.user)
- if (everyauth['dailycred'])
p= JSON.stringify(everyauth.dailycred.user.email)
- if (everyauth['weibo'])
p= JSON.stringify(everyauth.weibo.user)
h3
a(href='/logout') Logout
View
@@ -0,0 +1,74 @@
var oauthModule = require('./oauth2')
, querystring= require('querystring')
, request = require('request');
var weibo = module.exports =
oauthModule.submodule('weibo')
.configurable({
scope: "There's no idea about weibo's scope"
})
//fetch weibo user needs userid in extra
.step('fetchOAuthUser')
.accepts('accessToken extra')
.promises('oauthUser')
.oauthHost('https://api.weibo.com')
.apiHost('https://api.weibo.com')
.authPath('/oauth2/authorize')
.authQueryParam('response_type', 'code')
.accessTokenPath('/oauth2/access_token')
.accessTokenParam('grant_type', '')
.postAccessTokenParamsVia('data')
.entryPath('/auth/weibo')
.callbackPath('/auth/weibo/callback')
.authQueryParam('scope', function () {
return this._scope && this.scope();
})
.getAccessToken( function (code) {
var p = this.Promise()
, url = this._oauthHost + this._accessTokenPath
, opts = { url: url };
opts.form = {
client_id: this._appId
, redirect_uri: this._myHostname + this._callbackPath
, code: code
, client_secret: this._appSecret
};
request.post( opts, function(err, res, body){
var data;
if (err) {
p.fail(err);
} else {
data = JSON.parse(body); // sina weibo return a JSON with text/plain
p.fulfill(data.access_token, data);
delete data.access_token;
}
});
return p;
})
.fetchOAuthUser( function (accessToken, extra) {
var p = this.Promise();
var uid = extra.uid;
var url = this.apiHost() + "/2/users/show.json?uid=" + uid;
this.oauth.get(url, accessToken, function (err, user) {
if (err) {
p.fail(err);
}else{
p.fulfill(JSON.parse(user));
}
});
return p;
})
.convertErr( function (err) {
return new Error(err.data ? err.data : err);
});

0 comments on commit 8c11710

Please sign in to comment.