Skip to content

Commit

Permalink
fix integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexRoehm committed Sep 27, 2022
1 parent 130732f commit 2f6f607
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 61 deletions.
8 changes: 5 additions & 3 deletions src/ldapcache.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,12 @@ const getGlobals = (sitename) => {
}

exports.getUserPropertyForAuth = (userdn, sitename) => {
const user = ldapcache[sitename].users.attributes.elements.find((u)=>u.dn===userdn)
log.debugSite(sitename,JSON.stringify(user))
const users = ldapcache[sitename].users.attributes.elements
if (!users || !Array.isArray(users))
return userdn
const user = users.find((u)=>u.dn===userdn)
if (!user)
return null
return userdn
return user.attributes.entryUUID
}

Expand Down
116 changes: 60 additions & 56 deletions src/ldapserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,30 @@ var parseDN = require('ldapjs').parseDN;
const c = require('./constants');
const log = require('./logging');

log.loglevel = log.loglevels.debug
log.loglevel = log.loglevels.debug;

const normalize = (astring) => {
const str = astring
return str.replaceAll(", ",",")
}
const str = astring;
return str.replaceAll(', ', ',');
};
stopServer = () => {
ldapjs.close()
}
ldapjs.close();
};

startUp = (server, ldapjs, cb) => {
var port = parseInt(server.port)
ldapjs.listen(port, server.ip, cb(server.ip,port))
var port = parseInt(server.port);
ldapjs.listen(port, server.ip, cb(server.ip, port));
};

exports.getLdapServer = (server) => {
if (server.crt && server.key) {
var ldapCert = fs.readFileSync(server.crt, { encoding: 'utf8' });
var ldapKey = fs.readFileSync(server.key, { encoding: 'utf8' });
ldapjs = ldap.createServer({ log: log.logger, certificate: ldapCert, key: ldapKey });
ldapjs = ldap.createServer({
log: log.logger,
certificate: ldapCert,
key: ldapKey,
});
log.info('LDAP Server started with ssl');
} else {
ldapjs = ldap.createServer({ log: log.logger });
Expand All @@ -32,13 +36,13 @@ exports.getLdapServer = (server) => {
initSite: (site, cacheFunctions) => initSite(site, cacheFunctions, ldapjs),
startUp: (cb) => startUp(server, ldapjs, cb),
getConnections: (cb) => ldapjs.getConnections(cb),
stopServer: () => stopServer()
}
}
stopServer: () => stopServer(),
};
};

initSite = (site, cacheFunctions, ldapjs) => {
const sitename = site.site.name
const dc = site.ldap.dc
const sitename = site.site.name;
const dc = site.ldap.dc;

function authorize(req, _res, next) {
const adminDn = cacheFunctions.getGlobals().adminDn.dn;
Expand All @@ -57,8 +61,8 @@ initSite = (site, cacheFunctions, ldapjs) => {
);
log.debugSite(sitename, 'Filter: ' + req.filter.toString());
} catch (err) {
log.debug(req)
log.debug(err)
log.debug(req);
log.debug(err);
}
return next();
}
Expand All @@ -67,7 +71,8 @@ initSite = (site, cacheFunctions, ldapjs) => {
var strDn = req.dn.toString();
try {
cacheFunctions.getUsers().forEach((user) => {
if (parseDN(strDn).equals(parseDN(user.dn)) ||
if (
parseDN(strDn).equals(parseDN(user.dn)) ||
(!req.checkAll && req.filter.matches(user.attributes))
) {
log.debugSite(sitename, 'MatchUser: ' + user.dn);
Expand All @@ -84,8 +89,10 @@ initSite = (site, cacheFunctions, ldapjs) => {
var strDn = req.dn.toString();
try {
cacheFunctions.getGroups().forEach((group) => {
if (parseDN(strDn).equals(parseDN(group.dn)) ||
(!req.checkAll && req.filter.matches(group.attributes))) {
if (
parseDN(strDn).equals(parseDN(group.dn)) ||
(!req.checkAll && req.filter.matches(group.attributes))
) {
log.debugSite(sitename, 'MatchGroup: ' + group.dn);
res.send(group);
}
Expand Down Expand Up @@ -117,37 +124,34 @@ initSite = (site, cacheFunctions, ldapjs) => {
return next();
}

async function authenticate (req, _res, next) {
async function authenticate(req, _res, next) {
try {
var valid = await cacheFunctions.checkAuthentication(
req.dn.toString(),
req.credentials
);
if (!valid) {
log.errorSite(sitename, 'Authentication error');
return next(new ldap.InvalidCredentialsError());
}
log.debugSite(
sitename,
'Authentication successful for ' + req.dn.toString()
);
} catch(err) {
log.debug(err)
var valid = await cacheFunctions.checkAuthentication(
req.dn.toString(),
req.credentials
);
if (!valid) {
log.errorSite(sitename, 'Authentication error');
return next(new ldap.InvalidCredentialsError());
}
log.debugSite(
sitename,
'Authentication successful for ' + req.dn.toString()
);
} catch (err) {
log.debug(err);
return next(new ldap.InvalidCredentialsError());
}
return next();
};
}

log.debugSite(sitename,"Resgistering routes")
log.debugSite(sitename, 'Resgistering routes');
// Login bind for user
ldapjs.bind(
'ou=' + c.LDAP_OU_USERS + ","+dc,
'ou=' + c.LDAP_OU_USERS + ',' + dc,
(req, res, next) => {
log.debugSite(
sitename,
'BIND dn: ' + req.dn.toString()
);
next()
log.debugSite(sitename, 'BIND dn: ' + req.dn.toString());
next();
},
authenticate,
endSuccess
Expand All @@ -157,11 +161,8 @@ initSite = (site, cacheFunctions, ldapjs) => {
//"cn=admin,dc=ccfreiburg,dc=de",
cacheFunctions.getGlobals().adminDn.dn,
(req, res, next) => {
log.debugSite(
sitename,
'BIND dn: ' + req.dn
);
next()
log.debugSite(sitename, 'BIND dn: ' + req.dn);
next();
},
authenticate,
endSuccess
Expand All @@ -174,7 +175,9 @@ initSite = (site, cacheFunctions, ldapjs) => {
authorize,
function (req, _res, next) {
log.debugSite(sitename, 'Search for users');
req.checkAll = (req.scope !== 'base' && req.scope !== 'sub') || req.dn.rdns.length > parseDN(dc).rdns.length+1;
req.checkAll =
(req.scope !== 'base' && req.scope !== 'sub') ||
req.dn.rdns.length > parseDN(dc).rdns.length + 1;
return next();
},
sendUsers,
Expand All @@ -188,7 +191,9 @@ initSite = (site, cacheFunctions, ldapjs) => {
authorize,
function (req, _res, next) {
log.debugSite(sitename, 'Search for groups');
req.checkAll = req.scope !== 'base' || req.dn.rdns.length > parseDN(dc).rdns.length+1;
req.checkAll =
(req.scope !== 'base' && req.scope !== 'sub') ||
req.dn.rdns.length > parseDN(dc).rdns.length + 1;
return next();
},
sendGroups,
Expand All @@ -201,9 +206,9 @@ initSite = (site, cacheFunctions, ldapjs) => {
searchLogging,
authorize,
function (req, _res, next) {
log.debugSite(sitename, 'Search for users and groups combined');
req.checkAll = (req.scope !== 'base' && req.scope !== 'sub');
return next();
log.debugSite(sitename, 'Search for users and groups combined');
req.checkAll = req.scope !== 'base' && req.scope !== 'sub';
return next();
},
sendUsers,
sendGroups,
Expand Down Expand Up @@ -238,11 +243,10 @@ initSite = (site, cacheFunctions, ldapjs) => {
ldapjs.search(
'cn=eroor,ou=error,dc=error,o=error',
function (req, res) {
throw new Error("for testing")
throw new Error('for testing');
},
endSuccess
);

log.debugSite(sitename,"Routes registered")
};

log.debugSite(sitename, 'Routes registered');
};
2 changes: 1 addition & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const initCache = async (site, getChurchToolsDataFunc, authChurchToolsFunc) => {
const churchtoolsdata = await getChurchToolsDataFunc(site.selectionGroupIds, site.tranformedGroups, site.site)
const {users,groups} = transform.getLdapData(site, churchtoolsdata, adminuser)

siteCacheFunctions.setData(site.site.name,users,groups)
siteCacheFunctions.setData(users,groups)
return siteCacheFunctions
}

Expand Down
2 changes: 1 addition & 1 deletion test/ctconnection.integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe("API call integration ctconnection", () => {
it("Info: gets info object", async () => {
const result = await ctconn.infoReal(site.url)
expect(result.shortName).to.equal("CCF")
}).timeout(2000);
}).timeout(4000);

it("getCsrfTokenReal: throws exception", async () => {
const cookie = "";
Expand Down

0 comments on commit 2f6f607

Please sign in to comment.