Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

- Added OAuth strategy for t.sina.com.cn

- Added example app-sina.js for t.sina.com.cn
  • Loading branch information...
commit e8b9836380fc160d0638cffdd67e8452ba4f99b9 1 parent 8d7de78
@dsiu dsiu authored
Showing with 265 additions and 0 deletions.
  1. +182 −0 examples/app-sina.js
  2. +83 −0 lib/auth.strategies/sina.js
View
182 examples/app-sina.js
@@ -0,0 +1,182 @@
+var connect = require('connect');
+var MemoryStore = require('connect/middleware/session/memory');
+var auth = require('../lib/index');
+var util = require('util');
+
+// We let the example run without npm, by setting up the require paths
+// so the node-oauth submodule inside of git is used. You do *NOT*
+// need to bother with this line if you're using npm ...
+require.paths.unshift('support');
+var OAuth = require('oauth').OAuth;
+
+
+// N.B. TO USE Any of the OAuth or RPX strategies you will need to provide
+// a copy of the example_keys_file (named keys_file)
+try {
+ var example_keys = require('./keys_file');
+ for (var key in example_keys) {
+ global[key] = example_keys[key];
+ }
+}
+catch(e) {
+ console.log('Unable to locate the keys_file.js file. Please copy and ammend the example_keys_file.js as appropriate');
+ return;
+}
+
+var sinaWBOAuth = new OAuth("http://api.t.sina.com.cn/oauth/request_token",
+ "http://api.t.sina.com.cn/oauth/access_token",
+ sinaConsumerKey, sinaConsumerSecret,
+ "1.0", sinaCallbackAddress, "HMAC-SHA1");
+
+function routes(app) {
+ app.get('/auth/sina', function(req, res, params) {
+ req.authenticate(['sina'], function(error, authenticated) {
+ res.writeHead(200, {'Content-Type': 'text/html'});
+ if (authenticated) {
+ res.end("<html><h1>Hello Sina user:" + JSON.stringify(req.getAuthDetails().user) + ".</h1></html>")
+ }
+ else {
+ res.end("<html><h1>Sina authentication failed :( </h1></html>")
+ }
+ });
+ });
+
+ app.get('/sina/user_timeline', function(req, res, params) {
+ req.authenticate((['sina']), function(error, authenticated) {
+ res.writeHead(200, {'Content-Type': 'text/html'});
+ if (authenticated) {
+ sinaWBOAuth.getProtectedResource('http://api.t.sina.com.cn/statuses/user_timeline.json', 'GET',
+ req.getAuthDetails()['sina_oauth_token'],
+ req.getAuthDetails()['sina_oauth_token_secret'],
+ function (error, data) {
+ if (error) {
+ var r = ["<html><pre>",
+ "error = " + util.inspect(error),
+ "</pre></html>"];
+ res.end(r.join(""));
+ } else {
+ var utl = JSON.parse(data);
+ res.write("<html><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");
+ res.write("<body>");
+
+ res.write("<h1>Fetch user_timeline for Sina user:" + JSON.stringify(req.getAuthDetails().user) + ".</h1>");
+ res.write(util.inspect(utl));
+ res.end("</html>");
+ }
+ }
+ )
+ }
+ else {
+ res.end("<html><h1>You are not logged in to Sina</h1></html>");
+ }
+ });
+ });
+
+ app.get('/sina/post_current_time', function(req, res, params) {
+ req.authenticate((['sina']), function(error, authenticated) {
+ res.writeHead(200, {'Content-Type': 'text/html'});
+ if (authenticated) {
+ var s = "The time now is: " + new Date();
+ sinaWBOAuth.getProtectedResource('http://api.t.sina.com.cn/statuses/update.json?status=' + encodeURIComponent(s), 'POST',
+ req.getAuthDetails()['sina_oauth_token'],
+ req.getAuthDetails()['sina_oauth_token_secret'],
+ function (error, data) {
+ if (error) {
+ var r = ["<html><pre>",
+ "error = " + util.inspect(error),
+ "</pre></html>"];
+ res.end(r.join(""));
+ }
+ else {
+ var result = JSON.parse(data);
+ res.write("<html><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");
+ res.write("<body>");
+ res.write("<h1>Message Posted for Sina user:" + JSON.stringify(req.getAuthDetails().user) + ".</h1>");
+ res.write("<body>");
+ res.write("<ol>");
+ res.write("<li>Message = " + s);
+ res.write("<li>");
+ res.write("<li>Result = " + JSON.stringify(util.inspect(result)));
+ res.write("</ol>");
+ res.write("</body>");
+ res.end("</html>");
+ }
+ }
+ )
+ }
+ else {
+ res.end("<html><h1>You are not logged in to Sina</h1></html>");
+ }
+ });
+ });
+
+
+ app.get('/auth/anon', function(req, res, params) {
+ req.authenticate(['anon'], function(error, authenticated) {
+ res.writeHead(200, {'Content-Type': 'text/html'})
+ res.end("<html><h1>Hello! Full anonymous access</h1></html>")
+ });
+ })
+
+ app.get('/auth/never', function(req, res, params) {
+ req.authenticate(['anon'], function(error, authenticated) {
+ res.writeHead(200, {'Content-Type': 'text/html'})
+ res.end("<html><h1>Hello! Authenticated: " + authenticated + "</h1></html>")
+ });
+ })
+
+ app.get('/logout', function(req, res, params) {
+ req.logout();
+ res.writeHead(303, { 'Location': "/" });
+ res.end('');
+ })
+
+ app.get('/', function(req, res, params) {
+ var self = this;
+ res.writeHead(200, {'Content-Type': 'text/html'})
+ if (!req.isAuthenticated()) {
+ res.end('<html> \n\
+ <head> \n\
+ <title>connect Auth -- Not Authenticated</title> \n\
+ </head> \n\
+ <body> \n\
+ <div id="wrapper"> \n\
+ <h1>Not authenticated</h1> \n\
+ <div style="float:left;margin-left:5px"> \n\
+ <a href="/auth/sina" style="border:0px"> \n\
+ <img style="border:0px" src="http://open.sinaimg.cn/wikipic/button/16.png"/> \n\
+ </a> \n\
+ </div> \n\
+ </div> \n\
+ </body> \n\
+ </html>')
+ }
+ else {
+ res.end('<html> \n\
+ <head> \n\
+ <title>Express Auth -- Authenticated</title>\n\
+ </head> \n\
+ <body> \n\
+ <div id="wrapper"> \n\
+ <h1>Authenticated</h1> \n\
+ ' + JSON.stringify(req.getAuthDetails().user) + ' \n\
+ <h2><a href="/logout">Logout</a></h2> \n\
+ </div> \n\
+ </body> \n\
+ </html>')
+ }
+ })
+}
+
+var server = connect.createServer(
+ connect.cookieDecoder(),
+ connect.session({ store: new MemoryStore({ reapInterval: -1 }) }),
+ connect.bodyDecoder() /* Only required for the janrain strategy*/,
+ auth([
+ auth.Anonymous(),
+ auth.Never(),
+ auth.Sina({consumerKey: sinaConsumerKey, consumerSecret: sinaConsumerSecret, callback: sinaCallbackAddress})
+ ]),
+ connect.router(routes));
+
+server.listen(80);
View
83 lib/auth.strategies/sina.js
@@ -0,0 +1,83 @@
+/*!
+ * Copyright(c) 2010 Danny Siu <danny.siu@gmail.com>
+ */
+var OAuth = require("oauth").OAuth,
+ url = require("url"),
+ connect = require("connect"),
+ util = require("util"),
+ http = require('http');
+
+Sina = module.exports = function(options, server) {
+ options = options || {};
+ var that = {};
+ var my = {};
+
+ // Construct the internal OAuth client
+ my._oAuth = new OAuth("http://api.t.sina.com.cn/oauth/request_token",
+ "http://api.t.sina.com.cn/oauth/access_token",
+ options.consumerKey, options.consumerSecret,
+ "1.0", options.callback, "HMAC-SHA1");
+
+ // Give the strategy a name
+ that.name = options.name || "sina";
+
+ // Build the authentication routes required
+ that.setupRoutes = function(server) {
+ server.use('/', connect.router(function routes(app) {
+ app.get('/auth/sina_callback', function(req, res) {
+ req.authenticate([that.name], function(error, authenticated) {
+ res.writeHead(303, { 'Location': req.session.sina_redirect_url });
+ res.end('');
+ });
+ });
+ }));
+ };
+
+
+ // Declare the method that actually does the authentication
+ that.authenticate = function(request, response, callback) {
+ //todo: if multiple connect middlewares were doing this, it would be more efficient to do it in the stack??
+
+ var parsedUrl = url.parse(request.url, true);
+
+ //todo: makw the call timeout ....
+ var self = this;
+ if (parsedUrl.query &&
+ parsedUrl.query.oauth_token &&
+ parsedUrl.query.oauth_verifier &&
+ request.session.auth["sina_oauth_token_secret"] &&
+ parsedUrl.query.oauth_token == request.session.auth["sina_oauth_token"]) {
+
+ my._oAuth.getOAuthAccessToken(parsedUrl.query.oauth_token,
+ request.session.auth["sina_oauth_token_secret"],
+ parsedUrl.query.oauth_verifier,
+ function(error, oauth_token, oauth_token_secret, additionalParameters) {
+ if (error) {
+ callback(null);
+ }
+ else {
+ request.session.auth["sina_oauth_token_secret"] = oauth_token_secret;
+ request.session.auth["sina_oauth_token"] = oauth_token;
+ self.success(additionalParameters, callback);
+ }
+ })
+ }
+ else {
+ my._oAuth.getOAuthRequestToken(function(error, oauth_token, oauth_token_secret, results) {
+ if (error) {
+ callback(null); // Ignore the error upstream, treat as validation failure.
+ } else {
+ request.session['sina_redirect_url'] = request.url;
+ request.session.auth["sina_oauth_token_secret"] = oauth_token_secret;
+ request.session.auth["sina_oauth_token"] = oauth_token;
+
+ self.redirect(response,
+ "http://api.t.sina.com.cn/oauth/authorize?oauth_token=" + oauth_token
+ + "&oauth_callback=" + encodeURIComponent(options.callback),
+ callback);
+ }
+ });
+ }
+ };
+ return that;
+};

2 comments on commit e8b9836

Please sign in to comment.
Something went wrong with that request. Please try again.