2016-04-04 12:36:30 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-07-26 19:42:05 +00:00
|
|
|
const config = require('config');
|
2018-09-27 19:32:35 +00:00
|
|
|
const log = require('./log');
|
2017-07-26 19:42:05 +00:00
|
|
|
const util = require('util');
|
2016-08-11 11:21:48 +00:00
|
|
|
|
2018-11-18 20:31:22 +00:00
|
|
|
const passport = require('passport');
|
2017-07-26 19:42:05 +00:00
|
|
|
const LocalStrategy = require('passport-local').Strategy;
|
2016-08-29 10:57:27 +00:00
|
|
|
|
2017-07-26 19:42:05 +00:00
|
|
|
const csrf = require('csurf');
|
|
|
|
const bodyParser = require('body-parser');
|
2017-07-08 13:48:34 +00:00
|
|
|
|
|
|
|
const users = require('../models/users');
|
|
|
|
const { nodeifyFunction, nodeifyPromise } = require('./nodeify');
|
2018-11-18 14:38:52 +00:00
|
|
|
const interoperableErrors = require('../../shared/interoperable-errors');
|
2017-07-29 19:42:07 +00:00
|
|
|
const contextHelpers = require('./context-helpers');
|
2016-04-04 12:36:30 +00:00
|
|
|
|
2018-02-24 22:05:01 +00:00
|
|
|
let authMode = 'local';
|
2018-02-25 19:54:15 +00:00
|
|
|
|
|
|
|
let LdapStrategy;
|
|
|
|
let ldapStrategyOpts;
|
2018-02-24 22:05:01 +00:00
|
|
|
if (config.ldap.enabled) {
|
2018-02-25 19:54:15 +00:00
|
|
|
if (!config.ldap.method || config.ldap.method == 'ldapjs') {
|
2018-02-24 22:05:01 +00:00
|
|
|
try {
|
|
|
|
LdapStrategy = require('passport-ldapjs').Strategy; // eslint-disable-line global-require
|
|
|
|
authMode = 'ldapjs';
|
2018-02-25 19:54:15 +00:00
|
|
|
log.info('LDAP', 'Found module "passport-ldapjs". It will be used for LDAP auth.');
|
|
|
|
|
|
|
|
ldapStrategyOpts = {
|
|
|
|
server: {
|
|
|
|
url: 'ldap://' + config.ldap.host + ':' + config.ldap.port
|
|
|
|
},
|
|
|
|
base: config.ldap.baseDN,
|
|
|
|
search: {
|
|
|
|
filter: config.ldap.filter,
|
|
|
|
attributes: [config.ldap.uidTag, config.ldap.nameTag, 'mail'],
|
|
|
|
scope: 'sub'
|
|
|
|
},
|
|
|
|
uidTag: config.ldap.uidTag,
|
|
|
|
bindUser: config.ldap.bindUser,
|
|
|
|
bindPassword: config.ldap.bindPassword
|
|
|
|
};
|
|
|
|
|
2018-02-24 22:05:01 +00:00
|
|
|
} catch (exc) {
|
|
|
|
log.info('LDAP', 'Module "passport-ldapjs" not installed. It will not be used for LDAP auth.');
|
|
|
|
}
|
2018-02-25 19:54:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!LdapStrategy && (!config.ldap.method || config.ldap.method == 'ldapauth')) {
|
2018-02-24 22:05:01 +00:00
|
|
|
try {
|
|
|
|
LdapStrategy = require('passport-ldapauth').Strategy; // eslint-disable-line global-require
|
|
|
|
authMode = 'ldapauth';
|
2018-02-25 19:54:15 +00:00
|
|
|
log.info('LDAP', 'Found module "passport-ldapauth". It will be used for LDAP auth.');
|
|
|
|
|
|
|
|
ldapStrategyOpts = {
|
|
|
|
server: {
|
|
|
|
url: 'ldap://' + config.ldap.host + ':' + config.ldap.port,
|
|
|
|
searchBase: config.ldap.baseDN,
|
|
|
|
searchFilter: config.ldap.filter,
|
|
|
|
searchAttributes: [config.ldap.uidTag, config.ldap.nameTag, 'mail'],
|
|
|
|
bindDN: config.ldap.bindUser,
|
|
|
|
bindCredentials: config.ldap.bindPassword
|
|
|
|
},
|
|
|
|
};
|
2018-02-24 22:05:01 +00:00
|
|
|
} catch (exc) {
|
|
|
|
log.info('LDAP', 'Module "passport-ldapauth" not installed. It will not be used for LDAP auth.');
|
|
|
|
}
|
2017-03-15 18:44:12 +00:00
|
|
|
}
|
2016-08-29 10:57:27 +00:00
|
|
|
}
|
|
|
|
|
2016-04-04 12:36:30 +00:00
|
|
|
module.exports.csrfProtection = csrf({
|
|
|
|
cookie: true
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports.parseForm = bodyParser.urlencoded({
|
2016-04-13 05:36:55 +00:00
|
|
|
extended: false,
|
2018-04-02 09:58:32 +00:00
|
|
|
limit: config.www.postSize
|
2016-04-04 12:36:30 +00:00
|
|
|
});
|
|
|
|
|
2017-07-08 13:48:34 +00:00
|
|
|
module.exports.loggedIn = (req, res, next) => {
|
|
|
|
if (!req.user) {
|
|
|
|
next(new interoperableErrors.NotLoggedInError());
|
|
|
|
} else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-12-10 20:44:35 +00:00
|
|
|
module.exports.authByAccessToken = (req, res, next) => {
|
2018-04-02 09:58:32 +00:00
|
|
|
if (!req.query.access_token) {
|
|
|
|
res.status(403);
|
|
|
|
res.json({
|
|
|
|
error: 'Missing access_token',
|
|
|
|
data: []
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
users.getByAccessToken(req.query.access_token).then(user => {
|
|
|
|
req.user = user;
|
|
|
|
next();
|
|
|
|
}).catch(err => {
|
|
|
|
if (err instanceof interoperableErrors.PermissionDeniedError) {
|
2017-12-10 20:44:35 +00:00
|
|
|
res.status(403);
|
2018-04-02 09:58:32 +00:00
|
|
|
res.json({
|
|
|
|
error: 'Invalid or expired access_token',
|
|
|
|
data: []
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
res.status(500);
|
|
|
|
res.json({
|
|
|
|
error: err.message || err,
|
2017-12-10 20:44:35 +00:00
|
|
|
data: []
|
|
|
|
});
|
|
|
|
}
|
2018-04-02 09:58:32 +00:00
|
|
|
});
|
|
|
|
};
|
2017-12-10 20:44:35 +00:00
|
|
|
|
2018-04-02 09:58:32 +00:00
|
|
|
module.exports.tryAuthByRestrictedAccessToken = (req, res, next) => {
|
2018-05-09 02:07:01 +00:00
|
|
|
const pathComps = req.url.split('/');
|
|
|
|
|
|
|
|
pathComps.shift();
|
|
|
|
const restrictedAccessToken = pathComps.shift();
|
|
|
|
pathComps.unshift('');
|
|
|
|
|
|
|
|
const url = pathComps.join('/');
|
|
|
|
|
|
|
|
req.url = url;
|
|
|
|
|
|
|
|
users.getByRestrictedAccessToken(restrictedAccessToken).then(user => {
|
|
|
|
req.user = user;
|
2018-04-02 09:58:32 +00:00
|
|
|
next();
|
2018-05-09 02:07:01 +00:00
|
|
|
}).catch(err => {
|
|
|
|
next();
|
|
|
|
});
|
2017-12-10 20:44:35 +00:00
|
|
|
};
|
|
|
|
|
2018-07-18 17:41:18 +00:00
|
|
|
|
|
|
|
module.exports.setupRegularAuth = app => {
|
2016-04-04 12:36:30 +00:00
|
|
|
app.use(passport.initialize());
|
|
|
|
app.use(passport.session());
|
|
|
|
};
|
|
|
|
|
2017-07-08 13:48:34 +00:00
|
|
|
module.exports.restLogout = (req, res) => {
|
|
|
|
req.logout();
|
|
|
|
res.json();
|
2016-04-04 12:36:30 +00:00
|
|
|
};
|
|
|
|
|
2017-07-08 13:48:34 +00:00
|
|
|
module.exports.restLogin = (req, res, next) => {
|
2017-11-08 10:47:46 +00:00
|
|
|
passport.authenticate(authMode, (err, user, info) => {
|
2017-07-08 16:57:41 +00:00
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
2017-07-08 13:48:34 +00:00
|
|
|
|
2016-04-04 12:36:30 +00:00
|
|
|
if (!user) {
|
2017-07-08 13:48:34 +00:00
|
|
|
return next(new interoperableErrors.IncorrectPasswordError());
|
2016-04-04 12:36:30 +00:00
|
|
|
}
|
2017-07-08 16:57:41 +00:00
|
|
|
|
2016-04-04 12:36:30 +00:00
|
|
|
req.logIn(user, err => {
|
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.body.remember) {
|
|
|
|
// Cookie expires after 30 days
|
|
|
|
req.session.cookie.maxAge = 30 * 24 * 60 * 60 * 1000;
|
|
|
|
} else {
|
|
|
|
// Cookie expires at end of session
|
|
|
|
req.session.cookie.expires = false;
|
|
|
|
}
|
|
|
|
|
2017-07-08 13:48:34 +00:00
|
|
|
return res.json();
|
2016-04-04 12:36:30 +00:00
|
|
|
});
|
|
|
|
})(req, res, next);
|
|
|
|
};
|
|
|
|
|
2018-02-25 19:54:15 +00:00
|
|
|
if (LdapStrategy) {
|
2018-02-24 22:05:01 +00:00
|
|
|
log.info('Using LDAP auth (passport-' + authMode + ')');
|
2017-07-08 13:48:34 +00:00
|
|
|
module.exports.authMethod = 'ldap';
|
2017-07-08 18:32:04 +00:00
|
|
|
module.exports.isAuthMethodLocal = false;
|
2016-04-04 12:36:30 +00:00
|
|
|
|
2018-02-25 19:54:15 +00:00
|
|
|
passport.use(new LdapStrategy(ldapStrategyOpts, nodeifyFunction(async (profile) => {
|
2017-07-08 13:48:34 +00:00
|
|
|
try {
|
|
|
|
const user = await users.getByUsername(profile[config.ldap.uidTag]);
|
|
|
|
|
|
|
|
return {
|
|
|
|
id: user.id,
|
|
|
|
username: user.username,
|
|
|
|
name: profile[config.ldap.nameTag],
|
2017-07-26 19:42:05 +00:00
|
|
|
email: profile.mail,
|
|
|
|
role: user.role
|
2017-07-08 13:48:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
if (err instanceof interoperableErrors.NotFoundError) {
|
2017-07-27 14:11:22 +00:00
|
|
|
const userId = await users.create(null, {
|
2017-07-08 13:48:34 +00:00
|
|
|
username: profile[config.ldap.uidTag],
|
2017-07-26 19:42:05 +00:00
|
|
|
role: config.ldap.newUserRole,
|
|
|
|
namespace: config.ldap.newUserNamespaceId
|
2016-08-11 11:21:48 +00:00
|
|
|
});
|
2017-07-08 13:48:34 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
id: userId,
|
|
|
|
username: profile[config.ldap.uidTag],
|
|
|
|
name: profile[config.ldap.nameTag],
|
2017-07-26 19:42:05 +00:00
|
|
|
email: profile.mail,
|
|
|
|
role: config.ldap.newUserRole
|
2017-07-08 13:48:34 +00:00
|
|
|
};
|
2016-08-11 11:21:48 +00:00
|
|
|
} else {
|
2017-07-08 13:48:34 +00:00
|
|
|
throw err;
|
2016-08-11 11:21:48 +00:00
|
|
|
}
|
2017-07-08 13:48:34 +00:00
|
|
|
}
|
|
|
|
})));
|
|
|
|
|
2017-07-09 13:41:53 +00:00
|
|
|
passport.serializeUser((user, done) => done(null, user));
|
2017-07-08 13:48:34 +00:00
|
|
|
passport.deserializeUser((user, done) => done(null, user));
|
|
|
|
|
2016-08-11 11:21:48 +00:00
|
|
|
} else {
|
|
|
|
log.info('Using local auth');
|
2017-07-08 13:48:34 +00:00
|
|
|
module.exports.authMethod = 'local';
|
|
|
|
module.exports.isAuthMethodLocal = true;
|
2016-04-04 12:36:30 +00:00
|
|
|
|
2017-07-08 13:48:34 +00:00
|
|
|
passport.use(new LocalStrategy(nodeifyFunction(async (username, password) => {
|
|
|
|
return await users.getByUsernameIfPasswordMatch(username, password);
|
|
|
|
})));
|
2016-08-11 11:21:48 +00:00
|
|
|
|
2017-07-08 13:48:34 +00:00
|
|
|
passport.serializeUser((user, done) => done(null, user.id));
|
2017-07-29 19:42:07 +00:00
|
|
|
passport.deserializeUser((id, done) => nodeifyPromise(users.getById(contextHelpers.getAdminContext(), id), done));
|
2016-08-11 11:21:48 +00:00
|
|
|
}
|
2016-04-04 12:36:30 +00:00
|
|
|
|