Using HBR$PWDL from a script to check/change passwords

The article Changing Passwords and Validating User IDs gives you details about the HostBridge module HBR$PWDL. Here is an HB.js implementation that shows how you can make use of that module.

This example comes in a module and a script. The first is the HB.js copybook object that was generated by uploading the COBOL copybook found in the above article to http://support.hostbridge.com. The HostBridge/AJAX application generated several pieces of code, one being the hbrPwdlCopybook object. The copybook object is in "requires" format so that it can be reused by any service you create that needs access to the facilities provided by HBR$PWDL.

The script is an example implementation of the hbrPwdlCopybook object.

hbrPwdlCopybook object
(function () {
        exports.commareaCopybook = function(o) {
                var options = {
                        initialize : true,
                        initChar : ' '
                }
                
                if (typeof(o) == 'object') 
                        for (var p in o) {
                                options[p] = o[p];
                        }
                
                var bufferLength = 128;
                
                this.buffer =  new StringBuffer(bufferLength, options.initChar);
                
                var me = this;
                
                this.zeroIndexes = function() {
                        if (options.initialize) {
                                for (var n in this.fields) {
                                        if (this.fields[n]._index) {
                                                this.fields[n]._index = 0;
                                        }
                                }
                        }
                }
                
                function fieldSetter(v) {
                        var offSet = this.offset();
                        switch(this.type) {
                                case 'CHAR':
                                case 'GROUP':
                                        v = v.toString();
                                        if (v.length < this.length) {
                                                me.buffer.fillAt(offSet, ' ', this.length);
                                        }
                                        me.buffer.assignAt(offSet, v.length > this.length ? v.substring(0,this.length) : v);
                                        break;
                                case 'BINARY':
                                        me.buffer.setBinaryAt(offSet, v, this.precision, this.signed ? true : false);
                                        break;
                                case 'COMP3':
                                        me.buffer.setPackedAt(offSet, v, this.precision, this.scale ? this.scale : 0, this.signed ? true : false);
                                        break;
                                case 'ZONED':
                                        me.buffer.setZonedAt(offSet, v, this.precision, this.scale ? this.scale : 0, this.signed ? true : false);
                                        break;
                                default:
                                        throw new TypeError("Invalid copybook data type " + this.type + " for " + this.name.cobol);
                        }
                }
                
                function fieldValueOf () {
                        switch(this.type) {
                                case 'CHAR':
                                case 'GROUP':
                                        return(me.buffer.substr(this.offset(), this.length));
                                case 'BINARY':
                                        return(me.buffer.binaryAt(this.offset(), this.precision, this.signed ? true : false));
                                case 'COMP3':
                                        return(me.buffer.packedAt(this.offset(), this.precision, this.scale ? this.scale : 0));
                                case 'ZONED':
                                        return(me.buffer.zonedAt(this.offset(), this.precision, this.scale ? this.scale : 0));
                                default:
                                        throw "Invalid copybook data type:" + this.type;                        
                        }
                }
                
                function fieldtoString () {
                        switch(this.type) {
                                case 'CHAR':
                                case 'GROUP':
                                        return(me.buffer.substr(this.offset(), this.length));
                                case 'BINARY':
                                        return(me.buffer.binaryAt(this.offset(), this.precision, this.signed ? true : false).toString());
                                case 'COMP3':
                                        return(me.buffer.packedAt(this.offset(), this.precision, this.scale ? this.scale : 0).toString());
                                case 'ZONED':
                                        return(me.buffer.zonedAt(this.offset(), this.precision, this.scale ? this.scale : 0).toString());
                                default:
                                        throw "Invalid copybook data type:" + this.type;
                        }
                }
                
                function fieldOffset() {
                        var o = this.position;;
                        if (this.scalar) return (o);
                        
                        if (this.occurs)
                                o += this.length * this._index;
                        
                        for (var e = 0; e < this.occurParents.length ; e++) {
                                o += me.fields[this.occurParents[e]].length * me.fields[this.occurParents[e]]._index;
                        }
                                
                        return (o);
                }
                
                this.autoIndex = function (f) {
                        var max = [], current = [];
                        if (f.occurs) max.push(f.occurs); 
                        for (var e = 0; e < f.occurParents.length; e++) {
                                max.push(me.fields[f.occurParents[e]].occurs);
                        }
                        for (var i = 0; i < max.length; i++) current[i] = 0;
                        var first = true;
                        this.next = function () {
                                if (first) {
                                        first = false;
                                        return(current);
                                }
                                for (var i = max.length; i>0; i--) {
                                        if (++current[i-1] < max[i-1]) {
                                                return (current);
                                        }
                                        current[i-1] = 0;
                                }
                                return(false);
                        }
                }
                
                function fieldIndex() {
                        if (arguments.length == 0) return(this);
                        var indexes = [].slice.call(arguments);
                        if (this.occurs) {
                                var idx = indexes.pop();
                                if (idx >= 0 && idx < this.occurs ) this._index = idx; 
                                else throw "Index (" + idx + ") out of bounds on " + this.name.cobol;
                        }
                
                        fieldIndex.apply(me.fields[this.occurParents[this.occurParents.length - 1]],indexes);
                        
                        return(this);
                }
                
                this.fields = {
                commarea : {
                                name            : { js : 'commarea', cobol : 'COMMAREA' },
                                level           : 01,
                                type            : 'GROUP',
                                dependingOn     : false,
                                scalar          : true,
                                length          : 128,
                                position        : 0,
                                parent          : '',
                                occurParents: []
                        },userid : {
                                name            : { js : 'userid', cobol : 'USERID' },
                                level           : 05,
                                type            : 'CHAR',
                                picture         : 'X(8)',
                                dependingOn     : false,
                                scalar          : true,
                                length          : 8,
                                position        : 0,
                                parent          : 'commarea',
                                occurParents: []
                        },password : {
                                name            : { js : 'password', cobol : 'PASSWORD' },
                                level           : 05,
                                type            : 'CHAR',
                                picture         : 'X(8)',
                                dependingOn     : false,
                                scalar          : true,
                                length          : 8,
                                position        : 8,
                                parent          : 'commarea',
                                occurParents: []
                        },newpass : {
                                name            : { js : 'newpass', cobol : 'NEWPASS' },
                                level           : 05,
                                type            : 'CHAR',
                                picture         : 'X(8)',
                                dependingOn     : false,
                                scalar          : true,
                                length          : 8,
                                position        : 16,
                                parent          : 'commarea',
                                occurParents: []
                        },retcode : {
                                name            : { js : 'retcode', cobol : 'RETCODE' },
                                level           : 05,
                                type            : 'BINARY',
                                signed          : true,
                                picture         : 'S9(8)',
                                precision       : 8,
                                dependingOn     : false,
                                scalar          : true,
                                length          : 4,
                                position        : 24,
                                parent          : 'commarea',
                                occurParents: []
                        },respval : {
                                name            : { js : 'respval', cobol : 'RESPVAL' },
                                level           : 05,
                                type            : 'BINARY',
                                signed          : true,
                                picture         : 'S9(8)',
                                precision       : 8,
                                dependingOn     : false,
                                scalar          : true,
                                length          : 4,
                                position        : 28,
                                parent          : 'commarea',
                                occurParents: []
                        },resp2val : {
                                name            : { js : 'resp2val', cobol : 'RESP2VAL' },
                                level           : 05,
                                type            : 'BINARY',
                                signed          : true,
                                picture         : 'S9(8)',
                                precision       : 8,
                                dependingOn     : false,
                                scalar          : true,
                                length          : 4,
                                position        : 32,
                                parent          : 'commarea',
                                occurParents: []
                        },respcond : {
                                name            : { js : 'respcond', cobol : 'RESPCOND' },
                                level           : 05,
                                type            : 'CHAR',
                                picture         : 'X(12)',
                                dependingOn     : false,
                                scalar          : true,
                                length          : 12,
                                position        : 36,
                                parent          : 'commarea',
                                occurParents: []
                        },message : {
                                name            : { js : 'message', cobol : 'MESSAGE' },
                                level           : 05,
                                type            : 'CHAR',
                                picture         : 'X(80)',
                                dependingOn     : false,
                                scalar          : true,
                                length          : 80,
                                position        : 48,
                                parent          : 'commarea',
                                occurParents: []
                        },}
                
                for each (var n in this.fields) {
                        n.offset = fieldOffset;
                        n.set = fieldSetter;
                        n.get = fieldValueOf;
                        n.valueOf = fieldValueOf;
                        n.index = fieldIndex;
                        n.toString = fieldtoString;
                        if (n.initial && options.initialize != false) {
                                var initialValue = n.type == 'CHAR' || n.type == 'GROUP' ? n.initial : Number(n.initial);
                                if (n.scalar) {
                                        n.set(initialValue);
                                }
                                else {
                                        var a = new     this.autoIndex(n);
                                        var ixArray = a.next();
                                        while (ixArray instanceof Array) {
                                                fieldIndex.apply(n, ixArray);
                                                n.set(initialValue);
                                                ixArray = a.next();
                                        }
                                }
                        }
                }
        }
}) ();


Sample Usage
/*
 * Require standard functions I might need
 */
var common = require('common', 'hbutils');
var debugging = require('debugging', 'hbutils');

/*
 * Require the copybook "factory"
 */
var Hbr$pwdlCB = require('hbrPwdlCopybook', 'hbscript');

var pgm = new CommareaProgram('HBR$PWDL');

function main(){

        /*
         * Create the Object
         */
        var hbr$pwdl = new Hbr$pwdlCB.commareaCopybook();       
        
        /*
         * Create rooted aliases for the fields
         */ 
        var commarea    = hbr$pwdl.fields.commarea;             // COMMAREA 
        var userid              = hbr$pwdl.fields.userid;               // USERID 
        var password    = hbr$pwdl.fields.password;             // PASSWORD 
        var newpass     = hbr$pwdl.fields.newpass;              // NEWPASS 
        var retcode     = hbr$pwdl.fields.retcode;              // RETCODE 
        var respval     = hbr$pwdl.fields.respval;              // RESPVAL 
        var resp2val    = hbr$pwdl.fields.resp2val;             // RESP2VAL 
        var respcond    = hbr$pwdl.fields.respcond;             // RESPCOND 
        var message     = hbr$pwdl.fields.message;              // MESSAGE 
        
        
        /*
         * Set the values before invoking
         */
        hbr$pwdl.fields.userid.set('DEMO');
        hbr$pwdl.fields.password.set('badPass');
        
        /*
         * Invoke the program TRADERBL with the buffer
         */
        pgm.run(hbr$pwdl.buffer);
                
        /*
         * Output the entire buffer according to the copybook layout
         */
        common.headers.xml();
        common.cbToXml(hbr$pwdl, {
                replaceNonDisplay: false,               
                replaceChar: ' ',
                normalize: true,                                
                includeFiller: false
        });
        
        /*
         * Sample output of the above request 
         
                <?xml version="1.0" encoding="UTF-8"?>
                <commarea>
                  <userid>DEMO</userid>
                  <password>********</password>
                  <retcode>8</retcode>
                  <respval>70</respval>
                  <resp2val>2</resp2val>
                  <respcond>NOTAUTH</respcond>
                  <message>VERIFY NOTAUTH: THE PASSWORD IS WRONG</message>
                </commarea>
         */
}

main();