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
|
||||
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.
|
||||
*/
|
||||
|
||||
var User = module.parent.require('./user'),
|
||||
Groups = module.parent.require('./groups'),
|
||||
meta = module.parent.require('./meta'),
|
||||
db = module.parent.require('../src/database'),
|
||||
passport = module.parent.require('passport'),
|
||||
fs = module.parent.require('fs'),
|
||||
path = module.parent.require('path'),
|
||||
nconf = module.parent.require('nconf'),
|
||||
winston = module.parent.require('winston'),
|
||||
async = module.parent.require('async');
|
||||
const User = require.main.require('./src/user');
|
||||
const Groups = require.main.require('./src/groups');
|
||||
const db = require.main.require('./src/database');
|
||||
const authenticationController = require.main.require('./src/controllers/authentication');
|
||||
|
||||
const async = require('async');
|
||||
|
||||
const passport = module.parent.require('passport');
|
||||
const nconf = module.parent.require('nconf');
|
||||
const winston = module.parent.require('winston');
|
||||
|
||||
var authenticationController = module.parent.require('./controllers/authentication');
|
||||
|
||||
/**
|
||||
* REMEMBER
|
||||
@ -49,25 +48,30 @@
|
||||
*/
|
||||
|
||||
var constants = Object.freeze({
|
||||
type: '', // Either 'oauth' or 'oauth2'
|
||||
name: '', // Something unique to your OAuth provider in lowercase, like "github", or "nodebb"
|
||||
oauth: {
|
||||
requestTokenURL: '',
|
||||
accessTokenURL: '',
|
||||
userAuthorizationURL: '',
|
||||
consumerKey: nconf.get('oauth:key'), // don't change this line
|
||||
consumerSecret: nconf.get('oauth:secret'), // don't change this line
|
||||
},
|
||||
oauth2: {
|
||||
authorizationURL: '',
|
||||
tokenURL: '',
|
||||
clientID: nconf.get('oauth:id'), // 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)
|
||||
}),
|
||||
configOk = false,
|
||||
OAuth = {}, passportOAuth, opts;
|
||||
type: '', // Either 'oauth' or 'oauth2'
|
||||
name: '', // Something unique to your OAuth provider in lowercase, like "github", or "nodebb"
|
||||
oauth: {
|
||||
requestTokenURL: '',
|
||||
accessTokenURL: '',
|
||||
userAuthorizationURL: '',
|
||||
consumerKey: nconf.get('oauth:key'), // don't change this line
|
||||
consumerSecret: nconf.get('oauth:secret'), // don't change this line
|
||||
},
|
||||
oauth2: {
|
||||
authorizationURL: '',
|
||||
tokenURL: '',
|
||||
clientID: nconf.get('oauth:id'), // 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)
|
||||
});
|
||||
|
||||
|
||||
var configOk = false;
|
||||
|
||||
|
||||
var OAuth = {}; var passportOAuth; var
|
||||
opts;
|
||||
|
||||
if (!constants.name) {
|
||||
winston.error('[sso-oauth] Please specify a name for your OAuth provider (library.js:32)');
|
||||
@ -79,7 +83,7 @@
|
||||
configOk = true;
|
||||
}
|
||||
|
||||
OAuth.getStrategy = function(strategies, callback) {
|
||||
OAuth.getStrategy = function (strategies, callback) {
|
||||
if (configOk) {
|
||||
passportOAuth = require('passport-oauth')[constants.type === 'oauth' ? 'OAuthStrategy' : 'OAuth2Strategy'];
|
||||
|
||||
@ -88,19 +92,21 @@
|
||||
opts = constants.oauth;
|
||||
opts.callbackURL = nconf.get('url') + '/auth/' + constants.name + '/callback';
|
||||
|
||||
passportOAuth.Strategy.prototype.userProfile = function(token, secret, params, done) {
|
||||
this._oauth.get(constants.userRoute, token, secret, function(err, body, res) {
|
||||
if (err) { return done(new InternalOAuthError('failed to fetch user profile', err)); }
|
||||
passportOAuth.Strategy.prototype.userProfile = function (token, secret, params, done) {
|
||||
this._oauth.get(constants.userRoute, token, secret, function (err, body/* , res */) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
|
||||
try {
|
||||
var json = JSON.parse(body);
|
||||
OAuth.parseUserReturn(json, function(err, profile) {
|
||||
OAuth.parseUserReturn(json, function (err, profile) {
|
||||
if (err) return done(err);
|
||||
profile.provider = constants.name;
|
||||
|
||||
done(null, profile);
|
||||
});
|
||||
} catch(e) {
|
||||
} catch (e) {
|
||||
done(e);
|
||||
}
|
||||
});
|
||||
@ -110,19 +116,21 @@
|
||||
opts = constants.oauth2;
|
||||
opts.callbackURL = nconf.get('url') + '/auth/' + constants.name + '/callback';
|
||||
|
||||
passportOAuth.Strategy.prototype.userProfile = function(accessToken, done) {
|
||||
this._oauth2.get(constants.userRoute, accessToken, function(err, body, res) {
|
||||
if (err) { return done(new InternalOAuthError('failed to fetch user profile', err)); }
|
||||
passportOAuth.Strategy.prototype.userProfile = function (accessToken, done) {
|
||||
this._oauth2.get(constants.userRoute, accessToken, function (err, body/* , res */) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
|
||||
try {
|
||||
var json = JSON.parse(body);
|
||||
OAuth.parseUserReturn(json, function(err, profile) {
|
||||
OAuth.parseUserReturn(json, function (err, profile) {
|
||||
if (err) return done(err);
|
||||
profile.provider = constants.name;
|
||||
|
||||
done(null, profile);
|
||||
});
|
||||
} catch(e) {
|
||||
} catch (e) {
|
||||
done(e);
|
||||
}
|
||||
});
|
||||
@ -131,13 +139,13 @@
|
||||
|
||||
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({
|
||||
oAuthid: profile.id,
|
||||
handle: profile.displayName,
|
||||
email: profile.emails[0].value,
|
||||
isAdmin: profile.isAdmin
|
||||
}, function(err, user) {
|
||||
isAdmin: profile.isAdmin,
|
||||
}, function (err, user) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
@ -152,7 +160,7 @@
|
||||
url: '/auth/' + constants.name,
|
||||
callbackURL: '/auth/' + constants.name + '/callback',
|
||||
icon: 'fa-check-square',
|
||||
scope: (constants.scope || '').split(',')
|
||||
scope: (constants.scope || '').split(','),
|
||||
});
|
||||
|
||||
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
|
||||
// NodeBB *requires* the following: id, displayName, emails.
|
||||
// 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===');
|
||||
return callback(new Error('Congrats! So far so good -- please see server log for details'));
|
||||
|
||||
// eslint-disable-next-line
|
||||
callback(null, profile);
|
||||
}
|
||||
};
|
||||
|
||||
OAuth.login = function(payload, callback) {
|
||||
OAuth.getUidByOAuthid(payload.oAuthid, function(err, uid) {
|
||||
if(err) {
|
||||
OAuth.login = function (payload, callback) {
|
||||
OAuth.getUidByOAuthid(payload.oAuthid, function (err, uid) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
if (uid !== null) {
|
||||
// Existing User
|
||||
callback(null, {
|
||||
uid: uid
|
||||
uid: uid,
|
||||
});
|
||||
} else {
|
||||
// New User
|
||||
var success = function(uid) {
|
||||
var success = function (uid) {
|
||||
// Save provider-specific information to the user
|
||||
User.setUserField(uid, constants.name + 'Id', payload.oAuthid);
|
||||
db.setObjectField(constants.name + 'Id:uid', payload.oAuthid, uid);
|
||||
|
||||
if (payload.isAdmin) {
|
||||
Groups.join('administrators', uid, function(err) {
|
||||
callback(null, {
|
||||
uid: uid
|
||||
Groups.join('administrators', uid, function (err) {
|
||||
callback(err, {
|
||||
uid: uid,
|
||||
});
|
||||
});
|
||||
} else {
|
||||
callback(null, {
|
||||
uid: uid
|
||||
uid: uid,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
User.getUidByEmail(payload.email, function(err, uid) {
|
||||
if(err) {
|
||||
User.getUidByEmail(payload.email, function (err, uid) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
if (!uid) {
|
||||
User.create({
|
||||
username: payload.handle,
|
||||
email: payload.email
|
||||
}, function(err, uid) {
|
||||
if(err) {
|
||||
email: payload.email,
|
||||
}, function (err, uid) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
@ -239,8 +248,8 @@
|
||||
});
|
||||
};
|
||||
|
||||
OAuth.getUidByOAuthid = function(oAuthid, callback) {
|
||||
db.getObjectField(constants.name + 'Id:uid', oAuthid, function(err, uid) {
|
||||
OAuth.getUidByOAuthid = function (oAuthid, callback) {
|
||||
db.getObjectField(constants.name + 'Id:uid', oAuthid, function (err, uid) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
@ -248,13 +257,13 @@
|
||||
});
|
||||
};
|
||||
|
||||
OAuth.deleteUserData = function(data, callback) {
|
||||
OAuth.deleteUserData = function (data, callback) {
|
||||
async.waterfall([
|
||||
async.apply(User.getUserField, data.uid, constants.name + 'Id'),
|
||||
function(oAuthIdToDelete, next) {
|
||||
function (oAuthIdToDelete, next) {
|
||||
db.deleteObjectField(constants.name + 'Id:uid', oAuthIdToDelete, next);
|
||||
}
|
||||
], function(err) {
|
||||
},
|
||||
], function (err) {
|
||||
if (err) {
|
||||
winston.error('[sso-oauth] Could not remove OAuthId data for uid ' + data.uid + '. Error: ' + 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.
|
||||
OAuth.whitelistFields = function(params, callback) {
|
||||
params.whitelist.push(constants.name + 'Id');
|
||||
callback(null, params);
|
||||
};
|
||||
// If this filter is not there, the deleteUserData function will fail when getting the oauthId for deletion.
|
||||
OAuth.whitelistFields = function (params, callback) {
|
||||
params.whitelist.push(constants.name + 'Id');
|
||||
callback(null, params);
|
||||
};
|
||||
|
||||
module.exports = OAuth;
|
||||
}(module));
|
||||
|
@ -28,6 +28,7 @@
|
||||
"readme": "",
|
||||
"readmeFilename": "README.md",
|
||||
"dependencies": {
|
||||
"async": "^2.6.1",
|
||||
"passport-oauth": "~1.0.0"
|
||||
},
|
||||
"nbbpm": {
|
||||
|
@ -315,6 +315,13 @@ astral-regex@^1.0.0:
|
||||
resolved "https://registry.yarnpkg.com/astral-regex/-/astral-regex-1.0.0.tgz#6c8c3fb827dd43ee3918f27b82782ab7658a6fd9"
|
||||
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:
|
||||
version "2.1.2"
|
||||
resolved "https://registry.yarnpkg.com/atob/-/atob-2.1.2.tgz#6d9517eb9e030d2436666651e86bd9f6f13533c9"
|
||||
|
Loading…
Reference in New Issue
Block a user