Skip to content

Commit

Permalink
Merge pull request #311 from justan/master
Browse files Browse the repository at this point in the history
Added provider for weibo.com
  • Loading branch information
bnoguchi committed Aug 8, 2012
2 parents c5ee5fe + 20d643b commit 8c11710
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
6 changes: 5 additions & 1 deletion example/conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,9 @@ module.exports = {
, mendeley: {
consumerKey: 'Enter your consumer key here'
, consumerSecret: 'Enter your consumer secret here'
}
}
, weibo: {
appId: '3350967939'
, appSecret: 'ef7f0a836d0ef315dca53e8d73816cc0'
}
};
11 changes: 11 additions & 0 deletions example/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ var usersByMailchimpId = {};
var usersMailruId = {};
var usersByMendeleyId = {};
var usersByDcId = {};
var usersByWeiboId = {};
var usersByLogin = {
'[email protected]': addUser({ login: '[email protected]', password: 'password'})
};
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions example/views/home.jade
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
74 changes: 74 additions & 0 deletions lib/modules/weibo.js
Original file line number Diff line number Diff line change
@@ -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.