Liferay.Loader.define("@liferay/oauth2-provider-web@4.0.57/js/OAuth2Client",["module","exports","require","liferay!frontend-js-dependencies-web$pkce-challenge"],(function(e,t,r){window;{Object.defineProperty(t,"__esModule",{value:!0}),t.FromParameters=function FromParameters(e){return new OAuth2Client({authorizeURL:e.authorizeURL||Liferay.OAuth2.getAuthorizeURL(),clientId:e.clientId,encodedRedirectURL:encodeURIComponent(e.redirectURIs&&e.redirectURIs[0]||Liferay.OAuth2.getBuiltInRedirectURL()),homePageURL:e.homePageURL,redirectURIs:e.redirectURIs||[Liferay.OAuth2.getBuiltInRedirectURL()],tokenURL:e.tokenURL||Liferay.OAuth2.getTokenURL()})},t.FromUserAgentApplication=function FromUserAgentApplication(e){const t=Liferay.OAuth2.getUserAgentApplication(e);if(!t)throw new Error("No Application User Agent profile found for ".concat(e));return new OAuth2Client({authorizeURL:Liferay.OAuth2.getAuthorizeURL(),clientId:t.clientId,encodedRedirectURL:encodeURIComponent(t.redirectURIs[0]),homePageURL:t.homePageURL,redirectURIs:t.redirectURIs,tokenURL:Liferay.OAuth2.getTokenURL()})};var n=function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}(r("liferay!frontend-js-dependencies-web$pkce-challenge"));function ownKeys(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function _objectSpread(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?ownKeys(Object(r),!0).forEach((function(t){_defineProperty(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):ownKeys(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}function _defineProperty(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}class OAuth2Client{constructor(e){_defineProperty(this,"authorizeURL",void 0),_defineProperty(this,"clientId",void 0),_defineProperty(this,"encodedRedirectURL",void 0),_defineProperty(this,"homePageURL",void 0),_defineProperty(this,"redirectURIs",void 0),_defineProperty(this,"tokenURL",void 0),this.authorizeURL=e.authorizeURL,this.clientId=e.clientId,this.encodedRedirectURL=e.encodedRedirectURL,this.homePageURL=e.homePageURL,this.redirectURIs=e.redirectURIs,this.tokenURL=e.tokenURL}async fetch(e,t={}){return this._fetch(e,t).then((e=>{if(e.ok){const t=e.headers.get("content-type");return t&&-1!==t.indexOf("application/json")?e.json():Promise.resolve(e)}return Promise.reject(e)}))}_createIframe(e,t){const r=this,n=document.createElement("iframe");return n.src="".concat(r.authorizeURL,"?client_id=").concat(r.clientId,"&code_challenge=").concat(e.code_challenge,"&code_challenge_method=S256&redirect_uri=").concat(r.encodedRedirectURL,"&response_type=code&prompt=none"),n.style.display="none",document.body.appendChild(n),new Promise(((o,i)=>{const eventHandler=c=>{try{if(c.data.error)return void i(c.data.error);if(null===c.data.code)return void i();const s=r._requestToken(e.code_verifier,c.data.code);o(s),s.then((e=>Liferay.Util.SessionStorage.setItem(t,JSON.stringify(e),Liferay.Util.SessionStorage.TYPES.NECESSARY)))}finally{var a;null===(a=n.parentElement)||void 0===a||a.removeChild(n)}};n.contentWindow&&n.contentWindow.addEventListener("message",eventHandler)}))}async _fetch(e,t={}){const r=this;let n=e instanceof Request?e.url:e.toString();if(n.includes("//")&&!n.startsWith(r.homePageURL))throw new Error("This client only supports calls to ".concat(r.homePageURL));n.startsWith(r.homePageURL)||(n.startsWith("/")&&(n=n.substring(1)),n="".concat(r.homePageURL,"/").concat(n));const o=await r._getOrRequestToken();return e=e instanceof Request?_objectSpread(_objectSpread({},e),{},{url:n}):n,await fetch(e,_objectSpread({headers:{Authorization:"Bearer ".concat(o.access_token)}},t))}_getOrRequestToken(){const e=this,t="".concat(e.clientId,"-").concat(Liferay.authToken,"-token");return new Promise((r=>{const n=Liferay.Util.SessionStorage.getItem(t,Liferay.Util.SessionStorage.TYPES.NECESSARY);r(null==n?e._requestTokenSilently(t):JSON.parse(n))}))}_requestTokenSilently(e){const t=(0,n.default)(128);return this._createIframe(t,e)}async _requestToken(e,t){const r=this,n=await fetch(r.tokenURL,{body:new URLSearchParams({client_id:r.clientId,code:t,code_verifier:e,grant_type:"authorization_code",redirect_uri:r.redirectURIs[0]}),cache:"no-cache",headers:{"Content-Type":"application/x-www-form-urlencoded"},method:"POST",mode:"cors"});return n.ok?n.json():await Promise.reject(n)}}}}));
//# sourceMappingURL=OAuth2Client.js.map