Skip to content

Commit

Permalink
node sample jwssign added
Browse files Browse the repository at this point in the history
  • Loading branch information
kjur committed Nov 24, 2015
1 parent 67913f2 commit d91c1b7
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ChangeLog.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

ChangeLog for jsrsasign

* Changes between 5.0.4 to next release
- node sample 'jwssign' added.

* Changes between 5.0.2 to 5.0.4 (2015-Nov-23)
- jwtverify, asn1dump and pemtobin sample Node script in
sample_node directory.
Expand Down
6 changes: 6 additions & 0 deletions sample_node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ This script dumps ASN.1 DER formatted binary file.
NULL
BITSTRING 003081890...(total ???bytes)...

1. [jwssign](https://github.com/kjur/jsrsasign/tree/master/sample_node/jwssign) - sign JWS by header and payload file or string

This script is to sign JWS(JSON Web Signature) for specified header and payload file or string
using [KJUR.jws.JWS.sign()](http://kjur.github.io/jsrsasign/api/symbols/KJUR.jws.JWS.html#.sign) method.
See [here](https://github.com/kjur/jsrsasign/wiki/Sample-Node-Script---jwssign) in detail.

1. [pemtobin](https://github.com/kjur/jsrsasign/tree/master/sample_node/pemtobin) - convert any PEM file to binary

This script converts from any PEM format file to binary.
Expand Down
107 changes: 107 additions & 0 deletions sample_node/jwssign
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#!/usr/bin/env node

/*
* jwssign - sign JWS by header/payload file or string
*
* Copyright (c) 2015 Kenji Urushima ([email protected])
*
* This software is licensed under the terms of the MIT License.
* http://kjur.github.com/jsrsasign/license
*
* The above copyright and license notice shall be
* included in all copies or substantial portions of the Software.
*
* Please use '-h' option for this script usage.
*/

var program = require('commander');
var rs = require('jsrsasign');
var path = require('path');
var JWS = rs.jws.JWS;

program
.version('1.0.0 (2015-Nov-25)')
.usage('[options] <JWS Header file/string> <JWS payload file/string> <output JWS file>')
.description('sign JWS by header/payload file or string')
.option('-t, --passtype <utf8|hex|b64|b64u>', 'Hmac(HS*) pass type', 'utf8')
.option('-p, --pass <pass>', 'Hmac(HS*) password in specfied type', 'passwd')
.option('-k, --prvkey <file>', 'private key file (ex. PKCS#8 PEM)')
.option('-f, --forcealg <sigalg>', 'overwrite alg in header (ex. HS512)')
.parse(process.argv);

if (program.args.length !== 3)
throw "wrong number of arguments";
var outFile = program.args[2];

var sHeader, pHeader, sPayload;
var pass, prvKeyObj;

/*
* Read Header and Payload
*/

try {
var inFile = program.args[0];
sHeader = rs.readFile(inFile);
} catch(ex) {
sHeader = program.args[0] // as string
}

try {
var inFile = program.args[1];
sPayload = rs.readFile(inFile);
} catch(ex) {
sPayload = program.args[1] // as string
}

pHeader = JWS.readSafeJSONString(sHeader);
if (pHeader === null)
throw "error: not safe JSON header: " + sHeader;

/*
* pass, prvkey and sigalg check
*/
if (program.forcealg !== undefined && pHeader.alg !== program.forcealg) {
pHeader.alg = program.forcealg;
sHeader = JSON.stringify(pHeader);
}

pass = {};
if (! JWS.inArray(program.passtype, ['utf8', 'hex', 'b64', 'b64u']))
throw "unsupported HS* password type: " + program.passtype;
if (program.passtype !== undefined && program.pass !== undefined)
pass[program.passtype] = program.pass;

if (program.prvkey !== undefined) {
var prvPEM = rs.readFile(program.prvkey);
prvKeyObj = rs.KEYUTIL.getKey(prvPEM);
}

if (prvKeyObj === undefined && ! pHeader.alg.match(/^HS/))
throw "sigalg shall be HS* in header for hmac password";

if (prvKeyObj !== undefined && prvKeyObj instanceof rs.RSAKey &&
! pHeader.alg.match(/^[PR]S/))
throw "sigalg shall be PS* or RS* in header for RSA key";

if (prvKeyObj !== undefined && prvKeyObj instanceof rs.crypto.ECDSA &&
! pHeader.alg.match(/^ES/))
throw "sigalg shall be ES* in header for ECDSA key: " + pHeader.alg;

/*
* sign JWS
*/
var sJWS
if (prvKeyObj === undefined) {
sJWS = JWS.sign(pHeader.alg, sHeader, sPayload, pass);
} else {
sJWS = JWS.sign(pHeader.alg, sHeader, sPayload, prvKeyObj);
}

if (outFile === "-")
console.log(sJWS);
else
rs.saveFile(outFile, sJWS);



0 comments on commit d91c1b7

Please sign in to comment.