Permalink
Browse files

Merge pull request #91 from gillyb/master

Added a sample for people working with the expressjs framework
  • Loading branch information...
2 parents 5bb0082 + b46d9c2 commit 8ff5103b278a5718bd299c0f8eeb602cf9b9995f @havard committed Jan 17, 2013
Showing with 54 additions and 0 deletions.
  1. +43 −0 expressjs_sample/authentication_controller.js
  2. +11 −0 expressjs_sample/login.jade
@@ -0,0 +1,43 @@
+var openid = require('openid');
+
+var relyingParty = new openid.RelyingParty(
+ 'http://localhost:8888/login/verify', // Verification URL (yours)
+ null, // Realm (optional, specifies realm for OpenID authentication)
+ false, // Use stateless verification
+ false, // Strict mode
+ []); // List of extensions to enable and include
+
+app.get('/login', function(request, response) {
+ response.render('login');
+});
+
+app.get('/login/authenticate', function(request, response) {
+ var identifier = request.query.openid_identifier;
+
+ // Resolve identifier, associate, and build authentication URL
+ relyingParty.authenticate(identifier, false, function(error, authUrl) {
+ if (error) {
+ response.writeHead(200);
+ response.end('Authentication failed: ' + error.message);
+ }
+ else if (!authUrl) {
+ response.writeHead(200);
+ response.end('Authentication failed');
+ }
+ else {
+ response.writeHead(302, { Location: authUrl });
+ response.end();
+ }
+ });
+});
+
+app.get('/login/verify', function(request, response) {
+ // Verify identity assertion
+ // NOTE: Passing just the URL is also possible
+ relyingParty.verifyAssertion(request, function(error, result) {
+ response.writeHead(200);
+ response.end(!error && result.authenticated
+ ? 'Success :)' // TODO: redirect to something interesting!
+ : 'Failure :('); // TODO: show some error message!
+ });
+});
@@ -0,0 +1,11 @@
+doctype 5
+html
+ head
+ title OpenId Authentication with expressjs sample
+
+ body
+ div#main
+ h1 Log in using your OpenId account
+ form(method='get', action='/login/authenticate')
+ input(name='openid_identifier')
+ input(type='submit', value='Login')

0 comments on commit 8ff5103

Please sign in to comment.