Monday, 10 April 2017

Display JSON Response in a HTML page from an LDAPSEARCH post request

I have a form which will be used to search a user in LDAP. After the submitting the request, it will initiate a POST request and will search the user via ldapjs module in nodejs.

Below is my form:

    <h4>User Search</h4>
        <form class="form-horizontal" method="POST" action="/usersearch">
                <label><span class="glyphicon glyphicon-user"></span> TID/XID:</label>
                <input type="text" name="userid">
                <br>
                <label><span class="glyphicon glyphicon-tree-conifer"></span> Environment: </label><select name=environment>
                        <option value='ldap://dev:389'>DV</option>
                        <option value='ldap://at:389'>AT</option>
                        <option value='ldap://pt:389'>PT</option>
                        <option value='ldap://st:389'>ST</option>
                        <option value='ldap://ps:389'>PS</option>
                </select>
                <br><br>
                <input type="submit" value="Submit">
        </form>

And here is my POST from my app.js.

var express = require('express');
var bodyParser = require('body-parser');
var path = require('path');
var ejs = require('ejs');
//var usersearch = require("./routes/ldap.js");
var usersearch = require("./views/usersearch.ejs");
var ldap = require('ldapjs');
var parseDN = require('ldapjs').parseDN;
const url = require('url');
const assert = require('assert');

var app = express(); 

var urlencondedParser = bodyParser.urlencoded({extended: false});

// View Engine
app.set('view engine','ejs');
app.set('views', path.join(__dirname, 'views'));

// Load Body Parser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));

// Set Static Path
app.use(express.static(path.join(__dirname, 'public')));

app.get('/',function(req, res){
        res.render('index', {
                title : 'Self Serve Portal',
        });
});

app.get('/ldapss.ejs',function(req, res){
        res.render('ldapss', {
                title : 'LDAP Self Serve Portal',
        });
});

app.get('/usersearch.ejs',function(req, res){
        res.render('usersearch', {
                title : 'User Search',
        });
});

app.post('/usersearch', function(req, res, next){
        var postuserid = req.body.userid;
        var postldapurl = req.body.environment;
        var client = ldap.createClient({
        url: (postldapurl)
        });
        var opts = {
                filter: ("uid="+ postuserid),
                scope: 'sub',
                attributes: ['dn', 'sn', 'cn', 'ismemberof' ]
        };
        client.bind('cn=dirmanager', 'secret', function(err) {
        assert.ifError(err);
        });
        client.search('o=test', opts, function(err, res){
    assert.ifError(err);
        res.on('searchEntry', function(entry) {
    console.log(JSON.stringify(entry.object))})
        });
});

app.listen(3000,function(){
        console.log('Server started on port 3000...');
});

I can get a JSON response but I don't know how to display it in an HTML to another page.

JSON Response:

{"dn":"uid=T123456,o=test","controls":[],"
sn":"User","cn":"Test User","ismemberof":["cn=domo,o=test","cn=pingfedTM,o=test"]}


via jon

No comments:

Post a Comment