From 436ec3cb621b0e7311396c3cfba76b5468f1fdaf Mon Sep 17 00:00:00 2001 From: oren Date: Wed, 6 Jan 2016 11:16:22 -0800 Subject: [PATCH 1/7] Clean up CLI feature --- bin/openbadges-validate | 28 ++++++++++++++++++++++++++++ index.js | 1 + 2 files changed, 29 insertions(+) create mode 100755 bin/openbadges-validate diff --git a/bin/openbadges-validate b/bin/openbadges-validate new file mode 100755 index 0000000..321ac01 --- /dev/null +++ b/bin/openbadges-validate @@ -0,0 +1,28 @@ +#!/usr/bin/env node +var validator = require('../'); +var input = process.argv.slice(2)[0]; + +(function main () { + console.log('Validating input: ' + input); + validator.validate(input, handleResponse); +})(); + +function handleResponse (response) { + if (response === null) { + console.log('Okay'); + process.exit(0); + } + if (response instanceof Error) { + console.log('Error'); + if (response.name && response.message) { + console.log(response.name + ': ' + response.message); + } + if (response.reason) { + console.log('└── ' + response.reason); + } + process.exit(1); + } + console.log('Unknown error:'); + console.log(response); + process.exit(1); +} diff --git a/index.js b/index.js index 69a8907..421a1c6 100644 --- a/index.js +++ b/index.js @@ -725,3 +725,4 @@ validate.doesHashedEmailMatch = doesHashedEmailMatch; validate.VALID_HASHES = VALID_HASHES; validate.validateOldInterdependentFields = validateOldInterdependentFields; validate.validateInterdependentFields = validateInterdependentFields; +validate.validate = validate; \ No newline at end of file From fa7e454cbaf645a892ab1fe3405fd8e9ae85116f Mon Sep 17 00:00:00 2001 From: oren Date: Wed, 6 Jan 2016 11:48:33 -0800 Subject: [PATCH 2/7] Update README with CLI usage --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 9a8fe08..d5b99d0 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,15 @@ and viewed in `cover_html/index.html`. The coverage tool used is [node-cover][], see its documentation for details. + +### Command line tool + +Once installed, you can validate a badge URL at the command line: + +```bash +node bin/openbadges-validate http://hostedbadge.com/ +``` + [jake]: https://github.com/mde/jake [node-cover]: https://github.com/itay/node-cover From 0c92f97301ef51db73612b8dec222f53dc9a4d9b Mon Sep 17 00:00:00 2001 From: oren Date: Thu, 7 Jan 2016 17:56:04 -0800 Subject: [PATCH 3/7] Squashed commit of the following: commit 51543ebc4bbed72f55b2e9e9e8282b0d99c98a40 Author: oren Date: Thu Jan 7 17:55:58 2016 -0800 Handle JSON in validate() commit b9cbc51262bd99485dadda8e4dc476cd7a74dd39 Author: oren Date: Wed Jan 6 12:02:01 2016 -0800 Handle JSON in validate() --- index.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/index.js b/index.js index 421a1c6..9b92527 100644 --- a/index.js +++ b/index.js @@ -362,6 +362,10 @@ function validate(input, callback) { return validate.validateSigned(input, callback); if (isUrl(input)) return validate.validateHostedUrl(input, callback); + if (isJson(input)) + var url = urlFromAssertion(JSON.parse(input)); + if (url) + return validate.validateHostedUrl(url, callback, ''); return callback(makeError('input', 'not a valid signed badge or url', { input: input })); } return callback(makeError('input', 'input must be a string or object', { input: input })); @@ -541,6 +545,19 @@ function getInternalClass(thing) { return Object.prototype.toString.call(thing); } +function isJson (str) { + try { JSON.parse(str); return true } catch(e) { return false } +} + +function urlFromAssertion (json) { + try { + var url = json.verify.url; + if (isUrl(url)) + return url; + return false; + } catch(e) { return false; } +} + const isUrl = regexToValidator(re.url, 'must be a URL'); const isAbsoluteUrl = regexToValidator(re.absoluteUrl, 'must be an absolute URL'); const isEmail = regexToValidator(re.email, 'must be an email address'); From 274ae150c8872878de9ee5d1683936facbfa26bb Mon Sep 17 00:00:00 2001 From: Oren Robinson Date: Tue, 12 Jan 2016 08:59:07 -0800 Subject: [PATCH 4/7] Scaffold validate switch func --- index.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/index.js b/index.js index 9b92527..1af2a03 100644 --- a/index.js +++ b/index.js @@ -94,6 +94,20 @@ function isOldAssertion(assertion) { return true; } +function validateToSpec(specVersion, assertion) { + switch (specVersion) { + case "1.0.0": + return validateBadgeAssertion(assertion); + break; + case "0.5.0": + return validateOldAssertion(assertion); + break; + default: + return makeError("Validation for " + specVersion + " not available."); + break; + } +} + function validateAssertion(assertion, prefix){ if (isOldAssertion(assertion)) return validateOldAssertion(assertion); From 3a0989326efc4e4acf64d875fc200ebd48d24aaa Mon Sep 17 00:00:00 2001 From: oren Date: Thu, 14 Jan 2016 09:28:24 -0800 Subject: [PATCH 5/7] Revert switch --- index.js | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/index.js b/index.js index 1af2a03..9b92527 100644 --- a/index.js +++ b/index.js @@ -94,20 +94,6 @@ function isOldAssertion(assertion) { return true; } -function validateToSpec(specVersion, assertion) { - switch (specVersion) { - case "1.0.0": - return validateBadgeAssertion(assertion); - break; - case "0.5.0": - return validateOldAssertion(assertion); - break; - default: - return makeError("Validation for " + specVersion + " not available."); - break; - } -} - function validateAssertion(assertion, prefix){ if (isOldAssertion(assertion)) return validateOldAssertion(assertion); From f06a0a60622a63aa44398bbc74e1f08b9184089a Mon Sep 17 00:00:00 2001 From: oren Date: Wed, 30 Mar 2016 13:50:09 -0700 Subject: [PATCH 6/7] Add optional vars & CLI usage --- bin/openbadges-validate | 70 +++++++++++++++++++++++++++++++++-------- 1 file changed, 57 insertions(+), 13 deletions(-) diff --git a/bin/openbadges-validate b/bin/openbadges-validate index 321ac01..016d88c 100755 --- a/bin/openbadges-validate +++ b/bin/openbadges-validate @@ -1,28 +1,72 @@ #!/usr/bin/env node var validator = require('../'); -var input = process.argv.slice(2)[0]; +var args = process.argv.slice(2); +var assertion = args[0] || ''; +var specVersion = args[1] || ''; +var verificationType = args[2] || ''; + +function usage() { + console.log("usage: openbadges-validate assertion [spec_version] [verification_type]"); + console.log(''); + console.log(' assertion: URL, JWS signature or JSON representation'); + console.log(''); + console.log(' spec_version: The target specification, allowed values: "0.5.0", "1.0.0", "1.1.0"'); + console.log(' (Omit or use "auto" for auto-detect)'); + console.log(''); + console.log(' verification_type: The target verification type, allowed values: "hosted", "signed"'); + console.log(' (Omit or use "auto" for auto-detect)'); + console.log(''); +} (function main () { - console.log('Validating input: ' + input); - validator.validate(input, handleResponse); + if (assertion.length) { + if (specVersion.length == 0 || specVersion == 'auto') { + specVersion = undefined; + } + else { + if (['1.1.0', '1.0.0', '0.5.0'].indexOf(specVersion) === -1) { + usage(); + process.exit(1); + } + } + if (verificationType.length == 0 || verificationType == 'auto') { + verificationType = undefined; + } + else { + if (['hosted', 'signed'].indexOf(verificationType) === -1) { + usage(); + process.exit(1); + } + } + console.log('assertion: ' + assertion); + console.log('spec_version: ' + (specVersion || '')); + console.log('verification_type: ' + (verificationType || '')); + console.log('--------------------------------'); + validator.validate(assertion, handleResponse, specVersion, verificationType); + } + else { + usage(); + process.exit(1); + } })(); -function handleResponse (response) { - if (response === null) { +function handleResponse (err, data) { + if (err === null) { console.log('Okay'); process.exit(0); } - if (response instanceof Error) { - console.log('Error'); - if (response.name && response.message) { - console.log(response.name + ': ' + response.message); + if (err instanceof Error) { + console.log('Error:'); + if (err.name && err.message) { + console.log(err.name + ': ' + err.message); } - if (response.reason) { - console.log('└── ' + response.reason); + if (err.reason) { + console.log('└── ' + err.reason); } process.exit(1); } - console.log('Unknown error:'); - console.log(response); + console.log('Error details:'); + console.log(err); + console.log(data); process.exit(1); } From 6fdc5539c889571eba29c201c0b86c9edfd93bfd Mon Sep 17 00:00:00 2001 From: oren Date: Wed, 30 Mar 2016 14:22:03 -0700 Subject: [PATCH 7/7] Add usage to README.md --- README.md | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d5b99d0..95d5ede 100644 --- a/README.md +++ b/README.md @@ -113,8 +113,17 @@ The coverage tool used is [node-cover][], see its documentation for details. Once installed, you can validate a badge URL at the command line: -```bash -node bin/openbadges-validate http://hostedbadge.com/ +``` +$ node bin/openbadges-validate +usage: openbadges-validate assertion [spec_version] [verification_type] + + assertion: URL, JWS signature or JSON representation + + spec_version: The target specification, allowed values: "0.5.0", "1.0.0", "1.1.0" + (Omit or use "auto" for auto-detect) + + verification_type: The target verification type, allowed values: "hosted", "signed" + (Omit or use "auto" for auto-detect) ``` [jake]: https://github.com/mde/jake