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

Added provider for weibo.com #311

Merged
merged 1 commit into from Aug 8, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
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
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
Expand Up @@ -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'})
};
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
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
@@ -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);
});