From 50516a613b8ada443a0339ba622e6af3bb4b7a76 Mon Sep 17 00:00:00 2001 From: Alberto Asuero Date: Tue, 23 Sep 2014 11:35:29 +0200 Subject: [PATCH] Doc and icons --- README.md | 23 ++++- library.js | 149 +++++++++++++++------------ templates/admin/plugins/sso-saml.tpl | 6 +- 3 files changed, 107 insertions(+), 71 deletions(-) diff --git a/README.md b/README.md index 80df0b0..2aae546 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,24 @@ -# NodeBB Twitter SSO +# NodeBB SAML SSO -NodeBB Plugin that allows users to login/register via their Twitter account. +NodeBB Plugin that allows users to login/register via SAML IDP ## Installation - npm install nodebb-plugin-sso-twitter + 1) npm install nodebb-plugin-sso-saml + + 2) Activate plugin at nodebb admin panel + + 3) Once you activated it you can configure all the params at SAML section. + + - IdP entry point: it's the saml IdP entry point. E.g https:///simplesaml/saml2/idp/SSOService.php. + - Callback path: path to callback. Eg: /auth/saml/callback. + - Issuer: issuer string to supply IdP. Eg: 'nodebb-saml' + - Metadata url: url where metadata will be served at. Optional. + - Server CRT file: Server crt path. Mandatory if used metadata url + +## + If you need more info or if you need some help, please report an issue at https://github.com/GeographicaGS/nodebb-plugin-sso-saml/issues + + +## + This plugin has been built on top of passport.saml, for more info visit https://github.com/bergie/passport-saml diff --git a/library.js b/library.js index 2983b50..d334d46 100644 --- a/library.js +++ b/library.js @@ -15,44 +15,47 @@ 'name': "SAML", 'admin': { 'route': '/plugins/sso-saml', - 'icon': 'fa-twitter-square' + 'icon': 'fa-university' } }); var SAML = {}; + var samlObj; - if (!meta.config['sso:saml:idpentrypoint'] || meta.config['sso:saml:callbackpath']) { - var err = new Error('Missing config variables'); - throw err; - } + if (meta.config['sso:saml:idpentrypoint'] && meta.config['sso:saml:callbackpath']&& meta.config["sso:saml:metadata"] && meta.config["sso:saml:issuer"]) { + + samlObj = new passportSAML({ + path: meta.config['sso:saml:callbackpath'], + entryPoint: meta.config['sso:saml:idpentrypoint'], + issuer: 'passport-saml', + callbackUrl: nconf.get('url') + meta.config['sso:saml:callbackpath'] + }, + function(profile, done) { + + var user = { + nameID: profile.nameID, + nameIDFormat: profile.nameIDFormat, + sn: profile.sn, + cn: profile.cn, + mail: profile.mail, + eduPersonAffiliation: profile.eduPersonAffiliation, + email: profile.email, + username: profile.displayName + }; - - var samlObj = new passportSAML({ - path: meta.config['sso:saml:callbackpath'], - entryPoint: meta.config['sso:saml:idpentrypoint'], - issuer: 'passport-saml', - callbackUrl: nconf.get('url') + ':' + nconf.get('port') + meta.config['sso:saml:callbackpath'] - }, - function(profile, done) { - - var user = { - nameID: profile.nameID, - nameIDFormat: profile.nameIDFormat, - sn: profile.sn, - cn: profile.cn, - mail: profile.mail, - eduPersonAffiliation: profile.eduPersonAffiliation, - email: profile.email - }; - - SAML.login(user.nameID,user.nameID,function(err, user) { - if (err) { - return done(err); - } - done(null, user); - }); - } - ); + SAML.login(user.nameID,user.username,function(err, user) { + if (err) { + return done(err); + } + done(null, user); + }); + } + ); + } + else{ + console.log("No config info") + console.log(meta.config); + } SAML.init = function(app, middleware, controllers, callback) { @@ -63,36 +66,49 @@ app.get('/admin/plugins/sso-saml', middleware.admin.buildHeader, render); app.get('/api/admin/plugins/sso-saml', render); - app.get(meta.config["sso:saml:metadata"], function(req, res) { - var cert = fs.readFileSync('/Users/alasarr/dev/nodebb/node_modules/nodebb-plugin-sso-saml/server.crt', 'utf-8'); - res.header("Content-Type", "application/xml"); - res.send(samlObj.generateServiceProviderMetadata(cert)) - - }); + if (samlObj){ - app.post(meta.config['sso:saml:callbackpath'], - passport.authenticate('saml', { successRedirect: '/',failureRedirect: '/', failureFlash: true }) - ); + if (meta.config["sso:saml:metadata"]) { + app.get(meta.config["sso:saml:metadata"], function(req, res) { + if (meta.config["sso:saml:servercrt"]){ + var cert = fs.readFileSync(meta.config["sso:saml:servercrt"], 'utf-8'); + res.header("Content-Type", "application/xml"); + res.send(samlObj.generateServiceProviderMetadata(cert)) + } + else{ + res.send("No servercrt specified. Please enter it at nodebb admin panel."); + } + }); + } + + + app.post(meta.config['sso:saml:callbackpath'], + passport.authenticate('saml', { successRedirect: '/',failureRedirect: '/', failureFlash: true }) + ); + } callback(); }; SAML.getStrategy = function(strategies, callback) { - - passport.use(samlObj); - strategies.push({ - name: 'saml', - url: '/auth/saml', - callbackURL: meta.config['sso:saml:callbackpath'], - icon: constants.admin.icon, - scope: '' - }); + if (samlObj){ + + passport.use(samlObj); + + strategies.push({ + name: 'saml', + url: '/auth/saml', + callbackURL: meta.config['sso:saml:callbackpath'], + icon: constants.admin.icon, + scope: '' + }); + } callback(null, strategies); }; - SAML.login = function(samlid,email, callback) { + SAML.login = function(samlid,username, callback) { SAML.getUidBySAMLId(samlid, function(err, uid) { if(err) { @@ -102,11 +118,12 @@ if (uid !== null) { // Existing User callback(null, { - uid: uid + uid: uid }); - } else { + } + else { // New User - user.create({username: email}, function(err, uid) { + user.create({username: username}, function(err, uid) { if(err) { return callback(err); } @@ -142,18 +159,18 @@ }; SAML.deleteUserData = function(uid, callback) { - // async.waterfall([ - // async.apply(user.getUserField, uid, 'samlid'), - // function(oAuthIdToDelete, next) { - // db.deleteObjectField('twid:uid', oAuthIdToDelete, next); - // } - // ], function(err) { - // if (err) { - // winston.error('[sso-twitter] Could not remove OAuthId data for uid ' + uid + '. Error: ' + err); - // return callback(err); - // } - // callback(null, uid); - // }); + async.waterfall([ + async.apply(user.getUserField, uid, 'samlid'), + function(idToDelete, next) { + db.deleteObjectField('samlid:uid', idToDelete, next); + } + ], function(err) { + if (err) { + winston.error('[sso-saml] Could not remove user data for uid ' + uid + '. Error: ' + err); + return callback(err); + } + callback(null, uid); + }); }; module.exports = SAML; diff --git a/templates/admin/plugins/sso-saml.tpl b/templates/admin/plugins/sso-saml.tpl index 41b58e2..cb679f9 100644 --- a/templates/admin/plugins/sso-saml.tpl +++ b/templates/admin/plugins/sso-saml.tpl @@ -1,4 +1,4 @@ -

Simple samp Authentication

+

Simple samp Authentication


@@ -10,7 +10,9 @@


- +
+ +