@crossid/crossid-react
Advanced tools
Comparing version 0.1.2 to 0.2.0
@@ -1,7 +0,9 @@ | ||
## 0.1.2 | ||
## 0.2.0 | ||
### Minor Changes | ||
[All Changes](https://github.com/crossid/crossid-spa-js/compare/v0.1.1...v0.2.0) | ||
- export handle login state | ||
### Major Changes | ||
- Combines two hooks into a single `useAuth`. | ||
## 0.1.1 | ||
@@ -8,0 +10,0 @@ |
@@ -15,3 +15,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. | ||
***************************************************************************** */function i(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 a(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=a.trys,(o=o.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])}}} | ||
***************************************************************************** */function i(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 a(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=a.trys,(o=o.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])}}}function s(){throw new Error("Please wrap your app with <AuthProvider>")}var u={loading:!0},c={loginWithRedirect:s,logoutWithRedirect:s,getAccessToken:s},l=o(o({},u),c),d=e.createContext(l),p=function(){return e.useContext(d)},f=function(){return""+window.location.pathname+window.location.search}; | ||
/*! ***************************************************************************** | ||
@@ -30,3 +30,4 @@ Copyright (c) Microsoft Corporation. | ||
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)},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})})); | ||
***************************************************************************** */ | ||
var h=function(t,e){return(h=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 v(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}h(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}var _=function(){return(_=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 y(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 g(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 w(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 m=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 v(e,t),e.create=function(t){return new e(t.error,t.error_description)},e}(Error);function b(t){var e=t.wellknown_endpoint;return g(this,void 0,void 0,(function(){var t;return w(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 k(t){var e=t.tokenEndpoint,n=t.timeout,r=y(t,["tokenEndpoint","timeout"]);return g(this,void 0,void 0,(function(){var t,o,i;return w(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={}),g(void 0,void 0,void 0,(function(){var t,e,n,r,o;return w(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,_(_({},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 m.create(i);return[2,i]}var s,u}))}))}var T="crossid-spa-js|index",O=function(t){return null!==t&&"Object"===(null==t?void 0:t.constructor.name)},E=function(t){return"number"==typeof t},R=function(t){return"string"==typeof t},S=String.fromCharCode(0),x=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={})[S]=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 O(n=i)&&S 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[S]: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}(O(i)&&S 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}(),I=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 v(e,t),e}(x),j=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 v(e,t),e}(x),A=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}(),C=function(t){function e(e){return t.call(this,new A,e)||this}return v(e,t),e}(x),D=function(t){return Array.from(new Set(t))},P=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return D(t.join(" ").trim().split(/\s+/))},L=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},U=function(t){return btoa(t)},K=function(t){return atob(t)},F=function(t){var e={"+":"-","/":"_","=":""};return t.replace(/[+/=]/g,(function(t){return e[t]}))},z=function(t){var e={"-":"+",_:"/"};return t.replace(/[-_]/g,(function(t){return e[t]}))},N=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("&")},W=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(K(z(r)));if(a.aud)if(R(a.aud))a.aud=[a.aud];else if(i=a.aud,!Array.isArray(i)||!i.every((function(t){return R(t)})))throw new Error("Audience (aud) must be a string or an array of strings");return{header:JSON.parse(K(z(n))),payload:a}},q=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 v(e,t),e.prototype.getMissingFields=function(){return this.fields_},e}(Error),J=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 q(r,r.join(","));return null},M=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}},G=function(t){return function(e){return Q("Issuer (iss)",t,e.payload.iss)}},Z=function(t){return function(e){return Q("Issuer (iss) claim",t,e.header.alg)}},B=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}},H=function(t,e){return void 0===t&&(t=!0),void 0===e&&(e=40),function(n){if(!t&&!n.payload.exp)return null;if(!E(n.payload.exp))return"Expiration Time (exp) claim must be a present number";var r=X(n.payload.exp,e);return new Date(Date.now())>r?"Expiration Time (exp) claim expired ("+r+")":null}},V=function(t,e){return void 0===t&&(t=!0),void 0===e&&(e=40),function(n){if(!t&&!n.payload.nbf)return null;if(!E(n.payload.nbf))return"Not Before (nbf) claim must be a present number";var r=X(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}},Q=function(t,e,n){return e!==n?t+' mismatch: expected "'+e+'" but got "'+n+'"':null},X=function(t,e){void 0===e&&(e=0);var n=new Date(0);return n.setUTCSeconds(t+e),n},Y=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={}),g(this,void 0,void 0,(function(){var e;return w(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 g(this,void 0,void 0,(function(){var e;return w(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)),g(this,void 0,void 0,(function(){var e,n,r,o,i,a,s;return w(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,k({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()).id_token&&((a=W(i.id_token)).payload.__bearer=i.id_token,this._assertIDToken(a,o.nonce)),s=W(i.access_token),this.state.remove(this.loginStateKey),this._assertAccessToken(s,o.audience),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={}),g(this,void 0,void 0,(function(){var e,n,r,o;return w(this,(function(i){return e=t.audience||this.opts.audience,n=P(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={}),g(this,void 0,void 0,(function(){var n,r,o,i;return w(this,(function(a){return n=t.audience||this.opts.audience,r=P(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.introspectAccessToken=function(t){return void 0===t&&(t={}),g(this,void 0,void 0,(function(){var e,n,r,o;return w(this,(function(i){return e=t.audience||this.opts.audience,n=P(this.scope,t.scope),r=this._getTokensKeysFromCache("access_token",e,n),(o=this._getNarrowedKey(r))?[2,_(_({active:!0},o.payload),{_raw:void 0})]:[2,void 0]}))}))},t.prototype.createLogoutRedirectURL=function(t){return void 0===t&&(t={}),g(this,void 0,void 0,(function(){var e;return w(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 g(this,void 0,void 0,(function(){var e;return w(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)),g(this,void 0,void 0,(function(){var e,n,r,o;return w(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 g(this,void 0,void 0,(function(){var e,n,r,o,i,a;return w(this,(function(s){switch(s.label){case 0:return e=F(U(L())),n=F(U(L())),r=L(),[4,(c=r,g(void 0,void 0,void 0,(function(){return w(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=F(U(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 g(this,void 0,void 0,(function(){var e,n;return w(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 g(this,void 0,void 0,(function(){var e,n;return w(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=F(U(L()))),[2,{request:e,audience:t.audience||this.opts.audience,scopes:(t.scope||this.scope).split(" "),appState:t.state}]}}))}))},t.prototype._persistLogoutData=function(t){return g(this,void 0,void 0,(function(){var e;return w(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){J(t,M(["alg"],["iss","sub","aud","exp","iat"]),G(this.opts.issuer),Z("RS256"),B(e),H(!0),V(!0))},t.prototype._assertIDToken=function(t,e){var n;return J(t,M(["alg"],["iss","sub","aud","nonce","exp","iat"]),G(this.opts.issuer),(n=e,function(t){return Q("Nonce (nonce)",n,t.payload.nonce)}),Z("RS256"),B([this.opts.client_id]),H(!0),V(!1),(function(t){return E(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+"?"+N(t)},t.prototype._logoutUrl=function(t){var e=this.opts.logout_endpoint;return t&&(e=e+"?"+N(t)),e},t.prototype._stateFactory=function(t){var e={ttl:300};switch(t){case"local_storage":return new j(e);case"session_storage":return new I(e);default:throw new Error('Invalid cache type "'+t+'"')}},t.prototype._cacheFactory=function(t){switch(t){case"memory":return new C;case"local_storage":return new j({purgeOnInit:!0});case"session_storage":return new I({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),t&&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(T);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(T,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(T)||{})[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(T);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)}},t}();function $(t){return g(this,void 0,void 0,(function(){var e,n,r,o,i,a;return w(this,(function(s){switch(s.label){case 0:return e=t.tenant_id,n=t.auth_server,r=void 0===n?"default":n,o=y(t,["tenant_id","auth_server"]),i=o,[4,b({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 Y(i)]}}))}))}function tt(t){return g(this,void 0,void 0,(function(){var e,n,r;return w(this,(function(o){switch(o.label){case 0:return[4,b(t)];case 1:return e=o.sent(),n=y(t,["wellknown_endpoint"]),(r=n).authorization_endpoint=e.authorization_endpoint,r.token_endpoint=e.token_endpoint,r.issuer=e.issuer,[2,new Y(r)]}}))}))}function et(t){return g(this,void 0,void 0,(function(){return w(this,(function(e){return[2,new Y(t)]}))}))}var nt=function(t,e){switch(e.type){case"INITIALIZED":return o(o({},t),{loading:!1,idToken:e.idToken,error:void 0});case"LOGOUT_COMPLETED":return o(o({},t),{loading:!1,idToken:void 0,error:void 0});case"ERROR":return o(o({},t),{loading:!1,error:e.error})}},rt=function(t,e){void 0===t&&(t=window.location);var n=t.origin,r=t.pathname;if(e.some((function(t){return t===n+r}))){var o=new URLSearchParams(t.search);if(o.get("error"))return m.create({error:o.get("error")||"",error_description:o.get("error_description")||""})}},ot=function(t){return m.create({error:t.message,error_description:""})},it=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,$(n)]:[3,2];case 1:return[2,i.sent()];case 2:return r.wellknown_endpoint?[4,tt(r)]:[3,4];case 3:return[2,i.sent()];case 4:return o.authorization_endpoint&&o.token_endpoint&&o.issuer?[4,et(o)]:[3,6];case 5:return[2,i.sent()];case 6:throw new Error("invalid props")}}))}))},at=function(t){window.history.replaceState({},document.title,(null==t?void 0:t.return_to)||window.location.pathname)};t.AuthProvider=function(t){var n=e.useState(),s=n[0],c=n[1],l=e.useReducer(nt,u),p=l[0],f=l[1],h=t.onRedirectTo,v=void 0===h?at:h,_=t.redirect_uri,y=t.post_logout_redirect_uri,g=t.children;e.useEffect((function(){i(void 0,void 0,void 0,(function(){var e,n;return a(this,(function(r){switch(r.label){case 0:if(s)return[3,4];r.label=1;case 1:return r.trys.push([1,3,,4]),[4,it(t)];case 2:return e=r.sent(),c(e),[3,4];case 3:return n=r.sent(),f({type:"ERROR",error:ot(n)}),[3,4];case 4:return[2]}}))}))}),[s]),e.useEffect((function(){i(void 0,void 0,void 0,(function(){var t,e,n,r,o;return a(this,(function(i){switch(i.label){case 0:if(!s)return[3,11];i.label=1;case 1:return i.trys.push([1,10,,11]),function(t,e){void 0===t&&(t=window.location);var n=t.origin,r=t.pathname,o=new URLSearchParams(t.search);return n+r===e&&o.has("code")&&!o.has("error")}(window.location,_)?[4,s.handleRedirectCallback()]:[3,4];case 2:return t=i.sent().state,[4,s.getUser()];case 3:return r=i.sent(),f({type:"INITIALIZED",idToken:r}),v(t),[3,9];case 4:return function(t,e){void 0===t&&(t=window.location);var n=t.origin,r=t.pathname,o=new URLSearchParams(t.search);return n+r===e&&o.has("state")&&!o.has("error")}(window.location,y)?[4,s.handleLogoutRedirectCallback()]:[3,6];case 5:return e=i.sent().state,f({type:"LOGOUT_COMPLETED"}),v(e),[3,9];case 6:return rt(window.location,[_,y])?((n=rt(window.location,[_,y]))&&f({type:"ERROR",error:n}),[3,9]):[3,7];case 7:return[4,s.getUser()];case 8:r=i.sent(),f({type:"INITIALIZED",idToken:r}),i.label=9;case 9:return[3,11];case 10:return o=i.sent(),f({type:"ERROR",error:ot(o)}),[3,11];case 11:return[2]}}))}))}),[s]);var w=e.useCallback((function(t){void 0===t&&(t={}),null==s||s.loginWithRedirect(t)}),[s]),m=e.useCallback((function(e){void 0===e&&(e={}),e.post_logout_redirect_uri||(e.post_logout_redirect_uri=t.post_logout_redirect_uri),null==s||s.logoutWithRedirect(e)}),[s]),b=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==s?void 0:s.getAccessToken(t)];case 1:return[2,e.sent()||""]}}))}))}),[s]);return r.default.createElement(d.Provider,{value:o(o({},p),{loginWithRedirect:w,logoutWithRedirect:m,getAccessToken:b})},g)},t.useAuth=p,t.withAuth=function(t,n){var s=this;return void 0===n&&(n={}),function(u){var c=e.useRef(!1),l=p(),d=l.loading,h=l.loginWithRedirect,v=l.idToken,_=n.return_to,y=void 0===_?f:_,g=n.login_opts,w=!!v;return e.useEffect((function(){if(c.current=!0,!d&&!w)return i(s,void 0,void 0,(function(){return a(this,(function(t){switch(t.label){case 0:return[4,h(o(o({},g),{state:{return_to:"function"==typeof y?y():y}}))];case 1:return t.sent(),[2]}}))})),function(){c.current=!1}}),[w,d,h,y]),w?r.default.createElement(t,o({},u)):r.default.createElement("div",null,"Loading...")}},Object.defineProperty(t,"__esModule",{value:!0})})); | ||
//# sourceMappingURL=crossid-react.js.map |
@@ -1,3 +0,5 @@ | ||
import CrossidAuthProvider, { AuthState, useCrossidAuth, useCrossidAuthActions } from './provider'; | ||
import { AuthState } from './provider-context'; | ||
import { useAuth } from './use-auth'; | ||
import { withAuth } from './with-auth'; | ||
export { CrossidAuthProvider, AuthState, useCrossidAuth as useAuth, useCrossidAuthActions as useAuthActions, withAuth, }; | ||
import { AuthProvider, AppState } from './provider'; | ||
export { AuthProvider, AuthState, useAuth, withAuth, AppState }; |
@@ -1,2 +0,2 @@ | ||
import t,{createContext as e,useContext as n,useState as r,useEffect as o,useCallback as i,useRef as a}from"react"; | ||
import t,{createContext as e,useContext as n,useRef as r,useEffect as o,useState as i,useReducer as a,useCallback as s}from"react"; | ||
/*! ***************************************************************************** | ||
@@ -15,3 +15,3 @@ Copyright (c) Microsoft Corporation. | ||
PERFORMANCE OF THIS SOFTWARE. | ||
***************************************************************************** */var s=function(){return(s=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 u(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 c(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=a.trys,(o=o.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 u=function(){return(u=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 c(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 l(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=a.trys,(o=o.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])}}}function d(){throw new Error("Please wrap your app with <AuthProvider>")}var p={loading:!0},h={loginWithRedirect:d,logoutWithRedirect:d,getAccessToken:d},f=e(u(u({},p),h)),v=function(){return n(f)},_=function(){return""+window.location.pathname+window.location.search};function y(e,n){var i=this;void 0===n&&(n={});return function(a){var s=r(!1),d=v(),p=d.loading,h=d.loginWithRedirect,f=d.idToken,y=n.return_to,g=void 0===y?_:y,w=n.login_opts,m=!!f;return o((function(){if(s.current=!0,!p&&!m)return c(i,void 0,void 0,(function(){return l(this,(function(t){switch(t.label){case 0:return[4,h(u(u({},w),{state:{return_to:"function"==typeof g?g():g}}))];case 1:return t.sent(),[2]}}))})),function(){s.current=!1}}),[m,p,h,g]),m?t.createElement(e,u({},a)):t.createElement("div",null,"Loading...")}} | ||
/*! ***************************************************************************** | ||
@@ -30,3 +30,3 @@ Copyright (c) Microsoft Corporation. | ||
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 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}; | ||
***************************************************************************** */var g=function(t,e){return(g=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 w(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}g(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}var m=function(){return(m=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 b(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 k(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 T(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 O=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 w(e,t),e.create=function(t){return new e(t.error,t.error_description)},e}(Error);function E(t){var e=t.wellknown_endpoint;return k(this,void 0,void 0,(function(){var t;return T(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 R(t){var e=t.tokenEndpoint,n=t.timeout,r=b(t,["tokenEndpoint","timeout"]);return k(this,void 0,void 0,(function(){var t,o,i;return T(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={}),k(void 0,void 0,void 0,(function(){var t,e,n,r,o;return T(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,m(m({},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 O.create(i);return[2,i]}var s,u}))}))}var S="crossid-spa-js|index",I=function(t){return null!==t&&"Object"===(null==t?void 0:t.constructor.name)},x=function(t){return"number"==typeof t},j=function(t){return"string"==typeof t},A=String.fromCharCode(0),D=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={})[A]=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 I(n=i)&&A 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[A]: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}(I(i)&&A 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}(),L=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 w(e,t),e}(D),U=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 w(e,t),e}(D),C=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}(),P=function(t){function e(e){return t.call(this,new C,e)||this}return w(e,t),e}(D),K=function(t){return Array.from(new Set(t))},F=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return K(t.join(" ").trim().split(/\s+/))},z=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},N=function(t){return btoa(t)},W=function(t){return atob(t)},q=function(t){var e={"+":"-","/":"_","=":""};return t.replace(/[+/=]/g,(function(t){return e[t]}))},J=function(t){var e={"-":"+",_:"/"};return t.replace(/[-_]/g,(function(t){return e[t]}))},M=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("&")},G=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(W(J(r)));if(a.aud)if(j(a.aud))a.aud=[a.aud];else if(i=a.aud,!Array.isArray(i)||!i.every((function(t){return j(t)})))throw new Error("Audience (aud) must be a string or an array of strings");return{header:JSON.parse(W(J(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 w(e,t),e.prototype.getMissingFields=function(){return this.fields_},e}(Error),B=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},H=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}},V=function(t){return function(e){return tt("Issuer (iss)",t,e.payload.iss)}},Q=function(t){return function(e){return tt("Issuer (iss) claim",t,e.header.alg)}},X=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}},Y=function(t,e){return void 0===t&&(t=!0),void 0===e&&(e=40),function(n){if(!t&&!n.payload.exp)return null;if(!x(n.payload.exp))return"Expiration Time (exp) claim must be a present number";var r=et(n.payload.exp,e);return new Date(Date.now())>r?"Expiration Time (exp) claim expired ("+r+")":null}},$=function(t,e){return void 0===t&&(t=!0),void 0===e&&(e=40),function(n){if(!t&&!n.payload.nbf)return null;if(!x(n.payload.nbf))return"Not Before (nbf) claim must be a present number";var r=et(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}},tt=function(t,e,n){return e!==n?t+' mismatch: expected "'+e+'" but got "'+n+'"':null},et=function(t,e){void 0===e&&(e=0);var n=new Date(0);return n.setUTCSeconds(t+e),n},nt=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={}),k(this,void 0,void 0,(function(){var e;return T(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 k(this,void 0,void 0,(function(){var e;return T(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)),k(this,void 0,void 0,(function(){var e,n,r,o,i,a,s;return T(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,R({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()).id_token&&((a=G(i.id_token)).payload.__bearer=i.id_token,this._assertIDToken(a,o.nonce)),s=G(i.access_token),this.state.remove(this.loginStateKey),this._assertAccessToken(s,o.audience),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={}),k(this,void 0,void 0,(function(){var e,n,r,o;return T(this,(function(i){return e=t.audience||this.opts.audience,n=F(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={}),k(this,void 0,void 0,(function(){var n,r,o,i;return T(this,(function(a){return n=t.audience||this.opts.audience,r=F(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.introspectAccessToken=function(t){return void 0===t&&(t={}),k(this,void 0,void 0,(function(){var e,n,r,o;return T(this,(function(i){return e=t.audience||this.opts.audience,n=F(this.scope,t.scope),r=this._getTokensKeysFromCache("access_token",e,n),(o=this._getNarrowedKey(r))?[2,m(m({active:!0},o.payload),{_raw:void 0})]:[2,void 0]}))}))},t.prototype.createLogoutRedirectURL=function(t){return void 0===t&&(t={}),k(this,void 0,void 0,(function(){var e;return T(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 k(this,void 0,void 0,(function(){var e;return T(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)),k(this,void 0,void 0,(function(){var e,n,r,o;return T(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 k(this,void 0,void 0,(function(){var e,n,r,o,i,a;return T(this,(function(s){switch(s.label){case 0:return e=q(N(z())),n=q(N(z())),r=z(),[4,(c=r,k(void 0,void 0,void 0,(function(){return T(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=q(N(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 k(this,void 0,void 0,(function(){var e,n;return T(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 k(this,void 0,void 0,(function(){var e,n;return T(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=q(N(z()))),[2,{request:e,audience:t.audience||this.opts.audience,scopes:(t.scope||this.scope).split(" "),appState:t.state}]}}))}))},t.prototype._persistLogoutData=function(t){return k(this,void 0,void 0,(function(){var e;return T(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){B(t,H(["alg"],["iss","sub","aud","exp","iat"]),V(this.opts.issuer),Q("RS256"),X(e),Y(!0),$(!0))},t.prototype._assertIDToken=function(t,e){var n;return B(t,H(["alg"],["iss","sub","aud","nonce","exp","iat"]),V(this.opts.issuer),(n=e,function(t){return tt("Nonce (nonce)",n,t.payload.nonce)}),Q("RS256"),X([this.opts.client_id]),Y(!0),$(!1),(function(t){return x(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+"?"+M(t)},t.prototype._logoutUrl=function(t){var e=this.opts.logout_endpoint;return t&&(e=e+"?"+M(t)),e},t.prototype._stateFactory=function(t){var e={ttl:300};switch(t){case"local_storage":return new U(e);case"session_storage":return new L(e);default:throw new Error('Invalid cache type "'+t+'"')}},t.prototype._cacheFactory=function(t){switch(t){case"memory":return new P;case"local_storage":return new U({purgeOnInit:!0});case"session_storage":return new L({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),t&&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(S);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(S,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(S)||{})[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(S);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(S,t)}},t}();function rt(t){return k(this,void 0,void 0,(function(){var e,n,r,o,i,a;return T(this,(function(s){switch(s.label){case 0:return e=t.tenant_id,n=t.auth_server,r=void 0===n?"default":n,o=b(t,["tenant_id","auth_server"]),i=o,[4,E({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 nt(i)]}}))}))}function ot(t){return k(this,void 0,void 0,(function(){var e,n,r;return T(this,(function(o){switch(o.label){case 0:return[4,E(t)];case 1:return e=o.sent(),n=b(t,["wellknown_endpoint"]),(r=n).authorization_endpoint=e.authorization_endpoint,r.token_endpoint=e.token_endpoint,r.issuer=e.issuer,[2,new nt(r)]}}))}))}function it(t){return k(this,void 0,void 0,(function(){return T(this,(function(e){return[2,new nt(t)]}))}))}var at=function(t,e){switch(e.type){case"INITIALIZED":return u(u({},t),{loading:!1,idToken:e.idToken,error:void 0});case"LOGOUT_COMPLETED":return u(u({},t),{loading:!1,idToken:void 0,error:void 0});case"ERROR":return u(u({},t),{loading:!1,error:e.error})}},st=function(t,e){void 0===t&&(t=window.location);var n=t.origin,r=t.pathname;if(e.some((function(t){return t===n+r}))){var o=new URLSearchParams(t.search);if(o.get("error"))return O.create({error:o.get("error")||"",error_description:o.get("error_description")||""})}},ut=function(t){return O.create({error:t.message,error_description:""})},ct=function(t){return c(void 0,void 0,void 0,(function(){var e,n,r,o;return l(this,(function(i){switch(i.label){case 0:return r=e=t,o=e,(n=e).tenant_id?[4,rt(n)]:[3,2];case 1:return[2,i.sent()];case 2:return r.wellknown_endpoint?[4,ot(r)]:[3,4];case 3:return[2,i.sent()];case 4:return o.authorization_endpoint&&o.token_endpoint&&o.issuer?[4,it(o)]:[3,6];case 5:return[2,i.sent()];case 6:throw new Error("invalid props")}}))}))},lt=function(t){window.history.replaceState({},document.title,(null==t?void 0:t.return_to)||window.location.pathname)},dt=function(e){var n=i(),r=n[0],d=n[1],h=a(at,p),v=h[0],_=h[1],y=e.onRedirectTo,g=void 0===y?lt:y,w=e.redirect_uri,m=e.post_logout_redirect_uri,b=e.children;o((function(){c(void 0,void 0,void 0,(function(){var t,n;return l(this,(function(o){switch(o.label){case 0:if(r)return[3,4];o.label=1;case 1:return o.trys.push([1,3,,4]),[4,ct(e)];case 2:return t=o.sent(),d(t),[3,4];case 3:return n=o.sent(),_({type:"ERROR",error:ut(n)}),[3,4];case 4:return[2]}}))}))}),[r]),o((function(){c(void 0,void 0,void 0,(function(){var t,e,n,o,i;return l(this,(function(a){switch(a.label){case 0:if(!r)return[3,11];a.label=1;case 1:return a.trys.push([1,10,,11]),function(t,e){void 0===t&&(t=window.location);var n=t.origin,r=t.pathname,o=new URLSearchParams(t.search);return n+r===e&&o.has("code")&&!o.has("error")}(window.location,w)?[4,r.handleRedirectCallback()]:[3,4];case 2:return t=a.sent().state,[4,r.getUser()];case 3:return o=a.sent(),_({type:"INITIALIZED",idToken:o}),g(t),[3,9];case 4:return function(t,e){void 0===t&&(t=window.location);var n=t.origin,r=t.pathname,o=new URLSearchParams(t.search);return n+r===e&&o.has("state")&&!o.has("error")}(window.location,m)?[4,r.handleLogoutRedirectCallback()]:[3,6];case 5:return e=a.sent().state,_({type:"LOGOUT_COMPLETED"}),g(e),[3,9];case 6:return st(window.location,[w,m])?((n=st(window.location,[w,m]))&&_({type:"ERROR",error:n}),[3,9]):[3,7];case 7:return[4,r.getUser()];case 8:o=a.sent(),_({type:"INITIALIZED",idToken:o}),a.label=9;case 9:return[3,11];case 10:return i=a.sent(),_({type:"ERROR",error:ut(i)}),[3,11];case 11:return[2]}}))}))}),[r]);var k=s((function(t){void 0===t&&(t={}),null==r||r.loginWithRedirect(t)}),[r]),T=s((function(t){void 0===t&&(t={}),t.post_logout_redirect_uri||(t.post_logout_redirect_uri=e.post_logout_redirect_uri),null==r||r.logoutWithRedirect(t)}),[r]),O=s((function(t){return void 0===t&&(t={}),c(void 0,void 0,void 0,(function(){return l(this,(function(e){switch(e.label){case 0:return[4,null==r?void 0:r.getAccessToken(t)];case 1:return[2,e.sent()||""]}}))}))}),[r]);return t.createElement(f.Provider,{value:u(u({},v),{loginWithRedirect:k,logoutWithRedirect:T,getAccessToken:O})},b)};export{dt as AuthProvider,v as useAuth,y as withAuth}; | ||
//# sourceMappingURL=index.js.map |
@@ -1,35 +0,11 @@ | ||
import React from 'react'; | ||
import { Client, IDToken, ClientOpts, ClientDiscoveryOpts, ClientCrossidOpts, AuthorizationOpts, LogoutOpts, GetAccessTokenOpts } from '@crossid/crossid-spa-js'; | ||
/// <reference types="react" /> | ||
import { IDToken } from '@crossid/crossid-spa-js'; | ||
import { AuthProps } from './provider-options'; | ||
/** | ||
* The state which is returned when using the auth hook. | ||
* A state that can be passed before starting the login process. | ||
*/ | ||
export interface AuthState<User extends IDToken = IDToken> { | ||
loading: boolean; | ||
error?: Error; | ||
user?: User; | ||
client?: Client; | ||
loginState?: string; | ||
export interface AppState { | ||
return_to?: string; | ||
[key: string]: any; | ||
} | ||
export declare const AuthContext: React.Context<AuthState<IDToken>>; | ||
interface AuthActions { | ||
loginWithRedirect: (opts: AuthorizationOpts, returnTo: string) => void; | ||
logoutWithRedirect: (opts: LogoutOpts) => void; | ||
getAccessToken: (opts?: GetAccessTokenOpts) => Promise<string>; | ||
} | ||
export declare const AuthActionsContext: React.Context<AuthActions>; | ||
declare class AuthProviderOptsBase { | ||
children: React.ReactNode; | ||
goTo?: (url: string) => void; | ||
post_logout_redirect_uri?: string; | ||
} | ||
interface AuthProviderClientCrossidOpts extends ClientCrossidOpts, AuthProviderOptsBase { | ||
} | ||
interface AuthProviderClientDiscoveryOpts extends ClientDiscoveryOpts, AuthProviderOptsBase { | ||
} | ||
interface AuthProviderClientCustomOpts extends ClientOpts, AuthProviderOptsBase { | ||
} | ||
export declare const useCrossidAuth: () => AuthState<IDToken>; | ||
export declare const useCrossidAuthActions: () => AuthActions; | ||
declare type AuthProps = AuthProviderClientCrossidOpts | AuthProviderClientDiscoveryOpts | AuthProviderClientCustomOpts; | ||
declare const AuthProvider: <U extends IDToken>(props: AuthProps) => JSX.Element; | ||
export default AuthProvider; | ||
export declare const AuthProvider: <T extends IDToken>(props: AuthProps) => JSX.Element; |
@@ -0,6 +1,16 @@ | ||
import { AuthorizationOpts } from '@crossid/crossid-spa-js'; | ||
import React from 'react'; | ||
export interface AuthRequiredOpts { | ||
returnTo?: string; | ||
scope?: string; | ||
return_to?: string | (() => string); | ||
login_opts?: AuthorizationOpts; | ||
} | ||
export declare function withAuth<T>(WrappedComponent: React.ComponentType<T>, ErrorComponent: Function, opts?: AuthRequiredOpts): (props: T) => JSX.Element; | ||
/** | ||
* a high order component that renders children only if user is authenticated. | ||
* anonymous visitors will be redirected to the login page. | ||
* | ||
* wrap your private routes with this high order component. | ||
* @param WrappedComponent | ||
* @param opts | ||
* @returns | ||
*/ | ||
export declare function withAuth<T>(WrappedComponent: React.ComponentType<T>, opts?: AuthRequiredOpts): (props: T) => JSX.Element; |
{ | ||
"author": "Crossid", | ||
"name": "@crossid/crossid-react", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"description": "Openid authentication hooks for React", | ||
@@ -37,3 +37,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"@crossid/crossid-spa-js": "^0.1.1" | ||
"@crossid/crossid-spa-js": "^0.3.0" | ||
}, | ||
@@ -40,0 +40,0 @@ "peerDependencies": { |
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
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
221635
17
415
1
119
+ Added@crossid/crossid-spa-js@0.3.2(transitive)
- Removed@crossid/crossid-spa-js@0.1.1(transitive)