@crossid/crossid-react
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -0,1 +1,7 @@ | ||
## 0.1.2 | ||
### Minor Changes | ||
- export handle login state | ||
## 0.1.1 | ||
@@ -2,0 +8,0 @@ |
@@ -29,3 +29,3 @@ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).reactCrossid={},t.React)}(this,(function(t,e){"use strict";function n(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var r=n(e),o=function(){return(o=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var o in e=arguments[n])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t}).apply(this,arguments)}; | ||
PERFORMANCE OF THIS SOFTWARE. | ||
***************************************************************************** */var s=function(t,e){return(s=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])})(t,e)};function u(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function n(){this.constructor=t}s(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}var c=function(){return(c=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var o in e=arguments[n])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t}).apply(this,arguments)};function l(t,e){var n={};for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&e.indexOf(r)<0&&(n[r]=t[r]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(r=Object.getOwnPropertySymbols(t);o<r.length;o++)e.indexOf(r[o])<0&&Object.prototype.propertyIsEnumerable.call(t,r[o])&&(n[r[o]]=t[r[o]])}return n}function d(t,e,n,r){return new(n||(n=Promise))((function(o,i){function a(t){try{u(r.next(t))}catch(t){i(t)}}function s(t){try{u(r.throw(t))}catch(t){i(t)}}function u(t){var e;t.done?o(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(a,s)}u((r=r.apply(t,e||[])).next())}))}function p(t,e){var n,r,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function s(i){return function(s){return function(i){if(n)throw new TypeError("Generator is already executing.");for(;a;)try{if(n=1,r&&(o=2&i[0]?r.return:i[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,i[1])).done)return o;switch(r=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return a.label++,{value:i[1],done:!1};case 5:a.label++,r=i[1],i=[0];continue;case 7:i=a.ops.pop(),a.trys.pop();continue;default:if(!((o=(o=a.trys).length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){a=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){a.label=i[1];break}if(6===i[0]&&a.label<o[1]){a.label=o[1],o=i;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(i);break}o[2]&&a.ops.pop(),a.trys.pop();continue}i=e.call(t,a)}catch(t){i=[6,t],r=0}finally{n=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,s])}}}var h=function(t){function e(n,r){var o=t.call(this,r)||this;return o.error=n,o.error_description=r,Object.setPrototypeOf(o,e.prototype),o}return u(e,t),e.create=function(t){return new e(t.error,t.error_description)},e}(Error);function f(t){var e=t.wellknown_endpoint;return d(this,void 0,void 0,(function(){var t;return p(this,(function(n){switch(n.label){case 0:return[4,fetch(e)];case 1:return 200!==(t=n.sent()).status?[3,3]:[4,t.json()];case 2:return[2,n.sent()];case 3:throw Error("failed to discover data from well known discovery endpoint.")}}))}))}function v(t){var e=t.tokenEndpoint,n=t.timeout,r=l(t,["tokenEndpoint","timeout"]);return d(this,void 0,void 0,(function(){var t,o,i;return p(this,(function(a){switch(a.label){case 0:return t=new FormData,Object.keys(r).forEach((function(e){return t.append(e,r[e])})),[4,(s=e,u={timeout:n,method:"POST",body:t},void 0===u&&(u={}),d(void 0,void 0,void 0,(function(){var t,e,n,r,o;return p(this,(function(i){switch(i.label){case 0:return t=u.timeout,e=void 0===t?5e3:t,n=new AbortController,r=setTimeout((function(){return n.abort()}),e),[4,fetch(s,c(c({},u),{signal:n.signal}))];case 1:return o=i.sent(),clearTimeout(r),[2,o]}}))})))];case 1:return[4,(o=a.sent()).json()];case 2:if(i=a.sent(),200!==o.status)throw h.create(i);return[2,i]}var s,u}))}))}var _="crossid-spa-js|index",y=function(t){return null!==t&&"Object"===(null==t?void 0:t.constructor.name)},g=function(t){return"number"==typeof t},w=function(t){return"string"==typeof t},m=String.fromCharCode(0),b=function(){function t(t,e){void 0===e&&(e={}),this.gopts=e,this.storage=t,e.purgeOnInit&&this.purge(!0)}return t.prototype.set=function(t,e,n){var r=this._key(t),o=this._ttl(null==n?void 0:n.ttl);try{var i=function(t,e){var n;return e&&e>0?((n={})[m]=t,n.ttl=Date.now()+1e3*e,n):t}(e,o);return this.storage.setItem(r,JSON.stringify(i)),!0}catch(t){return!1}},t.prototype.get=function(t,e){var n,r=this._key(t);try{var o=this.storage.getItem(r);if(null===o)return;var i=JSON.parse(o);return y(n=i)&&m in n?function(t,e){return Date.now()-1e3*e>t.ttl}(i,(null==e?void 0:e.ttl)||0)?void this.storage.removeItem(t):i[m]:i}catch(t){return}},t.prototype.remove=function(t){var e=this._key(t);return!!this.storage.getItem(e)&&(this.storage.removeItem(e),!0)},t.prototype.purge=function(t){var e=this;void 0===t&&(t=!1);var n=0;return Object.keys(this.storage).filter((function(t){return t.startsWith(e.gopts.prefix)})).forEach((function(r){var o=e.storage.getItem(r);if(o){var i;try{i=JSON.parse(o)}catch(t){return}(y(i)&&m in i&&Date.now()>i.ttl||t)&&(n++,e.storage.removeItem(r))}})),n},t.prototype._key=function(t){return this.gopts.prefix?""+this.gopts.prefix+t:t},t.prototype._ttl=function(t){return null===t?null:t||this.gopts.ttl||null},t}(),k=function(t){function e(e){var n=t.call(this,sessionStorage,e)||this;if("undefined"==typeof sessionStorage)throw Error("no support for session storage.");return n}return u(e,t),e}(b),S=function(t){function e(e){var n=t.call(this,localStorage,e)||this;if("undefined"==typeof localStorage)throw Error("no support for local storage.");return n}return u(e,t),e}(b),T=function(){function t(){}return t.prototype.getItem=function(t){return t in this?this[t]:null},t.prototype.setItem=function(t,e){void 0===e?this.removeItem(t):this[t]=""+e},t.prototype.removeItem=function(t){this.hasOwnProperty(t)&&delete this[t]},t}(),x=function(t){function e(e){return t.call(this,new T,e)||this}return u(e,t),e}(b),O=function(t){return Array.from(new Set(t))},E=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return O(t.join(" ").trim().split(/\s+/))},j=function(){return t="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~",e=Math.floor(86*Math.random()+43),Array.from(window.crypto.getRandomValues(new Uint8Array(e))).map((function(e){return t[e%t.length]})).join("");var t,e},A=function(t){return btoa(t)},C=function(t){return atob(t)},R=function(t){var e={"+":"-","/":"_","=":""};return t.replace(/[+/=]/g,(function(t){return e[t]}))},I=function(t){var e={"-":"+",_:"/"};return t.replace(/[-_]/g,(function(t){return e[t]}))},P=function(t){return Object.keys(t).filter((function(e){return void 0!==t[e]&&null!==t[e]})).map((function(e){return encodeURIComponent(e)+"="+encodeURIComponent(t[e])})).join("&")},U=function(t){var e=t.split("."),n=e[0],r=e[1],o=e[2];if(3!==e.length||!n||!r||!o)throw new Error("malformed JWT token.");var i,a=JSON.parse(C(I(r)));if(a.aud)if(w(a.aud))a.aud=[a.aud];else if(i=a.aud,!Array.isArray(i)||!i.every((function(t){return w(t)})))throw new Error("Audience (aud) must be a string or an array of strings");return{header:JSON.parse(C(I(n))),payload:a}},D=function(t){function e(e){for(var n=[],r=1;r<arguments.length;r++)n[r-1]=arguments[r];var o=t.apply(this,n)||this;return o.fields_=e,o}return u(e,t),e.prototype.getMissingFields=function(){return this.fields_},e}(Error),K=function(t){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];var r=e.map((function(e){return e(t)})).filter((function(t){return null!==t}));if(r&&r.length)throw new D(r,r.join(","));return null},L=function(t,e){var n=[];return function(r){return e&&e.filter((function(t){return!r.payload[t]})).forEach((function(t){return n.push(t)})),t&&t.filter((function(t){return!r.header[t]})).forEach((function(t){return n.push(t)})),n&&n.length?n.join(",")+" missing":null}},z=function(t){return function(e){return J("Issuer (iss)",t,e.payload.iss)}},F=function(t){return function(e){return J("Issuer (iss) claim",t,e.header.alg)}},N=function(t){return void 0===t&&(t=[]),function(e){return e.payload.aud&&e.payload.aud.length?(n=t,e.payload.aud.every((function(t){return n.includes(t)}))?null:"Audience (aud) claim mismatch: audience ("+e.payload.aud+") must contain only ("+t+")"):"Audience (aud) claim must be a present string";var n}},W=function(t,e){return void 0===t&&(t=!0),void 0===e&&(e=40),function(n){if(!t&&!n.payload.exp)return null;if(!g(n.payload.exp))return"Expiration Time (exp) claim must be a present number";var r=M(n.payload.exp,e);return new Date(Date.now())>r?"Expiration Time (exp) claim expired ("+r+")":null}},q=function(t,e){return void 0===t&&(t=!0),void 0===e&&(e=40),function(n){if(!t&&!n.payload.nbf)return null;if(!g(n.payload.nbf))return"Not Before (nbf) claim must be a present number";var r=M(n.payload.nbf,-1*e),o=new Date(Date.now());return o<r?"Not Before (nbf) claim cannot be used yet, current time ("+o+") is before ("+r+")":null}},J=function(t,e,n){return e!==n?t+' mismatch: expected "'+e+'" but got "'+n+'"':null},M=function(t,e){void 0===e&&(e=0);var n=new Date(0);return n.setUTCSeconds(t+e),n},B=function(){function t(t){this.opts=t,this.loginStateKey="crossid-spa-js|login",this.logoutStateKey="crossid-spa-js|logout",this.scope=t.scope,this.state=this._stateFactory(this.opts.state_type||"session_storage"),this.cache=this._cacheFactory(this.opts.cache_type||"memory"),this._purgeIndex()}return t.prototype.createRedirectURL=function(t){return void 0===t&&(t={}),d(this,void 0,void 0,(function(){var e;return p(this,(function(n){switch(n.label){case 0:return[4,this._createAuthorizationData(t)];case 1:return e=n.sent(),[4,this._persistAuthorizationData(e)];case 2:return n.sent(),[2,this._authorizeUrl(e.request)]}}))}))},t.prototype.loginWithRedirect=function(t){return d(this,void 0,void 0,(function(){var e;return p(this,(function(n){switch(n.label){case 0:return[4,this.createRedirectURL(t)];case 1:return e=n.sent(),window.location.replace(e),[2]}}))}))},t.prototype.handleRedirectCallback=function(t){return void 0===t&&(t=new URL(window.location.href)),d(this,void 0,void 0,(function(){var e,n,r,o,i,a,s;return p(this,(function(u){switch(u.label){case 0:if(e=t.searchParams,n=e.get("code"),r=e.get("error"))throw new Error(r);if(!(null==(o=this.state.get(this.loginStateKey))?void 0:o.code_verifier))throw new Error("invalid state, try sign-in again");return[4,v({tokenEndpoint:this.opts.token_endpoint,client_id:this.opts.client_id,code_verifier:o.code_verifier,grant_type:"authorization_code",redirect_uri:o.redirect_uri,code:n})];case 1:return i=u.sent(),(a=U(i.id_token)).payload.__bearer=i.id_token,s=U(i.access_token),this.state.remove(this.loginStateKey),this._assertAccessToken(s,o.audience),this._assertIDToken(a,o.nonce),s.payload._raw=i.access_token,this._cacheTokens(a,s,i.refresh_token),[2,{state:o.state}]}}))}))},t.prototype.getUser=function(t){return void 0===t&&(t={}),d(this,void 0,void 0,(function(){var e,n,r,o;return p(this,(function(i){return e=t.audience||this.opts.audience,n=E(this.scope,t.scope),r=this._getTokensKeysFromCache("id_token",e,n),[2,null==(o=this._getNarrowedKey(r))?void 0:o.payload]}))}))},t.prototype.getAccessToken=function(t){var e;return void 0===t&&(t={}),d(this,void 0,void 0,(function(){var n,r,o,i;return p(this,(function(a){return n=t.audience||this.opts.audience,r=E(this.scope,t.scope),o=this._getTokensKeysFromCache("access_token",n,r),i=this._getNarrowedKey(o),[2,null===(e=null==i?void 0:i.payload)||void 0===e?void 0:e._raw]}))}))},t.prototype.createLogoutRedirectURL=function(t){return void 0===t&&(t={}),d(this,void 0,void 0,(function(){var e;return p(this,(function(n){switch(n.label){case 0:return[4,this._createLogoutData(t)];case 1:return(e=n.sent()).request.id_token_hint&&t.post_logout_redirect_uri?[4,this._persistLogoutData(e)]:(this._removeTokens(e.audience,e.scopes),[2,this._logoutUrl()]);case 2:return n.sent(),[2,this._logoutUrl(e.request)]}}))}))},t.prototype.logoutWithRedirect=function(t){return d(this,void 0,void 0,(function(){var e;return p(this,(function(n){switch(n.label){case 0:return[4,this.createLogoutRedirectURL(t)];case 1:return e=n.sent(),window.location.assign(e),[2]}}))}))},t.prototype.handleLogoutRedirectCallback=function(t){return void 0===t&&(t=new URL(window.location.href)),d(this,void 0,void 0,(function(){var e,n,r,o;return p(this,(function(i){if(e=t.searchParams,n=e.get("state"),r=e.get("error"))throw new Error(r);if(!(null==(o=this.state.get(this.logoutStateKey))?void 0:o.state)||o.state!==n)throw new Error("invalid state, try sign-in again");return this._removeTokens(o.audience,o.scopes),this.state.remove(this.logoutStateKey),[2,{state:o.appState}]}))}))},t.prototype._createAuthorizationData=function(t){return d(this,void 0,void 0,(function(){var e,n,r,o,i,a;return p(this,(function(s){switch(s.label){case 0:return e=R(A(j())),n=R(A(j())),r=j(),[4,(c=r,d(void 0,void 0,void 0,(function(){return p(this,(function(t){switch(t.label){case 0:return[4,window.crypto.subtle.digest({name:"SHA-256"},(new TextEncoder).encode(c))];case 1:return[2,t.sent()]}}))})))];case 1:return o=s.sent(),u=new Uint8Array(o),i=R(A(String.fromCharCode.apply(String,Array.from(u)))),a={audience:t.audience,redirect_uri:t.redirect_uri,response_type:t.response_type,scope:t.scope,state:e,nonce:n,code_challenge:i},[2,{request:this._mergeAuthorizationCodeParams(a),code_verifier:r,appState:t.state}]}var u,c}))}))},t.prototype._persistAuthorizationData=function(t){return d(this,void 0,void 0,(function(){var e,n;return p(this,(function(r){return e=t.request,n={audience:e.audience,redirect_uri:e.redirect_uri,scope:e.scope,nonce:e.nonce,state:t.appState,code_verifier:t.code_verifier},this.state.set(this.loginStateKey,n),[2]}))}))},t.prototype._mergeAuthorizationCodeParams=function(t){return{client_id:this.opts.client_id,audience:t.audience||this.opts.audience,response_type:t.response_type||this.opts.response_type||"code",redirect_uri:t.redirect_uri||this.opts.redirect_uri,nonce:t.nonce,state:t.state,scope:t.scope||this.opts.scope,code_challenge:t.code_challenge,code_challenge_method:"S256"}},t.prototype._createLogoutData=function(t){return d(this,void 0,void 0,(function(){var e,n;return p(this,(function(r){switch(r.label){case 0:return(e={id_token_hint:t.id_token_hint,post_logout_redirect_uri:t.post_logout_redirect_uri}).id_token_hint||null===e.id_token_hint?[3,2]:[4,this.getUser()];case 1:(n=r.sent())&&n.__bearer&&(e.id_token_hint=n.__bearer),r.label=2;case 2:return e.id_token_hint&&(e.state=R(A(j()))),[2,{request:e,audience:t.audience||this.opts.audience,scopes:(t.scope||this.scope).split(" "),appState:t.state}]}}))}))},t.prototype._persistLogoutData=function(t){return d(this,void 0,void 0,(function(){var e;return p(this,(function(n){return e={client_id:this.opts.client_id,audience:t.audience,scopes:t.scopes,post_logout_redirect_uri:t.request.post_logout_redirect_uri,state:t.request.state,appState:t.appState},this.state.set(this.logoutStateKey,e),[2]}))}))},t.prototype._assertAccessToken=function(t,e){K(t,L(["alg"],["iss","sub","aud","exp","iat"]),z(this.opts.issuer),F("RS256"),N(e),W(!0),q(!0))},t.prototype._assertIDToken=function(t,e){var n;return K(t,L(["alg"],["iss","sub","aud","nonce","exp","iat"]),z(this.opts.issuer),(n=e,function(t){return J("Nonce (nonce)",n,t.payload.nonce)}),F("RS256"),N([this.opts.client_id]),W(!0),q(!1),(function(t){return g(t.payload.iat)?null:"Issued At (iat) claim must be a present number"})),null},t.prototype._removeTokens=function(t,e){var n=this,r=this._getTokensKeysFromCache("id_token",t,e),o=this._getTokensKeysFromCache("access_token",t,e),i=this._getTokensKeysFromCache("refresh_token",t,e);r.concat(o).concat(i).forEach((function(t){return n.cache.remove(t)})),this._purgeIndex()},t.prototype._authorizeUrl=function(t){return this.opts.authorization_endpoint+"?"+P(t)},t.prototype._logoutUrl=function(t){var e=this.opts.logout_endpoint;return t&&(e=e+"?"+P(t)),e},t.prototype._stateFactory=function(t){var e={ttl:300};switch(t){case"local_storage":return new S(e);case"session_storage":return new k(e);default:throw new Error('Invalid cache type "'+t+'"')}},t.prototype._cacheFactory=function(t){switch(t){case"memory":return new x;case"local_storage":return new S({purgeOnInit:!0});case"session_storage":return new k({purgeOnInit:!0});default:throw new Error('Invalid cache type "'+t+'"')}},t.prototype._cacheTokens=function(t,e,n){var r=this._ttlFromToken(e);this._cacheToken("access_token",e,this.opts.client_id,e.payload.aud,e.payload.scp,r),this._cacheToken("id_token",t,this.opts.client_id,e.payload.aud,e.payload.scp||[],this._ttlFromToken(t)),n&&this._cacheToken("refresh_token",n,this.opts.client_id,e.payload.aud,e.payload.scp,432e3)},t.prototype._cacheToken=function(t,e,n,r,o,i){var a=this._cacheKey({tokType:t,client_id:n,audience:r,scope:o});this.cache.set(a,e,{ttl:i});var s=[r[0]],u=this.cache.get(_);u||(u={}),s.forEach((function(t){u[t]||(u[t]={});var e=u[t];o.forEach((function(t){e[t]||(e[t]=[]),e[t].push(a)}))})),this.cache.set(_,u)},t.prototype._cacheKey=function(t){var e,n=t.tokType,r=t.client_id,o=t.audience,i=t.scope;return i&&i.length&&(i.sort(),e=i.join("__")),["crossid-spa-js",n,r,o.join("__"),e].join("|")},t.prototype._decodeKey=function(t){var e=t.split("|"),n=e[1],r=e[2],o=e[3];return{tokenType:n,audience:r.split("__"),scope:o.split("__")}},t.prototype._getTokensKeysFromCache=function(t,e,n){var r,o=this,i=(this.cache.get(_)||{})[e[0]];if(!i)return[];for(var a=function(e){return i[e]&&i[e].length?r?void(r=r.filter((function(t){return i[e].includes(t)}))):(r=i[e].filter((function(e){return o._decodeKey(e).tokenType===t})),"continue"):{value:[]}},s=0,u=n;s<u.length;s++){var c=a(u[s]);if("object"==typeof c)return c.value}return r},t.prototype._getNarrowedKey=function(t){for(var e in t){var n=this.cache.get(t[e]);if(n)return n}},t.prototype._ttlFromToken=function(t){return(new Date(1e3*t.payload.exp).getTime()-Date.now())/1e3},t.prototype._purgeIndex=function(){var t=this.cache.get(_);if(t){for(var e=0,n=Object.entries(t);e<n.length;e++){for(var r=n[e],o=r[0],i=r[1],a=function(e,n){for(var r=[],i=0;i<n.length;i++)s.cache.get(n[i])||r.push(i);t[o][e]=n.filter((function(t,e){return-1===r.indexOf(e)})),t[o][e].length||delete t[o][e]},s=this,u=0,c=Object.entries(i);u<c.length;u++){var l=c[u];a(l[0],l[1])}Object.keys(t[o]).length||delete t[o]}this.cache.set(_,t)}},t}();function G(t){return d(this,void 0,void 0,(function(){var e,n,r,o,i,a;return p(this,(function(s){switch(s.label){case 0:return e=t.tenant_id,n=t.auth_server,r=void 0===n?"default":n,o=l(t,["tenant_id","auth_server"]),i=o,[4,f({wellknown_endpoint:"https://"+e+".crossid.io/oauth2/"+r+"/.well-known/openid-configuration"})];case 1:return a=s.sent(),i.authorization_endpoint=a.authorization_endpoint,i.token_endpoint=a.token_endpoint,i.issuer=a.issuer,i.logout_endpoint=i.issuer+"logout",[2,new B(i)]}}))}))}function H(t){return d(this,void 0,void 0,(function(){var e,n,r;return p(this,(function(o){switch(o.label){case 0:return[4,f(t)];case 1:return e=o.sent(),n=l(t,["wellknown_endpoint"]),(r=n).authorization_endpoint=e.authorization_endpoint,r.token_endpoint=e.token_endpoint,r.issuer=e.issuer,[2,new B(r)]}}))}))}function V(t){return d(this,void 0,void 0,(function(){return p(this,(function(e){return[2,new B(t)]}))}))}function Q(){throw new Error("please wrap your app with CrossidAuthProvider")}var X=e.createContext({loading:!0}),Y=e.createContext({loginWithRedirect:Q,logoutWithRedirect:Q,getAccessToken:Q}),Z=function(){return e.useContext(X)},$=function(){return e.useContext(Y)},tt=function(t){return i(void 0,void 0,void 0,(function(){var e,n,r,o;return a(this,(function(i){switch(i.label){case 0:return r=e=t,o=e,(n=e).tenant_id?[4,G(n)]:[3,2];case 1:return[2,i.sent()];case 2:return r.wellknown_endpoint?[4,H(r)]:[3,4];case 3:return[2,i.sent()];case 4:return o.authorization_endpoint&&o.token_endpoint&&o.issuer?[4,V(o)]:[3,6];case 5:return[2,i.sent()];case 6:throw new Error("invalid props")}}))}))};var et=function(){return""+window.location.pathname+window.location.search};t.CrossidAuthProvider=function(t){var n=e.useState(),o=n[0],s=n[1],u=e.useState(),c=u[0],l=u[1],d=e.useState(!0),p=d[0],h=d[1],f=e.useState(),v=f[0],_=f[1],y=t.goTo;e.useEffect((function(){i(void 0,void 0,void 0,(function(){var e,n,r,i,u,c,d,p,f;return a(this,(function(a){switch(a.label){case 0:if(o)return[3,6];a.label=1;case 1:return a.trys.push([1,4,5,6]),[4,tt(t)];case 2:return e=a.sent(),s(e),[4,e.getUser()];case 3:return n=a.sent(),_(n),[3,6];case 4:return r=a.sent(),l(r),h(!1),[3,6];case 5:return[2];case 6:return i=window.location,u=i.origin,c=i.pathname,d=new URLSearchParams(window.location.search),o&&u+c===t.redirect_uri&&d.has("code")&&!d.has("error")?[4,o.handleRedirectCallback()]:[3,9];case 7:return f=a.sent(),[4,o.getUser()];case 8:return p=a.sent(),_(p),h(!1),(null==f?void 0:f.state)&&(y?y(f.state):window.history.replaceState({},document.title,f.state)),[3,12];case 9:return o&&u+c===t.post_logout_redirect_uri&&d.has("state")&&!d.has("error")?[4,o.handleLogoutRedirectCallback()]:[3,11];case 10:return f=a.sent(),_(void 0),h(!1),(null==f?void 0:f.state)&&(y?y(f.state):window.history.replaceState({},document.title,f.state)),[3,12];case 11:h(!1),a.label=12;case 12:return[2]}}))}))}),[o,y,t]);var g=e.useCallback((function(t,e){void 0===t&&(t={}),t.state=e,null==o||o.loginWithRedirect(t)}),[o]),w=e.useCallback((function(e){void 0===e&&(e={}),e.post_logout_redirect_uri||(e.post_logout_redirect_uri=t.post_logout_redirect_uri),null==o||o.logoutWithRedirect(e)}),[o]),m=e.useCallback((function(t){return void 0===t&&(t={}),i(void 0,void 0,void 0,(function(){return a(this,(function(e){switch(e.label){case 0:return[4,null==o?void 0:o.getAccessToken(t)];case 1:return[2,e.sent()||""]}}))}))}),[o]);return r.default.createElement(X.Provider,{value:{user:v,error:c,loading:p,client:o}},r.default.createElement(Y.Provider,{value:{loginWithRedirect:g,logoutWithRedirect:w,getAccessToken:m}},t.children))},t.useAuth=Z,t.useAuthActions=$,t.withAuth=function(t,n){var s=this;return void 0===n&&(n={}),function(u){var c=e.useRef(!1),l=e.useState(!1),d=l[0],p=l[1],h=Z(),f=h.loading,v=h.client,_=$().loginWithRedirect,y=n.returnTo,g=void 0===y?et():y;return e.useEffect((function(){if(c.current=!0,!f&&!d)return i(s,void 0,void 0,(function(){return a(this,(function(t){switch(t.label){case 0:return[4,null==v?void 0:v.getUser()];case 1:return t.sent()&&c?(p(!0),[2]):[4,_({},g)];case 2:return t.sent(),[2]}}))})),function(){c.current=!1}}),[d,v,f,_,g]),d?r.default.createElement(t,o({},u)):r.default.createElement("div",null,"Loading...")}},Object.defineProperty(t,"__esModule",{value:!0})})); | ||
***************************************************************************** */var s=function(t,e){return(s=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])})(t,e)};function u(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function n(){this.constructor=t}s(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}var c=function(){return(c=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var o in e=arguments[n])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t}).apply(this,arguments)};function l(t,e){var n={};for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&e.indexOf(r)<0&&(n[r]=t[r]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(r=Object.getOwnPropertySymbols(t);o<r.length;o++)e.indexOf(r[o])<0&&Object.prototype.propertyIsEnumerable.call(t,r[o])&&(n[r[o]]=t[r[o]])}return n}function d(t,e,n,r){return new(n||(n=Promise))((function(o,i){function a(t){try{u(r.next(t))}catch(t){i(t)}}function s(t){try{u(r.throw(t))}catch(t){i(t)}}function u(t){var e;t.done?o(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(a,s)}u((r=r.apply(t,e||[])).next())}))}function p(t,e){var n,r,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function s(i){return function(s){return function(i){if(n)throw new TypeError("Generator is already executing.");for(;a;)try{if(n=1,r&&(o=2&i[0]?r.return:i[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,i[1])).done)return o;switch(r=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return a.label++,{value:i[1],done:!1};case 5:a.label++,r=i[1],i=[0];continue;case 7:i=a.ops.pop(),a.trys.pop();continue;default:if(!((o=(o=a.trys).length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){a=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){a.label=i[1];break}if(6===i[0]&&a.label<o[1]){a.label=o[1],o=i;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(i);break}o[2]&&a.ops.pop(),a.trys.pop();continue}i=e.call(t,a)}catch(t){i=[6,t],r=0}finally{n=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,s])}}}var h=function(t){function e(n,r){var o=t.call(this,r)||this;return o.error=n,o.error_description=r,Object.setPrototypeOf(o,e.prototype),o}return u(e,t),e.create=function(t){return new e(t.error,t.error_description)},e}(Error);function f(t){var e=t.wellknown_endpoint;return d(this,void 0,void 0,(function(){var t;return p(this,(function(n){switch(n.label){case 0:return[4,fetch(e)];case 1:return 200!==(t=n.sent()).status?[3,3]:[4,t.json()];case 2:return[2,n.sent()];case 3:throw Error("failed to discover data from well known discovery endpoint.")}}))}))}function v(t){var e=t.tokenEndpoint,n=t.timeout,r=l(t,["tokenEndpoint","timeout"]);return d(this,void 0,void 0,(function(){var t,o,i;return p(this,(function(a){switch(a.label){case 0:return t=new FormData,Object.keys(r).forEach((function(e){return t.append(e,r[e])})),[4,(s=e,u={timeout:n,method:"POST",body:t},void 0===u&&(u={}),d(void 0,void 0,void 0,(function(){var t,e,n,r,o;return p(this,(function(i){switch(i.label){case 0:return t=u.timeout,e=void 0===t?5e3:t,n=new AbortController,r=setTimeout((function(){return n.abort()}),e),[4,fetch(s,c(c({},u),{signal:n.signal}))];case 1:return o=i.sent(),clearTimeout(r),[2,o]}}))})))];case 1:return[4,(o=a.sent()).json()];case 2:if(i=a.sent(),200!==o.status)throw h.create(i);return[2,i]}var s,u}))}))}var _="crossid-spa-js|index",y=function(t){return null!==t&&"Object"===(null==t?void 0:t.constructor.name)},g=function(t){return"number"==typeof t},w=function(t){return"string"==typeof t},m=String.fromCharCode(0),b=function(){function t(t,e){void 0===e&&(e={}),this.gopts=e,this.storage=t,e.purgeOnInit&&this.purge(!0)}return t.prototype.set=function(t,e,n){var r=this._key(t),o=this._ttl(null==n?void 0:n.ttl);try{var i=function(t,e){var n;return e&&e>0?((n={})[m]=t,n.ttl=Date.now()+1e3*e,n):t}(e,o);return this.storage.setItem(r,JSON.stringify(i)),!0}catch(t){return!1}},t.prototype.get=function(t,e){var n,r=this._key(t);try{var o=this.storage.getItem(r);if(null===o)return;var i=JSON.parse(o);return y(n=i)&&m in n?function(t,e){return Date.now()-1e3*e>t.ttl}(i,(null==e?void 0:e.ttl)||0)?void this.storage.removeItem(t):i[m]:i}catch(t){return}},t.prototype.remove=function(t){var e=this._key(t);return!!this.storage.getItem(e)&&(this.storage.removeItem(e),!0)},t.prototype.purge=function(t){var e=this;void 0===t&&(t=!1);var n=0;return Object.keys(this.storage).filter((function(t){return t.startsWith(e.gopts.prefix)})).forEach((function(r){var o=e.storage.getItem(r);if(o){var i;try{i=JSON.parse(o)}catch(t){return}(y(i)&&m in i&&Date.now()>i.ttl||t)&&(n++,e.storage.removeItem(r))}})),n},t.prototype._key=function(t){return this.gopts.prefix?""+this.gopts.prefix+t:t},t.prototype._ttl=function(t){return null===t?null:t||this.gopts.ttl||null},t}(),k=function(t){function e(e){var n=t.call(this,sessionStorage,e)||this;if("undefined"==typeof sessionStorage)throw Error("no support for session storage.");return n}return u(e,t),e}(b),S=function(t){function e(e){var n=t.call(this,localStorage,e)||this;if("undefined"==typeof localStorage)throw Error("no support for local storage.");return n}return u(e,t),e}(b),T=function(){function t(){}return t.prototype.getItem=function(t){return t in this?this[t]:null},t.prototype.setItem=function(t,e){void 0===e?this.removeItem(t):this[t]=""+e},t.prototype.removeItem=function(t){this.hasOwnProperty(t)&&delete this[t]},t}(),x=function(t){function e(e){return t.call(this,new T,e)||this}return u(e,t),e}(b),O=function(t){return Array.from(new Set(t))},E=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return O(t.join(" ").trim().split(/\s+/))},j=function(){return t="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~",e=Math.floor(86*Math.random()+43),Array.from(window.crypto.getRandomValues(new Uint8Array(e))).map((function(e){return t[e%t.length]})).join("");var t,e},A=function(t){return btoa(t)},R=function(t){return atob(t)},C=function(t){var e={"+":"-","/":"_","=":""};return t.replace(/[+/=]/g,(function(t){return e[t]}))},I=function(t){var e={"-":"+",_:"/"};return t.replace(/[-_]/g,(function(t){return e[t]}))},P=function(t){return Object.keys(t).filter((function(e){return void 0!==t[e]&&null!==t[e]})).map((function(e){return encodeURIComponent(e)+"="+encodeURIComponent(t[e])})).join("&")},U=function(t){var e=t.split("."),n=e[0],r=e[1],o=e[2];if(3!==e.length||!n||!r||!o)throw new Error("malformed JWT token.");var i,a=JSON.parse(R(I(r)));if(a.aud)if(w(a.aud))a.aud=[a.aud];else if(i=a.aud,!Array.isArray(i)||!i.every((function(t){return w(t)})))throw new Error("Audience (aud) must be a string or an array of strings");return{header:JSON.parse(R(I(n))),payload:a}},D=function(t){function e(e){for(var n=[],r=1;r<arguments.length;r++)n[r-1]=arguments[r];var o=t.apply(this,n)||this;return o.fields_=e,o}return u(e,t),e.prototype.getMissingFields=function(){return this.fields_},e}(Error),K=function(t){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];var r=e.map((function(e){return e(t)})).filter((function(t){return null!==t}));if(r&&r.length)throw new D(r,r.join(","));return null},L=function(t,e){var n=[];return function(r){return e&&e.filter((function(t){return!r.payload[t]})).forEach((function(t){return n.push(t)})),t&&t.filter((function(t){return!r.header[t]})).forEach((function(t){return n.push(t)})),n&&n.length?n.join(",")+" missing":null}},z=function(t){return function(e){return J("Issuer (iss)",t,e.payload.iss)}},F=function(t){return function(e){return J("Issuer (iss) claim",t,e.header.alg)}},N=function(t){return void 0===t&&(t=[]),function(e){return e.payload.aud&&e.payload.aud.length?(n=t,e.payload.aud.every((function(t){return n.includes(t)}))?null:"Audience (aud) claim mismatch: audience ("+e.payload.aud+") must contain only ("+t+")"):"Audience (aud) claim must be a present string";var n}},W=function(t,e){return void 0===t&&(t=!0),void 0===e&&(e=40),function(n){if(!t&&!n.payload.exp)return null;if(!g(n.payload.exp))return"Expiration Time (exp) claim must be a present number";var r=M(n.payload.exp,e);return new Date(Date.now())>r?"Expiration Time (exp) claim expired ("+r+")":null}},q=function(t,e){return void 0===t&&(t=!0),void 0===e&&(e=40),function(n){if(!t&&!n.payload.nbf)return null;if(!g(n.payload.nbf))return"Not Before (nbf) claim must be a present number";var r=M(n.payload.nbf,-1*e),o=new Date(Date.now());return o<r?"Not Before (nbf) claim cannot be used yet, current time ("+o+") is before ("+r+")":null}},J=function(t,e,n){return e!==n?t+' mismatch: expected "'+e+'" but got "'+n+'"':null},M=function(t,e){void 0===e&&(e=0);var n=new Date(0);return n.setUTCSeconds(t+e),n},B=function(){function t(t){this.opts=t,this.loginStateKey="crossid-spa-js|login",this.logoutStateKey="crossid-spa-js|logout",this.scope=t.scope,this.state=this._stateFactory(this.opts.state_type||"session_storage"),this.cache=this._cacheFactory(this.opts.cache_type||"memory"),this._purgeIndex()}return t.prototype.createRedirectURL=function(t){return void 0===t&&(t={}),d(this,void 0,void 0,(function(){var e;return p(this,(function(n){switch(n.label){case 0:return[4,this._createAuthorizationData(t)];case 1:return e=n.sent(),[4,this._persistAuthorizationData(e)];case 2:return n.sent(),[2,this._authorizeUrl(e.request)]}}))}))},t.prototype.loginWithRedirect=function(t){return d(this,void 0,void 0,(function(){var e;return p(this,(function(n){switch(n.label){case 0:return[4,this.createRedirectURL(t)];case 1:return e=n.sent(),window.location.replace(e),[2]}}))}))},t.prototype.handleRedirectCallback=function(t){return void 0===t&&(t=new URL(window.location.href)),d(this,void 0,void 0,(function(){var e,n,r,o,i,a,s;return p(this,(function(u){switch(u.label){case 0:if(e=t.searchParams,n=e.get("code"),r=e.get("error"))throw new Error(r);if(!(null==(o=this.state.get(this.loginStateKey))?void 0:o.code_verifier))throw new Error("invalid state, try sign-in again");return[4,v({tokenEndpoint:this.opts.token_endpoint,client_id:this.opts.client_id,code_verifier:o.code_verifier,grant_type:"authorization_code",redirect_uri:o.redirect_uri,code:n})];case 1:return i=u.sent(),(a=U(i.id_token)).payload.__bearer=i.id_token,s=U(i.access_token),this.state.remove(this.loginStateKey),this._assertAccessToken(s,o.audience),this._assertIDToken(a,o.nonce),s.payload._raw=i.access_token,this._cacheTokens(a,s,i.refresh_token),[2,{state:o.state}]}}))}))},t.prototype.getUser=function(t){return void 0===t&&(t={}),d(this,void 0,void 0,(function(){var e,n,r,o;return p(this,(function(i){return e=t.audience||this.opts.audience,n=E(this.scope,t.scope),r=this._getTokensKeysFromCache("id_token",e,n),[2,null==(o=this._getNarrowedKey(r))?void 0:o.payload]}))}))},t.prototype.getAccessToken=function(t){var e;return void 0===t&&(t={}),d(this,void 0,void 0,(function(){var n,r,o,i;return p(this,(function(a){return n=t.audience||this.opts.audience,r=E(this.scope,t.scope),o=this._getTokensKeysFromCache("access_token",n,r),i=this._getNarrowedKey(o),[2,null===(e=null==i?void 0:i.payload)||void 0===e?void 0:e._raw]}))}))},t.prototype.createLogoutRedirectURL=function(t){return void 0===t&&(t={}),d(this,void 0,void 0,(function(){var e;return p(this,(function(n){switch(n.label){case 0:return[4,this._createLogoutData(t)];case 1:return(e=n.sent()).request.id_token_hint&&t.post_logout_redirect_uri?[4,this._persistLogoutData(e)]:(this._removeTokens(e.audience,e.scopes),[2,this._logoutUrl()]);case 2:return n.sent(),[2,this._logoutUrl(e.request)]}}))}))},t.prototype.logoutWithRedirect=function(t){return d(this,void 0,void 0,(function(){var e;return p(this,(function(n){switch(n.label){case 0:return[4,this.createLogoutRedirectURL(t)];case 1:return e=n.sent(),window.location.assign(e),[2]}}))}))},t.prototype.handleLogoutRedirectCallback=function(t){return void 0===t&&(t=new URL(window.location.href)),d(this,void 0,void 0,(function(){var e,n,r,o;return p(this,(function(i){if(e=t.searchParams,n=e.get("state"),r=e.get("error"))throw new Error(r);if(!(null==(o=this.state.get(this.logoutStateKey))?void 0:o.state)||o.state!==n)throw new Error("invalid state, try sign-in again");return this._removeTokens(o.audience,o.scopes),this.state.remove(this.logoutStateKey),[2,{state:o.appState}]}))}))},t.prototype._createAuthorizationData=function(t){return d(this,void 0,void 0,(function(){var e,n,r,o,i,a;return p(this,(function(s){switch(s.label){case 0:return e=C(A(j())),n=C(A(j())),r=j(),[4,(c=r,d(void 0,void 0,void 0,(function(){return p(this,(function(t){switch(t.label){case 0:return[4,window.crypto.subtle.digest({name:"SHA-256"},(new TextEncoder).encode(c))];case 1:return[2,t.sent()]}}))})))];case 1:return o=s.sent(),u=new Uint8Array(o),i=C(A(String.fromCharCode.apply(String,Array.from(u)))),a={audience:t.audience,redirect_uri:t.redirect_uri,response_type:t.response_type,scope:t.scope,state:e,nonce:n,code_challenge:i},[2,{request:this._mergeAuthorizationCodeParams(a),code_verifier:r,appState:t.state}]}var u,c}))}))},t.prototype._persistAuthorizationData=function(t){return d(this,void 0,void 0,(function(){var e,n;return p(this,(function(r){return e=t.request,n={audience:e.audience,redirect_uri:e.redirect_uri,scope:e.scope,nonce:e.nonce,state:t.appState,code_verifier:t.code_verifier},this.state.set(this.loginStateKey,n),[2]}))}))},t.prototype._mergeAuthorizationCodeParams=function(t){return{client_id:this.opts.client_id,audience:t.audience||this.opts.audience,response_type:t.response_type||this.opts.response_type||"code",redirect_uri:t.redirect_uri||this.opts.redirect_uri,nonce:t.nonce,state:t.state,scope:t.scope||this.opts.scope,code_challenge:t.code_challenge,code_challenge_method:"S256"}},t.prototype._createLogoutData=function(t){return d(this,void 0,void 0,(function(){var e,n;return p(this,(function(r){switch(r.label){case 0:return(e={id_token_hint:t.id_token_hint,post_logout_redirect_uri:t.post_logout_redirect_uri}).id_token_hint||null===e.id_token_hint?[3,2]:[4,this.getUser()];case 1:(n=r.sent())&&n.__bearer&&(e.id_token_hint=n.__bearer),r.label=2;case 2:return e.id_token_hint&&(e.state=C(A(j()))),[2,{request:e,audience:t.audience||this.opts.audience,scopes:(t.scope||this.scope).split(" "),appState:t.state}]}}))}))},t.prototype._persistLogoutData=function(t){return d(this,void 0,void 0,(function(){var e;return p(this,(function(n){return e={client_id:this.opts.client_id,audience:t.audience,scopes:t.scopes,post_logout_redirect_uri:t.request.post_logout_redirect_uri,state:t.request.state,appState:t.appState},this.state.set(this.logoutStateKey,e),[2]}))}))},t.prototype._assertAccessToken=function(t,e){K(t,L(["alg"],["iss","sub","aud","exp","iat"]),z(this.opts.issuer),F("RS256"),N(e),W(!0),q(!0))},t.prototype._assertIDToken=function(t,e){var n;return K(t,L(["alg"],["iss","sub","aud","nonce","exp","iat"]),z(this.opts.issuer),(n=e,function(t){return J("Nonce (nonce)",n,t.payload.nonce)}),F("RS256"),N([this.opts.client_id]),W(!0),q(!1),(function(t){return g(t.payload.iat)?null:"Issued At (iat) claim must be a present number"})),null},t.prototype._removeTokens=function(t,e){var n=this,r=this._getTokensKeysFromCache("id_token",t,e),o=this._getTokensKeysFromCache("access_token",t,e),i=this._getTokensKeysFromCache("refresh_token",t,e);r.concat(o).concat(i).forEach((function(t){return n.cache.remove(t)})),this._purgeIndex()},t.prototype._authorizeUrl=function(t){return this.opts.authorization_endpoint+"?"+P(t)},t.prototype._logoutUrl=function(t){var e=this.opts.logout_endpoint;return t&&(e=e+"?"+P(t)),e},t.prototype._stateFactory=function(t){var e={ttl:300};switch(t){case"local_storage":return new S(e);case"session_storage":return new k(e);default:throw new Error('Invalid cache type "'+t+'"')}},t.prototype._cacheFactory=function(t){switch(t){case"memory":return new x;case"local_storage":return new S({purgeOnInit:!0});case"session_storage":return new k({purgeOnInit:!0});default:throw new Error('Invalid cache type "'+t+'"')}},t.prototype._cacheTokens=function(t,e,n){var r=this._ttlFromToken(e);this._cacheToken("access_token",e,this.opts.client_id,e.payload.aud,e.payload.scp,r),this._cacheToken("id_token",t,this.opts.client_id,e.payload.aud,e.payload.scp||[],this._ttlFromToken(t)),n&&this._cacheToken("refresh_token",n,this.opts.client_id,e.payload.aud,e.payload.scp,432e3)},t.prototype._cacheToken=function(t,e,n,r,o,i){var a=this._cacheKey({tokType:t,client_id:n,audience:r,scope:o});this.cache.set(a,e,{ttl:i});var s=[r[0]],u=this.cache.get(_);u||(u={}),s.forEach((function(t){u[t]||(u[t]={});var e=u[t];o.forEach((function(t){e[t]||(e[t]=[]),e[t].push(a)}))})),this.cache.set(_,u)},t.prototype._cacheKey=function(t){var e,n=t.tokType,r=t.client_id,o=t.audience,i=t.scope;return i&&i.length&&(i.sort(),e=i.join("__")),["crossid-spa-js",n,r,o.join("__"),e].join("|")},t.prototype._decodeKey=function(t){var e=t.split("|"),n=e[1],r=e[2],o=e[3];return{tokenType:n,audience:r.split("__"),scope:o.split("__")}},t.prototype._getTokensKeysFromCache=function(t,e,n){var r,o=this,i=(this.cache.get(_)||{})[e[0]];if(!i)return[];for(var a=function(e){return i[e]&&i[e].length?r?void(r=r.filter((function(t){return i[e].includes(t)}))):(r=i[e].filter((function(e){return o._decodeKey(e).tokenType===t})),"continue"):{value:[]}},s=0,u=n;s<u.length;s++){var c=a(u[s]);if("object"==typeof c)return c.value}return r},t.prototype._getNarrowedKey=function(t){for(var e in t){var n=this.cache.get(t[e]);if(n)return n}},t.prototype._ttlFromToken=function(t){return(new Date(1e3*t.payload.exp).getTime()-Date.now())/1e3},t.prototype._purgeIndex=function(){var t=this.cache.get(_);if(t){for(var e=0,n=Object.entries(t);e<n.length;e++){for(var r=n[e],o=r[0],i=r[1],a=function(e,n){for(var r=[],i=0;i<n.length;i++)s.cache.get(n[i])||r.push(i);t[o][e]=n.filter((function(t,e){return-1===r.indexOf(e)})),t[o][e].length||delete t[o][e]},s=this,u=0,c=Object.entries(i);u<c.length;u++){var l=c[u];a(l[0],l[1])}Object.keys(t[o]).length||delete t[o]}this.cache.set(_,t)}},t}();function G(t){return d(this,void 0,void 0,(function(){var e,n,r,o,i,a;return p(this,(function(s){switch(s.label){case 0:return e=t.tenant_id,n=t.auth_server,r=void 0===n?"default":n,o=l(t,["tenant_id","auth_server"]),i=o,[4,f({wellknown_endpoint:"https://"+e+".crossid.io/oauth2/"+r+"/.well-known/openid-configuration"})];case 1:return a=s.sent(),i.authorization_endpoint=a.authorization_endpoint,i.token_endpoint=a.token_endpoint,i.issuer=a.issuer,i.logout_endpoint=i.issuer+"logout",[2,new B(i)]}}))}))}function H(t){return d(this,void 0,void 0,(function(){var e,n,r;return p(this,(function(o){switch(o.label){case 0:return[4,f(t)];case 1:return e=o.sent(),n=l(t,["wellknown_endpoint"]),(r=n).authorization_endpoint=e.authorization_endpoint,r.token_endpoint=e.token_endpoint,r.issuer=e.issuer,[2,new B(r)]}}))}))}function V(t){return d(this,void 0,void 0,(function(){return p(this,(function(e){return[2,new B(t)]}))}))}function Q(){throw new Error("please wrap your app with CrossidAuthProvider")}var X=e.createContext({loading:!0}),Y=e.createContext({loginWithRedirect:Q,logoutWithRedirect:Q,getAccessToken:Q}),Z=function(){return e.useContext(X)},$=function(){return e.useContext(Y)},tt=function(t){return i(void 0,void 0,void 0,(function(){var e,n,r,o;return a(this,(function(i){switch(i.label){case 0:return r=e=t,o=e,(n=e).tenant_id?[4,G(n)]:[3,2];case 1:return[2,i.sent()];case 2:return r.wellknown_endpoint?[4,H(r)]:[3,4];case 3:return[2,i.sent()];case 4:return o.authorization_endpoint&&o.token_endpoint&&o.issuer?[4,V(o)]:[3,6];case 5:return[2,i.sent()];case 6:throw new Error("invalid props")}}))}))},et=new RegExp(/returnTo=(.*)/i);var nt=function(){return""+window.location.pathname+window.location.search};t.CrossidAuthProvider=function(t){var n=e.useState(),o=n[0],s=n[1],u=e.useState(),c=u[0],l=u[1],d=e.useState(!0),p=d[0],h=d[1],f=e.useState(),v=f[0],_=f[1],y=e.useState(""),g=y[0],w=y[1],m=t.goTo;e.useEffect((function(){i(void 0,void 0,void 0,(function(){var e,n,r,i,u,c,d,p,f,v;return a(this,(function(a){switch(a.label){case 0:if(o)return[3,6];a.label=1;case 1:return a.trys.push([1,4,5,6]),[4,tt(t)];case 2:return e=a.sent(),s(e),[4,e.getUser()];case 3:return n=a.sent(),_(n),[3,6];case 4:return r=a.sent(),l(r),h(!1),[3,6];case 5:return[2];case 6:return i=window.location,u=i.origin,c=i.pathname,d=new URLSearchParams(window.location.search),o&&u+c===t.redirect_uri&&d.has("code")&&!d.has("error")?[4,o.handleRedirectCallback()]:[3,9];case 7:return f=a.sent(),[4,o.getUser()];case 8:return p=a.sent(),_(p),h(!1),(null==f?void 0:f.state)&&(w(f.state),v=(f.state.match(et)||[])[1],m?m(v):window.history.replaceState({},document.title,f.state)),[3,12];case 9:return o&&u+c===t.post_logout_redirect_uri&&d.has("state")&&!d.has("error")?[4,o.handleLogoutRedirectCallback()]:[3,11];case 10:return f=a.sent(),_(void 0),h(!1),(null==f?void 0:f.state)&&(v=(f.state.match(et)||[])[1],m?m(v):window.history.replaceState({},document.title,v)),[3,12];case 11:h(!1),a.label=12;case 12:return[2]}}))}))}),[o,m,t]);var b=e.useCallback((function(t,e){void 0===t&&(t={}),t.state=t.state+"&returnTo="+e,null==o||o.loginWithRedirect(t)}),[o]),k=e.useCallback((function(e){void 0===e&&(e={}),e.post_logout_redirect_uri||(e.post_logout_redirect_uri=t.post_logout_redirect_uri),null==o||o.logoutWithRedirect(e)}),[o]),S=e.useCallback((function(t){return void 0===t&&(t={}),i(void 0,void 0,void 0,(function(){return a(this,(function(e){switch(e.label){case 0:return[4,null==o?void 0:o.getAccessToken(t)];case 1:return[2,e.sent()||""]}}))}))}),[o]);return r.default.createElement(X.Provider,{value:{user:v,error:c,loading:p,client:o,loginState:g}},r.default.createElement(Y.Provider,{value:{loginWithRedirect:b,logoutWithRedirect:k,getAccessToken:S}},t.children))},t.useAuth=Z,t.useAuthActions=$,t.withAuth=function(t,n,s){var u=this;return void 0===s&&(s={}),function(c){var l=e.useRef(!1),d=e.useState(!1),p=d[0],h=d[1],f=e.useState(!1),v=f[0],_=f[1],y=Z(),g=y.loading,w=y.client,m=y.loginState,b=void 0===m?"":m,k=$().loginWithRedirect,S=s.returnTo,T=void 0===S?nt():S,x=s.scope,O=void 0===x?"":x;return O=("openid "+O).trim(),e.useEffect((function(){if(l.current=!0,!g&&!p)return i(u,void 0,void 0,(function(){return a(this,(function(t){switch(t.label){case 0:return[4,null==w?void 0:w.getUser()];case 1:return t.sent()&&l?(h(!0),[2]):[3,2];case 2:return b.indexOf("attemptedLogin=true")>-1?(_(!0),[3,5]):[3,3];case 3:return[4,k({scope:O,state:"attemptedLogin=true"},T)];case 4:t.sent(),t.label=5;case 5:return[2]}}))})),function(){l.current=!1}}),[p,w,g,k,T]),p?v?n():r.default.createElement(t,o({},c)):r.default.createElement("div",null,"Loading...")}},Object.defineProperty(t,"__esModule",{value:!0})})); | ||
//# sourceMappingURL=crossid-react.js.map |
@@ -29,3 +29,3 @@ import t,{createContext as e,useContext as n,useState as r,useEffect as o,useCallback as i,useRef as a}from"react"; | ||
PERFORMANCE OF THIS SOFTWARE. | ||
***************************************************************************** */var l=function(t,e){return(l=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])})(t,e)};function p(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function n(){this.constructor=t}l(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}var h=function(){return(h=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var o in e=arguments[n])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t}).apply(this,arguments)};function d(t,e){var n={};for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&e.indexOf(r)<0&&(n[r]=t[r]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(r=Object.getOwnPropertySymbols(t);o<r.length;o++)e.indexOf(r[o])<0&&Object.prototype.propertyIsEnumerable.call(t,r[o])&&(n[r[o]]=t[r[o]])}return n}function f(t,e,n,r){return new(n||(n=Promise))((function(o,i){function a(t){try{u(r.next(t))}catch(t){i(t)}}function s(t){try{u(r.throw(t))}catch(t){i(t)}}function u(t){var e;t.done?o(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(a,s)}u((r=r.apply(t,e||[])).next())}))}function v(t,e){var n,r,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function s(i){return function(s){return function(i){if(n)throw new TypeError("Generator is already executing.");for(;a;)try{if(n=1,r&&(o=2&i[0]?r.return:i[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,i[1])).done)return o;switch(r=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return a.label++,{value:i[1],done:!1};case 5:a.label++,r=i[1],i=[0];continue;case 7:i=a.ops.pop(),a.trys.pop();continue;default:if(!((o=(o=a.trys).length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){a=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){a.label=i[1];break}if(6===i[0]&&a.label<o[1]){a.label=o[1],o=i;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(i);break}o[2]&&a.ops.pop(),a.trys.pop();continue}i=e.call(t,a)}catch(t){i=[6,t],r=0}finally{n=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,s])}}}var _=function(t){function e(n,r){var o=t.call(this,r)||this;return o.error=n,o.error_description=r,Object.setPrototypeOf(o,e.prototype),o}return p(e,t),e.create=function(t){return new e(t.error,t.error_description)},e}(Error);function y(t){var e=t.wellknown_endpoint;return f(this,void 0,void 0,(function(){var t;return v(this,(function(n){switch(n.label){case 0:return[4,fetch(e)];case 1:return 200!==(t=n.sent()).status?[3,3]:[4,t.json()];case 2:return[2,n.sent()];case 3:throw Error("failed to discover data from well known discovery endpoint.")}}))}))}function g(t){var e=t.tokenEndpoint,n=t.timeout,r=d(t,["tokenEndpoint","timeout"]);return f(this,void 0,void 0,(function(){var t,o,i;return v(this,(function(a){switch(a.label){case 0:return t=new FormData,Object.keys(r).forEach((function(e){return t.append(e,r[e])})),[4,(s=e,u={timeout:n,method:"POST",body:t},void 0===u&&(u={}),f(void 0,void 0,void 0,(function(){var t,e,n,r,o;return v(this,(function(i){switch(i.label){case 0:return t=u.timeout,e=void 0===t?5e3:t,n=new AbortController,r=setTimeout((function(){return n.abort()}),e),[4,fetch(s,h(h({},u),{signal:n.signal}))];case 1:return o=i.sent(),clearTimeout(r),[2,o]}}))})))];case 1:return[4,(o=a.sent()).json()];case 2:if(i=a.sent(),200!==o.status)throw _.create(i);return[2,i]}var s,u}))}))}var w="crossid-spa-js|index",m=function(t){return null!==t&&"Object"===(null==t?void 0:t.constructor.name)},b=function(t){return"number"==typeof t},k=function(t){return"string"==typeof t},T=String.fromCharCode(0),S=function(){function t(t,e){void 0===e&&(e={}),this.gopts=e,this.storage=t,e.purgeOnInit&&this.purge(!0)}return t.prototype.set=function(t,e,n){var r=this._key(t),o=this._ttl(null==n?void 0:n.ttl);try{var i=function(t,e){var n;return e&&e>0?((n={})[T]=t,n.ttl=Date.now()+1e3*e,n):t}(e,o);return this.storage.setItem(r,JSON.stringify(i)),!0}catch(t){return!1}},t.prototype.get=function(t,e){var n,r=this._key(t);try{var o=this.storage.getItem(r);if(null===o)return;var i=JSON.parse(o);return m(n=i)&&T in n?function(t,e){return Date.now()-1e3*e>t.ttl}(i,(null==e?void 0:e.ttl)||0)?void this.storage.removeItem(t):i[T]:i}catch(t){return}},t.prototype.remove=function(t){var e=this._key(t);return!!this.storage.getItem(e)&&(this.storage.removeItem(e),!0)},t.prototype.purge=function(t){var e=this;void 0===t&&(t=!1);var n=0;return Object.keys(this.storage).filter((function(t){return t.startsWith(e.gopts.prefix)})).forEach((function(r){var o=e.storage.getItem(r);if(o){var i;try{i=JSON.parse(o)}catch(t){return}(m(i)&&T in i&&Date.now()>i.ttl||t)&&(n++,e.storage.removeItem(r))}})),n},t.prototype._key=function(t){return this.gopts.prefix?""+this.gopts.prefix+t:t},t.prototype._ttl=function(t){return null===t?null:t||this.gopts.ttl||null},t}(),O=function(t){function e(e){var n=t.call(this,sessionStorage,e)||this;if("undefined"==typeof sessionStorage)throw Error("no support for session storage.");return n}return p(e,t),e}(S),E=function(t){function e(e){var n=t.call(this,localStorage,e)||this;if("undefined"==typeof localStorage)throw Error("no support for local storage.");return n}return p(e,t),e}(S),x=function(){function t(){}return t.prototype.getItem=function(t){return t in this?this[t]:null},t.prototype.setItem=function(t,e){void 0===e?this.removeItem(t):this[t]=""+e},t.prototype.removeItem=function(t){this.hasOwnProperty(t)&&delete this[t]},t}(),j=function(t){function e(e){return t.call(this,new x,e)||this}return p(e,t),e}(S),A=function(t){return Array.from(new Set(t))},R=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return A(t.join(" ").trim().split(/\s+/))},I=function(){return t="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~",e=Math.floor(86*Math.random()+43),Array.from(window.crypto.getRandomValues(new Uint8Array(e))).map((function(e){return t[e%t.length]})).join("");var t,e},C=function(t){return btoa(t)},U=function(t){return atob(t)},D=function(t){var e={"+":"-","/":"_","=":""};return t.replace(/[+/=]/g,(function(t){return e[t]}))},K=function(t){var e={"-":"+",_:"/"};return t.replace(/[-_]/g,(function(t){return e[t]}))},P=function(t){return Object.keys(t).filter((function(e){return void 0!==t[e]&&null!==t[e]})).map((function(e){return encodeURIComponent(e)+"="+encodeURIComponent(t[e])})).join("&")},L=function(t){var e=t.split("."),n=e[0],r=e[1],o=e[2];if(3!==e.length||!n||!r||!o)throw new Error("malformed JWT token.");var i,a=JSON.parse(U(K(r)));if(a.aud)if(k(a.aud))a.aud=[a.aud];else if(i=a.aud,!Array.isArray(i)||!i.every((function(t){return k(t)})))throw new Error("Audience (aud) must be a string or an array of strings");return{header:JSON.parse(U(K(n))),payload:a}},z=function(t){function e(e){for(var n=[],r=1;r<arguments.length;r++)n[r-1]=arguments[r];var o=t.apply(this,n)||this;return o.fields_=e,o}return p(e,t),e.prototype.getMissingFields=function(){return this.fields_},e}(Error),F=function(t){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];var r=e.map((function(e){return e(t)})).filter((function(t){return null!==t}));if(r&&r.length)throw new z(r,r.join(","));return null},N=function(t,e){var n=[];return function(r){return e&&e.filter((function(t){return!r.payload[t]})).forEach((function(t){return n.push(t)})),t&&t.filter((function(t){return!r.header[t]})).forEach((function(t){return n.push(t)})),n&&n.length?n.join(",")+" missing":null}},W=function(t){return function(e){return G("Issuer (iss)",t,e.payload.iss)}},q=function(t){return function(e){return G("Issuer (iss) claim",t,e.header.alg)}},J=function(t){return void 0===t&&(t=[]),function(e){return e.payload.aud&&e.payload.aud.length?(n=t,e.payload.aud.every((function(t){return n.includes(t)}))?null:"Audience (aud) claim mismatch: audience ("+e.payload.aud+") must contain only ("+t+")"):"Audience (aud) claim must be a present string";var n}},M=function(t,e){return void 0===t&&(t=!0),void 0===e&&(e=40),function(n){if(!t&&!n.payload.exp)return null;if(!b(n.payload.exp))return"Expiration Time (exp) claim must be a present number";var r=H(n.payload.exp,e);return new Date(Date.now())>r?"Expiration Time (exp) claim expired ("+r+")":null}},B=function(t,e){return void 0===t&&(t=!0),void 0===e&&(e=40),function(n){if(!t&&!n.payload.nbf)return null;if(!b(n.payload.nbf))return"Not Before (nbf) claim must be a present number";var r=H(n.payload.nbf,-1*e),o=new Date(Date.now());return o<r?"Not Before (nbf) claim cannot be used yet, current time ("+o+") is before ("+r+")":null}},G=function(t,e,n){return e!==n?t+' mismatch: expected "'+e+'" but got "'+n+'"':null},H=function(t,e){void 0===e&&(e=0);var n=new Date(0);return n.setUTCSeconds(t+e),n},V=function(){function t(t){this.opts=t,this.loginStateKey="crossid-spa-js|login",this.logoutStateKey="crossid-spa-js|logout",this.scope=t.scope,this.state=this._stateFactory(this.opts.state_type||"session_storage"),this.cache=this._cacheFactory(this.opts.cache_type||"memory"),this._purgeIndex()}return t.prototype.createRedirectURL=function(t){return void 0===t&&(t={}),f(this,void 0,void 0,(function(){var e;return v(this,(function(n){switch(n.label){case 0:return[4,this._createAuthorizationData(t)];case 1:return e=n.sent(),[4,this._persistAuthorizationData(e)];case 2:return n.sent(),[2,this._authorizeUrl(e.request)]}}))}))},t.prototype.loginWithRedirect=function(t){return f(this,void 0,void 0,(function(){var e;return v(this,(function(n){switch(n.label){case 0:return[4,this.createRedirectURL(t)];case 1:return e=n.sent(),window.location.replace(e),[2]}}))}))},t.prototype.handleRedirectCallback=function(t){return void 0===t&&(t=new URL(window.location.href)),f(this,void 0,void 0,(function(){var e,n,r,o,i,a,s;return v(this,(function(u){switch(u.label){case 0:if(e=t.searchParams,n=e.get("code"),r=e.get("error"))throw new Error(r);if(!(null==(o=this.state.get(this.loginStateKey))?void 0:o.code_verifier))throw new Error("invalid state, try sign-in again");return[4,g({tokenEndpoint:this.opts.token_endpoint,client_id:this.opts.client_id,code_verifier:o.code_verifier,grant_type:"authorization_code",redirect_uri:o.redirect_uri,code:n})];case 1:return i=u.sent(),(a=L(i.id_token)).payload.__bearer=i.id_token,s=L(i.access_token),this.state.remove(this.loginStateKey),this._assertAccessToken(s,o.audience),this._assertIDToken(a,o.nonce),s.payload._raw=i.access_token,this._cacheTokens(a,s,i.refresh_token),[2,{state:o.state}]}}))}))},t.prototype.getUser=function(t){return void 0===t&&(t={}),f(this,void 0,void 0,(function(){var e,n,r,o;return v(this,(function(i){return e=t.audience||this.opts.audience,n=R(this.scope,t.scope),r=this._getTokensKeysFromCache("id_token",e,n),[2,null==(o=this._getNarrowedKey(r))?void 0:o.payload]}))}))},t.prototype.getAccessToken=function(t){var e;return void 0===t&&(t={}),f(this,void 0,void 0,(function(){var n,r,o,i;return v(this,(function(a){return n=t.audience||this.opts.audience,r=R(this.scope,t.scope),o=this._getTokensKeysFromCache("access_token",n,r),i=this._getNarrowedKey(o),[2,null===(e=null==i?void 0:i.payload)||void 0===e?void 0:e._raw]}))}))},t.prototype.createLogoutRedirectURL=function(t){return void 0===t&&(t={}),f(this,void 0,void 0,(function(){var e;return v(this,(function(n){switch(n.label){case 0:return[4,this._createLogoutData(t)];case 1:return(e=n.sent()).request.id_token_hint&&t.post_logout_redirect_uri?[4,this._persistLogoutData(e)]:(this._removeTokens(e.audience,e.scopes),[2,this._logoutUrl()]);case 2:return n.sent(),[2,this._logoutUrl(e.request)]}}))}))},t.prototype.logoutWithRedirect=function(t){return f(this,void 0,void 0,(function(){var e;return v(this,(function(n){switch(n.label){case 0:return[4,this.createLogoutRedirectURL(t)];case 1:return e=n.sent(),window.location.assign(e),[2]}}))}))},t.prototype.handleLogoutRedirectCallback=function(t){return void 0===t&&(t=new URL(window.location.href)),f(this,void 0,void 0,(function(){var e,n,r,o;return v(this,(function(i){if(e=t.searchParams,n=e.get("state"),r=e.get("error"))throw new Error(r);if(!(null==(o=this.state.get(this.logoutStateKey))?void 0:o.state)||o.state!==n)throw new Error("invalid state, try sign-in again");return this._removeTokens(o.audience,o.scopes),this.state.remove(this.logoutStateKey),[2,{state:o.appState}]}))}))},t.prototype._createAuthorizationData=function(t){return f(this,void 0,void 0,(function(){var e,n,r,o,i,a;return v(this,(function(s){switch(s.label){case 0:return e=D(C(I())),n=D(C(I())),r=I(),[4,(c=r,f(void 0,void 0,void 0,(function(){return v(this,(function(t){switch(t.label){case 0:return[4,window.crypto.subtle.digest({name:"SHA-256"},(new TextEncoder).encode(c))];case 1:return[2,t.sent()]}}))})))];case 1:return o=s.sent(),u=new Uint8Array(o),i=D(C(String.fromCharCode.apply(String,Array.from(u)))),a={audience:t.audience,redirect_uri:t.redirect_uri,response_type:t.response_type,scope:t.scope,state:e,nonce:n,code_challenge:i},[2,{request:this._mergeAuthorizationCodeParams(a),code_verifier:r,appState:t.state}]}var u,c}))}))},t.prototype._persistAuthorizationData=function(t){return f(this,void 0,void 0,(function(){var e,n;return v(this,(function(r){return e=t.request,n={audience:e.audience,redirect_uri:e.redirect_uri,scope:e.scope,nonce:e.nonce,state:t.appState,code_verifier:t.code_verifier},this.state.set(this.loginStateKey,n),[2]}))}))},t.prototype._mergeAuthorizationCodeParams=function(t){return{client_id:this.opts.client_id,audience:t.audience||this.opts.audience,response_type:t.response_type||this.opts.response_type||"code",redirect_uri:t.redirect_uri||this.opts.redirect_uri,nonce:t.nonce,state:t.state,scope:t.scope||this.opts.scope,code_challenge:t.code_challenge,code_challenge_method:"S256"}},t.prototype._createLogoutData=function(t){return f(this,void 0,void 0,(function(){var e,n;return v(this,(function(r){switch(r.label){case 0:return(e={id_token_hint:t.id_token_hint,post_logout_redirect_uri:t.post_logout_redirect_uri}).id_token_hint||null===e.id_token_hint?[3,2]:[4,this.getUser()];case 1:(n=r.sent())&&n.__bearer&&(e.id_token_hint=n.__bearer),r.label=2;case 2:return e.id_token_hint&&(e.state=D(C(I()))),[2,{request:e,audience:t.audience||this.opts.audience,scopes:(t.scope||this.scope).split(" "),appState:t.state}]}}))}))},t.prototype._persistLogoutData=function(t){return f(this,void 0,void 0,(function(){var e;return v(this,(function(n){return e={client_id:this.opts.client_id,audience:t.audience,scopes:t.scopes,post_logout_redirect_uri:t.request.post_logout_redirect_uri,state:t.request.state,appState:t.appState},this.state.set(this.logoutStateKey,e),[2]}))}))},t.prototype._assertAccessToken=function(t,e){F(t,N(["alg"],["iss","sub","aud","exp","iat"]),W(this.opts.issuer),q("RS256"),J(e),M(!0),B(!0))},t.prototype._assertIDToken=function(t,e){var n;return F(t,N(["alg"],["iss","sub","aud","nonce","exp","iat"]),W(this.opts.issuer),(n=e,function(t){return G("Nonce (nonce)",n,t.payload.nonce)}),q("RS256"),J([this.opts.client_id]),M(!0),B(!1),(function(t){return b(t.payload.iat)?null:"Issued At (iat) claim must be a present number"})),null},t.prototype._removeTokens=function(t,e){var n=this,r=this._getTokensKeysFromCache("id_token",t,e),o=this._getTokensKeysFromCache("access_token",t,e),i=this._getTokensKeysFromCache("refresh_token",t,e);r.concat(o).concat(i).forEach((function(t){return n.cache.remove(t)})),this._purgeIndex()},t.prototype._authorizeUrl=function(t){return this.opts.authorization_endpoint+"?"+P(t)},t.prototype._logoutUrl=function(t){var e=this.opts.logout_endpoint;return t&&(e=e+"?"+P(t)),e},t.prototype._stateFactory=function(t){var e={ttl:300};switch(t){case"local_storage":return new E(e);case"session_storage":return new O(e);default:throw new Error('Invalid cache type "'+t+'"')}},t.prototype._cacheFactory=function(t){switch(t){case"memory":return new j;case"local_storage":return new E({purgeOnInit:!0});case"session_storage":return new O({purgeOnInit:!0});default:throw new Error('Invalid cache type "'+t+'"')}},t.prototype._cacheTokens=function(t,e,n){var r=this._ttlFromToken(e);this._cacheToken("access_token",e,this.opts.client_id,e.payload.aud,e.payload.scp,r),this._cacheToken("id_token",t,this.opts.client_id,e.payload.aud,e.payload.scp||[],this._ttlFromToken(t)),n&&this._cacheToken("refresh_token",n,this.opts.client_id,e.payload.aud,e.payload.scp,432e3)},t.prototype._cacheToken=function(t,e,n,r,o,i){var a=this._cacheKey({tokType:t,client_id:n,audience:r,scope:o});this.cache.set(a,e,{ttl:i});var s=[r[0]],u=this.cache.get(w);u||(u={}),s.forEach((function(t){u[t]||(u[t]={});var e=u[t];o.forEach((function(t){e[t]||(e[t]=[]),e[t].push(a)}))})),this.cache.set(w,u)},t.prototype._cacheKey=function(t){var e,n=t.tokType,r=t.client_id,o=t.audience,i=t.scope;return i&&i.length&&(i.sort(),e=i.join("__")),["crossid-spa-js",n,r,o.join("__"),e].join("|")},t.prototype._decodeKey=function(t){var e=t.split("|"),n=e[1],r=e[2],o=e[3];return{tokenType:n,audience:r.split("__"),scope:o.split("__")}},t.prototype._getTokensKeysFromCache=function(t,e,n){var r,o=this,i=(this.cache.get(w)||{})[e[0]];if(!i)return[];for(var a=function(e){return i[e]&&i[e].length?r?void(r=r.filter((function(t){return i[e].includes(t)}))):(r=i[e].filter((function(e){return o._decodeKey(e).tokenType===t})),"continue"):{value:[]}},s=0,u=n;s<u.length;s++){var c=a(u[s]);if("object"==typeof c)return c.value}return r},t.prototype._getNarrowedKey=function(t){for(var e in t){var n=this.cache.get(t[e]);if(n)return n}},t.prototype._ttlFromToken=function(t){return(new Date(1e3*t.payload.exp).getTime()-Date.now())/1e3},t.prototype._purgeIndex=function(){var t=this.cache.get(w);if(t){for(var e=0,n=Object.entries(t);e<n.length;e++){for(var r=n[e],o=r[0],i=r[1],a=function(e,n){for(var r=[],i=0;i<n.length;i++)s.cache.get(n[i])||r.push(i);t[o][e]=n.filter((function(t,e){return-1===r.indexOf(e)})),t[o][e].length||delete t[o][e]},s=this,u=0,c=Object.entries(i);u<c.length;u++){var l=c[u];a(l[0],l[1])}Object.keys(t[o]).length||delete t[o]}this.cache.set(w,t)}},t}();function Q(t){return f(this,void 0,void 0,(function(){var e,n,r,o,i,a;return v(this,(function(s){switch(s.label){case 0:return e=t.tenant_id,n=t.auth_server,r=void 0===n?"default":n,o=d(t,["tenant_id","auth_server"]),i=o,[4,y({wellknown_endpoint:"https://"+e+".crossid.io/oauth2/"+r+"/.well-known/openid-configuration"})];case 1:return a=s.sent(),i.authorization_endpoint=a.authorization_endpoint,i.token_endpoint=a.token_endpoint,i.issuer=a.issuer,i.logout_endpoint=i.issuer+"logout",[2,new V(i)]}}))}))}function X(t){return f(this,void 0,void 0,(function(){var e,n,r;return v(this,(function(o){switch(o.label){case 0:return[4,y(t)];case 1:return e=o.sent(),n=d(t,["wellknown_endpoint"]),(r=n).authorization_endpoint=e.authorization_endpoint,r.token_endpoint=e.token_endpoint,r.issuer=e.issuer,[2,new V(r)]}}))}))}function Y(t){return f(this,void 0,void 0,(function(){return v(this,(function(e){return[2,new V(t)]}))}))}function Z(){throw new Error("please wrap your app with CrossidAuthProvider")}var $=e({loading:!0}),tt=e({loginWithRedirect:Z,logoutWithRedirect:Z,getAccessToken:Z}),et=function(){return n($)},nt=function(){return n(tt)},rt=function(t){return u(void 0,void 0,void 0,(function(){var e,n,r,o;return c(this,(function(i){switch(i.label){case 0:return r=e=t,o=e,(n=e).tenant_id?[4,Q(n)]:[3,2];case 1:return[2,i.sent()];case 2:return r.wellknown_endpoint?[4,X(r)]:[3,4];case 3:return[2,i.sent()];case 4:return o.authorization_endpoint&&o.token_endpoint&&o.issuer?[4,Y(o)]:[3,6];case 5:return[2,i.sent()];case 6:throw new Error("invalid props")}}))}))},ot=function(e){var n=r(),a=n[0],s=n[1],l=r(),p=l[0],h=l[1],d=r(!0),f=d[0],v=d[1],_=r(),y=_[0],g=_[1],w=e.goTo;o((function(){u(void 0,void 0,void 0,(function(){var t,n,r,o,i,u,l,p,d;return c(this,(function(c){switch(c.label){case 0:if(a)return[3,6];c.label=1;case 1:return c.trys.push([1,4,5,6]),[4,rt(e)];case 2:return t=c.sent(),s(t),[4,t.getUser()];case 3:return n=c.sent(),g(n),[3,6];case 4:return r=c.sent(),h(r),v(!1),[3,6];case 5:return[2];case 6:return o=window.location,i=o.origin,u=o.pathname,l=new URLSearchParams(window.location.search),a&&i+u===e.redirect_uri&&l.has("code")&&!l.has("error")?[4,a.handleRedirectCallback()]:[3,9];case 7:return d=c.sent(),[4,a.getUser()];case 8:return p=c.sent(),g(p),v(!1),(null==d?void 0:d.state)&&(w?w(d.state):window.history.replaceState({},document.title,d.state)),[3,12];case 9:return a&&i+u===e.post_logout_redirect_uri&&l.has("state")&&!l.has("error")?[4,a.handleLogoutRedirectCallback()]:[3,11];case 10:return d=c.sent(),g(void 0),v(!1),(null==d?void 0:d.state)&&(w?w(d.state):window.history.replaceState({},document.title,d.state)),[3,12];case 11:v(!1),c.label=12;case 12:return[2]}}))}))}),[a,w,e]);var m=i((function(t,e){void 0===t&&(t={}),t.state=e,null==a||a.loginWithRedirect(t)}),[a]),b=i((function(t){void 0===t&&(t={}),t.post_logout_redirect_uri||(t.post_logout_redirect_uri=e.post_logout_redirect_uri),null==a||a.logoutWithRedirect(t)}),[a]),k=i((function(t){return void 0===t&&(t={}),u(void 0,void 0,void 0,(function(){return c(this,(function(e){switch(e.label){case 0:return[4,null==a?void 0:a.getAccessToken(t)];case 1:return[2,e.sent()||""]}}))}))}),[a]);return t.createElement($.Provider,{value:{user:y,error:p,loading:f,client:a}},t.createElement(tt.Provider,{value:{loginWithRedirect:m,logoutWithRedirect:b,getAccessToken:k}},e.children))};function it(e,n){var i=this;void 0===n&&(n={});return function(l){var p=a(!1),h=r(!1),d=h[0],f=h[1],v=et(),_=v.loading,y=v.client,g=nt().loginWithRedirect,w=n.returnTo,m=void 0===w?at():w;return o((function(){if(p.current=!0,!_&&!d)return u(i,void 0,void 0,(function(){return c(this,(function(t){switch(t.label){case 0:return[4,null==y?void 0:y.getUser()];case 1:return t.sent()&&p?(f(!0),[2]):[4,g({},m)];case 2:return t.sent(),[2]}}))})),function(){p.current=!1}}),[d,y,_,g,m]),d?t.createElement(e,s({},l)):t.createElement("div",null,"Loading...")}}var at=function(){return""+window.location.pathname+window.location.search};export{ot as CrossidAuthProvider,et as useAuth,nt as useAuthActions,it as withAuth}; | ||
***************************************************************************** */var l=function(t,e){return(l=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])})(t,e)};function p(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function n(){this.constructor=t}l(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}var d=function(){return(d=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var o in e=arguments[n])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t}).apply(this,arguments)};function h(t,e){var n={};for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&e.indexOf(r)<0&&(n[r]=t[r]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(r=Object.getOwnPropertySymbols(t);o<r.length;o++)e.indexOf(r[o])<0&&Object.prototype.propertyIsEnumerable.call(t,r[o])&&(n[r[o]]=t[r[o]])}return n}function f(t,e,n,r){return new(n||(n=Promise))((function(o,i){function a(t){try{u(r.next(t))}catch(t){i(t)}}function s(t){try{u(r.throw(t))}catch(t){i(t)}}function u(t){var e;t.done?o(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(a,s)}u((r=r.apply(t,e||[])).next())}))}function v(t,e){var n,r,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function s(i){return function(s){return function(i){if(n)throw new TypeError("Generator is already executing.");for(;a;)try{if(n=1,r&&(o=2&i[0]?r.return:i[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,i[1])).done)return o;switch(r=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return a.label++,{value:i[1],done:!1};case 5:a.label++,r=i[1],i=[0];continue;case 7:i=a.ops.pop(),a.trys.pop();continue;default:if(!((o=(o=a.trys).length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){a=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){a.label=i[1];break}if(6===i[0]&&a.label<o[1]){a.label=o[1],o=i;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(i);break}o[2]&&a.ops.pop(),a.trys.pop();continue}i=e.call(t,a)}catch(t){i=[6,t],r=0}finally{n=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,s])}}}var _=function(t){function e(n,r){var o=t.call(this,r)||this;return o.error=n,o.error_description=r,Object.setPrototypeOf(o,e.prototype),o}return p(e,t),e.create=function(t){return new e(t.error,t.error_description)},e}(Error);function y(t){var e=t.wellknown_endpoint;return f(this,void 0,void 0,(function(){var t;return v(this,(function(n){switch(n.label){case 0:return[4,fetch(e)];case 1:return 200!==(t=n.sent()).status?[3,3]:[4,t.json()];case 2:return[2,n.sent()];case 3:throw Error("failed to discover data from well known discovery endpoint.")}}))}))}function g(t){var e=t.tokenEndpoint,n=t.timeout,r=h(t,["tokenEndpoint","timeout"]);return f(this,void 0,void 0,(function(){var t,o,i;return v(this,(function(a){switch(a.label){case 0:return t=new FormData,Object.keys(r).forEach((function(e){return t.append(e,r[e])})),[4,(s=e,u={timeout:n,method:"POST",body:t},void 0===u&&(u={}),f(void 0,void 0,void 0,(function(){var t,e,n,r,o;return v(this,(function(i){switch(i.label){case 0:return t=u.timeout,e=void 0===t?5e3:t,n=new AbortController,r=setTimeout((function(){return n.abort()}),e),[4,fetch(s,d(d({},u),{signal:n.signal}))];case 1:return o=i.sent(),clearTimeout(r),[2,o]}}))})))];case 1:return[4,(o=a.sent()).json()];case 2:if(i=a.sent(),200!==o.status)throw _.create(i);return[2,i]}var s,u}))}))}var w="crossid-spa-js|index",m=function(t){return null!==t&&"Object"===(null==t?void 0:t.constructor.name)},b=function(t){return"number"==typeof t},k=function(t){return"string"==typeof t},T=String.fromCharCode(0),S=function(){function t(t,e){void 0===e&&(e={}),this.gopts=e,this.storage=t,e.purgeOnInit&&this.purge(!0)}return t.prototype.set=function(t,e,n){var r=this._key(t),o=this._ttl(null==n?void 0:n.ttl);try{var i=function(t,e){var n;return e&&e>0?((n={})[T]=t,n.ttl=Date.now()+1e3*e,n):t}(e,o);return this.storage.setItem(r,JSON.stringify(i)),!0}catch(t){return!1}},t.prototype.get=function(t,e){var n,r=this._key(t);try{var o=this.storage.getItem(r);if(null===o)return;var i=JSON.parse(o);return m(n=i)&&T in n?function(t,e){return Date.now()-1e3*e>t.ttl}(i,(null==e?void 0:e.ttl)||0)?void this.storage.removeItem(t):i[T]:i}catch(t){return}},t.prototype.remove=function(t){var e=this._key(t);return!!this.storage.getItem(e)&&(this.storage.removeItem(e),!0)},t.prototype.purge=function(t){var e=this;void 0===t&&(t=!1);var n=0;return Object.keys(this.storage).filter((function(t){return t.startsWith(e.gopts.prefix)})).forEach((function(r){var o=e.storage.getItem(r);if(o){var i;try{i=JSON.parse(o)}catch(t){return}(m(i)&&T in i&&Date.now()>i.ttl||t)&&(n++,e.storage.removeItem(r))}})),n},t.prototype._key=function(t){return this.gopts.prefix?""+this.gopts.prefix+t:t},t.prototype._ttl=function(t){return null===t?null:t||this.gopts.ttl||null},t}(),O=function(t){function e(e){var n=t.call(this,sessionStorage,e)||this;if("undefined"==typeof sessionStorage)throw Error("no support for session storage.");return n}return p(e,t),e}(S),x=function(t){function e(e){var n=t.call(this,localStorage,e)||this;if("undefined"==typeof localStorage)throw Error("no support for local storage.");return n}return p(e,t),e}(S),E=function(){function t(){}return t.prototype.getItem=function(t){return t in this?this[t]:null},t.prototype.setItem=function(t,e){void 0===e?this.removeItem(t):this[t]=""+e},t.prototype.removeItem=function(t){this.hasOwnProperty(t)&&delete this[t]},t}(),j=function(t){function e(e){return t.call(this,new E,e)||this}return p(e,t),e}(S),A=function(t){return Array.from(new Set(t))},R=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return A(t.join(" ").trim().split(/\s+/))},I=function(){return t="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~",e=Math.floor(86*Math.random()+43),Array.from(window.crypto.getRandomValues(new Uint8Array(e))).map((function(e){return t[e%t.length]})).join("");var t,e},C=function(t){return btoa(t)},U=function(t){return atob(t)},D=function(t){var e={"+":"-","/":"_","=":""};return t.replace(/[+/=]/g,(function(t){return e[t]}))},K=function(t){var e={"-":"+",_:"/"};return t.replace(/[-_]/g,(function(t){return e[t]}))},P=function(t){return Object.keys(t).filter((function(e){return void 0!==t[e]&&null!==t[e]})).map((function(e){return encodeURIComponent(e)+"="+encodeURIComponent(t[e])})).join("&")},L=function(t){var e=t.split("."),n=e[0],r=e[1],o=e[2];if(3!==e.length||!n||!r||!o)throw new Error("malformed JWT token.");var i,a=JSON.parse(U(K(r)));if(a.aud)if(k(a.aud))a.aud=[a.aud];else if(i=a.aud,!Array.isArray(i)||!i.every((function(t){return k(t)})))throw new Error("Audience (aud) must be a string or an array of strings");return{header:JSON.parse(U(K(n))),payload:a}},z=function(t){function e(e){for(var n=[],r=1;r<arguments.length;r++)n[r-1]=arguments[r];var o=t.apply(this,n)||this;return o.fields_=e,o}return p(e,t),e.prototype.getMissingFields=function(){return this.fields_},e}(Error),F=function(t){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];var r=e.map((function(e){return e(t)})).filter((function(t){return null!==t}));if(r&&r.length)throw new z(r,r.join(","));return null},N=function(t,e){var n=[];return function(r){return e&&e.filter((function(t){return!r.payload[t]})).forEach((function(t){return n.push(t)})),t&&t.filter((function(t){return!r.header[t]})).forEach((function(t){return n.push(t)})),n&&n.length?n.join(",")+" missing":null}},W=function(t){return function(e){return G("Issuer (iss)",t,e.payload.iss)}},q=function(t){return function(e){return G("Issuer (iss) claim",t,e.header.alg)}},J=function(t){return void 0===t&&(t=[]),function(e){return e.payload.aud&&e.payload.aud.length?(n=t,e.payload.aud.every((function(t){return n.includes(t)}))?null:"Audience (aud) claim mismatch: audience ("+e.payload.aud+") must contain only ("+t+")"):"Audience (aud) claim must be a present string";var n}},M=function(t,e){return void 0===t&&(t=!0),void 0===e&&(e=40),function(n){if(!t&&!n.payload.exp)return null;if(!b(n.payload.exp))return"Expiration Time (exp) claim must be a present number";var r=H(n.payload.exp,e);return new Date(Date.now())>r?"Expiration Time (exp) claim expired ("+r+")":null}},B=function(t,e){return void 0===t&&(t=!0),void 0===e&&(e=40),function(n){if(!t&&!n.payload.nbf)return null;if(!b(n.payload.nbf))return"Not Before (nbf) claim must be a present number";var r=H(n.payload.nbf,-1*e),o=new Date(Date.now());return o<r?"Not Before (nbf) claim cannot be used yet, current time ("+o+") is before ("+r+")":null}},G=function(t,e,n){return e!==n?t+' mismatch: expected "'+e+'" but got "'+n+'"':null},H=function(t,e){void 0===e&&(e=0);var n=new Date(0);return n.setUTCSeconds(t+e),n},V=function(){function t(t){this.opts=t,this.loginStateKey="crossid-spa-js|login",this.logoutStateKey="crossid-spa-js|logout",this.scope=t.scope,this.state=this._stateFactory(this.opts.state_type||"session_storage"),this.cache=this._cacheFactory(this.opts.cache_type||"memory"),this._purgeIndex()}return t.prototype.createRedirectURL=function(t){return void 0===t&&(t={}),f(this,void 0,void 0,(function(){var e;return v(this,(function(n){switch(n.label){case 0:return[4,this._createAuthorizationData(t)];case 1:return e=n.sent(),[4,this._persistAuthorizationData(e)];case 2:return n.sent(),[2,this._authorizeUrl(e.request)]}}))}))},t.prototype.loginWithRedirect=function(t){return f(this,void 0,void 0,(function(){var e;return v(this,(function(n){switch(n.label){case 0:return[4,this.createRedirectURL(t)];case 1:return e=n.sent(),window.location.replace(e),[2]}}))}))},t.prototype.handleRedirectCallback=function(t){return void 0===t&&(t=new URL(window.location.href)),f(this,void 0,void 0,(function(){var e,n,r,o,i,a,s;return v(this,(function(u){switch(u.label){case 0:if(e=t.searchParams,n=e.get("code"),r=e.get("error"))throw new Error(r);if(!(null==(o=this.state.get(this.loginStateKey))?void 0:o.code_verifier))throw new Error("invalid state, try sign-in again");return[4,g({tokenEndpoint:this.opts.token_endpoint,client_id:this.opts.client_id,code_verifier:o.code_verifier,grant_type:"authorization_code",redirect_uri:o.redirect_uri,code:n})];case 1:return i=u.sent(),(a=L(i.id_token)).payload.__bearer=i.id_token,s=L(i.access_token),this.state.remove(this.loginStateKey),this._assertAccessToken(s,o.audience),this._assertIDToken(a,o.nonce),s.payload._raw=i.access_token,this._cacheTokens(a,s,i.refresh_token),[2,{state:o.state}]}}))}))},t.prototype.getUser=function(t){return void 0===t&&(t={}),f(this,void 0,void 0,(function(){var e,n,r,o;return v(this,(function(i){return e=t.audience||this.opts.audience,n=R(this.scope,t.scope),r=this._getTokensKeysFromCache("id_token",e,n),[2,null==(o=this._getNarrowedKey(r))?void 0:o.payload]}))}))},t.prototype.getAccessToken=function(t){var e;return void 0===t&&(t={}),f(this,void 0,void 0,(function(){var n,r,o,i;return v(this,(function(a){return n=t.audience||this.opts.audience,r=R(this.scope,t.scope),o=this._getTokensKeysFromCache("access_token",n,r),i=this._getNarrowedKey(o),[2,null===(e=null==i?void 0:i.payload)||void 0===e?void 0:e._raw]}))}))},t.prototype.createLogoutRedirectURL=function(t){return void 0===t&&(t={}),f(this,void 0,void 0,(function(){var e;return v(this,(function(n){switch(n.label){case 0:return[4,this._createLogoutData(t)];case 1:return(e=n.sent()).request.id_token_hint&&t.post_logout_redirect_uri?[4,this._persistLogoutData(e)]:(this._removeTokens(e.audience,e.scopes),[2,this._logoutUrl()]);case 2:return n.sent(),[2,this._logoutUrl(e.request)]}}))}))},t.prototype.logoutWithRedirect=function(t){return f(this,void 0,void 0,(function(){var e;return v(this,(function(n){switch(n.label){case 0:return[4,this.createLogoutRedirectURL(t)];case 1:return e=n.sent(),window.location.assign(e),[2]}}))}))},t.prototype.handleLogoutRedirectCallback=function(t){return void 0===t&&(t=new URL(window.location.href)),f(this,void 0,void 0,(function(){var e,n,r,o;return v(this,(function(i){if(e=t.searchParams,n=e.get("state"),r=e.get("error"))throw new Error(r);if(!(null==(o=this.state.get(this.logoutStateKey))?void 0:o.state)||o.state!==n)throw new Error("invalid state, try sign-in again");return this._removeTokens(o.audience,o.scopes),this.state.remove(this.logoutStateKey),[2,{state:o.appState}]}))}))},t.prototype._createAuthorizationData=function(t){return f(this,void 0,void 0,(function(){var e,n,r,o,i,a;return v(this,(function(s){switch(s.label){case 0:return e=D(C(I())),n=D(C(I())),r=I(),[4,(c=r,f(void 0,void 0,void 0,(function(){return v(this,(function(t){switch(t.label){case 0:return[4,window.crypto.subtle.digest({name:"SHA-256"},(new TextEncoder).encode(c))];case 1:return[2,t.sent()]}}))})))];case 1:return o=s.sent(),u=new Uint8Array(o),i=D(C(String.fromCharCode.apply(String,Array.from(u)))),a={audience:t.audience,redirect_uri:t.redirect_uri,response_type:t.response_type,scope:t.scope,state:e,nonce:n,code_challenge:i},[2,{request:this._mergeAuthorizationCodeParams(a),code_verifier:r,appState:t.state}]}var u,c}))}))},t.prototype._persistAuthorizationData=function(t){return f(this,void 0,void 0,(function(){var e,n;return v(this,(function(r){return e=t.request,n={audience:e.audience,redirect_uri:e.redirect_uri,scope:e.scope,nonce:e.nonce,state:t.appState,code_verifier:t.code_verifier},this.state.set(this.loginStateKey,n),[2]}))}))},t.prototype._mergeAuthorizationCodeParams=function(t){return{client_id:this.opts.client_id,audience:t.audience||this.opts.audience,response_type:t.response_type||this.opts.response_type||"code",redirect_uri:t.redirect_uri||this.opts.redirect_uri,nonce:t.nonce,state:t.state,scope:t.scope||this.opts.scope,code_challenge:t.code_challenge,code_challenge_method:"S256"}},t.prototype._createLogoutData=function(t){return f(this,void 0,void 0,(function(){var e,n;return v(this,(function(r){switch(r.label){case 0:return(e={id_token_hint:t.id_token_hint,post_logout_redirect_uri:t.post_logout_redirect_uri}).id_token_hint||null===e.id_token_hint?[3,2]:[4,this.getUser()];case 1:(n=r.sent())&&n.__bearer&&(e.id_token_hint=n.__bearer),r.label=2;case 2:return e.id_token_hint&&(e.state=D(C(I()))),[2,{request:e,audience:t.audience||this.opts.audience,scopes:(t.scope||this.scope).split(" "),appState:t.state}]}}))}))},t.prototype._persistLogoutData=function(t){return f(this,void 0,void 0,(function(){var e;return v(this,(function(n){return e={client_id:this.opts.client_id,audience:t.audience,scopes:t.scopes,post_logout_redirect_uri:t.request.post_logout_redirect_uri,state:t.request.state,appState:t.appState},this.state.set(this.logoutStateKey,e),[2]}))}))},t.prototype._assertAccessToken=function(t,e){F(t,N(["alg"],["iss","sub","aud","exp","iat"]),W(this.opts.issuer),q("RS256"),J(e),M(!0),B(!0))},t.prototype._assertIDToken=function(t,e){var n;return F(t,N(["alg"],["iss","sub","aud","nonce","exp","iat"]),W(this.opts.issuer),(n=e,function(t){return G("Nonce (nonce)",n,t.payload.nonce)}),q("RS256"),J([this.opts.client_id]),M(!0),B(!1),(function(t){return b(t.payload.iat)?null:"Issued At (iat) claim must be a present number"})),null},t.prototype._removeTokens=function(t,e){var n=this,r=this._getTokensKeysFromCache("id_token",t,e),o=this._getTokensKeysFromCache("access_token",t,e),i=this._getTokensKeysFromCache("refresh_token",t,e);r.concat(o).concat(i).forEach((function(t){return n.cache.remove(t)})),this._purgeIndex()},t.prototype._authorizeUrl=function(t){return this.opts.authorization_endpoint+"?"+P(t)},t.prototype._logoutUrl=function(t){var e=this.opts.logout_endpoint;return t&&(e=e+"?"+P(t)),e},t.prototype._stateFactory=function(t){var e={ttl:300};switch(t){case"local_storage":return new x(e);case"session_storage":return new O(e);default:throw new Error('Invalid cache type "'+t+'"')}},t.prototype._cacheFactory=function(t){switch(t){case"memory":return new j;case"local_storage":return new x({purgeOnInit:!0});case"session_storage":return new O({purgeOnInit:!0});default:throw new Error('Invalid cache type "'+t+'"')}},t.prototype._cacheTokens=function(t,e,n){var r=this._ttlFromToken(e);this._cacheToken("access_token",e,this.opts.client_id,e.payload.aud,e.payload.scp,r),this._cacheToken("id_token",t,this.opts.client_id,e.payload.aud,e.payload.scp||[],this._ttlFromToken(t)),n&&this._cacheToken("refresh_token",n,this.opts.client_id,e.payload.aud,e.payload.scp,432e3)},t.prototype._cacheToken=function(t,e,n,r,o,i){var a=this._cacheKey({tokType:t,client_id:n,audience:r,scope:o});this.cache.set(a,e,{ttl:i});var s=[r[0]],u=this.cache.get(w);u||(u={}),s.forEach((function(t){u[t]||(u[t]={});var e=u[t];o.forEach((function(t){e[t]||(e[t]=[]),e[t].push(a)}))})),this.cache.set(w,u)},t.prototype._cacheKey=function(t){var e,n=t.tokType,r=t.client_id,o=t.audience,i=t.scope;return i&&i.length&&(i.sort(),e=i.join("__")),["crossid-spa-js",n,r,o.join("__"),e].join("|")},t.prototype._decodeKey=function(t){var e=t.split("|"),n=e[1],r=e[2],o=e[3];return{tokenType:n,audience:r.split("__"),scope:o.split("__")}},t.prototype._getTokensKeysFromCache=function(t,e,n){var r,o=this,i=(this.cache.get(w)||{})[e[0]];if(!i)return[];for(var a=function(e){return i[e]&&i[e].length?r?void(r=r.filter((function(t){return i[e].includes(t)}))):(r=i[e].filter((function(e){return o._decodeKey(e).tokenType===t})),"continue"):{value:[]}},s=0,u=n;s<u.length;s++){var c=a(u[s]);if("object"==typeof c)return c.value}return r},t.prototype._getNarrowedKey=function(t){for(var e in t){var n=this.cache.get(t[e]);if(n)return n}},t.prototype._ttlFromToken=function(t){return(new Date(1e3*t.payload.exp).getTime()-Date.now())/1e3},t.prototype._purgeIndex=function(){var t=this.cache.get(w);if(t){for(var e=0,n=Object.entries(t);e<n.length;e++){for(var r=n[e],o=r[0],i=r[1],a=function(e,n){for(var r=[],i=0;i<n.length;i++)s.cache.get(n[i])||r.push(i);t[o][e]=n.filter((function(t,e){return-1===r.indexOf(e)})),t[o][e].length||delete t[o][e]},s=this,u=0,c=Object.entries(i);u<c.length;u++){var l=c[u];a(l[0],l[1])}Object.keys(t[o]).length||delete t[o]}this.cache.set(w,t)}},t}();function Q(t){return f(this,void 0,void 0,(function(){var e,n,r,o,i,a;return v(this,(function(s){switch(s.label){case 0:return e=t.tenant_id,n=t.auth_server,r=void 0===n?"default":n,o=h(t,["tenant_id","auth_server"]),i=o,[4,y({wellknown_endpoint:"https://"+e+".crossid.io/oauth2/"+r+"/.well-known/openid-configuration"})];case 1:return a=s.sent(),i.authorization_endpoint=a.authorization_endpoint,i.token_endpoint=a.token_endpoint,i.issuer=a.issuer,i.logout_endpoint=i.issuer+"logout",[2,new V(i)]}}))}))}function X(t){return f(this,void 0,void 0,(function(){var e,n,r;return v(this,(function(o){switch(o.label){case 0:return[4,y(t)];case 1:return e=o.sent(),n=h(t,["wellknown_endpoint"]),(r=n).authorization_endpoint=e.authorization_endpoint,r.token_endpoint=e.token_endpoint,r.issuer=e.issuer,[2,new V(r)]}}))}))}function Y(t){return f(this,void 0,void 0,(function(){return v(this,(function(e){return[2,new V(t)]}))}))}function Z(){throw new Error("please wrap your app with CrossidAuthProvider")}var $=e({loading:!0}),tt=e({loginWithRedirect:Z,logoutWithRedirect:Z,getAccessToken:Z}),et=function(){return n($)},nt=function(){return n(tt)},rt=function(t){return u(void 0,void 0,void 0,(function(){var e,n,r,o;return c(this,(function(i){switch(i.label){case 0:return r=e=t,o=e,(n=e).tenant_id?[4,Q(n)]:[3,2];case 1:return[2,i.sent()];case 2:return r.wellknown_endpoint?[4,X(r)]:[3,4];case 3:return[2,i.sent()];case 4:return o.authorization_endpoint&&o.token_endpoint&&o.issuer?[4,Y(o)]:[3,6];case 5:return[2,i.sent()];case 6:throw new Error("invalid props")}}))}))},ot=new RegExp(/returnTo=(.*)/i),it=function(e){var n=r(),a=n[0],s=n[1],l=r(),p=l[0],d=l[1],h=r(!0),f=h[0],v=h[1],_=r(),y=_[0],g=_[1],w=r(""),m=w[0],b=w[1],k=e.goTo;o((function(){u(void 0,void 0,void 0,(function(){var t,n,r,o,i,u,l,p,h,f;return c(this,(function(c){switch(c.label){case 0:if(a)return[3,6];c.label=1;case 1:return c.trys.push([1,4,5,6]),[4,rt(e)];case 2:return t=c.sent(),s(t),[4,t.getUser()];case 3:return n=c.sent(),g(n),[3,6];case 4:return r=c.sent(),d(r),v(!1),[3,6];case 5:return[2];case 6:return o=window.location,i=o.origin,u=o.pathname,l=new URLSearchParams(window.location.search),a&&i+u===e.redirect_uri&&l.has("code")&&!l.has("error")?[4,a.handleRedirectCallback()]:[3,9];case 7:return h=c.sent(),[4,a.getUser()];case 8:return p=c.sent(),g(p),v(!1),(null==h?void 0:h.state)&&(b(h.state),f=(h.state.match(ot)||[])[1],k?k(f):window.history.replaceState({},document.title,h.state)),[3,12];case 9:return a&&i+u===e.post_logout_redirect_uri&&l.has("state")&&!l.has("error")?[4,a.handleLogoutRedirectCallback()]:[3,11];case 10:return h=c.sent(),g(void 0),v(!1),(null==h?void 0:h.state)&&(f=(h.state.match(ot)||[])[1],k?k(f):window.history.replaceState({},document.title,f)),[3,12];case 11:v(!1),c.label=12;case 12:return[2]}}))}))}),[a,k,e]);var T=i((function(t,e){void 0===t&&(t={}),t.state=t.state+"&returnTo="+e,null==a||a.loginWithRedirect(t)}),[a]),S=i((function(t){void 0===t&&(t={}),t.post_logout_redirect_uri||(t.post_logout_redirect_uri=e.post_logout_redirect_uri),null==a||a.logoutWithRedirect(t)}),[a]),O=i((function(t){return void 0===t&&(t={}),u(void 0,void 0,void 0,(function(){return c(this,(function(e){switch(e.label){case 0:return[4,null==a?void 0:a.getAccessToken(t)];case 1:return[2,e.sent()||""]}}))}))}),[a]);return t.createElement($.Provider,{value:{user:y,error:p,loading:f,client:a,loginState:m}},t.createElement(tt.Provider,{value:{loginWithRedirect:T,logoutWithRedirect:S,getAccessToken:O}},e.children))};function at(e,n,i){var l=this;void 0===i&&(i={});return function(p){var d=a(!1),h=r(!1),f=h[0],v=h[1],_=r(!1),y=_[0],g=_[1],w=et(),m=w.loading,b=w.client,k=w.loginState,T=void 0===k?"":k,S=nt().loginWithRedirect,O=i.returnTo,x=void 0===O?st():O,E=i.scope,j=void 0===E?"":E;return j=("openid "+j).trim(),o((function(){if(d.current=!0,!m&&!f)return u(l,void 0,void 0,(function(){return c(this,(function(t){switch(t.label){case 0:return[4,null==b?void 0:b.getUser()];case 1:return t.sent()&&d?(v(!0),[2]):[3,2];case 2:return T.indexOf("attemptedLogin=true")>-1?(g(!0),[3,5]):[3,3];case 3:return[4,S({scope:j,state:"attemptedLogin=true"},x)];case 4:t.sent(),t.label=5;case 5:return[2]}}))})),function(){d.current=!1}}),[f,b,m,S,x]),f?y?n():t.createElement(e,s({},p)):t.createElement("div",null,"Loading...")}}var st=function(){return""+window.location.pathname+window.location.search};export{it as CrossidAuthProvider,et as useAuth,nt as useAuthActions,at as withAuth}; | ||
//# sourceMappingURL=index.js.map |
@@ -11,2 +11,3 @@ import React from 'react'; | ||
client?: Client; | ||
loginState?: string; | ||
} | ||
@@ -13,0 +14,0 @@ export declare const AuthContext: React.Context<AuthState<IDToken>>; |
import React from 'react'; | ||
export interface AuthRequiredOpts { | ||
returnTo?: string; | ||
scope?: string; | ||
} | ||
export declare function withAuth<T>(WrappedComponent: React.ComponentType<T>, opts?: AuthRequiredOpts): (props: T) => JSX.Element; | ||
export declare function withAuth<T>(WrappedComponent: React.ComponentType<T>, ErrorComponent: Function, opts?: AuthRequiredOpts): (props: T) => JSX.Element; |
{ | ||
"author": "Crossid", | ||
"name": "@crossid/crossid-react", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Openid authentication hooks for React", | ||
@@ -6,0 +6,0 @@ "main": "index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
200484
282