Skip to content

Commit

Permalink
Unify debug logging to simplify search
Browse files Browse the repository at this point in the history
  • Loading branch information
a-schild committed Mar 7, 2019
1 parent 4fedf13 commit 572905b
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions ctldap.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ if (typeof config.cache_lifetime !== 'number') {
function apiLogin(site) {
if (site.loginPromise === null) {
if (config.debug) {
console.log("Performing API login...");
console.log("[DEBUG] Performing API login...");
}
site.loginPromise = rp({
"method": "POST",
Expand All @@ -148,23 +148,23 @@ function apiLogin(site) {
throw new Error(result.data);
}
if (config.debug) {
console.log("API login completed");
console.log("[DEBUG] API login completed");
}
// clear login promise
site.loginPromise = null;
// end gracefully
return null;
}).catch(function (error) {
if (config.debug) {
console.log("API login failed!");
console.log("[DEBUG] API login failed!");
}
// clear login promise
site.loginPromise = null;
// rethrow error
throw new Error(error);
});
} else if (config.debug) {
console.log("Return pending login promise");
console.log("[DEBUG] Return pending login promise");
}
return site.loginPromise;
}
Expand All @@ -188,12 +188,12 @@ function apiPost(site, func, data, triedLogin) {
// If this was the first attempt, login and try again
if (!triedLogin) {
if (config.debug) {
console.log("Session invalid, login and retry...");
console.log("[DEBUG] Session invalid, login and retry...");
}
return apiLogin(site).then(function () {
// Retry operation after login
if (config.debug) {
console.log("Retry request to API function " + func + " after login");
console.log("[DEBUG] Retry request to API function " + func + " after login");
}
// Set "triedLogin" parameter to prevent looping
return apiPost(site, func, data, true);
Expand Down Expand Up @@ -289,7 +289,7 @@ function requestUsers (req, res, next) {
}
var size = newCache.length;
if (config.debug && size > 0) {
console.log("Updated users: " + size);
console.log("[DEBUG] Updated users: " + size);
}
return newCache;
});
Expand Down Expand Up @@ -326,7 +326,7 @@ function requestGroups (req, res, next) {
});
var size = newCache.length;
if (config.debug && size > 0) {
console.log("Updated groups: " + size);
console.log("[DEBUG] Updated groups: " + size);
}
return newCache;
});
Expand Down Expand Up @@ -356,8 +356,8 @@ function authorize(req, res, next) {
*/
function searchLogging (req, res, next) {
if (config.debug) {
console.log("SEARCH base object: " + req.dn.toString() + " scope: " + req.scope);
console.log("Filter: " + req.filter.toString());
console.log("[DEBUG] SEARCH base object: " + req.dn.toString() + " scope: " + req.scope);
console.log("[DEBUG] Filter: " + req.filter.toString());
}
return next();
}
Expand All @@ -374,7 +374,7 @@ function sendUsers (req, res, next) {
users.forEach(function (u) {
if ((req.checkAll || strDn === u.dn) && (req.filter.matches(u.attributes))) {
if (config.debug) {
console.log("MatchUser: " + u.dn);
console.log("[DEBUG] MatchUser: " + u.dn);
}
res.send(u);
}
Expand All @@ -399,7 +399,7 @@ function sendGroups (req, res, next) {
groups.forEach(function (g) {
if ((req.checkAll || strDn === g.dn) && (req.filter.matches(g.attributes))) {
if (config.debug) {
console.log("MatchGroup: " + g.dn);
console.log("[DEBUG] MatchGroup: " + g.dn);
}
res.send(g);
}
Expand Down Expand Up @@ -433,14 +433,14 @@ function authenticate (req, res, next) {
var site = req.site;
if (req.dn.equals(site.adminDn)) {
if (config.debug) {
console.log('Admin bind DN: ' + req.dn.toString());
console.log('[DEBUG] Admin bind DN: ' + req.dn.toString());
}
// If ldap_password is undefined, try a default ChurchTools authentication with this user
if (site.ldap_password !== undefined) {
site.checkPassword(req.credentials, function (result) {
if (result) {
if (config.debug) {
console.log("Authentication success");
console.log("[DEBUG] Authentication success");
}
return next();
} else {
Expand All @@ -451,14 +451,14 @@ function authenticate (req, res, next) {
return;
}
} else if (config.debug) {
console.log('Bind user DN: ' + req.dn.toString());
console.log('[DEBUG] Bind user DN: ' + req.dn.toString());
}
apiPost(site, "authenticate", {
"user": req.dn.rdns[0].cn,
"password": req.credentials
}).then(function () {
if (config.debug) {
console.log("Authentication successful for " + req.dn.toString());
console.log("[DEBUG] Authentication successful for " + req.dn.toString());
}
return next();
}).catch(function (error) {
Expand Down

0 comments on commit 572905b

Please sign in to comment.