fix: linted library.js
This commit is contained in:
parent
8c57b5c105
commit
9f6ab199de
157
library.js
157
library.js
@ -1,6 +1,6 @@
|
|||||||
(function(module) {
|
'use strict';
|
||||||
"use strict";
|
|
||||||
|
|
||||||
|
(function (module) {
|
||||||
/*
|
/*
|
||||||
Welcome to the SSO OAuth plugin! If you're inspecting this code, you're probably looking to
|
Welcome to the SSO OAuth plugin! If you're inspecting this code, you're probably looking to
|
||||||
hook up NodeBB with your existing OAuth endpoint.
|
hook up NodeBB with your existing OAuth endpoint.
|
||||||
@ -16,18 +16,17 @@
|
|||||||
Step 4: If all goes well, you'll be able to login/register via your OAuth endpoint credentials.
|
Step 4: If all goes well, you'll be able to login/register via your OAuth endpoint credentials.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var User = module.parent.require('./user'),
|
const User = require.main.require('./src/user');
|
||||||
Groups = module.parent.require('./groups'),
|
const Groups = require.main.require('./src/groups');
|
||||||
meta = module.parent.require('./meta'),
|
const db = require.main.require('./src/database');
|
||||||
db = module.parent.require('../src/database'),
|
const authenticationController = require.main.require('./src/controllers/authentication');
|
||||||
passport = module.parent.require('passport'),
|
|
||||||
fs = module.parent.require('fs'),
|
const async = require('async');
|
||||||
path = module.parent.require('path'),
|
|
||||||
nconf = module.parent.require('nconf'),
|
const passport = module.parent.require('passport');
|
||||||
winston = module.parent.require('winston'),
|
const nconf = module.parent.require('nconf');
|
||||||
async = module.parent.require('async');
|
const winston = module.parent.require('winston');
|
||||||
|
|
||||||
var authenticationController = module.parent.require('./controllers/authentication');
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* REMEMBER
|
* REMEMBER
|
||||||
@ -49,25 +48,30 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
var constants = Object.freeze({
|
var constants = Object.freeze({
|
||||||
type: '', // Either 'oauth' or 'oauth2'
|
type: '', // Either 'oauth' or 'oauth2'
|
||||||
name: '', // Something unique to your OAuth provider in lowercase, like "github", or "nodebb"
|
name: '', // Something unique to your OAuth provider in lowercase, like "github", or "nodebb"
|
||||||
oauth: {
|
oauth: {
|
||||||
requestTokenURL: '',
|
requestTokenURL: '',
|
||||||
accessTokenURL: '',
|
accessTokenURL: '',
|
||||||
userAuthorizationURL: '',
|
userAuthorizationURL: '',
|
||||||
consumerKey: nconf.get('oauth:key'), // don't change this line
|
consumerKey: nconf.get('oauth:key'), // don't change this line
|
||||||
consumerSecret: nconf.get('oauth:secret'), // don't change this line
|
consumerSecret: nconf.get('oauth:secret'), // don't change this line
|
||||||
},
|
},
|
||||||
oauth2: {
|
oauth2: {
|
||||||
authorizationURL: '',
|
authorizationURL: '',
|
||||||
tokenURL: '',
|
tokenURL: '',
|
||||||
clientID: nconf.get('oauth:id'), // don't change this line
|
clientID: nconf.get('oauth:id'), // don't change this line
|
||||||
clientSecret: nconf.get('oauth:secret'), // don't change this line
|
clientSecret: nconf.get('oauth:secret'), // don't change this line
|
||||||
},
|
},
|
||||||
userRoute: '' // This is the address to your app's "user profile" API endpoint (expects JSON)
|
userRoute: '', // This is the address to your app's "user profile" API endpoint (expects JSON)
|
||||||
}),
|
});
|
||||||
configOk = false,
|
|
||||||
OAuth = {}, passportOAuth, opts;
|
|
||||||
|
var configOk = false;
|
||||||
|
|
||||||
|
|
||||||
|
var OAuth = {}; var passportOAuth; var
|
||||||
|
opts;
|
||||||
|
|
||||||
if (!constants.name) {
|
if (!constants.name) {
|
||||||
winston.error('[sso-oauth] Please specify a name for your OAuth provider (library.js:32)');
|
winston.error('[sso-oauth] Please specify a name for your OAuth provider (library.js:32)');
|
||||||
@ -79,7 +83,7 @@
|
|||||||
configOk = true;
|
configOk = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
OAuth.getStrategy = function(strategies, callback) {
|
OAuth.getStrategy = function (strategies, callback) {
|
||||||
if (configOk) {
|
if (configOk) {
|
||||||
passportOAuth = require('passport-oauth')[constants.type === 'oauth' ? 'OAuthStrategy' : 'OAuth2Strategy'];
|
passportOAuth = require('passport-oauth')[constants.type === 'oauth' ? 'OAuthStrategy' : 'OAuth2Strategy'];
|
||||||
|
|
||||||
@ -88,19 +92,21 @@
|
|||||||
opts = constants.oauth;
|
opts = constants.oauth;
|
||||||
opts.callbackURL = nconf.get('url') + '/auth/' + constants.name + '/callback';
|
opts.callbackURL = nconf.get('url') + '/auth/' + constants.name + '/callback';
|
||||||
|
|
||||||
passportOAuth.Strategy.prototype.userProfile = function(token, secret, params, done) {
|
passportOAuth.Strategy.prototype.userProfile = function (token, secret, params, done) {
|
||||||
this._oauth.get(constants.userRoute, token, secret, function(err, body, res) {
|
this._oauth.get(constants.userRoute, token, secret, function (err, body/* , res */) {
|
||||||
if (err) { return done(new InternalOAuthError('failed to fetch user profile', err)); }
|
if (err) {
|
||||||
|
return done(err);
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var json = JSON.parse(body);
|
var json = JSON.parse(body);
|
||||||
OAuth.parseUserReturn(json, function(err, profile) {
|
OAuth.parseUserReturn(json, function (err, profile) {
|
||||||
if (err) return done(err);
|
if (err) return done(err);
|
||||||
profile.provider = constants.name;
|
profile.provider = constants.name;
|
||||||
|
|
||||||
done(null, profile);
|
done(null, profile);
|
||||||
});
|
});
|
||||||
} catch(e) {
|
} catch (e) {
|
||||||
done(e);
|
done(e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -110,19 +116,21 @@
|
|||||||
opts = constants.oauth2;
|
opts = constants.oauth2;
|
||||||
opts.callbackURL = nconf.get('url') + '/auth/' + constants.name + '/callback';
|
opts.callbackURL = nconf.get('url') + '/auth/' + constants.name + '/callback';
|
||||||
|
|
||||||
passportOAuth.Strategy.prototype.userProfile = function(accessToken, done) {
|
passportOAuth.Strategy.prototype.userProfile = function (accessToken, done) {
|
||||||
this._oauth2.get(constants.userRoute, accessToken, function(err, body, res) {
|
this._oauth2.get(constants.userRoute, accessToken, function (err, body/* , res */) {
|
||||||
if (err) { return done(new InternalOAuthError('failed to fetch user profile', err)); }
|
if (err) {
|
||||||
|
return done(err);
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var json = JSON.parse(body);
|
var json = JSON.parse(body);
|
||||||
OAuth.parseUserReturn(json, function(err, profile) {
|
OAuth.parseUserReturn(json, function (err, profile) {
|
||||||
if (err) return done(err);
|
if (err) return done(err);
|
||||||
profile.provider = constants.name;
|
profile.provider = constants.name;
|
||||||
|
|
||||||
done(null, profile);
|
done(null, profile);
|
||||||
});
|
});
|
||||||
} catch(e) {
|
} catch (e) {
|
||||||
done(e);
|
done(e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -131,13 +139,13 @@
|
|||||||
|
|
||||||
opts.passReqToCallback = true;
|
opts.passReqToCallback = true;
|
||||||
|
|
||||||
passport.use(constants.name, new passportOAuth(opts, function(req, token, secret, profile, done) {
|
passport.use(constants.name, new passportOAuth(opts, function (req, token, secret, profile, done) {
|
||||||
OAuth.login({
|
OAuth.login({
|
||||||
oAuthid: profile.id,
|
oAuthid: profile.id,
|
||||||
handle: profile.displayName,
|
handle: profile.displayName,
|
||||||
email: profile.emails[0].value,
|
email: profile.emails[0].value,
|
||||||
isAdmin: profile.isAdmin
|
isAdmin: profile.isAdmin,
|
||||||
}, function(err, user) {
|
}, function (err, user) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return done(err);
|
return done(err);
|
||||||
}
|
}
|
||||||
@ -152,7 +160,7 @@
|
|||||||
url: '/auth/' + constants.name,
|
url: '/auth/' + constants.name,
|
||||||
callbackURL: '/auth/' + constants.name + '/callback',
|
callbackURL: '/auth/' + constants.name + '/callback',
|
||||||
icon: 'fa-check-square',
|
icon: 'fa-check-square',
|
||||||
scope: (constants.scope || '').split(',')
|
scope: (constants.scope || '').split(','),
|
||||||
});
|
});
|
||||||
|
|
||||||
callback(null, strategies);
|
callback(null, strategies);
|
||||||
@ -161,7 +169,7 @@
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
OAuth.parseUserReturn = function(data, callback) {
|
OAuth.parseUserReturn = function (data, callback) {
|
||||||
// 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.
|
// NodeBB *requires* the following: id, displayName, emails.
|
||||||
// Everything else is optional.
|
// Everything else is optional.
|
||||||
@ -181,51 +189,52 @@
|
|||||||
process.stdout.write('===\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 callback(new Error('Congrats! So far so good -- please see server log for details'));
|
return callback(new Error('Congrats! So far so good -- please see server log for details'));
|
||||||
|
|
||||||
|
// eslint-disable-next-line
|
||||||
callback(null, profile);
|
callback(null, profile);
|
||||||
}
|
};
|
||||||
|
|
||||||
OAuth.login = function(payload, callback) {
|
OAuth.login = function (payload, callback) {
|
||||||
OAuth.getUidByOAuthid(payload.oAuthid, function(err, uid) {
|
OAuth.getUidByOAuthid(payload.oAuthid, function (err, uid) {
|
||||||
if(err) {
|
if (err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (uid !== null) {
|
if (uid !== null) {
|
||||||
// Existing User
|
// Existing User
|
||||||
callback(null, {
|
callback(null, {
|
||||||
uid: uid
|
uid: uid,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// 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, constants.name + 'Id', payload.oAuthid);
|
User.setUserField(uid, constants.name + 'Id', payload.oAuthid);
|
||||||
db.setObjectField(constants.name + 'Id:uid', payload.oAuthid, uid);
|
db.setObjectField(constants.name + 'Id:uid', payload.oAuthid, uid);
|
||||||
|
|
||||||
if (payload.isAdmin) {
|
if (payload.isAdmin) {
|
||||||
Groups.join('administrators', uid, function(err) {
|
Groups.join('administrators', uid, function (err) {
|
||||||
callback(null, {
|
callback(err, {
|
||||||
uid: uid
|
uid: uid,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
callback(null, {
|
callback(null, {
|
||||||
uid: uid
|
uid: uid,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
User.getUidByEmail(payload.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({
|
User.create({
|
||||||
username: payload.handle,
|
username: payload.handle,
|
||||||
email: payload.email
|
email: payload.email,
|
||||||
}, function(err, uid) {
|
}, function (err, uid) {
|
||||||
if(err) {
|
if (err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -239,8 +248,8 @@
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
OAuth.getUidByOAuthid = function(oAuthid, callback) {
|
OAuth.getUidByOAuthid = function (oAuthid, callback) {
|
||||||
db.getObjectField(constants.name + 'Id:uid', oAuthid, function(err, uid) {
|
db.getObjectField(constants.name + 'Id:uid', oAuthid, function (err, uid) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
@ -248,13 +257,13 @@
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
OAuth.deleteUserData = function(data, callback) {
|
OAuth.deleteUserData = function (data, callback) {
|
||||||
async.waterfall([
|
async.waterfall([
|
||||||
async.apply(User.getUserField, data.uid, constants.name + 'Id'),
|
async.apply(User.getUserField, data.uid, constants.name + 'Id'),
|
||||||
function(oAuthIdToDelete, next) {
|
function (oAuthIdToDelete, next) {
|
||||||
db.deleteObjectField(constants.name + 'Id:uid', oAuthIdToDelete, next);
|
db.deleteObjectField(constants.name + 'Id:uid', oAuthIdToDelete, next);
|
||||||
}
|
},
|
||||||
], function(err) {
|
], function (err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
winston.error('[sso-oauth] Could not remove OAuthId data for uid ' + data.uid + '. Error: ' + err);
|
winston.error('[sso-oauth] Could not remove OAuthId data for uid ' + data.uid + '. Error: ' + err);
|
||||||
return callback(err);
|
return callback(err);
|
||||||
@ -264,11 +273,11 @@
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
// If this filter is not there, the deleteUserData function will fail when getting the oauthId for deletion.
|
// If this filter is not there, the deleteUserData function will fail when getting the oauthId for deletion.
|
||||||
OAuth.whitelistFields = function(params, callback) {
|
OAuth.whitelistFields = function (params, callback) {
|
||||||
params.whitelist.push(constants.name + 'Id');
|
params.whitelist.push(constants.name + 'Id');
|
||||||
callback(null, params);
|
callback(null, params);
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = OAuth;
|
module.exports = OAuth;
|
||||||
}(module));
|
}(module));
|
||||||
|
@ -28,6 +28,7 @@
|
|||||||
"readme": "",
|
"readme": "",
|
||||||
"readmeFilename": "README.md",
|
"readmeFilename": "README.md",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"async": "^2.6.1",
|
||||||
"passport-oauth": "~1.0.0"
|
"passport-oauth": "~1.0.0"
|
||||||
},
|
},
|
||||||
"nbbpm": {
|
"nbbpm": {
|
||||||
|
@ -315,6 +315,13 @@ astral-regex@^1.0.0:
|
|||||||
resolved "https://registry.yarnpkg.com/astral-regex/-/astral-regex-1.0.0.tgz#6c8c3fb827dd43ee3918f27b82782ab7658a6fd9"
|
resolved "https://registry.yarnpkg.com/astral-regex/-/astral-regex-1.0.0.tgz#6c8c3fb827dd43ee3918f27b82782ab7658a6fd9"
|
||||||
integrity sha512-+Ryf6g3BKoRc7jfp7ad8tM4TtMiaWvbF/1/sQcZPkkS7ag3D5nMBCe2UfOTONtAkaG0tO0ij3C5Lwmf1EiyjHg==
|
integrity sha512-+Ryf6g3BKoRc7jfp7ad8tM4TtMiaWvbF/1/sQcZPkkS7ag3D5nMBCe2UfOTONtAkaG0tO0ij3C5Lwmf1EiyjHg==
|
||||||
|
|
||||||
|
async@^2.6.1:
|
||||||
|
version "2.6.1"
|
||||||
|
resolved "https://registry.yarnpkg.com/async/-/async-2.6.1.tgz#b245a23ca71930044ec53fa46aa00a3e87c6a610"
|
||||||
|
integrity sha512-fNEiL2+AZt6AlAw/29Cr0UDe4sRAHCpEHh54WMz+Bb7QfNcFw4h3loofyJpLeQs4Yx7yuqu/2dLgM5hKOs6HlQ==
|
||||||
|
dependencies:
|
||||||
|
lodash "^4.17.10"
|
||||||
|
|
||||||
atob@^2.1.1:
|
atob@^2.1.1:
|
||||||
version "2.1.2"
|
version "2.1.2"
|
||||||
resolved "https://registry.yarnpkg.com/atob/-/atob-2.1.2.tgz#6d9517eb9e030d2436666651e86bd9f6f13533c9"
|
resolved "https://registry.yarnpkg.com/atob/-/atob-2.1.2.tgz#6d9517eb9e030d2436666651e86bd9f6f13533c9"
|
||||||
|
Loading…
Reference in New Issue
Block a user