UNPKG

@kinde-oss/kinde-auth-pkce-js

Version:

Kinde PKCE authentication for SPAs

8 lines (7 loc) 37.5 kB
"use strict";const kr="4.3.0-2",tr="pkce-code-verifier";var lr=(u=>(u.s="string",u.i="integer",u.b="boolean",u))(lr||{}),P=(u=>(u.token_bundle="kinde_token",u.access_token="kinde_access_token",u.id_token="kinde_id_token",u.user="user",u.refresh_token="kinde_refresh_token",u))(P||{});class nr extends Error{}nr.prototype.name="InvalidTokenError";function jr(u){return decodeURIComponent(atob(u).replace(/(.)/g,(a,f)=>{let p=f.charCodeAt(0).toString(16).toUpperCase();return p.length<2&&(p="0"+p),"%"+p}))}function Jr(u){let a=u.replace(/-/g,"+").replace(/_/g,"/");switch(a.length%4){case 0:break;case 2:a+="==";break;case 3:a+="=";break;default:throw new Error("base64 string is not of the correct length")}try{return jr(a)}catch{return atob(a)}}function hr(u,a){if(typeof u!="string")throw new nr("Invalid token specified: must be a string");a||(a={});const f=a.header===!0?0:1,p=u.split(".")[f];if(typeof p!="string")throw new nr(`Invalid token specified: missing part #${f+1}`);let d;try{d=Jr(p)}catch(y){throw new nr(`Invalid token specified: invalid base64 for part #${f+1} (${y.message})`)}try{return JSON.parse(d)}catch(y){throw new nr(`Invalid token specified: invalid json for part #${f+1} (${y.message})`)}}const qr=u=>{var a;return(a=document.cookie.split("; ").find(f=>f.split("=")[0]===u))==null?void 0:a.split("=")[1]};var Fr={},er={},Sr;function Kr(){if(Sr)return er;Sr=1,er.byteLength=o,er.toByteArray=N,er.fromByteArray=K;for(var u=[],a=[],f=typeof Uint8Array<"u"?Uint8Array:Array,p="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",d=0,y=p.length;d<y;++d)u[d]=p[d],a[p.charCodeAt(d)]=d;a[45]=62,a[95]=63;function w(l){var g=l.length;if(g%4>0)throw new Error("Invalid string. Length must be a multiple of 4");var E=l.indexOf("=");E===-1&&(E=g);var A=E===g?0:4-E%4;return[E,A]}function o(l){var g=w(l),E=g[0],A=g[1];return(E+A)*3/4-A}function D(l,g,E){return(g+E)*3/4-E}function N(l){var g,E=w(l),A=E[0],B=E[1],k=new f(D(l,A,B)),b=0,V=B>0?A-4:A,U;for(U=0;U<V;U+=4)g=a[l.charCodeAt(U)]<<18|a[l.charCodeAt(U+1)]<<12|a[l.charCodeAt(U+2)]<<6|a[l.charCodeAt(U+3)],k[b++]=g>>16&255,k[b++]=g>>8&255,k[b++]=g&255;return B===2&&(g=a[l.charCodeAt(U)]<<2|a[l.charCodeAt(U+1)]>>4,k[b++]=g&255),B===1&&(g=a[l.charCodeAt(U)]<<10|a[l.charCodeAt(U+1)]<<4|a[l.charCodeAt(U+2)]>>2,k[b++]=g>>8&255,k[b++]=g&255),k}function R(l){return u[l>>18&63]+u[l>>12&63]+u[l>>6&63]+u[l&63]}function C(l,g,E){for(var A,B=[],k=g;k<E;k+=3)A=(l[k]<<16&16711680)+(l[k+1]<<8&65280)+(l[k+2]&255),B.push(R(A));return B.join("")}function K(l){for(var g,E=l.length,A=E%3,B=[],k=16383,b=0,V=E-A;b<V;b+=k)B.push(C(l,b,b+k>V?V:b+k));return A===1?(g=l[E-1],B.push(u[g>>2]+u[g<<4&63]+"==")):A===2&&(g=(l[E-2]<<8)+l[E-1],B.push(u[g>>10]+u[g>>4&63]+u[g<<2&63]+"=")),B.join("")}return er}var fr={};/*! ieee754. BSD-3-Clause License. Feross Aboukhadijeh <https://feross.org/opensource> */var Tr;function Vr(){return Tr||(Tr=1,fr.read=function(u,a,f,p,d){var y,w,o=d*8-p-1,D=(1<<o)-1,N=D>>1,R=-7,C=f?d-1:0,K=f?-1:1,l=u[a+C];for(C+=K,y=l&(1<<-R)-1,l>>=-R,R+=o;R>0;y=y*256+u[a+C],C+=K,R-=8);for(w=y&(1<<-R)-1,y>>=-R,R+=p;R>0;w=w*256+u[a+C],C+=K,R-=8);if(y===0)y=1-N;else{if(y===D)return w?NaN:(l?-1:1)*(1/0);w=w+Math.pow(2,p),y=y-N}return(l?-1:1)*w*Math.pow(2,y-p)},fr.write=function(u,a,f,p,d,y){var w,o,D,N=y*8-d-1,R=(1<<N)-1,C=R>>1,K=d===23?Math.pow(2,-24)-Math.pow(2,-77):0,l=p?0:y-1,g=p?1:-1,E=a<0||a===0&&1/a<0?1:0;for(a=Math.abs(a),isNaN(a)||a===1/0?(o=isNaN(a)?1:0,w=R):(w=Math.floor(Math.log(a)/Math.LN2),a*(D=Math.pow(2,-w))<1&&(w--,D*=2),w+C>=1?a+=K/D:a+=K*Math.pow(2,1-C),a*D>=2&&(w++,D/=2),w+C>=R?(o=0,w=R):w+C>=1?(o=(a*D-1)*Math.pow(2,d),w=w+C):(o=a*Math.pow(2,C-1)*Math.pow(2,d),w=0));d>=8;u[f+l]=o&255,l+=g,o/=256,d-=8);for(w=w<<d|o,N+=d;N>0;u[f+l]=w&255,l+=g,w/=256,N-=8);u[f+l-g]|=E*128}),fr}/*! * The buffer module from node.js, for the browser. * * @author Feross Aboukhadijeh <https://feross.org> * @license MIT */var Rr;function Gr(){return Rr||(Rr=1,function(u){const a=Kr(),f=Vr(),p=typeof Symbol=="function"&&typeof Symbol.for=="function"?Symbol.for("nodejs.util.inspect.custom"):null;u.Buffer=o,u.SlowBuffer=k,u.INSPECT_MAX_BYTES=50;const d=2147483647;u.kMaxLength=d,o.TYPED_ARRAY_SUPPORT=y(),!o.TYPED_ARRAY_SUPPORT&&typeof console<"u"&&typeof console.error=="function"&&console.error("This browser lacks typed array (Uint8Array) support which is required by `buffer` v5.x. Use `buffer` v4.x if you require old browser support.");function y(){try{const t=new Uint8Array(1),r={foo:function(){return 42}};return Object.setPrototypeOf(r,Uint8Array.prototype),Object.setPrototypeOf(t,r),t.foo()===42}catch{return!1}}Object.defineProperty(o.prototype,"parent",{enumerable:!0,get:function(){if(o.isBuffer(this))return this.buffer}}),Object.defineProperty(o.prototype,"offset",{enumerable:!0,get:function(){if(o.isBuffer(this))return this.byteOffset}});function w(t){if(t>d)throw new RangeError('The value "'+t+'" is invalid for option "size"');const r=new Uint8Array(t);return Object.setPrototypeOf(r,o.prototype),r}function o(t,r,e){if(typeof t=="number"){if(typeof r=="string")throw new TypeError('The "string" argument must be of type string. Received type number');return C(t)}return D(t,r,e)}o.poolSize=8192;function D(t,r,e){if(typeof t=="string")return K(t,r);if(ArrayBuffer.isView(t))return g(t);if(t==null)throw new TypeError("The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type "+typeof t);if(G(t,ArrayBuffer)||t&&G(t.buffer,ArrayBuffer)||typeof SharedArrayBuffer<"u"&&(G(t,SharedArrayBuffer)||t&&G(t.buffer,SharedArrayBuffer)))return E(t,r,e);if(typeof t=="number")throw new TypeError('The "value" argument must not be of type number. Received type number');const n=t.valueOf&&t.valueOf();if(n!=null&&n!==t)return o.from(n,r,e);const i=A(t);if(i)return i;if(typeof Symbol<"u"&&Symbol.toPrimitive!=null&&typeof t[Symbol.toPrimitive]=="function")return o.from(t[Symbol.toPrimitive]("string"),r,e);throw new TypeError("The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type "+typeof t)}o.from=function(t,r,e){return D(t,r,e)},Object.setPrototypeOf(o.prototype,Uint8Array.prototype),Object.setPrototypeOf(o,Uint8Array);function N(t){if(typeof t!="number")throw new TypeError('"size" argument must be of type number');if(t<0)throw new RangeError('The value "'+t+'" is invalid for option "size"')}function R(t,r,e){return N(t),t<=0?w(t):r!==void 0?typeof e=="string"?w(t).fill(r,e):w(t).fill(r):w(t)}o.alloc=function(t,r,e){return R(t,r,e)};function C(t){return N(t),w(t<0?0:B(t)|0)}o.allocUnsafe=function(t){return C(t)},o.allocUnsafeSlow=function(t){return C(t)};function K(t,r){if((typeof r!="string"||r==="")&&(r="utf8"),!o.isEncoding(r))throw new TypeError("Unknown encoding: "+r);const e=b(t,r)|0;let n=w(e);const i=n.write(t,r);return i!==e&&(n=n.slice(0,i)),n}function l(t){const r=t.length<0?0:B(t.length)|0,e=w(r);for(let n=0;n<r;n+=1)e[n]=t[n]&255;return e}function g(t){if(G(t,Uint8Array)){const r=new Uint8Array(t);return E(r.buffer,r.byteOffset,r.byteLength)}return l(t)}function E(t,r,e){if(r<0||t.byteLength<r)throw new RangeError('"offset" is outside of buffer bounds');if(t.byteLength<r+(e||0))throw new RangeError('"length" is outside of buffer bounds');let n;return r===void 0&&e===void 0?n=new Uint8Array(t):e===void 0?n=new Uint8Array(t,r):n=new Uint8Array(t,r,e),Object.setPrototypeOf(n,o.prototype),n}function A(t){if(o.isBuffer(t)){const r=B(t.length)|0,e=w(r);return e.length===0||t.copy(e,0,0,r),e}if(t.length!==void 0)return typeof t.length!="number"||_r(t.length)?w(0):l(t);if(t.type==="Buffer"&&Array.isArray(t.data))return l(t.data)}function B(t){if(t>=d)throw new RangeError("Attempt to allocate Buffer larger than maximum size: 0x"+d.toString(16)+" bytes");return t|0}function k(t){return+t!=t&&(t=0),o.alloc(+t)}o.isBuffer=function(r){return r!=null&&r._isBuffer===!0&&r!==o.prototype},o.compare=function(r,e){if(G(r,Uint8Array)&&(r=o.from(r,r.offset,r.byteLength)),G(e,Uint8Array)&&(e=o.from(e,e.offset,e.byteLength)),!o.isBuffer(r)||!o.isBuffer(e))throw new TypeError('The "buf1", "buf2" arguments must be one of type Buffer or Uint8Array');if(r===e)return 0;let n=r.length,i=e.length;for(let s=0,c=Math.min(n,i);s<c;++s)if(r[s]!==e[s]){n=r[s],i=e[s];break}return n<i?-1:i<n?1:0},o.isEncoding=function(r){switch(String(r).toLowerCase()){case"hex":case"utf8":case"utf-8":case"ascii":case"latin1":case"binary":case"base64":case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return!0;default:return!1}},o.concat=function(r,e){if(!Array.isArray(r))throw new TypeError('"list" argument must be an Array of Buffers');if(r.length===0)return o.alloc(0);let n;if(e===void 0)for(e=0,n=0;n<r.length;++n)e+=r[n].length;const i=o.allocUnsafe(e);let s=0;for(n=0;n<r.length;++n){let c=r[n];if(G(c,Uint8Array))s+c.length>i.length?(o.isBuffer(c)||(c=o.from(c)),c.copy(i,s)):Uint8Array.prototype.set.call(i,c,s);else if(o.isBuffer(c))c.copy(i,s);else throw new TypeError('"list" argument must be an Array of Buffers');s+=c.length}return i};function b(t,r){if(o.isBuffer(t))return t.length;if(ArrayBuffer.isView(t)||G(t,ArrayBuffer))return t.byteLength;if(typeof t!="string")throw new TypeError('The "string" argument must be one of type string, Buffer, or ArrayBuffer. Received type '+typeof t);const e=t.length,n=arguments.length>2&&arguments[2]===!0;if(!n&&e===0)return 0;let i=!1;for(;;)switch(r){case"ascii":case"latin1":case"binary":return e;case"utf8":case"utf-8":return Ir(t).length;case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return e*2;case"hex":return e>>>1;case"base64":return Ar(t).length;default:if(i)return n?-1:Ir(t).length;r=(""+r).toLowerCase(),i=!0}}o.byteLength=b;function V(t,r,e){let n=!1;if((r===void 0||r<0)&&(r=0),r>this.length||((e===void 0||e>this.length)&&(e=this.length),e<=0)||(e>>>=0,r>>>=0,e<=r))return"";for(t||(t="utf8");;)switch(t){case"hex":return Er(this,r,e);case"utf8":case"utf-8":return cr(this,r,e);case"ascii":return ur(this,r,e);case"latin1":case"binary":return xr(this,r,e);case"base64":return v(this,r,e);case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return Br(this,r,e);default:if(n)throw new TypeError("Unknown encoding: "+t);t=(t+"").toLowerCase(),n=!0}}o.prototype._isBuffer=!0;function U(t,r,e){const n=t[r];t[r]=t[e],t[e]=n}o.prototype.swap16=function(){const r=this.length;if(r%2!==0)throw new RangeError("Buffer size must be a multiple of 16-bits");for(let e=0;e<r;e+=2)U(this,e,e+1);return this},o.prototype.swap32=function(){const r=this.length;if(r%4!==0)throw new RangeError("Buffer size must be a multiple of 32-bits");for(let e=0;e<r;e+=4)U(this,e,e+3),U(this,e+1,e+2);return this},o.prototype.swap64=function(){const r=this.length;if(r%8!==0)throw new RangeError("Buffer size must be a multiple of 64-bits");for(let e=0;e<r;e+=8)U(this,e,e+7),U(this,e+1,e+6),U(this,e+2,e+5),U(this,e+3,e+4);return this},o.prototype.toString=function(){const r=this.length;return r===0?"":arguments.length===0?cr(this,0,r):V.apply(this,arguments)},o.prototype.toLocaleString=o.prototype.toString,o.prototype.equals=function(r){if(!o.isBuffer(r))throw new TypeError("Argument must be a Buffer");return this===r?!0:o.compare(this,r)===0},o.prototype.inspect=function(){let r="";const e=u.INSPECT_MAX_BYTES;return r=this.toString("hex",0,e).replace(/(.{2})/g,"$1 ").trim(),this.length>e&&(r+=" ... "),"<Buffer "+r+">"},p&&(o.prototype[p]=o.prototype.inspect),o.prototype.compare=function(r,e,n,i,s){if(G(r,Uint8Array)&&(r=o.from(r,r.offset,r.byteLength)),!o.isBuffer(r))throw new TypeError('The "target" argument must be one of type Buffer or Uint8Array. Received type '+typeof r);if(e===void 0&&(e=0),n===void 0&&(n=r?r.length:0),i===void 0&&(i=0),s===void 0&&(s=this.length),e<0||n>r.length||i<0||s>this.length)throw new RangeError("out of range index");if(i>=s&&e>=n)return 0;if(i>=s)return-1;if(e>=n)return 1;if(e>>>=0,n>>>=0,i>>>=0,s>>>=0,this===r)return 0;let c=s-i,m=n-e;const S=Math.min(c,m),F=this.slice(i,s),T=r.slice(e,n);for(let _=0;_<S;++_)if(F[_]!==T[_]){c=F[_],m=T[_];break}return c<m?-1:m<c?1:0};function ir(t,r,e,n,i){if(t.length===0)return-1;if(typeof e=="string"?(n=e,e=0):e>2147483647?e=2147483647:e<-2147483648&&(e=-2147483648),e=+e,_r(e)&&(e=i?0:t.length-1),e<0&&(e=t.length+e),e>=t.length){if(i)return-1;e=t.length-1}else if(e<0)if(i)e=0;else return-1;if(typeof r=="string"&&(r=o.from(r,n)),o.isBuffer(r))return r.length===0?-1:or(t,r,e,n,i);if(typeof r=="number")return r=r&255,typeof Uint8Array.prototype.indexOf=="function"?i?Uint8Array.prototype.indexOf.call(t,r,e):Uint8Array.prototype.lastIndexOf.call(t,r,e):or(t,[r],e,n,i);throw new TypeError("val must be string, number or Buffer")}function or(t,r,e,n,i){let s=1,c=t.length,m=r.length;if(n!==void 0&&(n=String(n).toLowerCase(),n==="ucs2"||n==="ucs-2"||n==="utf16le"||n==="utf-16le")){if(t.length<2||r.length<2)return-1;s=2,c/=2,m/=2,e/=2}function S(T,_){return s===1?T[_]:T.readUInt16BE(_*s)}let F;if(i){let T=-1;for(F=e;F<c;F++)if(S(t,F)===S(r,T===-1?0:F-T)){if(T===-1&&(T=F),F-T+1===m)return T*s}else T!==-1&&(F-=F-T),T=-1}else for(e+m>c&&(e=c-m),F=e;F>=0;F--){let T=!0;for(let _=0;_<m;_++)if(S(t,F+_)!==S(r,_)){T=!1;break}if(T)return F}return-1}o.prototype.includes=function(r,e,n){return this.indexOf(r,e,n)!==-1},o.prototype.indexOf=function(r,e,n){return ir(this,r,e,n,!0)},o.prototype.lastIndexOf=function(r,e,n){return ir(this,r,e,n,!1)};function dr(t,r,e,n){e=Number(e)||0;const i=t.length-e;n?(n=Number(n),n>i&&(n=i)):n=i;const s=r.length;n>s/2&&(n=s/2);let c;for(c=0;c<n;++c){const m=parseInt(r.substr(c*2,2),16);if(_r(m))return c;t[e+c]=m}return c}function wr(t,r,e,n){return ar(Ir(r,t.length-e),t,e,n)}function yr(t,r,e,n){return ar(Dr(r),t,e,n)}function sr(t,r,e,n){return ar(Ar(r),t,e,n)}function gr(t,r,e,n){return ar(Nr(r,t.length-e),t,e,n)}o.prototype.write=function(r,e,n,i){if(e===void 0)i="utf8",n=this.length,e=0;else if(n===void 0&&typeof e=="string")i=e,n=this.length,e=0;else if(isFinite(e))e=e>>>0,isFinite(n)?(n=n>>>0,i===void 0&&(i="utf8")):(i=n,n=void 0);else throw new Error("Buffer.write(string, encoding, offset[, length]) is no longer supported");const s=this.length-e;if((n===void 0||n>s)&&(n=s),r.length>0&&(n<0||e<0)||e>this.length)throw new RangeError("Attempt to write outside buffer bounds");i||(i="utf8");let c=!1;for(;;)switch(i){case"hex":return dr(this,r,e,n);case"utf8":case"utf-8":return wr(this,r,e,n);case"ascii":case"latin1":case"binary":return yr(this,r,e,n);case"base64":return sr(this,r,e,n);case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return gr(this,r,e,n);default:if(c)throw new TypeError("Unknown encoding: "+i);i=(""+i).toLowerCase(),c=!0}},o.prototype.toJSON=function(){return{type:"Buffer",data:Array.prototype.slice.call(this._arr||this,0)}};function v(t,r,e){return r===0&&e===t.length?a.fromByteArray(t):a.fromByteArray(t.slice(r,e))}function cr(t,r,e){e=Math.min(t.length,e);const n=[];let i=r;for(;i<e;){const s=t[i];let c=null,m=s>239?4:s>223?3:s>191?2:1;if(i+m<=e){let S,F,T,_;switch(m){case 1:s<128&&(c=s);break;case 2:S=t[i+1],(S&192)===128&&(_=(s&31)<<6|S&63,_>127&&(c=_));break;case 3:S=t[i+1],F=t[i+2],(S&192)===128&&(F&192)===128&&(_=(s&15)<<12|(S&63)<<6|F&63,_>2047&&(_<55296||_>57343)&&(c=_));break;case 4:S=t[i+1],F=t[i+2],T=t[i+3],(S&192)===128&&(F&192)===128&&(T&192)===128&&(_=(s&15)<<18|(S&63)<<12|(F&63)<<6|T&63,_>65535&&_<1114112&&(c=_))}}c===null?(c=65533,m=1):c>65535&&(c-=65536,n.push(c>>>10&1023|55296),c=56320|c&1023),n.push(c),i+=m}return mr(n)}const rr=4096;function mr(t){const r=t.length;if(r<=rr)return String.fromCharCode.apply(String,t);let e="",n=0;for(;n<r;)e+=String.fromCharCode.apply(String,t.slice(n,n+=rr));return e}function ur(t,r,e){let n="";e=Math.min(t.length,e);for(let i=r;i<e;++i)n+=String.fromCharCode(t[i]&127);return n}function xr(t,r,e){let n="";e=Math.min(t.length,e);for(let i=r;i<e;++i)n+=String.fromCharCode(t[i]);return n}function Er(t,r,e){const n=t.length;(!r||r<0)&&(r=0),(!e||e<0||e>n)&&(e=n);let i="";for(let s=r;s<e;++s)i+=Or[t[s]];return i}function Br(t,r,e){const n=t.slice(r,e);let i="";for(let s=0;s<n.length-1;s+=2)i+=String.fromCharCode(n[s]+n[s+1]*256);return i}o.prototype.slice=function(r,e){const n=this.length;r=~~r,e=e===void 0?n:~~e,r<0?(r+=n,r<0&&(r=0)):r>n&&(r=n),e<0?(e+=n,e<0&&(e=0)):e>n&&(e=n),e<r&&(e=r);const i=this.subarray(r,e);return Object.setPrototypeOf(i,o.prototype),i};function $(t,r,e){if(t%1!==0||t<0)throw new RangeError("offset is not uint");if(t+r>e)throw new RangeError("Trying to access beyond buffer length")}o.prototype.readUintLE=o.prototype.readUIntLE=function(r,e,n){r=r>>>0,e=e>>>0,n||$(r,e,this.length);let i=this[r],s=1,c=0;for(;++c<e&&(s*=256);)i+=this[r+c]*s;return i},o.prototype.readUintBE=o.prototype.readUIntBE=function(r,e,n){r=r>>>0,e=e>>>0,n||$(r,e,this.length);let i=this[r+--e],s=1;for(;e>0&&(s*=256);)i+=this[r+--e]*s;return i},o.prototype.readUint8=o.prototype.readUInt8=function(r,e){return r=r>>>0,e||$(r,1,this.length),this[r]},o.prototype.readUint16LE=o.prototype.readUInt16LE=function(r,e){return r=r>>>0,e||$(r,2,this.length),this[r]|this[r+1]<<8},o.prototype.readUint16BE=o.prototype.readUInt16BE=function(r,e){return r=r>>>0,e||$(r,2,this.length),this[r]<<8|this[r+1]},o.prototype.readUint32LE=o.prototype.readUInt32LE=function(r,e){return r=r>>>0,e||$(r,4,this.length),(this[r]|this[r+1]<<8|this[r+2]<<16)+this[r+3]*16777216},o.prototype.readUint32BE=o.prototype.readUInt32BE=function(r,e){return r=r>>>0,e||$(r,4,this.length),this[r]*16777216+(this[r+1]<<16|this[r+2]<<8|this[r+3])},o.prototype.readBigUInt64LE=H(function(r){r=r>>>0,z(r,"offset");const e=this[r],n=this[r+7];(e===void 0||n===void 0)&&Z(r,this.length-8);const i=e+this[++r]*2**8+this[++r]*2**16+this[++r]*2**24,s=this[++r]+this[++r]*2**8+this[++r]*2**16+n*2**24;return BigInt(i)+(BigInt(s)<<BigInt(32))}),o.prototype.readBigUInt64BE=H(function(r){r=r>>>0,z(r,"offset");const e=this[r],n=this[r+7];(e===void 0||n===void 0)&&Z(r,this.length-8);const i=e*2**24+this[++r]*2**16+this[++r]*2**8+this[++r],s=this[++r]*2**24+this[++r]*2**16+this[++r]*2**8+n;return(BigInt(i)<<BigInt(32))+BigInt(s)}),o.prototype.readIntLE=function(r,e,n){r=r>>>0,e=e>>>0,n||$(r,e,this.length);let i=this[r],s=1,c=0;for(;++c<e&&(s*=256);)i+=this[r+c]*s;return s*=128,i>=s&&(i-=Math.pow(2,8*e)),i},o.prototype.readIntBE=function(r,e,n){r=r>>>0,e=e>>>0,n||$(r,e,this.length);let i=e,s=1,c=this[r+--i];for(;i>0&&(s*=256);)c+=this[r+--i]*s;return s*=128,c>=s&&(c-=Math.pow(2,8*e)),c},o.prototype.readInt8=function(r,e){return r=r>>>0,e||$(r,1,this.length),this[r]&128?(255-this[r]+1)*-1:this[r]},o.prototype.readInt16LE=function(r,e){r=r>>>0,e||$(r,2,this.length);const n=this[r]|this[r+1]<<8;return n&32768?n|4294901760:n},o.prototype.readInt16BE=function(r,e){r=r>>>0,e||$(r,2,this.length);const n=this[r+1]|this[r]<<8;return n&32768?n|4294901760:n},o.prototype.readInt32LE=function(r,e){return r=r>>>0,e||$(r,4,this.length),this[r]|this[r+1]<<8|this[r+2]<<16|this[r+3]<<24},o.prototype.readInt32BE=function(r,e){return r=r>>>0,e||$(r,4,this.length),this[r]<<24|this[r+1]<<16|this[r+2]<<8|this[r+3]},o.prototype.readBigInt64LE=H(function(r){r=r>>>0,z(r,"offset");const e=this[r],n=this[r+7];(e===void 0||n===void 0)&&Z(r,this.length-8);const i=this[r+4]+this[r+5]*2**8+this[r+6]*2**16+(n<<24);return(BigInt(i)<<BigInt(32))+BigInt(e+this[++r]*2**8+this[++r]*2**16+this[++r]*2**24)}),o.prototype.readBigInt64BE=H(function(r){r=r>>>0,z(r,"offset");const e=this[r],n=this[r+7];(e===void 0||n===void 0)&&Z(r,this.length-8);const i=(e<<24)+this[++r]*2**16+this[++r]*2**8+this[++r];return(BigInt(i)<<BigInt(32))+BigInt(this[++r]*2**24+this[++r]*2**16+this[++r]*2**8+n)}),o.prototype.readFloatLE=function(r,e){return r=r>>>0,e||$(r,4,this.length),f.read(this,r,!0,23,4)},o.prototype.readFloatBE=function(r,e){return r=r>>>0,e||$(r,4,this.length),f.read(this,r,!1,23,4)},o.prototype.readDoubleLE=function(r,e){return r=r>>>0,e||$(r,8,this.length),f.read(this,r,!0,52,8)},o.prototype.readDoubleBE=function(r,e){return r=r>>>0,e||$(r,8,this.length),f.read(this,r,!1,52,8)};function h(t,r,e,n,i,s){if(!o.isBuffer(t))throw new TypeError('"buffer" argument must be a Buffer instance');if(r>i||r<s)throw new RangeError('"value" argument is out of bounds');if(e+n>t.length)throw new RangeError("Index out of range")}o.prototype.writeUintLE=o.prototype.writeUIntLE=function(r,e,n,i){if(r=+r,e=e>>>0,n=n>>>0,!i){const m=Math.pow(2,8*n)-1;h(this,r,e,n,m,0)}let s=1,c=0;for(this[e]=r&255;++c<n&&(s*=256);)this[e+c]=r/s&255;return e+n},o.prototype.writeUintBE=o.prototype.writeUIntBE=function(r,e,n,i){if(r=+r,e=e>>>0,n=n>>>0,!i){const m=Math.pow(2,8*n)-1;h(this,r,e,n,m,0)}let s=n-1,c=1;for(this[e+s]=r&255;--s>=0&&(c*=256);)this[e+s]=r/c&255;return e+n},o.prototype.writeUint8=o.prototype.writeUInt8=function(r,e,n){return r=+r,e=e>>>0,n||h(this,r,e,1,255,0),this[e]=r&255,e+1},o.prototype.writeUint16LE=o.prototype.writeUInt16LE=function(r,e,n){return r=+r,e=e>>>0,n||h(this,r,e,2,65535,0),this[e]=r&255,this[e+1]=r>>>8,e+2},o.prototype.writeUint16BE=o.prototype.writeUInt16BE=function(r,e,n){return r=+r,e=e>>>0,n||h(this,r,e,2,65535,0),this[e]=r>>>8,this[e+1]=r&255,e+2},o.prototype.writeUint32LE=o.prototype.writeUInt32LE=function(r,e,n){return r=+r,e=e>>>0,n||h(this,r,e,4,4294967295,0),this[e+3]=r>>>24,this[e+2]=r>>>16,this[e+1]=r>>>8,this[e]=r&255,e+4},o.prototype.writeUint32BE=o.prototype.writeUInt32BE=function(r,e,n){return r=+r,e=e>>>0,n||h(this,r,e,4,4294967295,0),this[e]=r>>>24,this[e+1]=r>>>16,this[e+2]=r>>>8,this[e+3]=r&255,e+4};function x(t,r,e,n,i){q(r,n,i,t,e,7);let s=Number(r&BigInt(4294967295));t[e++]=s,s=s>>8,t[e++]=s,s=s>>8,t[e++]=s,s=s>>8,t[e++]=s;let c=Number(r>>BigInt(32)&BigInt(4294967295));return t[e++]=c,c=c>>8,t[e++]=c,c=c>>8,t[e++]=c,c=c>>8,t[e++]=c,e}function L(t,r,e,n,i){q(r,n,i,t,e,7);let s=Number(r&BigInt(4294967295));t[e+7]=s,s=s>>8,t[e+6]=s,s=s>>8,t[e+5]=s,s=s>>8,t[e+4]=s;let c=Number(r>>BigInt(32)&BigInt(4294967295));return t[e+3]=c,c=c>>8,t[e+2]=c,c=c>>8,t[e+1]=c,c=c>>8,t[e]=c,e+8}o.prototype.writeBigUInt64LE=H(function(r,e=0){return x(this,r,e,BigInt(0),BigInt("0xffffffffffffffff"))}),o.prototype.writeBigUInt64BE=H(function(r,e=0){return L(this,r,e,BigInt(0),BigInt("0xffffffffffffffff"))}),o.prototype.writeIntLE=function(r,e,n,i){if(r=+r,e=e>>>0,!i){const S=Math.pow(2,8*n-1);h(this,r,e,n,S-1,-S)}let s=0,c=1,m=0;for(this[e]=r&255;++s<n&&(c*=256);)r<0&&m===0&&this[e+s-1]!==0&&(m=1),this[e+s]=(r/c>>0)-m&255;return e+n},o.prototype.writeIntBE=function(r,e,n,i){if(r=+r,e=e>>>0,!i){const S=Math.pow(2,8*n-1);h(this,r,e,n,S-1,-S)}let s=n-1,c=1,m=0;for(this[e+s]=r&255;--s>=0&&(c*=256);)r<0&&m===0&&this[e+s+1]!==0&&(m=1),this[e+s]=(r/c>>0)-m&255;return e+n},o.prototype.writeInt8=function(r,e,n){return r=+r,e=e>>>0,n||h(this,r,e,1,127,-128),r<0&&(r=255+r+1),this[e]=r&255,e+1},o.prototype.writeInt16LE=function(r,e,n){return r=+r,e=e>>>0,n||h(this,r,e,2,32767,-32768),this[e]=r&255,this[e+1]=r>>>8,e+2},o.prototype.writeInt16BE=function(r,e,n){return r=+r,e=e>>>0,n||h(this,r,e,2,32767,-32768),this[e]=r>>>8,this[e+1]=r&255,e+2},o.prototype.writeInt32LE=function(r,e,n){return r=+r,e=e>>>0,n||h(this,r,e,4,2147483647,-2147483648),this[e]=r&255,this[e+1]=r>>>8,this[e+2]=r>>>16,this[e+3]=r>>>24,e+4},o.prototype.writeInt32BE=function(r,e,n){return r=+r,e=e>>>0,n||h(this,r,e,4,2147483647,-2147483648),r<0&&(r=4294967295+r+1),this[e]=r>>>24,this[e+1]=r>>>16,this[e+2]=r>>>8,this[e+3]=r&255,e+4},o.prototype.writeBigInt64LE=H(function(r,e=0){return x(this,r,e,-BigInt("0x8000000000000000"),BigInt("0x7fffffffffffffff"))}),o.prototype.writeBigInt64BE=H(function(r,e=0){return L(this,r,e,-BigInt("0x8000000000000000"),BigInt("0x7fffffffffffffff"))});function I(t,r,e,n,i,s){if(e+n>t.length)throw new RangeError("Index out of range");if(e<0)throw new RangeError("Index out of range")}function O(t,r,e,n,i){return r=+r,e=e>>>0,i||I(t,r,e,4),f.write(t,r,e,n,23,4),e+4}o.prototype.writeFloatLE=function(r,e,n){return O(this,r,e,!0,n)},o.prototype.writeFloatBE=function(r,e,n){return O(this,r,e,!1,n)};function j(t,r,e,n,i){return r=+r,e=e>>>0,i||I(t,r,e,8),f.write(t,r,e,n,52,8),e+8}o.prototype.writeDoubleLE=function(r,e,n){return j(this,r,e,!0,n)},o.prototype.writeDoubleBE=function(r,e,n){return j(this,r,e,!1,n)},o.prototype.copy=function(r,e,n,i){if(!o.isBuffer(r))throw new TypeError("argument should be a Buffer");if(n||(n=0),!i&&i!==0&&(i=this.length),e>=r.length&&(e=r.length),e||(e=0),i>0&&i<n&&(i=n),i===n||r.length===0||this.length===0)return 0;if(e<0)throw new RangeError("targetStart out of bounds");if(n<0||n>=this.length)throw new RangeError("Index out of range");if(i<0)throw new RangeError("sourceEnd out of bounds");i>this.length&&(i=this.length),r.length-e<i-n&&(i=r.length-e+n);const s=i-n;return this===r&&typeof Uint8Array.prototype.copyWithin=="function"?this.copyWithin(e,n,i):Uint8Array.prototype.set.call(r,this.subarray(n,i),e),s},o.prototype.fill=function(r,e,n,i){if(typeof r=="string"){if(typeof e=="string"?(i=e,e=0,n=this.length):typeof n=="string"&&(i=n,n=this.length),i!==void 0&&typeof i!="string")throw new TypeError("encoding must be a string");if(typeof i=="string"&&!o.isEncoding(i))throw new TypeError("Unknown encoding: "+i);if(r.length===1){const c=r.charCodeAt(0);(i==="utf8"&&c<128||i==="latin1")&&(r=c)}}else typeof r=="number"?r=r&255:typeof r=="boolean"&&(r=Number(r));if(e<0||this.length<e||this.length<n)throw new RangeError("Out of range index");if(n<=e)return this;e=e>>>0,n=n===void 0?this.length:n>>>0,r||(r=0);let s;if(typeof r=="number")for(s=e;s<n;++s)this[s]=r;else{const c=o.isBuffer(r)?r:o.from(r,i),m=c.length;if(m===0)throw new TypeError('The value "'+r+'" is invalid for argument "value"');for(s=0;s<n-e;++s)this[s+e]=c[s%m]}return this};const M={};function X(t,r,e){M[t]=class extends e{constructor(){super(),Object.defineProperty(this,"message",{value:r.apply(this,arguments),writable:!0,configurable:!0}),this.name=`${this.name} [${t}]`,this.stack,delete this.name}get code(){return t}set code(i){Object.defineProperty(this,"code",{configurable:!0,enumerable:!0,value:i,writable:!0})}toString(){return`${this.name} [${t}]: ${this.message}`}}}X("ERR_BUFFER_OUT_OF_BOUNDS",function(t){return t?`${t} is outside of buffer bounds`:"Attempt to access memory outside buffer bounds"},RangeError),X("ERR_INVALID_ARG_TYPE",function(t,r){return`The "${t}" argument must be of type number. Received type ${typeof r}`},TypeError),X("ERR_OUT_OF_RANGE",function(t,r,e){let n=`The value of "${t}" is out of range.`,i=e;return Number.isInteger(e)&&Math.abs(e)>2**32?i=W(String(e)):typeof e=="bigint"&&(i=String(e),(e>BigInt(2)**BigInt(32)||e<-(BigInt(2)**BigInt(32)))&&(i=W(i)),i+="n"),n+=` It must be ${r}. Received ${i}`,n},RangeError);function W(t){let r="",e=t.length;const n=t[0]==="-"?1:0;for(;e>=n+4;e-=3)r=`_${t.slice(e-3,e)}${r}`;return`${t.slice(0,e)}${r}`}function Y(t,r,e){z(r,"offset"),(t[r]===void 0||t[r+e]===void 0)&&Z(r,t.length-(e+1))}function q(t,r,e,n,i,s){if(t>e||t<r){const c=typeof r=="bigint"?"n":"";let m;throw r===0||r===BigInt(0)?m=`>= 0${c} and < 2${c} ** ${(s+1)*8}${c}`:m=`>= -(2${c} ** ${(s+1)*8-1}${c}) and < 2 ** ${(s+1)*8-1}${c}`,new M.ERR_OUT_OF_RANGE("value",m,t)}Y(n,i,s)}function z(t,r){if(typeof t!="number")throw new M.ERR_INVALID_ARG_TYPE(r,"number",t)}function Z(t,r,e){throw Math.floor(t)!==t?(z(t,e),new M.ERR_OUT_OF_RANGE("offset","an integer",t)):r<0?new M.ERR_BUFFER_OUT_OF_BOUNDS:new M.ERR_OUT_OF_RANGE("offset",`>= 0 and <= ${r}`,t)}const Lr=/[^+/0-9A-Za-z-_]/g;function Pr(t){if(t=t.split("=")[0],t=t.trim().replace(Lr,""),t.length<2)return"";for(;t.length%4!==0;)t=t+"=";return t}function Ir(t,r){r=r||1/0;let e;const n=t.length;let i=null;const s=[];for(let c=0;c<n;++c){if(e=t.charCodeAt(c),e>55295&&e<57344){if(!i){if(e>56319){(r-=3)>-1&&s.push(239,191,189);continue}else if(c+1===n){(r-=3)>-1&&s.push(239,191,189);continue}i=e;continue}if(e<56320){(r-=3)>-1&&s.push(239,191,189),i=e;continue}e=(i-55296<<10|e-56320)+65536}else i&&(r-=3)>-1&&s.push(239,191,189);if(i=null,e<128){if((r-=1)<0)break;s.push(e)}else if(e<2048){if((r-=2)<0)break;s.push(e>>6|192,e&63|128)}else if(e<65536){if((r-=3)<0)break;s.push(e>>12|224,e>>6&63|128,e&63|128)}else if(e<1114112){if((r-=4)<0)break;s.push(e>>18|240,e>>12&63|128,e>>6&63|128,e&63|128)}else throw new Error("Invalid code point")}return s}function Dr(t){const r=[];for(let e=0;e<t.length;++e)r.push(t.charCodeAt(e)&255);return r}function Nr(t,r){let e,n,i;const s=[];for(let c=0;c<t.length&&!((r-=2)<0);++c)e=t.charCodeAt(c),n=e>>8,i=e%256,s.push(i),s.push(n);return s}function Ar(t){return a.toByteArray(Pr(t))}function ar(t,r,e,n){let i;for(i=0;i<n&&!(i+e>=r.length||i>=t.length);++i)r[i+e]=t[i];return i}function G(t,r){return t instanceof r||t!=null&&t.constructor!=null&&t.constructor.name!=null&&t.constructor.name===r.name}function _r(t){return t!==t}const Or=function(){const t="0123456789abcdef",r=new Array(256);for(let e=0;e<16;++e){const n=e*16;for(let i=0;i<16;++i)r[n+i]=t[e]+t[i]}return r}();function H(t){return typeof BigInt>"u"?Mr:t}function Mr(){throw new Error("BigInt not supported")}}(Fr)),Fr}var Wr=Gr();function zr(u){const a=Array.from(new Uint8Array(u));return btoa(String.fromCharCode.apply(null,a)).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,"")}function Yr(u){const f=new TextEncoder().encode(u);return window.crypto.subtle.digest("SHA-256",f)}async function Hr(u){const a=await Yr(u);return zr(a)}function Cr(){const u=new Uint32Array(28);return window.crypto.getRandomValues(u),Array.from(u,a=>("0"+a.toString(16)).substr(-2)).join("")}const Xr=60,Ur=u=>{const a=Math.floor(Date.now()/1e3);return u.exp+Xr>a},Zr=async(u,a)=>{const f=Cr(),p=Cr(),d=await Hr(p);sessionStorage.setItem(`${tr}-${f}`,JSON.stringify({codeVerifier:p,appState:a}));const y=new URL(u);return{state:f,code_challenge:d,url:y}},Qr=()=>{let u={};return{reset:()=>{u={}},getItem:y=>u[y],removeItem:y=>{delete u[y]},setItem:(y,w)=>{u[y]=w}}},J=Qr(),$r=(u,a="access_token")=>{const f=J.getItem(`kinde_${a}`);return f?{name:u,value:f[u]}:null},Q=(u,a="access_token")=>{const f=$r(u,a);return f&&f.value},pr=(u,a,f)=>{const p=Q("feature_flags"),d=p&&p[u]?p[u]:{};if(d.v==null&&a==null)throw Error(`Flag ${u} was not found, and no default value has been provided`);if(f&&d.t&&f!==d.t)throw Error(`Flag ${u} is of type ${lr[d.t]} - requested type ${lr[f]}`);return{code:u,type:lr[d.t||f],value:d.v==null?a:d.v,is_default:d.v==null}},vr=(u,a)=>{try{return pr(u,a,"b").value}catch(f){return console.error(f),f}},re=(u,a)=>{try{return pr(u,a,"i").value}catch(f){return console.error(f),f}},ee=(u,a)=>{try{return pr(u,a,"s").value}catch(f){return console.error(f),f}},te=()=>({orgCodes:Q("org_codes","id_token")??[]}),ne=["iss","azp"],br=(u,a)=>{if(!u)throw new Error("ID token is required");if(ne.forEach(p=>{if(!u.payload[p])throw new Error(`(${p}) claim is required.`);if(u.payload[p]!==a[p])throw new Error(`${p} claim mismatch. Expected: "${a[p]}", Received: "${u.payload[p]}"`)}),u.header.alg!=="RS256")throw new Error(`Unsupported signature alg. Expected: "RS256", Received: "${u.header.alg}"`);if(a.aud){if(!Array.isArray(u.payload.aud))throw new Error("(aud) claim must be an array");if(!a.aud.split(" ").every(y=>u.payload.aud.includes(y)))throw new Error(`(aud) claim mismatch. Expected: "${a.aud}", Received: "${u.payload.aud.join(", ")}"`)}if(!Ur(u.payload))throw new Error("Token expired");return!0},ie=u=>new URL(u).hostname.split(".").slice(-2).join(".")!=="kinde.com",oe=async u=>{if(!u)throw Error("Please provide your Kinde credentials");if(u!==Object(u))throw Error("The Kinde SDK must be initiated with an object");const{audience:a,client_id:f,domain:p,is_dangerously_use_local_storage:d=!1,redirect_uri:y,logout_uri:w=y,on_redirect_callback:o,on_error_callback:D,scope:N="openid profile email offline",proxy_redirect_uri:R,_framework:C,_frameworkVersion:K}=u;if(a&&typeof a!="string")throw Error("Please supply a valid audience for your api");if(N&&typeof N!="string")throw Error("Please supply a valid scope");if(!y||typeof u.redirect_uri!="string")throw Error("Please supply a valid redirect_uri for your users to be redirected after successful authentication");if(!p||typeof p!="string")throw Error("Please supply a valid Kinde domain so we can connect to your account");if(typeof d!="boolean")throw TypeError("Please supply a boolean value for is_dangerously_use_local_storage");const l=f||"spa@live",g=location.hostname==="localhost"||location.hostname==="127.0.0.1",E=!g&&!d&&ie(p),A=g||d,B={audience:a,client_id:l,redirect_uri:y,authorization_endpoint:`${p}/oauth2/auth`,token_endpoint:`${p}/oauth2/token`,requested_scopes:N,domain:p,_framework:C,_frameworkVersion:K},k=h=>{if(!h||h.error)return;const x=hr(h.id_token),L=hr(h.id_token,{header:!0}),I=hr(h.access_token),O=hr(h.access_token,{header:!0}),j={iss:p,azp:f,aud:a};br({payload:x,header:L},{...j,aud:f}),br({payload:I,header:O},j),J.setItem(P.token_bundle,h),J.setItem(P.access_token,I),J.setItem(P.id_token,x),x.sub&&J.setItem(P.user,{id:x.sub,given_name:x.given_name,family_name:x.family_name,email:x.email,picture:x.picture}),A?localStorage.setItem(P.refresh_token,h.refresh_token):J.setItem(P.refresh_token,h.refresh_token)},b=async({tokenType:h}={tokenType:P.access_token})=>{const x=A?localStorage.getItem(P.refresh_token):J.getItem(P.refresh_token);if(x||E&&qr("_kbrte"))try{const O=await(await fetch(B.token_endpoint,{method:"POST",...E&&{credentials:"include"},headers:new Headers({"Content-type":"application/x-www-form-urlencoded; charset=UTF-8","Kinde-SDK":` ${B._framework||"JavaScript"}/${B._frameworkVersion||kr}`}),body:new URLSearchParams({client_id:B.client_id,grant_type:"refresh_token",...!E&&x&&{refresh_token:x}})})).json();return k(O),h===P.id_token?O.id_token:O.access_token}catch(I){console.error(I)}},V=async(h,x)=>{const L=J.getItem(P.token_bundle);if(!L||x.isForceRefresh)return await b({tokenType:h});const I=J.getItem(h);return Ur(I)?h===P.access_token?L.access_token:L.id_token:await b({tokenType:h})},U=async(h={})=>await V(P.access_token,h),ir=async(h={})=>await V(P.id_token,h),or=async()=>{const h=J.getItem(P.access_token);return h?(Ur(h)||await b(),!0):!1},dr=()=>{const h=Q("org_code");return{permissions:Q("permissions")??[],orgCode:h}},wr=h=>{const x=Q("org_code");return{isGranted:(Q("permissions")??[]).some(I=>I===h),orgCode:x}},yr=()=>({orgCode:Q("org_code")}),sr=()=>{const h=new URL(window.location.toString());h.search="",window.history.pushState({},"",h)},gr=async h=>{const x=h.get("form_data");if(x){const M=JSON.parse(Wr.Buffer.from(x,"base64").toString("utf-8"));rr({authUrlParams:M});return}const L=h.get("code"),I=h.get("state"),O=h.get("error"),j=sessionStorage.getItem(`${tr}-${I}`);if(!j)console.error("Invalid state");else{if(O){const W=h.get("error"),Y=h.get("error_description");sr(),sessionStorage.removeItem(`${tr}-${I}`);const{appState:q}=JSON.parse(j);return D?D({error:W,errorDescription:Y,state:I,appState:q}):window.location.href=q.kindeOriginUrl,!1}const{appState:M,codeVerifier:X}=JSON.parse(j);try{const Y=await(await fetch(B.token_endpoint,{method:"POST",...E&&{credentials:"include"},headers:new Headers({"Content-type":"application/x-www-form-urlencoded; charset=UTF-8","Kinde-SDK":`${B._framework||"JavaScript"}/${B._frameworkVersion||kr}`}),body:new URLSearchParams({client_id:B.client_id,code:L,code_verifier:X,grant_type:"authorization_code",redirect_uri:B.redirect_uri})})).json();k(Y),sr(),sessionStorage.removeItem(`${tr}-${I}`);const q=ur();o&&o(q,M)}catch(W){console.error(W),sessionStorage.removeItem(`${tr}-${I}`)}}},v=async h=>{const{app_state:x={},prompt:L,is_create_org:I,org_name:O="",org_code:j,authUrlParams:M={}}=h;x.kindeOriginUrl||(x.kindeOriginUrl=window.location.href);const{state:X,code_challenge:W,url:Y}=await Zr(B.authorization_endpoint,x),q={redirect_uri:y,client_id:l,response_type:"code",scope:B.requested_scopes,code_challenge:W,code_challenge_method:"S256",state:X};L&&(q.prompt=L),j&&(q.org_code=j),I&&(q.is_create_org=String(I),q.org_name=O);const z=new URLSearchParams(Object.assign(M,q));a&&a.trim().split(/\s+/).forEach(Z=>{z.append("audience",Z)}),Y.search=String(z),window.location.href=Y.toString()},cr=async h=>{await v({...h,prompt:"create"})},rr=async h=>{await v({...h})},mr=async h=>{await v({...h,prompt:"create",is_create_org:!0})},ur=()=>J.getItem(P.user),xr=async()=>{const x={Accept:"application/json",Authorization:`Bearer ${await U()}`};try{const I=await(await fetch(`${B.domain}/oauth2/v2/user_profile`,{method:"GET",headers:x})).json();return J.setItem(P.user,{id:I.sub,given_name:I.given_name,family_name:I.family_name,email:I.email,picture:I.picture}),J.getItem(P.user)}catch(L){console.error(L)}},Er=async()=>{const h=new URL(`${B.domain}/logout`);try{J.reset(),A&&localStorage.removeItem(P.refresh_token);const x=new URLSearchParams({redirect:w});h.search=String(x),window.location.href=h.toString()}catch(x){console.error(x)}},Br=async()=>{const h=new URLSearchParams(window.location.search);$(h)?await gr(h):(E||A)&&await b()},$=h=>{const x=h.has("code"),L=h.has("error");if(!x&&!L)return!1;const{protocol:I,host:O,pathname:j}=window.location,M=R||`${I}//${O}${j}`;return M===y||M===`${y}/`};return await Br(),{getToken:U,getIdToken:ir,getUser:ur,getUserProfile:xr,login:rr,logout:Er,register:cr,isAuthenticated:or,createOrg:mr,getClaim:$r,getFlag:pr,getBooleanFlag:vr,getStringFlag:ee,getIntegerFlag:re,getPermissions:dr,getPermission:wr,getOrganization:yr,getUserOrganizations:te}};module.exports=oe;