SHARE
TWEET

nodebb-plugin-openid-devel

klip Mar 13th, 2014 29 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function(module) {
  2.   "use strict";
  3.  
  4.   var user = module.parent.require('./user.js'),
  5.       db = module.parent.require('../src/database.js'),
  6.       meta = module.parent.require('./meta'),
  7.       passport = module.parent.require('passport'),
  8.       passportOpenID = require('passport-openid').Strategy,
  9.       fs = module.parent.require('fs'),
  10.       path = module.parent.require('path'),
  11.       winston = require('winston'),
  12.       http = module.parent.require('http');
  13.  
  14.   var constants = Object.freeze({
  15.     'name': 'OpenID'
  16.   });
  17.  
  18.   var OpenID = {};
  19.  
  20.   OpenID.getStrategy = function(strategies) {
  21.  
  22.       winston.info('[OpenID] getStrategy');
  23.       passport.use(new passportOpenID({
  24.         returnURL: module.parent.require('nconf').get('url') + '/auth/openid/callback',
  25.         realm: module.parent.require('nconf').get('url'),
  26.         profile: true
  27.       }, function(identifier, profile, done) {
  28.         process.nextTick(function () {
  29.           winston.info('[OpenID] auth ', identifier);
  30.           OpenID.login(identifier, profile, function(err, user) {
  31.             if (err) {
  32.                  return done(err);
  33.             }
  34.             done(null, user);
  35.           });
  36.         });
  37.       }));
  38.  
  39.       strategies.push({
  40.         name: 'openid',
  41.         url: '/auth/openid',
  42.         callbackURL: '/auth/openid/callback',
  43.         icon: 'check',
  44.         scope: ''
  45.       });
  46.       winston.info('[OpenID] strategy pushed');
  47.  
  48.     return strategies;
  49.   };
  50.  
  51.   OpenID.login = function(identifier, profile, callback) {
  52.     winston.info('[OpenID] login ', identifier);
  53.     OpenID.getUidByOpenID(identifier, function(err, uid) {
  54.       winston.info('[OpenID] getUidByOpenID from login');
  55.       winston.info(err);
  56.       if(err) {
  57.         return callback(err);
  58.       }
  59.  
  60.       if (uid !== null) {
  61.         // Existing User
  62.         callback(null, {
  63.           uid: uid
  64.         });
  65.       } else {
  66.         // New User
  67.         user.create({username: profile.username}, function(err, uid) {
  68.           winston.info('[OpenID] create user');
  69.           if (err !== null) {
  70.             return callback(err);
  71.           } else {
  72.             // Save information to the user
  73.             winston.info('[OpenID] save user');
  74.             user.setUserField(uid, 'openid', identifier);
  75.             db.setObjectField('openid:uid', identifier, uid);
  76.  
  77.             callback(null, {
  78.               uid: uid
  79.             });
  80.           }
  81.         });
  82.       }
  83.     });
  84.   }
  85.  
  86.   OpenID.getUidByOpenID = function(identifier, callback) {
  87.     winston.info('[OpenID] getUidByOpenID');
  88.     db.getObjectField('openid:uid', identifier, function(err, uid) {
  89.       if (err !== null) {
  90.         return callback(err);
  91.       }
  92.       callback(null, uid);
  93.     });
  94.   };
  95.  
  96.   OpenID.addOpenIDLoginRoute = function(custom_routes, callback) {
  97.     winston.info('[OpenID] adding form route: /auth/openid_form ');
  98.     fs.readFile(path.resolve(__dirname, './static/openid_form.tpl'), function (err, template) {
  99.       custom_routes.routes.push({
  100.         "route": '/auth/openid_form',
  101.         "method": "get",
  102.         "options": function(req, res, callback) {
  103.           callback({
  104.             req: req,
  105.             res: res,
  106.             route: '/auth/openid_form',
  107.             name: constants.name,
  108.             content: template.toString().replace('{csrf}', res.locals.csrf_token)
  109.           });
  110.         }
  111.       });
  112.  
  113.       callback(null, custom_routes);
  114.     });
  115.   };
  116.  
  117.   module.exports = OpenID;
  118. }(module));
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top