Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added documentation to readme, refactored a bit

  • Loading branch information...
commit 0650bd1d0fc4a88167843aae37ba9f7781e08d23 1 parent d62ee70
@xrd authored
Showing with 198 additions and 9 deletions.
  1. +0 −9 README
  2. +56 −0 README.rdoc
  3. +30 −0 login.html
  4. +112 −0 rpx.js
View
9 README
@@ -1,9 +0,0 @@
-Use RPX with Connect:
-
-Usage:
-
-
-
-TODO:
-
-Remove dependency on restler
View
56 README.rdoc
@@ -0,0 +1,56 @@
+= connect-rpx.js
+
+Use RPX with Connect:
+
+== Usage:
+
+// Setup whatever require paths you need...
+var Connect = require('connect');
+var MemoryStore = require('connect/lib/connect/middleware/session/memory').MemoryStore;
+var RPX = require( 'rpx' );
+
+// Setup RPX
+//
+// Your API key, look in the settings on rpxnow.com
+RPX.config( 'apiKey', 'abcefgehijk' );
+// (Specify root paths to ignore, if you need to serve them for your login page)
+RPX.config( 'ignorePaths', [ '/stylesheets', '/images', '/javascript', '/css' ] );
+// This is where RPX will redirect you to once authentication has occurred
+RPX.config( 'reentryPoint', '/rpx_login' );
+// This will remove the user auth session cookie, effectively logging you out.
+RPX.config( 'logoutPoint', '/logout' );
+// The page which you use as a login page, see the sample login.html here.
+RPX.config( 'loginPage', '/static/login.html' );
+
+// Setup your connect. RPX requires session, cookieDecoder, redirect installed before RPX.
+var minute = 60000;
+var root = __dirname + "/public";
+var Server = module.exports = Connect.createServer(
+ Connect.logger(),
+ Connect.bodyDecoder(),
+ Connect.redirect(),
+ Connect.cookieDecoder(),
+ Connect.session({ store: new MemoryStore({ reapInterval: minute, maxAge: minute * 5 }) }),
+ RPX.handler,
+ Connect.staticProvider( root ) // this is not strictly required,
+ // but might be where you store login page and CSS, etc.
+);
+
+* Then, run your app, and it should redirect to you the loginPath specified in the configuration.
+
+* Need to test locally? This will enable a random user login rather than going into RPX for authentication
+* allowing you to test while on a plane and simulate a login.
+
+// Add this after the last RPX.config line
+// DON'T FORGET TO REMOVE THIS IN PRODUCTION!!!
+RPX.config( 'fakedAuthentication', true );
+
+== TODO:
+
+* Remove dependency on restler
+* Add something to better handle errors or cancellations
+* Write test cases, I'm so embarassed to publish without them....
+
+== THANKS
+
+This code was based on the rack-rpx module
View
30 login.html
@@ -0,0 +1,30 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
+<html>
+<head>
+<title>Login</title>
+</head>
+<body class='bp'>
+<div class='rpxlogin' style='display: none;'>
+<script>var base = '/rpx_login';</script>
+<script src='https://rpxnow.com/openid/v2/widget' type='text/javascript'></script>
+<script type='text/javascript'>
+ //<![CDATA[
+ // You probably want this to be a real server once you are not testing locally
+ RPXNOW.token_url = "http://localhost:3000" + base;
+ // Use the RPX realm here provided on rpxnow.com
+ RPXNOW.realm = "myrpxrealm";
+ RPXNOW.overlay = true;
+ RPXNOW.language_preference = 'en';
+ //]]>
+</script>
+
+</div>
+<div class='logincontainer' style='height: 200px; width: 300px; margin: 0pt auto; text-align: center; background-color: gray; padding: 50px;'>
+<h1>Welcome!</h1>
+<h1>
+<a class='rpxnow'>Please sign in to continue</a>
+</h1>
+</div>
+
+</body>
+</html>
View
112 rpx.js
@@ -0,0 +1,112 @@
+var sys = require('sys');
+// require.paths.unshift('../node-lib/restler/lib/');
+var restler = require( 'restler' );
+
+// Connect Middleware for integrating RPX Now into your application
+var RPX_HOST = 'http://rpxnow.com';
+var RPX_LOGIN_ROOT = "/api/v2/auth_info";
+var RPX_LOGIN_URL = "https://rpxnow.com/api/v2/auth_info";
+
+var options = {
+ callback_path : '/login_completed',
+ logout_path : '/logout',
+ host : 'localhost',
+ port : '80',
+ connect_session : 'connect.session',
+ name : 'default'
+};
+
+function isAuthenticated(req) {
+ return req && req.session && req.session.username;
+}
+
+function getCredentials(req,res,next) {
+ var token = req.body.token;
+ postWithCredentials( token, req, res, next );
+}
+
+function postWithCredentials( token, req, res, next ) {
+ var apiKey = options['apiKey'];
+ var toPost = { token : token, apiKey : apiKey, format : 'json', extended : true };
+ restler.post( RPX_LOGIN_URL, { data : toPost } ).
+ addListener( 'complete', function credentialize(incoming) { onCredentialsReceived( incoming, req, res, next ); } ).
+ addListener( 'error', onError );
+}
+
+function onError(response) {
+ sys.puts( "Something bad happened" );
+}
+
+function onCredentialsReceived(data, req, res, next) {
+ json = JSON.parse( data );
+ if( 'ok' == json.stat ) {
+ req.sessionStore.regenerate(req, function(err){
+ req.session.username = json.profile.displayName;
+ });
+ res.redirect( '/' );
+ }
+ else {
+ res.redirect( options.loginPage );
+ }
+}
+
+function initialize() {
+}
+
+function shouldFakeIt() {
+ return options.fakedAuthentication;
+}
+
+function fakeIt(req,res) {
+ req.sessionStore.regenerate(req, function(err){
+ req.session.username = ( req.body && req.body.fake_name ) ? req.body.fake_name : 'fakedUsername' + parseInt( Math.random() * 1000 );
+ res.redirect( '/' );
+ });
+}
+
+exports.config = function( key, value ) {
+ if( value ) {
+ options[key] = value;
+ }
+ return options[key];
+}
+
+exports.test_rpx = function( token, apiKey ) {
+ options['apiKey'] = apiKey;
+ post_with_credentials( token );
+}
+
+exports.handler = function(req,res,next) {
+ if( req.url == options.reentryPoint ) {
+ getCredentials(req,res,next);
+ }
+ else if( req.url == options.loginPage ) {
+ next();
+ }
+ else if( req.url == options.logoutPoint ) {
+ req.sessionStore.regenerate(req, function(err){
+ req.session.username = undefined;
+ res.redirect( options.loginPage );
+ });
+ }
+ else {
+ if( isAuthenticated(req) ) {
+ next();
+ }
+ else if( shouldFakeIt() ) {
+ fakeIt(req,res);
+ }
+ else {
+ ignore = options.ignorePaths;
+ for( x in ignore ) {
+ if( req.url.substr( 0, ignore[x].length ) == ignore[x] ) {
+ next();
+ }
+ }
+
+ // If we got here, then send to login page
+ res.redirect( options.loginPage );
+ }
+ }
+}
+
Please sign in to comment.
Something went wrong with that request. Please try again.