Permalink
Browse files

Added example login

  • Loading branch information...
1 parent ae31801 commit 1e0cf5ddba6694633cfb784bc7c0ba968cd5b2ff @xrd committed Jan 5, 2011
View
@@ -16,39 +16,50 @@ http://github.com/extjs/Connect
== Usage:
- // Setup whatever require paths you need. You'll need to at least make restler accessible to connect-rpx
- // require.paths.unshift('../node-lib/connect-rpx'); // This might work!
- // require.paths.unshift('../node-lib/restler/lib'); // And, something like this!
var Connect = require('connect');
- var MemoryStore = require('connect/lib/connect/middleware/session/memory').MemoryStore;
- var RPX = require( 'rpx' );
-
- // Setup RPX
- //
+ var MemoryStore = require('connect/middleware/session/memory');
+ var RPX = require( 'connect-rpx' );
+
+ // Setup RPX
// Your API key, look in the settings on rpxnow.com
- RPX.config( 'apiKey', 'abcefgehijk' );
- // (Specify root paths to ignore, paths not protected by RPX auth, or paths you need to serve for your login page, like CSS)
- RPX.config( 'ignorePaths', [ '/stylesheets', '/images', '/javascript', '/css' ] );
- // This is where RPX will redirect you to once authentication has occurred
+ RPX.config( 'apiKey', 'asdasdadadadadasdasdasd' );
+ RPX.config( 'ignorePaths', [ '/stylesheets', '/images', '/javascript', '/css', "/login" ] );
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.
+ RPX.config( 'loginPage', '/login/index.html' );
+ RPX.config( 'onSuccessfulLogin', handleLogin );
+
+ // Or, just load from JSON
+ RPX.load
+
+ function redirect(res,location) {
+ res.writeHead( 302, {
+ 'Location': location
+ });
+ res.end();
+ }
+
+ function handleLogin( json, req, res, next ) {
+ req.sessionStore.regenerate(req, function(err){
+ req.session.profile = json.profile;
+ req.session.username = json.profile.displayName;
+ // next();
+ });
+ redirect( res, '/' );
+ }
+
+ // 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.
- );
+ Connect.logger(),
+ Connect.bodyDecoder(),
+ Connect.cookieDecoder(),
+ Connect.session({ store: new MemoryStore({ reapInterval: minute, maxAge: minute * 5 }) }),
+ RPX.handler(),
+ Connect.staticProvider( root ) // this is not strictly required, );
+
+ Server.listen(3030);
* Then, run your app, and it should redirect to you the loginPath specified in the configuration.
View
@@ -0,0 +1,49 @@
+var Connect = require('connect');
+var MemoryStore = require('connect/middleware/session/memory');
+var RPX = require( '../../connect-rpx/lib/connect-rpx');
+// var RPX = require( 'connect-rpx' );
+
+// Setup RPX
+//
+// Your API key, look in the settings on rpxnow.com
+RPX.config( 'apiKey', 'asdasdadadadadadasdasdasd' );
+RPX.config( 'ignorePaths', [ '/stylesheets', '/images', '/javascript', '/css', "/login" ] );
+RPX.config( 'reentryPoint', '/rpx_login' );
+RPX.config( 'logoutPoint', '/logout' );
+RPX.config( 'loginPage', '/login/index.html' );
+RPX.config( 'onSuccessfulLogin', handleLogin );
+
+// Or, just load from config file. This will override existing settings, and could be used to
+// keep settings out of version control if you want to do that for things like the apiKey.
+RPX.loadConfig( "./config.json" );
+
+function redirect(res,location) {
+ res.writeHead( 302, {
+ 'Location': location
+ });
+ res.end();
+}
+
+function handleLogin( json, req, res, next ) {
+ req.sessionStore.regenerate(req, function(err){
+ req.session.profile = json.profile;
+ req.session.username = json.profile.displayName;
+ // next();
+ });
+ redirect( res, '/' );
+}
+
+// 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.cookieDecoder(),
+ Connect.session({ store: new MemoryStore({ reapInterval: minute, maxAge: minute * 5 }) }),
+ RPX.handler(),
+ Connect.staticProvider( root ) // this is not strictly required,
+);
+
+Server.listen(4040);
+
View
@@ -0,0 +1,6 @@
+{ 'apiKey' : 'asdasdadadadadadasdasdasd',
+ 'ignorePaths' : [ '/stylesheets', '/images', '/javascript', '/css', "/login" ],
+ 'reentryPoint' : '/rpx_login',
+ 'logoutPoint' : '/logout',
+ 'loginPage' : '/login/index.html',
+ 'onSuccessfulLogin' : 'handleLogin' }
View
@@ -0,0 +1,6 @@
+{ "apiKey" : "asdasdadadadadadasdasdasd",
+ "ignorePaths" : [ "/stylesheets", "/images", "/javascript", "/css", "/login" ],
+ "reentryPoint" : "/rpx_login",
+ "logoutPoint" : "/logout",
+ "loginPage" : "/login/index.html",
+ "onSuccessfulLogin" : "handleLogin" }
@@ -0,0 +1,7 @@
+<html>
+<body>
+Logged in!
+<br/>
+<a href="/logout">Logout here</a>
+</body>
+</html>
@@ -0,0 +1,5 @@
+<html>
+<body>
+Logged in!
+</body>
+</html>
@@ -0,0 +1,22 @@
+<html>
+<body>
+ <a class='rpxnow'>
+ <h2>Click here to login</h2>
+ </a>
+
+ <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[
+ RPXNOW.token_url = "http://localhost:3030" + base;
+ // Change this.
+ RPXNOW.realm = "myRpxRealm";
+ RPXNOW.overlay = true;
+ RPXNOW.language_preference = 'en';
+ //]]>
+ </script>
+ </div>
+
+</body>
+</html>
@@ -0,0 +1,21 @@
+<html>
+<body>
+ <a class='rpxnow'>
+ <h2>Click here to login</h2>
+ </a>
+
+ <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[
+ RPXNOW.token_url = "http://localhost:3030" + base;
+ RPXNOW.realm = "outreachdigital";
+ RPXNOW.overlay = true;
+ RPXNOW.language_preference = 'en';
+ //]]>
+ </script>
+ </div>
+
+</body>
+</html>
View
@@ -1,4 +1,5 @@
var sys = require('sys');
+var fs = require('fs');
var http = require('http');
var qs = require('querystring');
@@ -9,13 +10,13 @@ var RPX_LOGIN_URL = "https://rpxnow.com/api/v2/auth_info";
var options = {
callback_path : '/login_completed',
- logout_path : '/logout',
+ logoutPoint : '/logout',
host : 'localhost',
port : '80',
connect_session : 'connect.session',
name : 'default',
onSuccesfulLogin : function( json, req, res, next ) {
- sys.puts( "In default login" );
+ // sys.puts( "In default login" );
req.sessionStore.regenerate(req, function(err){
req.session.username = json.profile.displayName;
});
@@ -25,13 +26,19 @@ var options = {
};
function redirect(res,location) {
- res.writeHead( 302, {
- 'Location': location
- });
- res.end();
+ if( res ) {
+ res.writeHead( 302, {
+ 'Location': location
+ });
+ res.end();
+ }
+ else {
+ // sys.puts( "Redirecting to: " + location );
+ }
}
function isAuthenticated(req) {
+ // sys.puts( "Checking auth: " + sys.inspect( req.session ) );
return req && req.session && req.session.username;
}
@@ -40,27 +47,26 @@ function getCredentials(req,res,next) {
postWithCredentials( token, req, res, next );
}
-var rpxResponseBody = '';
function postWithCredentials( token, req, res, next ) {
var apiKey = options['apiKey'];
var toPost = qs.stringify( { token : token, apiKey : apiKey } );
var toPostHeader = { 'Host' : RPX_HOST,
'Content-Type' : 'application/x-www-form-urlencoded',
'Content-Length' : toPost.length };
+ var rpxResponseBody = '';
var postRequest = http.createClient( 443, RPX_HOST, true ).request( 'POST', RPX_LOGIN_ROOT, toPostHeader );
postRequest.write( toPost, 'utf8' );
postRequest.on( 'response', function(rpxResponse) {
- rpxResponse.on( 'data', chunkRpxResponse );
- rpxResponse.on( 'end', function() { onCredentialsReceived( rpxResponseBody, req, res, next ) } );
- rpxResponse.on( 'error', onError );
+ rpxResponse.on( 'data', function( data ) {
+ // sys.puts( "Chunk: " + data );
+ rpxResponseBody += data;
+ } );
+ rpxResponse.on( 'end', function() { onCredentialsReceived( rpxResponseBody, req, res, next ) } );
+ rpxResponse.on( 'error', onError );
});
postRequest.end();
}
-function chunkRpxResponse( chunk ) {
- rpxResponseBody += chunk;
-}
-
function onError(response) {
sys.puts( "Something bad happened" );
}
@@ -99,29 +105,52 @@ function fakeIt(req,res,next) {
exports.config = function( key, value ) {
if( value ) {
- // sys.puts( "Setting: " + key );
+ // sys.puts( "Setting: " + key + " to " + value );
options[key] = value;
}
return options[key];
}
-exports.test_rpx = function( token, apiKey ) {
+exports.testRpx = function( token, apiKey ) {
options['apiKey'] = apiKey;
- post_with_credentials( token );
+ postWithCredentials( token );
}
+exports.loadConfig = function( filename ) {
+ // Do something to load the settings
+ fs.readFile( filename, function (err, data) {
+ if (err) throw err;
+ // convert the data to JSON
+ var obj;
+ // sys.puts( "Data: " + data );
+ try {
+ obj = JSON.parse( data );
+ }
+ catch( e ) {
+ sys.puts( "Error in parsing settings file: " + sys.inspect( e ) );
+ }
+ for( x in obj ) {
+ if( 'onSuccessfulLogin' == x ) {
+ throw "onSuccessfulLogin needs to be a function, cannot be passed inside configuration file";
+ }
+ exports.config( x, obj[x] );
+ }
+ });
+};
+
exports.handler = function() {
return function(req,res,next) {
- sys.puts( "Inside RPX" );
+ // sys.puts( "Inside RPX: " + req.url );
if( req.url == options.reentryPoint ) {
getCredentials(req,res,next);
}
else if( req.url == options.loginPage ) {
next();
}
else if( req.url == options.logoutPoint ) {
+ // sys.puts( "Inside logout" );
req.sessionStore.regenerate(req, function(err){
- req.session.username = undefined;
+ req.session.username = null;
redirect( res, options.loginPage );
});
}
View
@@ -1,30 +0,0 @@
-<!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
@@ -1,4 +1,4 @@
{ "name" : "connect-rpx",
- "version" : "0.0.5",
+ "version" : "0.0.7",
"author" : "Chris Dawson <xrdawson@gmail.com>",
"main" : "./lib/connect-rpx" }

0 comments on commit 1e0cf5d

Please sign in to comment.