Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

did-jwt

Package Overview
Dependencies
Maintainers
6
Versions
144
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

did-jwt - npm Package Compare versions

Comparing version 0.2.1-alpha.2 to 0.2.1-alpha.3

2

lib/index.js

@@ -1,2 +0,2 @@

function r(r){return r&&"object"==typeof r&&"default"in r?r.default:r}var e=require("js-sha256"),t=require("js-sha3"),n=require("elliptic"),o=r(require("tweetnacl")),i=require("@stablelib/utf8"),a=require("buffer"),u=r(require("did-resolver"));function c(r){return a.Buffer.from(e.sha256.arrayBuffer(r))}var f=new n.ec("secp256k1");function s(r,e){return void 0===e&&(e=64),r.length===e?r:"0".repeat(e-r.length)+r}function d(r){return new Uint8Array(Array.prototype.slice.call(Buffer.from(r,"base64"),0))}function l(r){return r&&r.__esModule&&Object.prototype.hasOwnProperty.call(r,"default")?r.default:r}function h(r,e){return r(e={exports:{}},e.exports),e.exports}var p=h(function(r,e){Object.defineProperty(e,"__esModule",{value:!0}),e.default=function(r){var e=r.length,t=e%4;if(!t)return r;var n=e,o=4-t,i=Buffer.alloc(e+o);for(i.write(r);o--;)i.write("=",n++);return i.toString()}});l(p);var v=h(function(r,e){function t(r,e){return void 0===e&&(e="utf8"),Buffer.isBuffer(r)?o(r.toString("base64")):o(Buffer.from(r,e).toString("base64"))}function n(r){return r=r.toString(),p.default(r).replace(/\-/g,"+").replace(/_/g,"/")}function o(r){return r.replace(/=/g,"").replace(/\+/g,"-").replace(/\//g,"_")}Object.defineProperty(e,"__esModule",{value:!0});var i=t;i.encode=t,i.decode=function(r,e){return void 0===e&&(e="utf8"),Buffer.from(n(r),"base64").toString(e)},i.toBase64=n,i.fromBase64=o,i.toBuffer=function(r){return Buffer.from(n(r),"base64")},e.default=i});l(v);var g=h(function(r){r.exports=v.default,r.exports.default=r.exports}),w=new n.ec("secp256k1");function y(r,e){void 0===e&&(e=!1);var t=g.toBuffer(r);if(t.length!==(e?65:64))throw new Error("wrong signature length");var n={r:t.slice(0,32).toString("hex"),s:t.slice(32,64).toString("hex")};return e&&(n.recoveryParam=t[64]),n}var m={ES256K:function(r,e,t){var n=c(r),o=y(e),i=t.find(function(r){return w.keyFromPublic(r.publicKeyHex,"hex").verify(n,o)});if(!i)throw new Error("Signature invalid for JWT");return i},"ES256K-R":function(r,e,n){var o=y(e,!0),i=c(r),u=w.recoverPubKey(i,o,o.recoveryParam),f=u.encode("hex"),s=u.encode("hex",!0),d=function(r){return"0x"+(e=a.Buffer.from(f.slice(2),"hex"),a.Buffer.from(t.keccak_256.arrayBuffer(e))).slice(-20).toString("hex");var e}(),l=n.find(function(r){var e=r.publicKeyHex;return e===f||e===s||r.ethereumAddress===d});if(!l)throw new Error("Signature invalid for JWT");return l},Ed25519:function(r,e,t){var n=i.encode(r),a=d(g.toBase64(e)),u=t.find(function(r){return o.sign.detached.verify(n,a,d(r.publicKeyBase64))});if(!u)throw new Error("Signature invalid for JWT");return u}};function b(r){var e=m[r];if(!e)throw new Error("Unsupported algorithm "+r);return e}function E(r){return"object"==typeof r&&"r"in r&&"s"in r}function S(r){return function(e,t){try{return Promise.resolve(t(e)).then(function(e){if(E(e))return function(t){var n=e.r,o=e.s,i=e.recoveryParam,u=a.Buffer.alloc(r?65:64);if(a.Buffer.from(n,"hex").copy(u,0),a.Buffer.from(o,"hex").copy(u,32),r){if(void 0===i)throw new Error("Signer did not return a recoveryParam");u[64]=i}return g.encode(u)}();throw new Error("expected a signer function that returns a signature object instead of string")})}catch(r){return Promise.reject(r)}}}b.toSignatureObject=y;var x={ES256K:S(),"ES256K-R":S(!0),Ed25519:function(r,e){try{return Promise.resolve(e(r)).then(function(r){if(E(r))throw new Error("expected a signer function that returns a string instead of signature object");return r})}catch(r){return Promise.reject(r)}}},P={ES256K:["Secp256k1VerificationKey2018","Secp256k1SignatureVerificationKey2018","EcdsaPublicKeySecp256k1"],"ES256K-R":["Secp256k1VerificationKey2018","Secp256k1SignatureVerificationKey2018","EcdsaPublicKeySecp256k1"],Ed25519:["ED25519SignatureVerification"]};function B(r){return g.encode(JSON.stringify(r))}function j(r){return r.match(/^[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]+$/)}function k(r){if(r.match(/^did:/))return r;if(j(r))return"did:uport:"+r;throw new Error("Not a valid DID '"+r+"'")}function J(r){if(!r)throw new Error("no JWT passed into decodeJWT");var e=r.match(/^([a-zA-Z0-9_-]+)\.([a-zA-Z0-9_-]+)\.([a-zA-Z0-9_-]+)$/);if(e)return{header:JSON.parse(g.decode(e[1])),payload:JSON.parse(g.decode(e[2])),signature:e[3],data:e[1]+"."+e[2]};throw new Error("Incorrect format JWT")}module.exports={SimpleSigner:function(r){var e=f.keyFromPrivate(r);return function(r){try{var t=e.sign(c(r)),n=t.s,o=t.recoveryParam;return Promise.resolve({r:s(t.r.toString("hex")),s:s(n.toString("hex")),recoveryParam:o})}catch(r){return Promise.reject(r)}}},NaclSigner:function(r){var e=d(r);return function(r){try{var t=i.encode(r),n=o.sign.detached(t,e),u=g.encode(a.Buffer.from(n));return Promise.resolve(u)}catch(r){return Promise.reject(r)}}},verifyJWT:function(r,e){void 0===e&&(e={auth:null,audience:null,callbackUrl:null});try{var t=e.audience?k(e.audience):void 0,n=J(r),o=n.payload,i=n.header,a=n.signature,c=n.data;return Promise.resolve(function(r,e,t){try{var n=P[r];if(!n||0===n.length)throw new Error("No supported signature types for algorithm "+r);var o=k(e);return Promise.resolve(u(o)).then(function(e){if(!e)throw new Error("Unable to resolve DID document for "+o);var i=!t||(e.authentication||[]).map(function(r){return r.publicKey}),a=(e.publicKey||[]).filter(function(r){var e=r.type,o=r.id;return n.find(function(r){return r===e&&(!t||Array.isArray(i)&&i.indexOf(o)>=0)})});if(t&&(!a||0===a.length))throw new Error("DID document for "+o+" does not have public keys suitable for authenticationg user");if(!a||0===a.length)throw new Error("DID document for "+o+" does not have public keys for "+r);return{authenticators:a,issuer:o,doc:e}})}catch(r){return Promise.reject(r)}}(i.alg,o.iss,e.auth)).then(function(n){var u,f=n.doc,s=n.authenticators,d=n.issuer,l=b(i.alg)(c,a,s),h=Math.floor(Date.now()/1e3);if(l){if(o.iat&&o.iat>h+300)throw new Error("JWT not valid yet (issued in the future): iat: "+o.iat+" > now: "+h);if(o.exp&&o.exp<=h-300)throw new Error("JWT has expired: exp: "+o.exp+" < now: "+h);if(o.aud)if((u=o.aud)&&(u.match(/^did:/)||j(u))){if(!t)throw new Error("JWT audience is required but your app address has not been configured");if(t!==k(o.aud))throw new Error("JWT audience does not match your DID: aud: "+o.aud+" !== yours: "+t)}else{if(!e.callbackUrl)throw new Error("JWT audience matching your callback url is required but one wasn't passed in");if(o.aud!==e.callbackUrl)throw new Error("JWT audience does not match the callback url: aud: "+o.aud+" !== url: "+e.callbackUrl)}return{payload:o,doc:f,issuer:d,signer:l,jwt:r}}})}catch(r){return Promise.reject(r)}},createJWT:function(r,e){var t=e.issuer,n=e.signer,o=e.alg,i=e.expiresIn;try{if(!n)throw new Error("No Signer functionality has been configured");if(!t)throw new Error("No issuing DID has been configured");var a={typ:"JWT",alg:o||"ES256K"},u={iat:Math.floor(Date.now()/1e3),exp:void 0};if(i){if("number"!=typeof i)throw new Error("JWT expiresIn is not a number");u.exp=u.iat+Math.floor(i)}var c=[B(a),B(Object.assign({},u,r,{iss:t}))].join("."),f=function(r){var e=x[r];if(!e)throw new Error("Unsupported algorithm "+r);return e}(a.alg);return Promise.resolve(f(c,n)).then(function(r){return[c,r].join(".")})}catch(r){return Promise.reject(r)}},decodeJWT:J};
function r(r){return r&&"object"==typeof r&&"default"in r?r.default:r}var e=require("js-sha256"),t=require("js-sha3"),n=require("elliptic"),i=r(require("tweetnacl")),o=require("@stablelib/utf8"),a=require("buffer"),u=r(require("uport-base64url")),c=r(require("did-resolver"));function f(r){return a.Buffer.from(e.sha256.arrayBuffer(r))}var s=new n.ec("secp256k1");function d(r,e){return void 0===e&&(e=64),r.length===e?r:"0".repeat(e-r.length)+r}function l(r){return new Uint8Array(Array.prototype.slice.call(Buffer.from(r,"base64"),0))}var h=new n.ec("secp256k1");function v(r,e){void 0===e&&(e=!1);var t=u.toBuffer(r);if(t.length!==(e?65:64))throw new Error("wrong signature length");var n={r:t.slice(0,32).toString("hex"),s:t.slice(32,64).toString("hex")};return e&&(n.recoveryParam=t[64]),n}var w={ES256K:function(r,e,t){var n=f(r),i=v(e),o=t.find(function(r){return h.keyFromPublic(r.publicKeyHex,"hex").verify(n,i)});if(!o)throw new Error("Signature invalid for JWT");return o},"ES256K-R":function(r,e,n){var i=v(e,!0),o=f(r),u=h.recoverPubKey(o,i,i.recoveryParam),c=u.encode("hex"),s=u.encode("hex",!0),d=function(r){return"0x"+(e=a.Buffer.from(c.slice(2),"hex"),a.Buffer.from(t.keccak_256.arrayBuffer(e))).slice(-20).toString("hex");var e}(),l=n.find(function(r){var e=r.publicKeyHex;return e===c||e===s||r.ethereumAddress===d});if(!l)throw new Error("Signature invalid for JWT");return l},Ed25519:function(r,e,t){var n=o.encode(r),a=l(u.toBase64(e)),c=t.find(function(r){return i.sign.detached.verify(n,a,l(r.publicKeyBase64))});if(!c)throw new Error("Signature invalid for JWT");return c}};function p(r){var e=w[r];if(!e)throw new Error("Unsupported algorithm "+r);return e}function y(r){return"object"==typeof r&&"r"in r&&"s"in r}function g(r){return function(e,t){try{return Promise.resolve(t(e)).then(function(e){if(y(e))return function(t){var n=e.r,i=e.s,o=e.recoveryParam,c=a.Buffer.alloc(r?65:64);if(a.Buffer.from(n,"hex").copy(c,0),a.Buffer.from(i,"hex").copy(c,32),r){if(void 0===o)throw new Error("Signer did not return a recoveryParam");c[64]=o}return u.encode(c)}();throw new Error("expected a signer function that returns a signature object instead of string")})}catch(r){return Promise.reject(r)}}}p.toSignatureObject=v;var m={ES256K:g(),"ES256K-R":g(!0),Ed25519:function(r,e){try{return Promise.resolve(e(r)).then(function(r){if(y(r))throw new Error("expected a signer function that returns a string instead of signature object");return r})}catch(r){return Promise.reject(r)}}},E={ES256K:["Secp256k1VerificationKey2018","Secp256k1SignatureVerificationKey2018","EcdsaPublicKeySecp256k1"],"ES256K-R":["Secp256k1VerificationKey2018","Secp256k1SignatureVerificationKey2018","EcdsaPublicKeySecp256k1"],Ed25519:["ED25519SignatureVerification"]};function b(r){return u.encode(JSON.stringify(r))}function S(r){return r.match(/^[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]+$/)}function x(r){if(r.match(/^did:/))return r;if(S(r))return"did:uport:"+r;throw new Error("Not a valid DID '"+r+"'")}function P(r){if(!r)throw new Error("no JWT passed into decodeJWT");var e=r.match(/^([a-zA-Z0-9_-]+)\.([a-zA-Z0-9_-]+)\.([a-zA-Z0-9_-]+)$/);if(e)return{header:JSON.parse(u.decode(e[1])),payload:JSON.parse(u.decode(e[2])),signature:e[3],data:e[1]+"."+e[2]};throw new Error("Incorrect format JWT")}module.exports={SimpleSigner:function(r){var e=s.keyFromPrivate(r);return function(r){try{var t=e.sign(f(r)),n=t.s,i=t.recoveryParam;return Promise.resolve({r:d(t.r.toString("hex")),s:d(n.toString("hex")),recoveryParam:i})}catch(r){return Promise.reject(r)}}},NaclSigner:function(r){var e=l(r);return function(r){try{var t=o.encode(r),n=i.sign.detached(t,e),c=u.encode(a.Buffer.from(n));return Promise.resolve(c)}catch(r){return Promise.reject(r)}}},verifyJWT:function(r,e){void 0===e&&(e={auth:null,audience:null,callbackUrl:null});try{var t=e.audience?x(e.audience):void 0,n=P(r),i=n.payload,o=n.header,a=n.signature,u=n.data;return Promise.resolve(function(r,e,t){try{var n=E[r];if(!n||0===n.length)throw new Error("No supported signature types for algorithm "+r);var i=x(e);return Promise.resolve(c(i)).then(function(e){if(!e)throw new Error("Unable to resolve DID document for "+i);var o=!t||(e.authentication||[]).map(function(r){return r.publicKey}),a=(e.publicKey||[]).filter(function(r){var e=r.type,i=r.id;return n.find(function(r){return r===e&&(!t||Array.isArray(o)&&o.indexOf(i)>=0)})});if(t&&(!a||0===a.length))throw new Error("DID document for "+i+" does not have public keys suitable for authenticationg user");if(!a||0===a.length)throw new Error("DID document for "+i+" does not have public keys for "+r);return{authenticators:a,issuer:i,doc:e}})}catch(r){return Promise.reject(r)}}(o.alg,i.iss,e.auth)).then(function(n){var c,f=n.doc,s=n.authenticators,d=n.issuer,l=p(o.alg)(u,a,s),h=Math.floor(Date.now()/1e3);if(l){if(i.iat&&i.iat>h+300)throw new Error("JWT not valid yet (issued in the future): iat: "+i.iat+" > now: "+h);if(i.exp&&i.exp<=h-300)throw new Error("JWT has expired: exp: "+i.exp+" < now: "+h);if(i.aud)if((c=i.aud)&&(c.match(/^did:/)||S(c))){if(!t)throw new Error("JWT audience is required but your app address has not been configured");if(t!==x(i.aud))throw new Error("JWT audience does not match your DID: aud: "+i.aud+" !== yours: "+t)}else{if(!e.callbackUrl)throw new Error("JWT audience matching your callback url is required but one wasn't passed in");if(i.aud!==e.callbackUrl)throw new Error("JWT audience does not match the callback url: aud: "+i.aud+" !== url: "+e.callbackUrl)}return{payload:i,doc:f,issuer:d,signer:l,jwt:r}}})}catch(r){return Promise.reject(r)}},createJWT:function(r,e){var t=e.issuer,n=e.signer,i=e.alg,o=e.expiresIn;try{if(!n)throw new Error("No Signer functionality has been configured");if(!t)throw new Error("No issuing DID has been configured");var a={typ:"JWT",alg:i||"ES256K"},u={iat:Math.floor(Date.now()/1e3),exp:void 0};if(o){if("number"!=typeof o)throw new Error("JWT expiresIn is not a number");u.exp=u.iat+Math.floor(o)}var c=[b(a),b(Object.assign({},u,r,{iss:t}))].join("."),f=function(r){var e=m[r];if(!e)throw new Error("Unsupported algorithm "+r);return e}(a.alg);return Promise.resolve(f(c,n)).then(function(r){return[c,r].join(".")})}catch(r){return Promise.reject(r)}},decodeJWT:P};
//# sourceMappingURL=index.js.map

@@ -1,2 +0,2 @@

!function(r,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("js-sha256"),require("js-sha3"),require("elliptic"),require("tweetnacl"),require("@stablelib/utf8"),require("buffer"),require("did-resolver")):"function"==typeof define&&define.amd?define(["js-sha256","js-sha3","elliptic","tweetnacl","@stablelib/utf8","buffer","did-resolver"],e):r.didJwt=e(r.jsSha256,r.jsSha3,r.elliptic,r.tweetnacl,r.utf8,r.buffer,r.resolve)}(this,function(r,e,t,n,o,i,a){function u(e){return i.Buffer.from(r.sha256.arrayBuffer(e))}n=n&&n.hasOwnProperty("default")?n.default:n,a=a&&a.hasOwnProperty("default")?a.default:a;var c=new t.ec("secp256k1");function f(r,e){return void 0===e&&(e=64),r.length===e?r:"0".repeat(e-r.length)+r}function s(r){return new Uint8Array(Array.prototype.slice.call(Buffer.from(r,"base64"),0))}function d(r){return r&&r.__esModule&&Object.prototype.hasOwnProperty.call(r,"default")?r.default:r}function l(r,e){return r(e={exports:{}},e.exports),e.exports}var h=l(function(r,e){Object.defineProperty(e,"__esModule",{value:!0}),e.default=function(r){var e=r.length,t=e%4;if(!t)return r;var n=e,o=4-t,i=Buffer.alloc(e+o);for(i.write(r);o--;)i.write("=",n++);return i.toString()}});d(h);var p=l(function(r,e){function t(r,e){return void 0===e&&(e="utf8"),Buffer.isBuffer(r)?o(r.toString("base64")):o(Buffer.from(r,e).toString("base64"))}function n(r){return r=r.toString(),h.default(r).replace(/\-/g,"+").replace(/_/g,"/")}function o(r){return r.replace(/=/g,"").replace(/\+/g,"-").replace(/\//g,"_")}Object.defineProperty(e,"__esModule",{value:!0});var i=t;i.encode=t,i.decode=function(r,e){return void 0===e&&(e="utf8"),Buffer.from(n(r),"base64").toString(e)},i.toBase64=n,i.fromBase64=o,i.toBuffer=function(r){return Buffer.from(n(r),"base64")},e.default=i});d(p);var v=l(function(r){r.exports=p.default,r.exports.default=r.exports}),w=new t.ec("secp256k1");function g(r,e){void 0===e&&(e=!1);var t=v.toBuffer(r);if(t.length!==(e?65:64))throw new Error("wrong signature length");var n={r:t.slice(0,32).toString("hex"),s:t.slice(32,64).toString("hex")};return e&&(n.recoveryParam=t[64]),n}var y={ES256K:function(r,e,t){var n=u(r),o=g(e),i=t.find(function(r){return w.keyFromPublic(r.publicKeyHex,"hex").verify(n,o)});if(!i)throw new Error("Signature invalid for JWT");return i},"ES256K-R":function(r,t,n){var o=g(t,!0),a=u(r),c=w.recoverPubKey(a,o,o.recoveryParam),f=c.encode("hex"),s=c.encode("hex",!0),d=function(r){return"0x"+(t=i.Buffer.from(f.slice(2),"hex"),i.Buffer.from(e.keccak_256.arrayBuffer(t))).slice(-20).toString("hex");var t}(),l=n.find(function(r){var e=r.publicKeyHex;return e===f||e===s||r.ethereumAddress===d});if(!l)throw new Error("Signature invalid for JWT");return l},Ed25519:function(r,e,t){var i=o.encode(r),a=s(v.toBase64(e)),u=t.find(function(r){return n.sign.detached.verify(i,a,s(r.publicKeyBase64))});if(!u)throw new Error("Signature invalid for JWT");return u}};function m(r){var e=y[r];if(!e)throw new Error("Unsupported algorithm "+r);return e}function b(r){return"object"==typeof r&&"r"in r&&"s"in r}function S(r){return function(e,t){try{return Promise.resolve(t(e)).then(function(e){if(b(e))return function(t){var n=e.r,o=e.s,a=e.recoveryParam,u=i.Buffer.alloc(r?65:64);if(i.Buffer.from(n,"hex").copy(u,0),i.Buffer.from(o,"hex").copy(u,32),r){if(void 0===a)throw new Error("Signer did not return a recoveryParam");u[64]=a}return v.encode(u)}();throw new Error("expected a signer function that returns a signature object instead of string")})}catch(r){return Promise.reject(r)}}}m.toSignatureObject=g;var E={ES256K:S(),"ES256K-R":S(!0),Ed25519:function(r,e){try{return Promise.resolve(e(r)).then(function(r){if(b(r))throw new Error("expected a signer function that returns a string instead of signature object");return r})}catch(r){return Promise.reject(r)}}},x={ES256K:["Secp256k1VerificationKey2018","Secp256k1SignatureVerificationKey2018","EcdsaPublicKeySecp256k1"],"ES256K-R":["Secp256k1VerificationKey2018","Secp256k1SignatureVerificationKey2018","EcdsaPublicKeySecp256k1"],Ed25519:["ED25519SignatureVerification"]};function P(r){return v.encode(JSON.stringify(r))}function j(r){return r.match(/^[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]+$/)}function B(r){if(r.match(/^did:/))return r;if(j(r))return"did:uport:"+r;throw new Error("Not a valid DID '"+r+"'")}function J(r){if(!r)throw new Error("no JWT passed into decodeJWT");var e=r.match(/^([a-zA-Z0-9_-]+)\.([a-zA-Z0-9_-]+)\.([a-zA-Z0-9_-]+)$/);if(e)return{header:JSON.parse(v.decode(e[1])),payload:JSON.parse(v.decode(e[2])),signature:e[3],data:e[1]+"."+e[2]};throw new Error("Incorrect format JWT")}return{SimpleSigner:function(r){var e=c.keyFromPrivate(r);return function(r){try{var t=e.sign(u(r)),n=t.s,o=t.recoveryParam;return Promise.resolve({r:f(t.r.toString("hex")),s:f(n.toString("hex")),recoveryParam:o})}catch(r){return Promise.reject(r)}}},NaclSigner:function(r){var e=s(r);return function(r){try{var t=o.encode(r),a=n.sign.detached(t,e),u=v.encode(i.Buffer.from(a));return Promise.resolve(u)}catch(r){return Promise.reject(r)}}},verifyJWT:function(r,e){void 0===e&&(e={auth:null,audience:null,callbackUrl:null});try{var t=e.audience?B(e.audience):void 0,n=J(r),o=n.payload,i=n.header,u=n.signature,c=n.data;return Promise.resolve(function(r,e,t){try{var n=x[r];if(!n||0===n.length)throw new Error("No supported signature types for algorithm "+r);var o=B(e);return Promise.resolve(a(o)).then(function(e){if(!e)throw new Error("Unable to resolve DID document for "+o);var i=!t||(e.authentication||[]).map(function(r){return r.publicKey}),a=(e.publicKey||[]).filter(function(r){var e=r.type,o=r.id;return n.find(function(r){return r===e&&(!t||Array.isArray(i)&&i.indexOf(o)>=0)})});if(t&&(!a||0===a.length))throw new Error("DID document for "+o+" does not have public keys suitable for authenticationg user");if(!a||0===a.length)throw new Error("DID document for "+o+" does not have public keys for "+r);return{authenticators:a,issuer:o,doc:e}})}catch(r){return Promise.reject(r)}}(i.alg,o.iss,e.auth)).then(function(n){var a,f=n.doc,s=n.authenticators,d=n.issuer,l=m(i.alg)(c,u,s),h=Math.floor(Date.now()/1e3);if(l){if(o.iat&&o.iat>h+300)throw new Error("JWT not valid yet (issued in the future): iat: "+o.iat+" > now: "+h);if(o.exp&&o.exp<=h-300)throw new Error("JWT has expired: exp: "+o.exp+" < now: "+h);if(o.aud)if((a=o.aud)&&(a.match(/^did:/)||j(a))){if(!t)throw new Error("JWT audience is required but your app address has not been configured");if(t!==B(o.aud))throw new Error("JWT audience does not match your DID: aud: "+o.aud+" !== yours: "+t)}else{if(!e.callbackUrl)throw new Error("JWT audience matching your callback url is required but one wasn't passed in");if(o.aud!==e.callbackUrl)throw new Error("JWT audience does not match the callback url: aud: "+o.aud+" !== url: "+e.callbackUrl)}return{payload:o,doc:f,issuer:d,signer:l,jwt:r}}})}catch(r){return Promise.reject(r)}},createJWT:function(r,e){var t=e.issuer,n=e.signer,o=e.alg,i=e.expiresIn;try{if(!n)throw new Error("No Signer functionality has been configured");if(!t)throw new Error("No issuing DID has been configured");var a={typ:"JWT",alg:o||"ES256K"},u={iat:Math.floor(Date.now()/1e3),exp:void 0};if(i){if("number"!=typeof i)throw new Error("JWT expiresIn is not a number");u.exp=u.iat+Math.floor(i)}var c=[P(a),P(Object.assign({},u,r,{iss:t}))].join("."),f=function(r){var e=E[r];if(!e)throw new Error("Unsupported algorithm "+r);return e}(a.alg);return Promise.resolve(f(c,n)).then(function(r){return[c,r].join(".")})}catch(r){return Promise.reject(r)}},decodeJWT:J}});
!function(r,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("js-sha256"),require("js-sha3"),require("elliptic"),require("tweetnacl"),require("@stablelib/utf8"),require("buffer"),require("uport-base64url"),require("did-resolver")):"function"==typeof define&&define.amd?define(["js-sha256","js-sha3","elliptic","tweetnacl","@stablelib/utf8","buffer","uport-base64url","did-resolver"],e):r.didJwt=e(r.jsSha256,r.jsSha3,r.elliptic,r.tweetnacl,r.utf8,r.buffer,r.base64url,r.resolve)}(this,function(r,e,t,n,i,o,a,u){function c(e){return o.Buffer.from(r.sha256.arrayBuffer(e))}n=n&&n.hasOwnProperty("default")?n.default:n,a=a&&a.hasOwnProperty("default")?a.default:a,u=u&&u.hasOwnProperty("default")?u.default:u;var f=new t.ec("secp256k1");function s(r,e){return void 0===e&&(e=64),r.length===e?r:"0".repeat(e-r.length)+r}function d(r){return new Uint8Array(Array.prototype.slice.call(Buffer.from(r,"base64"),0))}var l=new t.ec("secp256k1");function h(r,e){void 0===e&&(e=!1);var t=a.toBuffer(r);if(t.length!==(e?65:64))throw new Error("wrong signature length");var n={r:t.slice(0,32).toString("hex"),s:t.slice(32,64).toString("hex")};return e&&(n.recoveryParam=t[64]),n}var p={ES256K:function(r,e,t){var n=c(r),i=h(e),o=t.find(function(r){return l.keyFromPublic(r.publicKeyHex,"hex").verify(n,i)});if(!o)throw new Error("Signature invalid for JWT");return o},"ES256K-R":function(r,t,n){var i=h(t,!0),a=c(r),u=l.recoverPubKey(a,i,i.recoveryParam),f=u.encode("hex"),s=u.encode("hex",!0),d=function(r){return"0x"+(t=o.Buffer.from(f.slice(2),"hex"),o.Buffer.from(e.keccak_256.arrayBuffer(t))).slice(-20).toString("hex");var t}(),p=n.find(function(r){var e=r.publicKeyHex;return e===f||e===s||r.ethereumAddress===d});if(!p)throw new Error("Signature invalid for JWT");return p},Ed25519:function(r,e,t){var o=i.encode(r),u=d(a.toBase64(e)),c=t.find(function(r){return n.sign.detached.verify(o,u,d(r.publicKeyBase64))});if(!c)throw new Error("Signature invalid for JWT");return c}};function w(r){var e=p[r];if(!e)throw new Error("Unsupported algorithm "+r);return e}function v(r){return"object"==typeof r&&"r"in r&&"s"in r}function y(r){return function(e,t){try{return Promise.resolve(t(e)).then(function(e){if(v(e))return function(t){var n=e.r,i=e.s,u=e.recoveryParam,c=o.Buffer.alloc(r?65:64);if(o.Buffer.from(n,"hex").copy(c,0),o.Buffer.from(i,"hex").copy(c,32),r){if(void 0===u)throw new Error("Signer did not return a recoveryParam");c[64]=u}return a.encode(c)}();throw new Error("expected a signer function that returns a signature object instead of string")})}catch(r){return Promise.reject(r)}}}w.toSignatureObject=h;var g={ES256K:y(),"ES256K-R":y(!0),Ed25519:function(r,e){try{return Promise.resolve(e(r)).then(function(r){if(v(r))throw new Error("expected a signer function that returns a string instead of signature object");return r})}catch(r){return Promise.reject(r)}}},m={ES256K:["Secp256k1VerificationKey2018","Secp256k1SignatureVerificationKey2018","EcdsaPublicKeySecp256k1"],"ES256K-R":["Secp256k1VerificationKey2018","Secp256k1SignatureVerificationKey2018","EcdsaPublicKeySecp256k1"],Ed25519:["ED25519SignatureVerification"]};function b(r){return a.encode(JSON.stringify(r))}function E(r){return r.match(/^[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]+$/)}function S(r){if(r.match(/^did:/))return r;if(E(r))return"did:uport:"+r;throw new Error("Not a valid DID '"+r+"'")}function x(r){if(!r)throw new Error("no JWT passed into decodeJWT");var e=r.match(/^([a-zA-Z0-9_-]+)\.([a-zA-Z0-9_-]+)\.([a-zA-Z0-9_-]+)$/);if(e)return{header:JSON.parse(a.decode(e[1])),payload:JSON.parse(a.decode(e[2])),signature:e[3],data:e[1]+"."+e[2]};throw new Error("Incorrect format JWT")}return{SimpleSigner:function(r){var e=f.keyFromPrivate(r);return function(r){try{var t=e.sign(c(r)),n=t.s,i=t.recoveryParam;return Promise.resolve({r:s(t.r.toString("hex")),s:s(n.toString("hex")),recoveryParam:i})}catch(r){return Promise.reject(r)}}},NaclSigner:function(r){var e=d(r);return function(r){try{var t=i.encode(r),u=n.sign.detached(t,e),c=a.encode(o.Buffer.from(u));return Promise.resolve(c)}catch(r){return Promise.reject(r)}}},verifyJWT:function(r,e){void 0===e&&(e={auth:null,audience:null,callbackUrl:null});try{var t=e.audience?S(e.audience):void 0,n=x(r),i=n.payload,o=n.header,a=n.signature,c=n.data;return Promise.resolve(function(r,e,t){try{var n=m[r];if(!n||0===n.length)throw new Error("No supported signature types for algorithm "+r);var i=S(e);return Promise.resolve(u(i)).then(function(e){if(!e)throw new Error("Unable to resolve DID document for "+i);var o=!t||(e.authentication||[]).map(function(r){return r.publicKey}),a=(e.publicKey||[]).filter(function(r){var e=r.type,i=r.id;return n.find(function(r){return r===e&&(!t||Array.isArray(o)&&o.indexOf(i)>=0)})});if(t&&(!a||0===a.length))throw new Error("DID document for "+i+" does not have public keys suitable for authenticationg user");if(!a||0===a.length)throw new Error("DID document for "+i+" does not have public keys for "+r);return{authenticators:a,issuer:i,doc:e}})}catch(r){return Promise.reject(r)}}(o.alg,i.iss,e.auth)).then(function(n){var u,f=n.doc,s=n.authenticators,d=n.issuer,l=w(o.alg)(c,a,s),h=Math.floor(Date.now()/1e3);if(l){if(i.iat&&i.iat>h+300)throw new Error("JWT not valid yet (issued in the future): iat: "+i.iat+" > now: "+h);if(i.exp&&i.exp<=h-300)throw new Error("JWT has expired: exp: "+i.exp+" < now: "+h);if(i.aud)if((u=i.aud)&&(u.match(/^did:/)||E(u))){if(!t)throw new Error("JWT audience is required but your app address has not been configured");if(t!==S(i.aud))throw new Error("JWT audience does not match your DID: aud: "+i.aud+" !== yours: "+t)}else{if(!e.callbackUrl)throw new Error("JWT audience matching your callback url is required but one wasn't passed in");if(i.aud!==e.callbackUrl)throw new Error("JWT audience does not match the callback url: aud: "+i.aud+" !== url: "+e.callbackUrl)}return{payload:i,doc:f,issuer:d,signer:l,jwt:r}}})}catch(r){return Promise.reject(r)}},createJWT:function(r,e){var t=e.issuer,n=e.signer,i=e.alg,o=e.expiresIn;try{if(!n)throw new Error("No Signer functionality has been configured");if(!t)throw new Error("No issuing DID has been configured");var a={typ:"JWT",alg:i||"ES256K"},u={iat:Math.floor(Date.now()/1e3),exp:void 0};if(o){if("number"!=typeof o)throw new Error("JWT expiresIn is not a number");u.exp=u.iat+Math.floor(o)}var c=[b(a),b(Object.assign({},u,r,{iss:t}))].join("."),f=function(r){var e=g[r];if(!e)throw new Error("Unsupported algorithm "+r);return e}(a.alg);return Promise.resolve(f(c,n)).then(function(r){return[c,r].join(".")})}catch(r){return Promise.reject(r)}},decodeJWT:x}});
//# sourceMappingURL=index.umd.js.map
{
"name": "did-jwt",
"version": "0.2.1-alpha.2",
"version": "0.2.1-alpha.3",
"description": "Library for Signing and Verifying JWTs compatible uPort and DID standards",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

@@ -5,3 +5,3 @@ import SignerAlgorithm from '../SignerAlgorithm'

import NaclSigner from '../NaclSigner'
import base64url from 'base64url'
import base64url from 'uport-base64url'
import { ec as EC } from 'elliptic'

@@ -8,0 +8,0 @@ import nacl from 'tweetnacl'

import VerifierAlgorithm from './VerifierAlgorithm'
import SignerAlgorithm from './SignerAlgorithm'
import base64url from 'base64url'
import base64url from 'uport-base64url'
import resolve, { DIDDocument, PublicKey } from 'did-resolver'

@@ -5,0 +5,0 @@

@@ -6,3 +6,3 @@ import nacl from 'tweetnacl'

import { base64ToBytes } from './util'
import base64url from 'base64url'
import base64url from 'uport-base64url'

@@ -9,0 +9,0 @@ /**

@@ -1,2 +0,2 @@

import base64url from 'base64url'
import base64url from 'uport-base64url'
import { Buffer } from 'buffer'

@@ -3,0 +3,0 @@ import { Signer, EcdsaSignature, SignerAlgorithm } from './JWT'

import { ec as EC } from 'elliptic'
import { sha256, toEthereumAddress } from './Digest'
import base64url from 'base64url'
import base64url from 'uport-base64url'
import nacl from 'tweetnacl'

@@ -5,0 +5,0 @@ import { EcdsaSignature } from './JWT'

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc