This commit is contained in:
Julian Lam 2014-07-22 10:03:52 -04:00
parent f8306923de
commit b0b5160de4
2 changed files with 40 additions and 16 deletions

View File

@ -2,6 +2,7 @@
"use strict"; "use strict";
var User = module.parent.require('./user'), var User = module.parent.require('./user'),
Groups = module.parent.require('./groups'),
meta = module.parent.require('./meta'), meta = module.parent.require('./meta'),
db = module.parent.require('../src/database'), db = module.parent.require('../src/database'),
passport = module.parent.require('passport'), passport = module.parent.require('passport'),
@ -21,13 +22,15 @@
var OAuth = {}; var OAuth = {};
OAuth.init = function(app, middleware, controllers) { OAuth.init = function(app, middleware, controllers, callback) {
function render(req, res, next) { function render(req, res, next) {
res.render('admin/plugins/sso-oauth', {}); res.render('admin/plugins/sso-oauth', {});
} }
app.get('/admin/plugins/sso-oauth', middleware.admin.buildHeader, render); app.get('/admin/plugins/sso-oauth', middleware.admin.buildHeader, render);
app.get('/api/admin/plugins/sso-oauth', render); app.get('/api/admin/plugins/sso-oauth', render);
callback();
}; };
OAuth.getStrategy = function(strategies, callback) { OAuth.getStrategy = function(strategies, callback) {
@ -104,18 +107,22 @@
var profile = { provider: 'generic' }; var profile = { provider: 'generic' };
// Alter this section to include whatever data is necessary // Alter this section to include whatever data is necessary
// NodeBB requires the following: id, displayName, emails, e.g.: // NodeBB *requires* the following: id, displayName, emails.
// Everything else is optional.
profile.id = json.id; profile.id = json.id;
profile.displayName = json.name; profile.displayName = json.name;
profile.emails = [{ value: json.email }]; profile.emails = [{ value: json.email }];
// Do you want to automatically make somebody an admin? This block might help you do that...
// profile.isAdmin = json.isAdmin ? true : false;
// Find out what is available by uncommenting this line: // Find out what is available by uncommenting this line:
// console.log(json); // console.log(json);
// Delete or comment out the next TWO (2) lines when you are ready to proceed // Delete or comment out the next TWO (2) lines when you are ready to proceed
// console.log('===\nAt this point, you\'ll need to customise the above section to id, displayName, and emails into the "profile" object.\n==='); process.stdout.write('===\nAt this point, you\'ll need to customise the above section to id, displayName, and emails into the "profile" object.\n===');
// return done(new Error('Congrats! So far so good -- please see server log for details')); return done(new Error('Congrats! So far so good -- please see server log for details'));
done(null, profile); done(null, profile);
} catch(e) { } catch(e) {
@ -126,7 +133,12 @@
} }
passport.use('Generic OAuth', new passportOAuth(opts, function(token, secret, profile, done) { passport.use('Generic OAuth', new passportOAuth(opts, function(token, secret, profile, done) {
OAuth.login(profile.id, profile.displayName, profile.emails[0].value, function(err, user) { OAuth.login({
oAuthid: profile.id,
handle: profile.displayName,
email: profile.emails[0].value,
isAdmin: profile.isAdmin
}, function(err, user) {
if (err) { if (err) {
return done(err); return done(err);
} }
@ -150,8 +162,8 @@
}); });
}; };
OAuth.login = function(oAuthid, handle, email, callback) { OAuth.login = function(payload, callback) {
OAuth.getUidByOAuthid(oAuthid, function(err, uid) { OAuth.getUidByOAuthid(payload.oAuthid, function(err, uid) {
if(err) { if(err) {
return callback(err); return callback(err);
} }
@ -165,20 +177,32 @@
// New User // New User
var success = function(uid) { var success = function(uid) {
// Save provider-specific information to the user // Save provider-specific information to the user
User.setUserField(uid, 'oAuthid', oAuthid); User.setUserField(uid, 'oAuthid', payload.oAuthid);
db.setObjectField('oAuthid:uid', oAuthid, uid); db.setObjectField('oAuthid:uid', payload.oAuthid, uid);
callback(null, {
uid: uid if (payload.isAdmin) {
}); Groups.join('administrators', uid, function(err) {
callback(null, {
uid: uid
});
});
} else {
callback(null, {
uid: uid
});
}
}; };
User.getUidByEmail(email, function(err, uid) { User.getUidByEmail(payload.email, function(err, uid) {
if(err) { if(err) {
return callback(err); return callback(err);
} }
if (!uid) { if (!uid) {
User.create({username: handle, email: email}, function(err, uid) { User.create({
username: payload.handle,
email: payload.email
}, function(err, uid) {
if(err) { if(err) {
return callback(err); return callback(err);
} }

View File

@ -5,10 +5,10 @@
"url": "https://github.com/julianlam/nodebb-plugin-sso-oauth", "url": "https://github.com/julianlam/nodebb-plugin-sso-oauth",
"library": "./library.js", "library": "./library.js",
"hooks": [ "hooks": [
{ "hook": "action:app.load", "method": "init" }, { "hook": "filter:app.load", "method": "init" },
{ "hook": "filter:auth.init", "method": "getStrategy" }, { "hook": "filter:auth.init", "method": "getStrategy" },
{ "hook": "filter:admin.header.build", "method": "addMenuItem" } { "hook": "filter:admin.header.build", "method": "addMenuItem" }
], ],
"templates": "./templates", "templates": "./templates",
"minver": "0.4.0" "minver": "0.5.0"
} }