Display LDAP thumbnail photos
Problem
In our Node.js applications, we use passport-ldapauth library to authenticate\retrieve the user info.
In the returned user object, the thumbnailPhoto field cannot be displayed as an image in HTML. It is because the ldapjs library convert all types of attributes as string.
Solution
In the file node_modules\ldapjs\lib\messages\search_entry.js, replace
if (a.vals && a.vals.length) { if (a.vals.length > 1) { obj[a.type] = a.vals.slice(); } else { obj[a.type] = a.vals[0]; } } else { obj[a.type] = []; }
as
var buf = a.buffers; var val = a.vals; var item; if ( a.type == 'thumbnailPhoto' ) item = buf; else item = val; if (item && item.length) { if (item.length > 1) { obj[a.type] = item.slice(); } else { obj[a.type] = item[0]; } } else { obj[a.type] = []; }
Then, overwrite the thumbnailPhoto attribute to be a string used in <img src='{thumbnailPhoto}'>:
router.post('/login', passport.authenticate('ldapauth'), (req, res) => { req.user.thumbnailPhoto = 'data:image/jpeg;base64,' + Buffer.from(req.user.thumbnailPhoto).toString('base64'); res.render('profile', req.user); });
Related articles
https://github.com/joyent/node-ldapjs/issues/137