diff --git a/index.js b/index.js index 69a8907..34f113b 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');