UNPKG

@transmute/jose-ld

Version:
3 lines (2 loc) 18.2 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("web-streams-polyfill/ponyfill"),t=require("jose");function r(e,t,r,n,o,a,i){try{var c=e[a](i),u=c.value}catch(e){return void r(e)}c.done?t(u):Promise.resolve(u).then(n,o)}function n(e){return function(){var t=this,n=arguments;return new Promise((function(o,a){var i=e.apply(t,n);function c(e){r(i,o,a,c,u,"next",e)}function u(e){r(i,o,a,c,u,"throw",e)}c(void 0)}))}}function o(){return(o=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e}).apply(this,arguments)}function a(e,t){if(null==e)return{};var r,n,o={},a=Object.keys(e);for(n=0;n<a.length;n++)t.indexOf(r=a[n])>=0||(o[r]=e[r]);return o}var i,c=(function(e){var t=function(e){var t=Object.prototype,r=t.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},o=n.iterator||"@@iterator",a=n.asyncIterator||"@@asyncIterator",i=n.toStringTag||"@@toStringTag";function c(e,t,r){return Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}),e[t]}try{c({},"")}catch(e){c=function(e,t,r){return e[t]=r}}function u(e,t,r,n){var o=Object.create((t&&t.prototype instanceof p?t:p).prototype),a=new E(n||[]);return o._invoke=function(e,t,r){var n="suspendedStart";return function(o,a){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw a;return{value:void 0,done:!0}}for(r.method=o,r.arg=a;;){var i=r.delegate;if(i){var c=b(i,r);if(c){if(c===f)continue;return c}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===n)throw n="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n="executing";var u=s(e,t,r);if("normal"===u.type){if(n=r.done?"completed":"suspendedYield",u.arg===f)continue;return{value:u.arg,done:r.done}}"throw"===u.type&&(n="completed",r.method="throw",r.arg=u.arg)}}}(e,r,a),o}function s(e,t,r){try{return{type:"normal",arg:e.call(t,r)}}catch(e){return{type:"throw",arg:e}}}e.wrap=u;var f={};function p(){}function h(){}function l(){}var y={};y[o]=function(){return this};var d=Object.getPrototypeOf,v=d&&d(d(j([])));v&&v!==t&&r.call(v,o)&&(y=v);var w=l.prototype=p.prototype=Object.create(y);function m(e){["next","throw","return"].forEach((function(t){c(e,t,(function(e){return this._invoke(t,e)}))}))}function g(e,t){var n;this._invoke=function(o,a){function i(){return new t((function(n,i){!function n(o,a,i,c){var u=s(e[o],e,a);if("throw"!==u.type){var f=u.arg,p=f.value;return p&&"object"==typeof p&&r.call(p,"__await")?t.resolve(p.__await).then((function(e){n("next",e,i,c)}),(function(e){n("throw",e,i,c)})):t.resolve(p).then((function(e){f.value=e,i(f)}),(function(e){return n("throw",e,i,c)}))}c(u.arg)}(o,a,n,i)}))}return n=n?n.then(i,i):i()}}function b(e,t){var r=e.iterator[t.method];if(void 0===r){if(t.delegate=null,"throw"===t.method){if(e.iterator.return&&(t.method="return",t.arg=void 0,b(e,t),"throw"===t.method))return f;t.method="throw",t.arg=new TypeError("The iterator does not provide a 'throw' method")}return f}var n=s(r,e.iterator,t.arg);if("throw"===n.type)return t.method="throw",t.arg=n.arg,t.delegate=null,f;var o=n.arg;return o?o.done?(t[e.resultName]=o.value,t.next=e.nextLoc,"return"!==t.method&&(t.method="next",t.arg=void 0),t.delegate=null,f):o:(t.method="throw",t.arg=new TypeError("iterator result is not an object"),t.delegate=null,f)}function x(e){var t={tryLoc:e[0]};1 in e&&(t.catchLoc=e[1]),2 in e&&(t.finallyLoc=e[2],t.afterLoc=e[3]),this.tryEntries.push(t)}function k(e){var t=e.completion||{};t.type="normal",delete t.arg,e.completion=t}function E(e){this.tryEntries=[{tryLoc:"root"}],e.forEach(x,this),this.reset(!0)}function j(e){if(e){var t=e[o];if(t)return t.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var n=-1,a=function t(){for(;++n<e.length;)if(r.call(e,n))return t.value=e[n],t.done=!1,t;return t.value=void 0,t.done=!0,t};return a.next=a}}return{next:A}}function A(){return{value:void 0,done:!0}}return h.prototype=w.constructor=l,l.constructor=h,h.displayName=c(l,i,"GeneratorFunction"),e.isGeneratorFunction=function(e){var t="function"==typeof e&&e.constructor;return!!t&&(t===h||"GeneratorFunction"===(t.displayName||t.name))},e.mark=function(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,l):(e.__proto__=l,c(e,i,"GeneratorFunction")),e.prototype=Object.create(w),e},e.awrap=function(e){return{__await:e}},m(g.prototype),g.prototype[a]=function(){return this},e.AsyncIterator=g,e.async=function(t,r,n,o,a){void 0===a&&(a=Promise);var i=new g(u(t,r,n,o),a);return e.isGeneratorFunction(r)?i:i.next().then((function(e){return e.done?e.value:i.next()}))},m(w),c(w,i,"Generator"),w[o]=function(){return this},w.toString=function(){return"[object Generator]"},e.keys=function(e){var t=[];for(var r in e)t.push(r);return t.reverse(),function r(){for(;t.length;){var n=t.pop();if(n in e)return r.value=n,r.done=!1,r}return r.done=!0,r}},e.values=j,E.prototype={constructor:E,reset:function(e){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(k),!e)for(var t in this)"t"===t.charAt(0)&&r.call(this,t)&&!isNaN(+t.slice(1))&&(this[t]=void 0)},stop:function(){this.done=!0;var e=this.tryEntries[0].completion;if("throw"===e.type)throw e.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var t=this;function n(r,n){return i.type="throw",i.arg=e,t.next=r,n&&(t.method="next",t.arg=void 0),!!n}for(var o=this.tryEntries.length-1;o>=0;--o){var a=this.tryEntries[o],i=a.completion;if("root"===a.tryLoc)return n("end");if(a.tryLoc<=this.prev){var c=r.call(a,"catchLoc"),u=r.call(a,"finallyLoc");if(c&&u){if(this.prev<a.catchLoc)return n(a.catchLoc,!0);if(this.prev<a.finallyLoc)return n(a.finallyLoc)}else if(c){if(this.prev<a.catchLoc)return n(a.catchLoc,!0)}else{if(!u)throw new Error("try statement without catch or finally");if(this.prev<a.finallyLoc)return n(a.finallyLoc)}}}},abrupt:function(e,t){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&r.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var a=o;break}}a&&("break"===e||"continue"===e)&&a.tryLoc<=t&&t<=a.finallyLoc&&(a=null);var i=a?a.completion:{};return i.type=e,i.arg=t,a?(this.method="next",this.next=a.finallyLoc,f):this.complete(i)},complete:function(e,t){if("throw"===e.type)throw e.arg;return"break"===e.type||"continue"===e.type?this.next=e.arg:"return"===e.type?(this.rval=this.arg=e.arg,this.method="return",this.next="end"):"normal"===e.type&&t&&(this.next=t),f},finish:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r.finallyLoc===e)return this.complete(r.completion,r.afterLoc),k(r),f}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r.tryLoc===e){var n=r.completion;if("throw"===n.type){var o=n.arg;k(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,r){return this.delegate={iterator:j(e),resultName:t,nextLoc:r},"next"===this.method&&(this.arg=void 0),f}},e}(e.exports);try{regeneratorRuntime=t}catch(e){Function("r","regeneratorRuntime = r")(t)}}(i={exports:{}}),i.exports),u=function(e){return function(e){return Buffer.from(e||"").toString("base64")}(e).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/g,"")},s={b64:!1,crit:["b64"]},f={__proto__:null,createSigner:function(e,t,r){return void 0===r&&(r={detached:!1}),{sign:(a=n(c.mark((function n(a){var i,f,p,h,l,y,d;return c.wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return i=a.data,f=o({alg:t},r.header,r.detached?s:void 0),p=u(JSON.stringify(f)),h=u(i instanceof Uint8Array?Buffer.from(i).toString("utf-8"):JSON.stringify(i)),l=r.detached?new Uint8Array(Buffer.concat([Buffer.from(p,"utf8"),Buffer.from(".","utf-8"),i])):new Uint8Array(Buffer.from(p+"."+h)),y=l,n.next=8,e.sign({data:y});case 8:return d=n.sent,n.abrupt("return",r.detached?p+".."+u(Buffer.from(d)):p+"."+h+"."+u(Buffer.from(d)));case 10:case"end":return n.stop()}}),n)}))),function(e){return a.apply(this,arguments)})};var a},createVerifier:function(e,t,r){return void 0===r&&(r={detached:!1}),{verify:(o=n(c.mark((function n(o){var a,i,u,s,f,p,h,l,y;return c.wrap((function(n){for(;;)switch(n.prev=n.next){case 0:if(a=o.data,i=o.signature){n.next=3;break}throw new Error("Signature cannot be empty.");case 3:if(i.split||(i=i.toString()),u=i.split("."),s=u[0],f=u[1],p=u[2],(h=JSON.parse(Buffer.from(s,"base64").toString())).alg===t){n.next=8;break}throw new Error("JWS.header contained unsupported alg: "+h.alg);case 8:if(l=s+"."+f,!r.detached||!h.b64){n.next=11;break}throw new Error("header.b64 must be false for detached jws.");case 11:if(!r.detached||h.crit.includes("b64")){n.next=13;break}throw new Error('header.crit must include "b64" for detached jws.');case 13:if(!r.detached||a){n.next=15;break}throw new Error("cannot verify a detached jws without data.");case 15:return r.detached&&a&&(l=Buffer.concat([Buffer.from(s+".","utf8"),Buffer.from(a.buffer,a.byteOffset,a.length)])),y=e.verify({data:Buffer.from(l),signature:Buffer.from(p,"base64")}),n.abrupt("return",y);case 18:case"end":return n.stop()}}),n)}))),function(e){return o.apply(this,arguments)})};var o}};function p(e){if("string"==typeof e)return new Uint8Array(Buffer.from(e));if(!(e instanceof Uint8Array))throw new TypeError('"data" be a string or Uint8Array.');return e}var h="ECDH-ES+A256KW",l=function(){function e(e){var t=(void 0===e?{}:e).keyAgreementKey;if(!t)throw new TypeError('"keyAgreementKey" is a required parameter.');this.keyAgreementKey=t}var r=e.prototype;return r.transform=function(){var e=n(c.mark((function e(t,r){var n,o,a;return c.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(t&&"object"==typeof t){e.next=2;break}throw new TypeError('"chunk" must be an object.');case 2:return n=t.jwe,e.next=5,this.decrypt(n);case 5:if(null!==(o=e.sent)){e.next=10;break}throw(a=new Error("Invalid decryption key.")).name="DataError",a;case 10:r.enqueue(o);case 11:case"end":return e.stop()}}),e,this)})));return function(t,r){return e.apply(this,arguments)}}(),r.decrypt=function(){var e=n(c.mark((function e(r){var n,o;return c.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r&&"object"==typeof r){e.next=2;break}throw new TypeError('"jwe" must be an object.');case 2:if("string"==typeof r.protected){e.next=4;break}throw new TypeError('"jwe.protected" is missing or not a string.');case 4:if("string"==typeof r.iv){e.next=6;break}throw new Error('Invalid or missing "iv".');case 6:if("string"==typeof r.ciphertext){e.next=8;break}throw new Error('Invalid or missing "ciphertext".');case 8:if("string"==typeof r.tag){e.next=10;break}throw new Error('Invalid or missing "tag".');case 10:e.prev=10,n=JSON.parse(Buffer.from(r.protected,"base64").toString()),e.next=17;break;case 14:throw e.prev=14,e.t0=e.catch(10),new Error('Invalid JWE "protected" header.');case 17:if(n.enc&&"string"==typeof n.enc){e.next=19;break}throw new Error('Invalid JWE "enc" header.');case 19:if("A256GCM"===n.enc){e.next=21;break}throw new Error('Unsupported encryption algorithm "'+n.enc+'".');case 21:if(Array.isArray(r.recipients)){e.next=23;break}throw new TypeError('"jwe.recipients" must be an array.');case 23:return e.next=25,this.keyAgreementKey.export({type:"JsonWebKey2020",privateKey:!0});case 25:return o=e.sent.privateKeyJwk,e.t1=t,e.t2=r,e.next=31,t.importJWK(o,"ECDH-ES+A256KW");case 31:return e.t3=e.sent,e.next=34,e.t1.generalDecrypt.call(e.t1,e.t2,e.t3);case 34:return e.abrupt("return",e.sent.plaintext);case 36:case"end":return e.stop()}}),e,this,[[10,14]])})));return function(t){return e.apply(this,arguments)}}(),e}(),y=function(){function e(e){var t=void 0===e?{}:e,r=t.alg,n=t.enc,o=t.chunkSize,a=void 0===o?1048576:o;this.recipients=t.recipients,this.chunkSize=a,this.offset=0,this.totalOffset=0,this.index=0,this.alg=r||"ECDH-ES+A256KW",this.enc=n||"A256GCM"}var r=e.prototype;return r.start=function(){this.buffer=new Uint8Array(this.chunkSize)},r.transform=function(){var e=n(c.mark((function e(t,r){var n,o,a;return c.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=this.buffer,t instanceof Uint8Array){e.next=3;break}throw new TypeError('"chunk" must be an object.');case 3:if(!t){e.next=11;break}if(t.length<=(o=n.length-this.offset)?(n.set(t,this.offset),this.offset+=t.byteLength,this.totalOffset+=t.byteLength,t=null):(a=new Uint8Array(t.buffer,t.byteOffset,o),t=new Uint8Array(t.buffer,t.byteOffset+o,t.length-o),n.set(a,this.offset),this.offset+=o,this.totalOffset+=o),!t){e.next=9;break}return e.next=9,this.flush(r);case 9:e.next=3;break;case 11:case"end":return e.stop()}}),e,this)})));return function(t,r){return e.apply(this,arguments)}}(),r.flush=function(){var e=n(c.mark((function e(t){var r,n,o;return c.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(0!==this.offset){e.next=2;break}return e.abrupt("return");case 2:return r=this.buffer,n=new Uint8Array(r.buffer,r.byteOffset,this.offset),e.next=6,this.encrypt(n);case 6:o=e.sent,this.offset=0,t.enqueue({index:this.index++,offset:this.totalOffset,jwe:o});case 9:case"end":return e.stop()}}),e,this)})));return function(t){return e.apply(this,arguments)}}(),r.encrypt=function(){var e=n(c.mark((function e(r){var o,a,i;return c.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return o=this.alg,a=this.enc,i=new t.GeneralEncrypt(r),e.next=4,Promise.all(this.recipients.map(function(){var e=n(c.mark((function e(r){return c.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.t0=i,e.next=3,t.importJWK(r.publicKeyJwk,o);case 3:return e.t1=e.sent,e.abrupt("return",e.t0.addRecipient.call(e.t0,e.t1).setUnprotectedHeader({alg:o,kid:r.id}));case 5:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}()));case 4:return i.setProtectedHeader({enc:a}),e.next=7,i.encrypt();case 7:return e.abrupt("return",e.sent);case 9:case"end":return e.stop()}}),e,this)})));return function(t){return e.apply(this,arguments)}}(),e}();exports.JWE={__proto__:null,Cipher:function(){function t(){}var r=t.prototype;return r.createEncryptStream=function(){var t=n(c.mark((function t(r){var n,o,a;return c.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=r.recipients,o=r.publicKeyResolver,a=r.chunkSize,t.next=3,this.createEncryptTransformer({recipients:n,publicKeyResolver:o,chunkSize:a});case 3:return t.abrupt("return",new e.TransformStream(t.sent));case 5:case"end":return t.stop()}}),t,this)})));return function(e){return t.apply(this,arguments)}}(),r.createDecryptStream=function(){var t=n(c.mark((function t(r){var n;return c.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=r.keyAgreementKey,t.next=3,this.createDecryptTransformer({keyAgreementKey:n});case 3:return t.abrupt("return",new e.TransformStream(t.sent));case 5:case"end":return t.stop()}}),t,this)})));return function(e){return t.apply(this,arguments)}}(),r.encrypt=function(){var e=n(c.mark((function e(t){var r,n,o;return c.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.recipients,o=t.publicKeyResolver,(r=t.data)instanceof Uint8Array||"string"==typeof r){e.next=3;break}throw new TypeError('"data" must be a Uint8Array or a string.');case 3:return r&&(r=p(r)),e.next=6,this.createEncryptTransformer({recipients:n,publicKeyResolver:o});case 6:return e.abrupt("return",e.sent.encrypt(r));case 8:case"end":return e.stop()}}),e,this)})));return function(t){return e.apply(this,arguments)}}(),r.encryptObject=function(){var e=n(c.mark((function e(t){var r,n;return c.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=t.obj,n=a(t,["obj"]),"object"==typeof r){e.next=3;break}throw new TypeError('"obj" must be an object.');case 3:return e.abrupt("return",this.encrypt(o({data:JSON.stringify(r)},n)));case 4:case"end":return e.stop()}}),e,this)})));return function(t){return e.apply(this,arguments)}}(),r.decrypt=function(){var e=n(c.mark((function e(t){var r,n;return c.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.jwe,n=t.keyAgreementKey,e.next=3,this.createDecryptTransformer({keyAgreementKey:n});case 3:return e.abrupt("return",e.sent.decrypt(r));case 5:case"end":return e.stop()}}),e,this)})));return function(t){return e.apply(this,arguments)}}(),r.decryptObject=function(){var e=n(c.mark((function e(t){var r,n,o;return c.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.jwe,n=t.keyAgreementKey,e.next=3,this.decrypt({jwe:r,keyAgreementKey:n});case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",JSON.parse(Buffer.from(o).toString()));case 7:case"end":return e.stop()}}),e,this)})));return function(t){return e.apply(this,arguments)}}(),r.createEncryptTransformer=function(){var e=n(c.mark((function e(t){var r,o,a;return c.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=t.recipients,o=t.publicKeyResolver,a=t.chunkSize,Array.isArray(r)&&r.length>0){e.next=3;break}throw new TypeError('"recipients" must be a non-empty array.');case 3:if(r.every((function(e){return e.header&&e.header.alg===h}))){e.next=5;break}throw new Error('All recipients must use the algorithm "'+h+'".');case 5:return e.next=7,Promise.all(r.map(function(){var e=n(c.mark((function e(t,n){return c.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,o(t.header.kid);case 2:r[n]=e.sent;case 4:case"end":return e.stop()}}),e)})));return function(t,r){return e.apply(this,arguments)}}()));case 7:return e.abrupt("return",new y({recipients:r,chunkSize:a}));case 8:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}(),r.createDecryptTransformer=function(){var e=n(c.mark((function e(t){return c.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.abrupt("return",new l({keyAgreementKey:t.keyAgreementKey}));case 2:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}(),t}()},exports.JWS=f; //# sourceMappingURL=jose-ld.cjs.production.min.js.map