!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).AuthLib={})}(this,(function(e){"use strict";var t=function(e,n){return(t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)t.hasOwnProperty(n)&&(e[n]=t[n])})(e,n)};function n(e,n){function r(){this.constructor=e}t(e,n),e.prototype=null===n?Object.create(n):(r.prototype=n.prototype,new r)}var r=function(){return(r=Object.assign||function(e){for(var t,n=1,r=arguments.length;n0&&i[i.length-1])||6!==o[0]&&2!==o[0])){s=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]0&&i.set("scope",t.scopes.join(" ")),r.href}function d(e){var t=new URL(window.location.origin);return t.pathname=e||window.location.pathname,t.href}function f(e){var t,n;return e.enableSessionStorageTokenCache&&console.warn("%c!!!WARNING: THE 'enableSessionStorageTokenCache' SETTING SHOULD NEVER BE USED ON ANYPRODUCTION STAGE BUT ONLY DURING LOCAL DEVELOPMENT!!! IF YOU CAN SEE THIS MESSAGE ON ANY STAGE,PLEASE REPORT IT AS A FINDING!","font-size: 30px; color: red; font-weight: 600"),!e.scopes&&e.scope&&"string"==typeof e.scope&&(console.warn("Using deprecated 'scope' configuration, please switch to 'scopes'"),e.scopes=decodeURIComponent(e.scope).split(" ")),"string"==typeof e.redirectUri&&0!==e.redirectUri.length||("standalone"===e.mode?e.redirectUri=d():e.redirectUri=d("/mail/client/_auth"),console.info("Redirect URI is not defined, using default one '"+e.redirectUri+"'")),"string"==typeof e.authCode&&0!==e.authCode.length||(e.authCode=(t="code",void 0===n&&(n=location.href),new URL(n).searchParams.get(t)),"string"==typeof e.authCode&&e.authCode.length>0&&console.info("AuthCode was not set but found in url")),"string"==typeof e.navsid&&0!==e.navsid.length||(e.navsid=s()||sessionStorage.getItem("mam.navsid"),"string"==typeof e.navsid&&e.navsid.length>0&&console.info("Navsid was not set but found in url or sessionStorage")),e}if("undefined"==typeof TextEncoder){TextEncoder=function(){},TextEncoder.prototype.encode=function(e){for(var t=e.length,n=-1,r="undefined"==typeof Uint8Array?new Array(1.5*t):new Uint8Array(3*t),i=0,o=0,s=0;s!==t;){if(i=e.charCodeAt(s),s+=1,i>=55296&&i<=56319){if(s===t){r[n+=1]=239,r[n+=1]=191,r[n+=1]=189;break}if(!((o=e.charCodeAt(s))>=56320&&o<=57343)){r[n+=1]=239,r[n+=1]=191,r[n+=1]=189;continue}if(s+=1,(i=1024*(i-55296)+o-56320+65536)>65535){r[n+=1]=240|i>>>18,r[n+=1]=128|i>>>12&63,r[n+=1]=128|i>>>6&63,r[n+=1]=128|63&i;continue}}i<=127?r[n+=1]=0|i:i<=2047?(r[n+=1]=192|i>>>6,r[n+=1]=128|63&i):(r[n+=1]=224|i>>>12,r[n+=1]=128|i>>>6&63,r[n+=1]=128|63&i)}return"undefined"!=typeof Uint8Array?r.subarray(0,n+1):(r.length=n+1,r)},TextEncoder.prototype.toString=function(){return"[object TextEncoder]"};try{Object.defineProperty(TextEncoder.prototype,"encoding",{get:function(){if(TextEncoder.prototype.isPrototypeOf(this))return"utf-8";throw TypeError("Illegal invocation")}})}catch(e){TextEncoder.prototype.encoding="utf-8"}"undefined"!=typeof Symbol&&(TextEncoder.prototype[Symbol.toStringTag]="TextEncoder")}function h(e){return function(e){let t="";const n=new Uint8Array(e),r=n.byteLength;for(let e=0;e{const r=window.msCrypto.subtle.digest("SHA-256",t);r.oncomplete=function(t){e(h(r.result))},r.onerror=function(e){n("Could not complete the SHA-256 hash.")}});throw"Could not find window.crypto or window.msCrypto. This browser is not supported."}(e).then(t=>({verifier:e,challenge:t}))}};function p(){return i(this,void 0,void 0,(function(){return o(this,(function(e){return[2,l.generateVerifierAndChallenge()]}))}))}function g(e,t,n){return i(this,void 0,void 0,(function(){var r,i,s;return o(this,(function(o){switch(o.label){case 0:return[4,fetch(e,{method:"POST",headers:new Headers({Authorization:a(t),"Content-Type":"application/x-www-form-urlencoded"}),body:new URLSearchParams({grant_type:"authorization_code",deviceNameB64:c(n.deviceName),redirect_uri:n.redirectUri,userAgentB64:c(n.userAgent),code:n.authCode,code_verifier:n.codeVerifier})})];case 1:if((r=o.sent()).status>=400)throw new Error("Token request failed: "+r.status+" "+r.statusText);o.label=2;case 2:return o.trys.push([2,4,,5]),[4,r.json()];case 3:return i=o.sent(),[3,5];case 4:throw s=o.sent(),new Error("Could not parse token response: "+s.message+" "+s.stack);case 5:if(!i.access_token||i.refresh_token&&i.refresh_token.length>0)throw new Error("Unexpected token data "+JSON.stringify(i,null,2));return[2,i]}}))}))}var v,w=function(){function e(e){this._settings=e}return e.prototype.start=function(e){var t=this;window.setTimeout((function(){return i(t,void 0,void 0,(function(){return o(this,(function(e){switch(e.label){case 0:return[4,V(this._settings,!0)];case 1:return e.sent(),[2]}}))}))}),1e3*e-1e3)},e.runTimer=function(t,n){new e(r({},t)).start(n)},e}(),y=new Uint8Array(16);function m(){if(!v&&!(v="undefined"!=typeof crypto&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto)||"undefined"!=typeof msCrypto&&"function"==typeof msCrypto.getRandomValues&&msCrypto.getRandomValues.bind(msCrypto)))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return v(y)}var b=/^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i;function _(e){return"string"==typeof e&&b.test(e)}for(var E=[],S=0;S<256;++S)E.push((S+256).toString(16).substr(1));function I(e,t,n){var r=(e=e||{}).random||(e.rng||m)();if(r[6]=15&r[6]|64,r[8]=63&r[8]|128,t){n=n||0;for(var i=0;i<16;++i)t[n+i]=r[i];return t}return function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,n=(E[e[t+0]]+E[e[t+1]]+E[e[t+2]]+E[e[t+3]]+"-"+E[e[t+4]]+E[e[t+5]]+"-"+E[e[t+6]]+E[e[t+7]]+"-"+E[e[t+8]]+E[e[t+9]]+"-"+E[e[t+10]]+E[e[t+11]]+E[e[t+12]]+E[e[t+13]]+E[e[t+14]]+E[e[t+15]]).toLowerCase();if(!_(n))throw TypeError("Stringified UUID is invalid");return n}(r)}function T(e){var t={id:I()};return e&&(t.navsid=e),t}function C(e){sessionStorage.removeItem(e)}var U={};function A(e){var t=(e.scopes||[]).sort().join(" ");return btoa(e.clientId+"/"+t)}function k(e){var t=A(e);e.enableSessionStorageTokenCache?C(t):function(e){delete U[e]}(t)}function N(e){var t=A(e);return e.enableSessionStorageTokenCache?function(e){var t=sessionStorage.getItem(e);try{var n=JSON.parse(t);if(!(((null==n?void 0:n.expires_on)||0)<=Date.now()))return n.token;C(e)}catch(e){}}(t):function(e){return U[e]}(t)}function P(e,t){var n=A(e);e.enableSessionStorageTokenCache?function(e,t,n){var r=Date.now()+1e3*n;sessionStorage.setItem(e,JSON.stringify({token:t,expires_on:r}))}(n,t.access_token,t.expires_in):function(e,t){U[e]=t}(n,t.access_token)}var O=function(){function e(e){this._subscribers=[],this._settings=e}return e.prototype.subscribe=function(e){this._subscribers.push(e)},e}(),x=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return n(t,e),t.prototype.start=function(){return i(this,void 0,void 0,(function(){var e,t,n=this;return o(this,(function(r){switch(r.label){case 0:if(this._iframeInstance)throw new Error("Process already started, please only subscribe to its outcome");return this._pkcePair?[3,2]:(e=this,[4,p()]);case 1:e._pkcePair=r.sent(),r.label=2;case 2:return this.state=T(this._settings.navsid),t=u(this._settings.oauthEndpointAuthorize,{clientId:this._settings.clientId,redirectUri:this._settings.redirectUri,scopes:this._settings.scopes,state:this.state,codeChallenge:this._pkcePair.challenge}),this._iframeInstance=document.body.appendChild(this.createIframe()),i=this._iframeInstance,o=function(e){return n.handleAuthCodeReceived(e)},s=function(e){if(i.contentWindow===e.source&&e.origin===window.location.origin){var t;try{t=JSON.parse(e.data)}catch(e){return}t.code&&t.state&&(o(t.code),window.removeEventListener("message",s))}},window.addEventListener("message",s),setTimeout((function(){return n.handleTimeout()}),this._settings.authCodeTimeout||5e3),this._iframeInstance.src=t,[2]}var i,o,s}))}))},t.prototype.handleAuthCodeReceived=function(e){return i(this,void 0,void 0,(function(){var t,n,r,i;return o(this,(function(o){switch(o.label){case 0:t={clientId:this._settings.clientId,clientSecret:this._settings.clientSecret},n={authCode:e,codeVerifier:this._pkcePair.verifier,redirectUri:this._settings.redirectUri,userAgent:navigator.userAgent,deviceName:this._settings.deviceName},o.label=1;case 1:return o.trys.push([1,3,,4]),[4,g(this._settings.oauthEndpointToken,t,n)];case 2:return r=o.sent(),P(this._settings,r),w.runTimer(this._settings,r.expires_in),this._iframeInstance&&(this._iframeInstance.parentNode.removeChild(this._iframeInstance),delete this._iframeInstance),this._subscribers.splice(0).forEach((function(e){e.resolve(r.access_token)})),[3,4];case 3:return i=o.sent(),this._subscribers.splice(0).forEach((function(e){e.reject(i)})),[3,4];case 4:return[2]}}))}))},t.prototype.handleTimeout=function(){var e=this;this._subscribers.splice(0).forEach((function(t){t.reject("AuthCode request timed out: \nclientID: "+e._settings.clientId+" \nredirectUri: "+e._settings.redirectUri+" \nscopes: "+e._settings.scopes.join()+" \nstateId: "+e.state.id+" \nnavsid: "+e.state.navsid)}))},t.prototype.createIframe=function(){var e=document.createElement("iframe"),t={border:"0",frameborder:"0",marginwidth:"0",marginheight:"0",allowtransparency:"true",background:"transparent",scrolling:"yes",style:"visibility: hidden; width: 0; height: 0; border: 0;"};return Object.keys(t).forEach((function(n){e.setAttribute(n,t[n])})),e},t}(O),R=function(){function e(e){this._settings=e}return e.prototype.start=function(e){var t=this;window.setTimeout((function(){return i(t,void 0,void 0,(function(){return o(this,(function(e){return k(this._settings),[2]}))}))}),1e3*e)},e.runTimer=function(t,n){new e(r({},t)).start(n)},e}();function j(e){return i(this,void 0,void 0,(function(){var t;return o(this,(function(n){switch(n.label){case 0:try{e=function(e){var t=f(e);if("string"!=typeof t.clientId||0===t.clientId.length)throw new Error("Missing required setting clientId");if("string"!=typeof t.oauthEndpointAuthorize||0===t.oauthEndpointAuthorize.length)throw new Error("Missing required setting oauthEndpointAuthorize");if("string"!=typeof t.redirectUri||0===t.redirectUri.length)throw new Error("Missing required setting redirectUri");return t}(e)}catch(e){return[2,Promise.reject(e)]}return[4,p()];case 1:return t=n.sent(),sessionStorage.setItem(D,t.verifier),[2,u(e.oauthEndpointAuthorize,{clientId:e.clientId,redirectUri:e.redirectUri,scopes:e.scopes,state:T(e.navsid),codeChallenge:t.challenge})]}}))}))}function M(e,t){return i(this,void 0,void 0,(function(){var n,r,i,s,c,a;return o(this,(function(o){switch(o.label){case 0:try{e=function(e){var t=f(e);if("string"!=typeof t.clientId||0===t.clientId.length)throw new Error("Missing required setting clientId");if("string"!=typeof t.clientSecret||0===e.clientSecret.length)throw new Error("Missing required setting clientSecret");if("string"!=typeof t.oauthEndpointToken||0===t.oauthEndpointToken.length)throw new Error("Missing required setting oauthEndpointToken");if("string"!=typeof t.redirectUri||0===t.redirectUri.length)throw new Error("Missing required setting redirectUri");if("string"!=typeof t.authCode||0===e.authCode.length)throw new Error("Missing required setting authCode");if("string"!=typeof t.deviceName||0===e.deviceName.length)throw new Error("Missing required setting deviceName");return t}(e)}catch(e){return[2,Promise.reject(e)]}if(n=N(e),!t&&n)return[2,n];if("string"!=typeof(r=sessionStorage.getItem(D))||0===r.length)return[2,Promise.reject("No pkce verifier was found in sessionStorage to validate the token request.")];o.label=1;case 1:return o.trys.push([1,3,,4]),i={clientId:e.clientId,clientSecret:e.clientSecret},s={authCode:e.authCode,codeVerifier:r,redirectUri:e.redirectUri,userAgent:navigator.userAgent,deviceName:e.deviceName},[4,g(e.oauthEndpointToken,i,s)];case 2:return c=o.sent(),P(e,c),R.runTimer(e,c.expires_in),[2,c.access_token];case 3:return a=o.sent(),[2,Promise.reject(a)];case 4:return[2]}}))}))}var q=function(e){function t(){var t=null!==e&&e.apply(this,arguments)||this;return t._processStarted=!1,t}return n(t,e),t.prototype.start=function(){return i(this,void 0,void 0,(function(){var e,t;return o(this,(function(n){switch(n.label){case 0:if(this._processStarted)throw new Error("Process already started, please only subscribe to its outcome");return this._processStarted=!0,"string"==typeof this._settings.authCode&&this._settings.authCode.length>0?[4,M(this._settings)]:[3,2];case 1:return e=n.sent(),this._subscribers.splice(0).forEach((function(t){t.resolve(e)})),[2];case 2:return[4,p()];case 3:return t=n.sent(),sessionStorage.setItem(D,t.verifier),function(e){window.location.href=e}(u(this._settings.oauthEndpointAuthorize,{clientId:this._settings.clientId,redirectUri:this._settings.redirectUri,scopes:this._settings.scopes,state:T(this._settings.navsid),codeChallenge:t.challenge})),this._subscribers.splice(0).forEach((function(e){e.reject("Redirecting to login form")})),[2]}}))}))},t}(O),L={};function V(e,t){var n=this;try{e=function(e){var t=f(e);if("string"!=typeof t.clientId||0===t.clientId.length)throw new Error("Missing required setting clientId");if("string"!=typeof t.clientSecret||0===e.clientSecret.length)throw new Error("Missing required setting clientSecret");if("string"!=typeof t.oauthEndpointAuthorize||0===t.oauthEndpointAuthorize.length)throw new Error("Missing required setting oauthEndpointAuthorize");if("string"!=typeof t.oauthEndpointToken||0===t.oauthEndpointToken.length)throw new Error("Missing required setting oauthEndpointToken");if("string"!=typeof t.redirectUri||0===t.redirectUri.length)throw new Error("Missing required setting redirectUri");if("string"!=typeof t.deviceName||0===e.deviceName.length)throw new Error("Missing required setting deviceName");if("standalone"!==t.mode&&("string"!=typeof t.navsid||0===e.navsid.length))throw new Error("Missing required setting navsid");return t}(e)}catch(e){return Promise.reject(e)}var r=N(e);return!t&&r?Promise.resolve(r):new Promise((function(t,r){return i(n,void 0,void 0,(function(){var n=this;return o(this,(function(s){return function(e,t){var n=A(e),r=L[n];if(r)r.subscribe(t);else{var i="standalone"===e.mode?new q(e):new x(e);L[n]=i,i.subscribe(t),i.subscribe({resolve:function(){return delete L[n]},reject:function(){return delete L[n]}}),i.start()}}(e,{resolve:function(e){return i(n,void 0,void 0,(function(){return o(this,(function(n){return[2,t(e)]}))}))},reject:function(e){return r(e)}}),[2]}))}))}))}var z="S256",D="oauth2-client_pkceVerifier";e.LIB_NAME="oauth2-client",e.PKCE_METHOD=z,e.PKCE_VERIFIER_KEY=D,e.VERSION="2.2.1",e.getAccessToken=function(e){return i(this,void 0,void 0,(function(){return o(this,(function(t){return[2,V(e)]}))}))},e.getAccessTokenFromCode=function(e){return i(this,void 0,void 0,(function(){return o(this,(function(t){return[2,M(e)]}))}))},e.getLoginUri=function(e){return i(this,void 0,void 0,(function(){return o(this,(function(t){return[2,j(e)]}))}))},e.getNavsidFromUrl=function(){return s()},e.refreshAccessToken=function(e){return i(this,void 0,void 0,(function(){return o(this,(function(t){return[2,V(e,!0)]}))}))},Object.defineProperty(e,"__esModule",{value:!0})}));//# sourceMappingURL=authlib.js.map