Skip to content
Browse files

adding a login sample file for the expressjs framework

  • Loading branch information...
1 parent 5bb0082 commit b46d9c2acbfa833c1a10fc85824268d210b2de3d @gillyb gillyb committed
Showing with 54 additions and 0 deletions.
  1. +43 −0 expressjs_sample/authentication_controller.js
  2. +11 −0 expressjs_sample/login.jade
View
43 expressjs_sample/authentication_controller.js
@@ -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!
+ });
+});
View
11 expressjs_sample/login.jade
@@ -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 b46d9c2

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