js/yubi.js
author Mahlon E. Smith <mahlon@martini.nu>
Mon, 09 Feb 2015 09:20:01 -0800
changeset 0 52d30e6014a0
permissions -rw-r--r--
Initial commit. Experimental cross-platform frontend for yubikey programming.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     1
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     2
/*######################################################################
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     3
### I N I T I A L I Z A T I O N
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     4
######################################################################*/
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     5
var Y = global.Y;
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     6
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     7
// Global jQuery callback queues.  Used across the application to trigger actions
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     8
// on various events.
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     9
//
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    10
Y.callback = function( name, flags ) {
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    11
        if ( ! Y.callbacks ) Y.callbacks = {};
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    12
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    13
        // Create a new callback queue if one doesn't already exist
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    14
        // for the provided +name+.
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    15
        //
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    16
        if ( ! Y.callbacks[name] ) {
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    17
                if ( ! flags ) flags = 'unique';
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    18
                Y.callbacks[name] = $.Callbacks( flags );
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    19
        }
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    20
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    21
        return Y.callbacks[name];
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    22
};
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    23
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    24
// Node's gui interface.
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    25
Y.gui = require( 'nw.gui' );
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    26
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    27
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    28
// Execute yubi external processes.
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    29
//
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    30
Y.yubiexec = function( cmd, args ) {
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    31
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    32
	// Add the dynamic lib path for libyubi.
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    33
	//
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    34
	var lib = './yubi/' + process.platform + '/lib';
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    35
	var env = process.env;
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    36
	env[ 'DYLD_LIBRARY_PATH' ] = env[ 'DYLD_LIBRARY_PATH' ] + ':' + lib;
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    37
	// FIXME:  need to add linux and windows
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    38
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    39
	if ( ! args ) args = [];
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    40
	var binary = './yubi/' + process.platform + '/bin/' + cmd;
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    41
	Y.fs.chmodSync( binary, 0755 );
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    42
	var child = Y.spawn( binary, args, { env: env });
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    43
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    44
	if ( ! child ) {
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    45
		Y.callback( cmd + '-fatal' ).fire();
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    46
		return;
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    47
	}
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    48
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    49
	// Send callbacks when data is received.
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    50
	//
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    51
	child.stdout.on( 'data', function(data) {
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    52
		Y.callback( cmd ).fire( data );
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    53
	});
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    54
	child.stderr.on( 'data', function(data) {
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    55
		Y.callback( cmd + '-error' ).fire( data );
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    56
	});
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    57
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    58
	// Log exit code.
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    59
	//
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    60
	child.on( 'close', function(code) {
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    61
		console.log( cmd + ' process exited with code: ' + code);
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    62
	});
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    63
};
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    64
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    65
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    66
// Callback behaviors for ykinfo execution.
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    67
//
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    68
Y.callback( 'ykinfo' ).add( function(data) {
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    69
	Y.keyinfo = {};
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    70
	$.each( data.toString().split("\n"), function(i, info) {
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    71
		var match = info.match( /^(\w+):\s+(.+)$/ );
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    72
		if ( match ) Y.keyinfo[ match[1] ] = match[2];
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    73
	});
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    74
});
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    75
Y.callback( 'ykinfo-fatal' ).add( function() { Y.keyinfo = {} } );
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    76
Y.callback( 'ykinfo-error' ).add( function() { Y.keyinfo = {} } );
52d30e6014a0 Initial commit. Experimental cross-platform frontend for yubikey programming.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    77