如何显示ldapatuh node jpegPhoto?

时间:2018-05-09 14:59:07

标签: node.js ldap jpeg ldapauth

我在node.js中使用ldapauth-fork来获取ldap中的用户信息但是当我尝试使用user.jpegPhoto时为什么它不能显示这个图像?

我试过这个:

 const jpeg = Buffer.from(user.jpegPhoto, 'binary')
 fs.writeFileSync("foto.jpeg", jpeg);

而且:

var img64 = Buffer.from(user.jpegPhoto).toString('base64');
var foto = "<div><img src='data:image/jpeg;base64,"+img64+"'/></div>"
fs.appendFile('index.html', foto, function (err) {
  if (err) throw err;
     console.log('Saved!');
  });

这是所有node.js代码:

var basicAuth = require('basic-auth');
var LdapAuth = require('ldapauth-fork');
var http = require('http');
var fs = require('fs');
var qs = require('querystring');

var username = "MOCKUSERNAME";
var password = "MOCKPASSWORD";

server = http.createServer( function(req, res) {

    if (req.method == 'POST') {
        console.log("POST");
        var body = '';
        req.on('data', function (data) {
            body += data;
            //console.log(body);
        });
        req.on('end', function () {
            var data = qs.parse(body);
            var userDecode = Buffer.from(data.username, 'base64').toString('ascii');
            var passDecode = Buffer.from(data.password, 'base64').toString('ascii'); 

            res.setHeader('Access-Control-Allow-Origin', '*');
            res.writeHead(200, {'Content-Type':'image/jpeg'});

            if (data.username && data.password) {

                var options = {
                   url:                'XXXXXX',
                   bindDN:             'XXXXX',
                   bindCredentials:    'XXXXX',
                   searchBase:         'XXXXX',
           includeRaw: true,
                   searchFilter:       '(aoldapkey=*' + userDecode + ')'
                   //  reconnect: true
                }
                var ldap = new LdapAuth(options);
                ldap.authenticate(userDecode, passDecode, function(err, user) {
                   if (err) {
                        console.log("login Error: " + err);
                        res.write(JSON.stringify({ success : false, message :     'Authentication failed', serverError: err }));
                            res.end('');
                       } else if(!user.uid) {
                        console.log("user not found Error: " + err);
                        res.write(JSON.stringify({ success : false, message :         
    'Authentication failed, user not found', serverError: err }));
                            res.end();
                       } else if(user.uid) {
                          console.log("success : user "+ user.uid +" found ");
                          var userJsonObject = {
                              success : true,
                              user: {
                                  uid : user.uid,
                                  cn: user.cn,
                                  givenName: user.givenName,
                                  sn: user.sn,
                                  title: user.title,
                                  mobile: user.mobile,
                                  mail: user.mail,
                                  aoJobPosition: user.aoJobPosition,
                                  jpegPhoto: user.jpegPhoto
                              }
                          }
    //------------------------------dont see the image in index.html
                /*var img64 = Buffer.from(user.jpegPhoto).toString('base64');
                var foto = "<div><img src='data:image/jpeg;base64,"+img64+"'/>    
    </div>"
                fs.appendFile('index.html', foto, function (err) {
                  if (err) throw err;
                  console.log('Saved!');
                });*/
    //--------------------------------
                          res.write(JSON.stringify(userJsonObject);
                          res.end();
                       }
                    });
                } else {
                res.write(JSON.stringify({ success : false, message : 'You must     
    specify a valid username and password'}));
                    res.end();
                }
            });
        }
        else {
            var html = fs.readFileSync('index.html');
            res.writeHead(200, {'Content-Type': 'text/html'});
            res.end(html);
        }
    });
    server.on('error', function(err) {
        console.log("Caught server error: ");
        console.log(err.stack);
    });

    port = 3000;
    host = '127.0.0.1';
    server.listen(port, host);
    console.log('Listening at http://' + host + ':' + port);

这是所有node.js代码我在node.js中使用ldapauth-fork来获取ldap中的用户信息,但是当我尝试获取user.jpegPhoto时,y无法显示此图像。

这是index.html:

<div><img src='data:imag/*;base64, 77x73..........3vv70='/></div>

index.html

0 个答案:

没有答案