UNPKG

@curity/token-handler-js-assistant

Version:

Curity Token Handler JavaScript helper library

1 lines 3.59 kB
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("TokenHandlerAssistant",[],t):"object"==typeof exports?exports.TokenHandlerAssistant=t():e.TokenHandlerAssistant=t()}(self,()=>(()=>{"use strict";var e={613:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.OAuthAgentRemoteError=void 0;class r extends Error{constructor(e,t,r){super(`${e}: ${t}`),this.status=e,this.code=t,this.details=r}}t.OAuthAgentRemoteError=r},943:function(e,t,r){var n=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))(function(o,s){function i(e){try{c(n.next(e))}catch(e){s(e)}}function a(e){try{c(n.throw(e))}catch(e){s(e)}}function c(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r(function(e){e(t)})).then(i,a)}c((n=n.apply(e,t||[])).next())})};Object.defineProperty(t,"__esModule",{value:!0}),t.OAuthAgentClient=void 0;const o=r(613);t.OAuthAgentClient=class{constructor(e){this.oauthAgentBaseUrl=e.oauthAgentBaseUrl.endsWith("/")?e.oauthAgentBaseUrl:`${e.oauthAgentBaseUrl}/`}refresh(e){return n(this,void 0,void 0,function*(){const t=this.toUrlSearchParams(null==e?void 0:e.extraRefreshParameters);return{accessTokenExpiresIn:(yield this.fetch("POST","refresh",t)).access_token_expires_in}})}session(){return n(this,void 0,void 0,function*(){const e=yield this.fetch("GET","session");return{isLoggedIn:e.is_logged_in,idTokenClaims:e.id_token_claims,accessTokenExpiresIn:e.access_token_expires_in}})}startLogin(e){return n(this,void 0,void 0,function*(){const t=this.toUrlSearchParams(null==e?void 0:e.extraAuthorizationParameters);return{authorizationUrl:(yield this.fetch("POST","login/start",t)).authorization_url}})}endLogin(e){return n(this,void 0,void 0,function*(){const t=yield this.fetch("POST","login/end",e.searchParams);return{isLoggedIn:t.is_logged_in,idTokenClaims:t.id_token_claims,accessTokenExpiresIn:t.access_token_expires_in}})}logout(){return n(this,void 0,void 0,function*(){return{logoutUrl:(yield this.fetch("POST","logout")).logout_url}})}onPageLoad(e){return n(this,void 0,void 0,function*(){const t=new URL(e);return this.isOAuthResponse(t)?yield this.endLogin({searchParams:t.searchParams}):yield this.session()})}isOAuthResponse(e){const t=e.searchParams,r=t.has("state")&&t.has("code"),n=t.has("response")&&1==Array.from(t).length,o=t.has("error")&&t.has("state");return r||n||o}toUrlSearchParams(e){return e?new URLSearchParams(e):new URLSearchParams}fetch(e,t,r){return n(this,void 0,void 0,function*(){const n={accept:"application/json","token-handler-version":"1"};r&&0!==r.size&&(n["content-type"]="application/x-www-form-urlencoded");const s={credentials:"include",headers:n,method:e,mode:"cors"};null!=r&&(s.body=r);const i=yield window.fetch(`${this.oauthAgentBaseUrl}${t}`,s);if(i.ok)return yield i.json();if("application/json"==i.headers.get("content-type")){const e=yield i.json();throw new o.OAuthAgentRemoteError(i.status,e.error_code,e.detailed_error)}throw new o.OAuthAgentRemoteError(i.status,"server_error",i.statusText)})}}}},t={};function r(n){var o=t[n];if(void 0!==o)return o.exports;var s=t[n]={exports:{}};return e[n].call(s.exports,s,s.exports,r),s.exports}var n={};return(()=>{var e=n;Object.defineProperty(e,"__esModule",{value:!0}),e.OAuthAgentClient=e.OAuthAgentRemoteError=void 0;var t=r(613);Object.defineProperty(e,"OAuthAgentRemoteError",{enumerable:!0,get:function(){return t.OAuthAgentRemoteError}});var o=r(943);Object.defineProperty(e,"OAuthAgentClient",{enumerable:!0,get:function(){return o.OAuthAgentClient}})})(),n})());