@crossid/crossid-react
Advanced tools
Comparing version 0.0.2 to 0.1.0
@@ -0,1 +1,9 @@ | ||
## 0.1.0 | ||
[All Changes](https://github.com/crossid/crossid-spa-js/compare/v0.0.2...v0.1.0) | ||
### Major Changes | ||
- logout via redirect. | ||
## 0.0.2 | ||
@@ -2,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{c(r.next(t))}catch(t){i(t)}}function u(t){try{c(r.throw(t))}catch(t){i(t)}}function c(t){var e;t.done?o(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(a,u)}c((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:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function u(i){return function(u){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,u])}}} | ||
***************************************************************************** */function i(t,e,n,r){return new(n||(n=Promise))((function(o,i){function a(t){try{s(r.next(t))}catch(t){i(t)}}function u(t){try{s(r.throw(t))}catch(t){i(t)}}function s(t){var e;t.done?o(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(a,u)}s((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:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function u(i){return function(u){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,u])}}} | ||
/*! ***************************************************************************** | ||
@@ -30,3 +30,3 @@ Copyright (c) Microsoft Corporation. | ||
PERFORMANCE OF THIS SOFTWARE. | ||
***************************************************************************** */var u=function(t,e){return(u=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 c(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}u(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}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 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 f(t,e,n,r){return new(n||(n=Promise))((function(o,i){function a(t){try{c(r.next(t))}catch(t){i(t)}}function u(t){try{c(r.throw(t))}catch(t){i(t)}}function c(t){var e;t.done?o(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(a,u)}c((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:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function u(i){return function(u){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,u])}}}var d=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 c(e,t),e.create=function(t){return new e(t.error,t.error_description)},e}(Error);function h(t){var e=t.wellknown_endpoint;return f(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 f(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,(u=e,c={timeout:n,method:"POST",body:t},void 0===c&&(c={}),f(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=c.timeout,e=void 0===t?5e3:t,n=new AbortController,r=setTimeout((function(){return n.abort()}),e),[4,fetch(u,s(s({},c),{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 d.create(i);return[2,i]}var u,c}))}))}var y=function(t){return null!==t&&"Object"===(null==t?void 0:t.constructor.name)},_=function(t){return"number"==typeof t},g=function(t){return"string"==typeof t},w=String.fromCharCode(0),m=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={})[w]=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)&&w 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[w]: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)&&w 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}(),b=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 c(e,t),e}(m),k=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 c(e,t),e}(m),O=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 O,e)||this}return c(e,t),e}(m),T=function(t){return Array.from(new Set(t))},j=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return T(t.join(" ").trim().split(/\s+/))},S=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},E=function(t){return btoa(t)},A=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){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(A(I(r)));if(a.aud)if(g(a.aud))a.aud=[a.aud];else if(i=a.aud,!Array.isArray(i)||!i.every((function(t){return g(t)})))throw new Error("Audience (aud) must be a string or an array of strings");return{header:JSON.parse(A(I(n))),payload:a}},R=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 c(e,t),e.prototype.getMissingFields=function(){return this.fields_},e}(Error),D=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 R(r,r.join(","));return null},z=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}},U=function(t){return function(e){return J("Issuer (iss)",t,e.payload.iss)}},K=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}},F=function(t,e){return void 0===t&&(t=!0),void 0===e&&(e=40),function(n){if(!t&&!n.payload.exp)return null;if(!_(n.payload.exp))return"Expiration Time (exp) claim must be a present number";var r=L(n.payload.exp,e);return new Date(Date.now())>r?"Expiration Time (exp) claim expired ("+r+")":null}},W=function(t,e){return void 0===t&&(t=!0),void 0===e&&(e=40),function(n){if(!t&&!n.payload.nbf)return null;if(!_(n.payload.nbf))return"Not Before (nbf) claim must be a present number";var r=L(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},L=function(t,e){void 0===e&&(e=0);var n=new Date(0);return n.setUTCSeconds(t+e),n},q="crossid-spa-js|index",M=function(){function t(t){this.opts=t,this.stateKey="crossid-spa-js|login",this.scope=t.scope,this.state=new b({ttl:300}),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 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 f(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)),f(this,void 0,void 0,(function(){var e,n,r,o,i,a,u;return p(this,(function(c){switch(c.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.stateKey))?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=c.sent(),a=P(i.id_token),u=P(i.access_token),this.state.remove(this.stateKey),this._assertAccessToken(u,o.audience),this._assertIDToken(a,o.nonce),u.payload._raw=i.access_token,this._cacheTokens(a,u,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 p(this,(function(i){return e=t.audience||this.opts.audience,n=j(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 p(this,(function(a){return n=t.audience||this.opts.audience,r=j(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._createAuthorizationData=function(t){return f(this,void 0,void 0,(function(){var e,n,r,o,i,a;return p(this,(function(u){switch(u.label){case 0:return e=C(E(S())),n=C(E(S())),r=S(),[4,(s=r,f(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(s))];case 1:return[2,t.sent()]}}))})))];case 1:return o=u.sent(),c=new Uint8Array(o),i=C(E(String.fromCharCode.apply(String,Array.from(c)))),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 c,s}))}))},t.prototype._persistAuthorizationData=function(t){return f(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.stateKey,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._assertAccessToken=function(t,e){D(t,z(["alg"],["iss","sub","aud","exp","iat"]),U(this.opts.issuer),K("RS256"),N(e),F(!0),W(!0))},t.prototype._assertIDToken=function(t,e){var n;return D(t,z(["alg"],["iss","sub","aud","nonce","exp","iat"]),U(this.opts.issuer),(n=e,function(t){return J("Nonce (nonce)",n,t.payload.nonce)}),K("RS256"),N([this.opts.client_id]),F(!0),W(!1),(function(t){return _(t.payload.iat)?null:"Issued At (iat) claim must be a present number"})),null},t.prototype._authorizeUrl=function(t){return this.opts.authorization_endpoint+"?"+(e=t,Object.keys(e).filter((function(t){return void 0!==e[t]&&null!==e[t]})).map((function(t){return encodeURIComponent(t)+"="+encodeURIComponent(e[t])})).join("&"));var e},t.prototype._cacheFactory=function(t){switch(t){case"memory":return new x;case"local_storage":return new k({purgeOnInit:!0});case"session_storage":return new b({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 u=[r[0]],c=this.cache.get(q);c||(c={}),u.forEach((function(t){c[t]||(c[t]={});var e=c[t];o.forEach((function(t){e[t]||(e[t]=[]),e[t].push(a)}))})),this.cache.set(q,c)},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(q)||{})[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:[]}},u=0,c=n;u<c.length;u++){var s=a(c[u]);if("object"==typeof s)return s.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(q);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++)u.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]},u=this,c=0,s=Object.entries(i);c<s.length;c++){var l=s[c];a(l[0],l[1])}Object.keys(t[o]).length||delete t[o]}this.cache.set(q,t)}},t}();function B(t){return f(this,void 0,void 0,(function(){var e,n,r,o,i,a;return p(this,(function(u){switch(u.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,h({wellknown_endpoint:"https://"+e+".crossid.io/oauth2/"+r+"/.well-known/openid-configuration"})];case 1:return a=u.sent(),i.authorization_endpoint=a.authorization_endpoint,i.token_endpoint=a.token_endpoint,i.issuer=a.issuer,[2,new M(i)]}}))}))}function G(t){return f(this,void 0,void 0,(function(){var e,n,r;return p(this,(function(o){switch(o.label){case 0:return[4,h(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 M(r)]}}))}))}function H(t){return f(this,void 0,void 0,(function(){return p(this,(function(e){return[2,new M(t)]}))}))}function V(){throw new Error("please wrap your app with CrossidAuthProvider")}var Q=e.createContext({loading:!0}),X=e.createContext({loginWithRedirect:V,getAccessToken:V}),Y=function(){return e.useContext(Q)},Z=function(){return e.useContext(X)},$=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,B(n)]:[3,2];case 1:return[2,i.sent()];case 2:return r.wellknown_endpoint?[4,G(r)]:[3,4];case 3:return[2,i.sent()];case 4:return o.authorization_endpoint&&o.token_endpoint&&o.issuer?[4,H(o)]:[3,6];case 5:return[2,i.sent()];case 6:throw new Error("invalid props")}}))}))};var tt=function(){return""+window.location.pathname+window.location.search};t.CrossidAuthProvider=function(t){var n=e.useState(),o=n[0],u=n[1],c=e.useState(),s=c[0],l=c[1],f=e.useState(!0),p=f[0],d=f[1],h=e.useState(),v=h[0],y=h[1],_=t.goTo;e.useEffect((function(){i(void 0,void 0,void 0,(function(){var e,n,r,i,c,s,f,p,h;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,$(t)];case 2:return e=a.sent(),u(e),[4,e.getUser()];case 3:return n=a.sent(),y(n),[3,6];case 4:return r=a.sent(),l(r),d(!1),[3,6];case 5:return[2];case 6:return i=window.location,c=i.origin,s=i.pathname,f=new URLSearchParams(window.location.search),o&&c+s===t.redirect_uri&&f.has("code")?[4,o.handleRedirectCallback()]:[3,9];case 7:return p=a.sent(),[4,o.getUser()];case 8:return h=a.sent(),y(h),d(!1),(null==p?void 0:p.state)&&(_?_(p.state):window.history.replaceState({},document.title,p.state)),[3,10];case 9:d(!1),a.label=10;case 10:return[2]}}))}))}),[o,_,t]);var g=e.useCallback((function(t,e){void 0===t&&(t={}),t.state=e,null==o||o.loginWithRedirect(t)}),[o]),w=e.useCallback((function(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(Q.Provider,{value:{user:v,error:s,loading:p,client:o}},r.default.createElement(X.Provider,{value:{loginWithRedirect:g,getAccessToken:w}},t.children))},t.useAuth=Y,t.useAuthActions=Z,t.withAuth=function(t,n){var u=this;return void 0===n&&(n={}),function(c){var s=e.useRef(!1),l=e.useState(!1),f=l[0],p=l[1],d=Y(),h=d.loading,v=d.client,y=Z().loginWithRedirect,_=n.returnTo,g=void 0===_?tt():_;return e.useEffect((function(){if(s.current=!0,!h&&!f)return i(u,void 0,void 0,(function(){return a(this,(function(t){switch(t.label){case 0:return[4,null==v?void 0:v.getUser()];case 1:return t.sent()&&s?(p(!0),[2]):[4,y({},g)];case 2:return t.sent(),[2]}}))})),function(){s.current=!1}}),[f,v,h,y,g]),f?r.default.createElement(t,o({},c)):r.default.createElement("div",null,"Loading...")}},Object.defineProperty(t,"__esModule",{value:!0})})); | ||
***************************************************************************** */var u=function(t,e){return(u=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 s(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}u(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{s(r.next(t))}catch(t){i(t)}}function u(t){try{s(r.throw(t))}catch(t){i(t)}}function s(t){var e;t.done?o(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(a,u)}s((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:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function u(i){return function(u){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,u])}}}var f=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 s(e,t),e.create=function(t){return new e(t.error,t.error_description)},e}(Error);function h(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,(u=e,s={timeout:n,method:"POST",body:t},void 0===s&&(s={}),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=s.timeout,e=void 0===t?5e3:t,n=new AbortController,r=setTimeout((function(){return n.abort()}),e),[4,fetch(u,c(c({},s),{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 f.create(i);return[2,i]}var u,s}))}))}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 s(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 s(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 s(e,t),e}(b),O=function(t){return Array.from(new Set(t))},j=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return O(t.join(" ").trim().split(/\s+/))},E=function(){return t="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~",e=Math.floor(86*Math.random()+43),Array.from(window.crypto.getRandomValues(new Uint8Array(e))).map((function(e){return t[e%t.length]})).join("");var t,e},A=function(t){return btoa(t)},C=function(t){return atob(t)},R=function(t){var e={"+":"-","/":"_","=":""};return t.replace(/[+/=]/g,(function(t){return e[t]}))},I=function(t){var e={"-":"+",_:"/"};return t.replace(/[-_]/g,(function(t){return e[t]}))},P=function(t){return Object.keys(t).filter((function(e){return void 0!==t[e]&&null!==t[e]})).map((function(e){return encodeURIComponent(e)+"="+encodeURIComponent(t[e])})).join("&")},U=function(t){var e=t.split("."),n=e[0],r=e[1],o=e[2];if(3!==e.length||!n||!r||!o)throw new Error("malformed JWT token.");var i,a=JSON.parse(C(I(r)));if(a.aud)if(w(a.aud))a.aud=[a.aud];else if(i=a.aud,!Array.isArray(i)||!i.every((function(t){return w(t)})))throw new Error("Audience (aud) must be a string or an array of strings");return{header:JSON.parse(C(I(n))),payload:a}},D=function(t){function e(e){for(var n=[],r=1;r<arguments.length;r++)n[r-1]=arguments[r];var o=t.apply(this,n)||this;return o.fields_=e,o}return s(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=new k({ttl:300}),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,u;return p(this,(function(s){switch(s.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=s.sent(),(a=U(i.id_token)).payload.__bearer=i.id_token,u=U(i.access_token),this.state.remove(this.loginStateKey),this._assertAccessToken(u,o.audience),this._assertIDToken(a,o.nonce),u.payload._raw=i.access_token,this._cacheTokens(a,u,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=j(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=j(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(u){switch(u.label){case 0:return e=R(A(E())),n=R(A(E())),r=E(),[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=u.sent(),s=new Uint8Array(o),i=R(A(String.fromCharCode.apply(String,Array.from(s)))),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 s,c}))}))},t.prototype._persistAuthorizationData=function(t){return d(this,void 0,void 0,(function(){var e,n;return p(this,(function(r){return e=t.request,n={audience:e.audience,redirect_uri:e.redirect_uri,scope:e.scope,nonce:e.nonce,state:t.appState,code_verifier:t.code_verifier},this.state.set(this.loginStateKey,n),[2]}))}))},t.prototype._mergeAuthorizationCodeParams=function(t){return{client_id:this.opts.client_id,audience:t.audience||this.opts.audience,response_type:t.response_type||this.opts.response_type||"code",redirect_uri:t.redirect_uri||this.opts.redirect_uri,nonce:t.nonce,state:t.state,scope:t.scope||this.opts.scope,code_challenge:t.code_challenge,code_challenge_method:"S256"}},t.prototype._createLogoutData=function(t){return d(this,void 0,void 0,(function(){var e,n;return p(this,(function(r){switch(r.label){case 0:return(e={id_token_hint:t.id_token_hint,post_logout_redirect_uri:t.post_logout_redirect_uri}).id_token_hint||null===e.id_token_hint?[3,2]:[4,this.getUser()];case 1:(n=r.sent())&&n.__bearer&&(e.id_token_hint=n.__bearer),r.label=2;case 2:return e.id_token_hint&&(e.state=R(A(E()))),[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._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 u=[r[0]],s=this.cache.get(_);s||(s={}),u.forEach((function(t){s[t]||(s[t]={});var e=s[t];o.forEach((function(t){e[t]||(e[t]=[]),e[t].push(a)}))})),this.cache.set(_,s)},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:[]}},u=0,s=n;u<s.length;u++){var c=a(s[u]);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++)u.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]},u=this,s=0,c=Object.entries(i);s<c.length;s++){var l=c[s];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(u){switch(u.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,h({wellknown_endpoint:"https://"+e+".crossid.io/oauth2/"+r+"/.well-known/openid-configuration"})];case 1:return a=u.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,h(t)];case 1:return e=o.sent(),n=l(t,["wellknown_endpoint"]),(r=n).authorization_endpoint=e.authorization_endpoint,r.token_endpoint=e.token_endpoint,r.issuer=e.issuer,[2,new B(r)]}}))}))}function V(t){return d(this,void 0,void 0,(function(){return p(this,(function(e){return[2,new B(t)]}))}))}function Q(){throw new Error("please wrap your app with CrossidAuthProvider")}var X=e.createContext({loading:!0}),Y=e.createContext({loginWithRedirect:Q,logoutWithRedirect:Q,getAccessToken:Q}),Z=function(){return e.useContext(X)},$=function(){return e.useContext(Y)},tt=function(t){return i(void 0,void 0,void 0,(function(){var e,n,r,o;return a(this,(function(i){switch(i.label){case 0:return r=e=t,o=e,(n=e).tenant_id?[4,G(n)]:[3,2];case 1:return[2,i.sent()];case 2:return r.wellknown_endpoint?[4,H(r)]:[3,4];case 3:return[2,i.sent()];case 4:return o.authorization_endpoint&&o.token_endpoint&&o.issuer?[4,V(o)]:[3,6];case 5:return[2,i.sent()];case 6:throw new Error("invalid props")}}))}))};var et=function(){return""+window.location.pathname+window.location.search};t.CrossidAuthProvider=function(t){var n=e.useState(),o=n[0],u=n[1],s=e.useState(),c=s[0],l=s[1],d=e.useState(!0),p=d[0],f=d[1],h=e.useState(),v=h[0],_=h[1],y=t.goTo;e.useEffect((function(){i(void 0,void 0,void 0,(function(){var e,n,r,i,s,c,d,p,h;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(),u(e),[4,e.getUser()];case 3:return n=a.sent(),_(n),[3,6];case 4:return r=a.sent(),l(r),f(!1),[3,6];case 5:return[2];case 6:return i=window.location,s=i.origin,c=i.pathname,d=new URLSearchParams(window.location.search),o&&s+c===t.redirect_uri&&d.has("code")&&!d.has("error")?[4,o.handleRedirectCallback()]:[3,9];case 7:return h=a.sent(),[4,o.getUser()];case 8:return p=a.sent(),_(p),f(!1),(null==h?void 0:h.state)&&(y?y(h.state):window.history.replaceState({},document.title,h.state)),[3,12];case 9:return o&&s+c===t.post_logout_redirect_uri&&d.has("state")&&!d.has("error")?[4,o.handleLogoutRedirectCallback()]:[3,11];case 10:return h=a.sent(),_(void 0),f(!1),(null==h?void 0:h.state)&&(y?y(h.state):window.history.replaceState({},document.title,h.state)),[3,12];case 11:f(!1),a.label=12;case 12:return[2]}}))}))}),[o,y,t]);var g=e.useCallback((function(t,e){void 0===t&&(t={}),t.state=e,null==o||o.loginWithRedirect(t)}),[o]),w=e.useCallback((function(e){void 0===e&&(e={}),e.post_logout_redirect_uri||(e.post_logout_redirect_uri=t.post_logout_redirect_uri),null==o||o.logoutWithRedirect(e)}),[o]),m=e.useCallback((function(t){return void 0===t&&(t={}),i(void 0,void 0,void 0,(function(){return a(this,(function(e){switch(e.label){case 0:return[4,null==o?void 0:o.getAccessToken(t)];case 1:return[2,e.sent()||""]}}))}))}),[o]);return r.default.createElement(X.Provider,{value:{user:v,error:c,loading:p,client:o}},r.default.createElement(Y.Provider,{value:{loginWithRedirect:g,logoutWithRedirect:w,getAccessToken:m}},t.children))},t.useAuth=Z,t.useAuthActions=$,t.withAuth=function(t,n){var u=this;return void 0===n&&(n={}),function(s){var c=e.useRef(!1),l=e.useState(!1),d=l[0],p=l[1],f=Z(),h=f.loading,v=f.client,_=$().loginWithRedirect,y=n.returnTo,g=void 0===y?et():y;return e.useEffect((function(){if(c.current=!0,!h&&!d)return i(u,void 0,void 0,(function(){return a(this,(function(t){switch(t.label){case 0:return[4,null==v?void 0:v.getUser()];case 1:return t.sent()&&c?(p(!0),[2]):[4,_({},g)];case 2:return t.sent(),[2]}}))})),function(){c.current=!1}}),[d,v,h,_,g]),d?r.default.createElement(t,o({},s)):r.default.createElement("div",null,"Loading...")}},Object.defineProperty(t,"__esModule",{value:!0})})); | ||
//# sourceMappingURL=crossid-react.js.map |
@@ -15,3 +15,3 @@ import t,{createContext as e,useContext as n,useState as r,useEffect as o,useCallback as i,useRef as a}from"react"; | ||
PERFORMANCE OF THIS SOFTWARE. | ||
***************************************************************************** */var 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{c(r.next(t))}catch(t){i(t)}}function u(t){try{c(r.throw(t))}catch(t){i(t)}}function c(t){var e;t.done?o(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(a,u)}c((r=r.apply(t,e||[])).next())}))}function s(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:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function u(i){return function(u){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,u])}}} | ||
***************************************************************************** */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 s(t,e,n,r){return new(n||(n=Promise))((function(o,i){function a(t){try{s(r.next(t))}catch(t){i(t)}}function u(t){try{s(r.throw(t))}catch(t){i(t)}}function s(t){var e;t.done?o(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(a,u)}s((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:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function u(i){return function(u){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,u])}}} | ||
/*! ***************************************************************************** | ||
@@ -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 f(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 p=function(){return(p=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var o in e=arguments[n])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t}).apply(this,arguments)};function d(t,e){var n={};for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&e.indexOf(r)<0&&(n[r]=t[r]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(r=Object.getOwnPropertySymbols(t);o<r.length;o++)e.indexOf(r[o])<0&&Object.prototype.propertyIsEnumerable.call(t,r[o])&&(n[r[o]]=t[r[o]])}return n}function h(t,e,n,r){return new(n||(n=Promise))((function(o,i){function a(t){try{c(r.next(t))}catch(t){i(t)}}function u(t){try{c(r.throw(t))}catch(t){i(t)}}function c(t){var e;t.done?o(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(a,u)}c((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:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function u(i){return function(u){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,u])}}}var y=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 f(e,t),e.create=function(t){return new e(t.error,t.error_description)},e}(Error);function _(t){var e=t.wellknown_endpoint;return h(this,void 0,void 0,(function(){var t;return v(this,(function(n){switch(n.label){case 0:return[4,fetch(e)];case 1:return 200!==(t=n.sent()).status?[3,3]:[4,t.json()];case 2:return[2,n.sent()];case 3:throw Error("failed to discover data from well known discovery endpoint.")}}))}))}function g(t){var e=t.tokenEndpoint,n=t.timeout,r=d(t,["tokenEndpoint","timeout"]);return h(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,(u=e,c={timeout:n,method:"POST",body:t},void 0===c&&(c={}),h(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=c.timeout,e=void 0===t?5e3:t,n=new AbortController,r=setTimeout((function(){return n.abort()}),e),[4,fetch(u,p(p({},c),{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 y.create(i);return[2,i]}var u,c}))}))}var w=function(t){return null!==t&&"Object"===(null==t?void 0:t.constructor.name)},m=function(t){return"number"==typeof t},b=function(t){return"string"==typeof t},k=String.fromCharCode(0),O=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={})[k]=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 w(n=i)&&k 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[k]: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}(w(i)&&k 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}(),T=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 f(e,t),e}(O),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 f(e,t),e}(O),j=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}(),E=function(t){function e(e){return t.call(this,new j,e)||this}return f(e,t),e}(O),A=function(t){return Array.from(new Set(t))},S=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},P=function(t){return btoa(t)},C=function(t){return atob(t)},R=function(t){var e={"+":"-","/":"_","=":""};return t.replace(/[+/=]/g,(function(t){return e[t]}))},D=function(t){var e={"-":"+",_:"/"};return t.replace(/[-_]/g,(function(t){return e[t]}))},z=function(t){var e=t.split("."),n=e[0],r=e[1],o=e[2];if(3!==e.length||!n||!r||!o)throw new Error("malformed JWT token.");var i,a=JSON.parse(C(D(r)));if(a.aud)if(b(a.aud))a.aud=[a.aud];else if(i=a.aud,!Array.isArray(i)||!i.every((function(t){return b(t)})))throw new Error("Audience (aud) must be a string or an array of strings");return{header:JSON.parse(C(D(n))),payload:a}},U=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 f(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 U(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}},F=function(t){return function(e){return M("Issuer (iss)",t,e.payload.iss)}},W=function(t){return function(e){return M("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}},L=function(t,e){return void 0===t&&(t=!0),void 0===e&&(e=40),function(n){if(!t&&!n.payload.exp)return null;if(!m(n.payload.exp))return"Expiration Time (exp) claim must be a present number";var r=B(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(!m(n.payload.nbf))return"Not Before (nbf) claim must be a present number";var r=B(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}},M=function(t,e,n){return e!==n?t+' mismatch: expected "'+e+'" but got "'+n+'"':null},B=function(t,e){void 0===e&&(e=0);var n=new Date(0);return n.setUTCSeconds(t+e),n},G="crossid-spa-js|index",H=function(){function t(t){this.opts=t,this.stateKey="crossid-spa-js|login",this.scope=t.scope,this.state=new T({ttl:300}),this.cache=this._cacheFactory(this.opts.cache_type||"memory"),this._purgeIndex()}return t.prototype.createRedirectURL=function(t){return void 0===t&&(t={}),h(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 h(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)),h(this,void 0,void 0,(function(){var e,n,r,o,i,a,u;return v(this,(function(c){switch(c.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.stateKey))?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=c.sent(),a=z(i.id_token),u=z(i.access_token),this.state.remove(this.stateKey),this._assertAccessToken(u,o.audience),this._assertIDToken(a,o.nonce),u.payload._raw=i.access_token,this._cacheTokens(a,u,i.refresh_token),[2,{state:o.state}]}}))}))},t.prototype.getUser=function(t){return void 0===t&&(t={}),h(this,void 0,void 0,(function(){var e,n,r,o;return v(this,(function(i){return e=t.audience||this.opts.audience,n=S(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={}),h(this,void 0,void 0,(function(){var n,r,o,i;return v(this,(function(a){return n=t.audience||this.opts.audience,r=S(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._createAuthorizationData=function(t){return h(this,void 0,void 0,(function(){var e,n,r,o,i,a;return v(this,(function(u){switch(u.label){case 0:return e=R(P(I())),n=R(P(I())),r=I(),[4,(s=r,h(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(s))];case 1:return[2,t.sent()]}}))})))];case 1:return o=u.sent(),c=new Uint8Array(o),i=R(P(String.fromCharCode.apply(String,Array.from(c)))),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 c,s}))}))},t.prototype._persistAuthorizationData=function(t){return h(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.stateKey,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._assertAccessToken=function(t,e){K(t,N(["alg"],["iss","sub","aud","exp","iat"]),F(this.opts.issuer),W("RS256"),J(e),L(!0),q(!0))},t.prototype._assertIDToken=function(t,e){var n;return K(t,N(["alg"],["iss","sub","aud","nonce","exp","iat"]),F(this.opts.issuer),(n=e,function(t){return M("Nonce (nonce)",n,t.payload.nonce)}),W("RS256"),J([this.opts.client_id]),L(!0),q(!1),(function(t){return m(t.payload.iat)?null:"Issued At (iat) claim must be a present number"})),null},t.prototype._authorizeUrl=function(t){return this.opts.authorization_endpoint+"?"+(e=t,Object.keys(e).filter((function(t){return void 0!==e[t]&&null!==e[t]})).map((function(t){return encodeURIComponent(t)+"="+encodeURIComponent(e[t])})).join("&"));var e},t.prototype._cacheFactory=function(t){switch(t){case"memory":return new E;case"local_storage":return new x({purgeOnInit:!0});case"session_storage":return new T({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 u=[r[0]],c=this.cache.get(G);c||(c={}),u.forEach((function(t){c[t]||(c[t]={});var e=c[t];o.forEach((function(t){e[t]||(e[t]=[]),e[t].push(a)}))})),this.cache.set(G,c)},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(G)||{})[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:[]}},u=0,c=n;u<c.length;u++){var s=a(c[u]);if("object"==typeof s)return s.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(G);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++)u.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]},u=this,c=0,s=Object.entries(i);c<s.length;c++){var l=s[c];a(l[0],l[1])}Object.keys(t[o]).length||delete t[o]}this.cache.set(G,t)}},t}();function V(t){return h(this,void 0,void 0,(function(){var e,n,r,o,i,a;return v(this,(function(u){switch(u.label){case 0:return e=t.tenant_id,n=t.auth_server,r=void 0===n?"default":n,o=d(t,["tenant_id","auth_server"]),i=o,[4,_({wellknown_endpoint:"https://"+e+".crossid.io/oauth2/"+r+"/.well-known/openid-configuration"})];case 1:return a=u.sent(),i.authorization_endpoint=a.authorization_endpoint,i.token_endpoint=a.token_endpoint,i.issuer=a.issuer,[2,new H(i)]}}))}))}function Q(t){return h(this,void 0,void 0,(function(){var e,n,r;return v(this,(function(o){switch(o.label){case 0:return[4,_(t)];case 1:return e=o.sent(),n=d(t,["wellknown_endpoint"]),(r=n).authorization_endpoint=e.authorization_endpoint,r.token_endpoint=e.token_endpoint,r.issuer=e.issuer,[2,new H(r)]}}))}))}function X(t){return h(this,void 0,void 0,(function(){return v(this,(function(e){return[2,new H(t)]}))}))}function Y(){throw new Error("please wrap your app with CrossidAuthProvider")}var Z=e({loading:!0}),$=e({loginWithRedirect:Y,getAccessToken:Y}),tt=function(){return n(Z)},et=function(){return n($)},nt=function(t){return c(void 0,void 0,void 0,(function(){var e,n,r,o;return s(this,(function(i){switch(i.label){case 0:return r=e=t,o=e,(n=e).tenant_id?[4,V(n)]:[3,2];case 1:return[2,i.sent()];case 2:return r.wellknown_endpoint?[4,Q(r)]:[3,4];case 3:return[2,i.sent()];case 4:return o.authorization_endpoint&&o.token_endpoint&&o.issuer?[4,X(o)]:[3,6];case 5:return[2,i.sent()];case 6:throw new Error("invalid props")}}))}))},rt=function(e){var n=r(),a=n[0],u=n[1],l=r(),f=l[0],p=l[1],d=r(!0),h=d[0],v=d[1],y=r(),_=y[0],g=y[1],w=e.goTo;o((function(){c(void 0,void 0,void 0,(function(){var t,n,r,o,i,c,l,f,d;return s(this,(function(s){switch(s.label){case 0:if(a)return[3,6];s.label=1;case 1:return s.trys.push([1,4,5,6]),[4,nt(e)];case 2:return t=s.sent(),u(t),[4,t.getUser()];case 3:return n=s.sent(),g(n),[3,6];case 4:return r=s.sent(),p(r),v(!1),[3,6];case 5:return[2];case 6:return o=window.location,i=o.origin,c=o.pathname,l=new URLSearchParams(window.location.search),a&&i+c===e.redirect_uri&&l.has("code")?[4,a.handleRedirectCallback()]:[3,9];case 7:return f=s.sent(),[4,a.getUser()];case 8:return d=s.sent(),g(d),v(!1),(null==f?void 0:f.state)&&(w?w(f.state):window.history.replaceState({},document.title,f.state)),[3,10];case 9:v(!1),s.label=10;case 10:return[2]}}))}))}),[a,w,e]);var m=i((function(t,e){void 0===t&&(t={}),t.state=e,null==a||a.loginWithRedirect(t)}),[a]),b=i((function(t){return void 0===t&&(t={}),c(void 0,void 0,void 0,(function(){return s(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(Z.Provider,{value:{user:_,error:f,loading:h,client:a}},t.createElement($.Provider,{value:{loginWithRedirect:m,getAccessToken:b}},e.children))};function ot(e,n){var i=this;void 0===n&&(n={});return function(l){var f=a(!1),p=r(!1),d=p[0],h=p[1],v=tt(),y=v.loading,_=v.client,g=et().loginWithRedirect,w=n.returnTo,m=void 0===w?it():w;return o((function(){if(f.current=!0,!y&&!d)return c(i,void 0,void 0,(function(){return s(this,(function(t){switch(t.label){case 0:return[4,null==_?void 0:_.getUser()];case 1:return t.sent()&&f?(h(!0),[2]):[4,g({},m)];case 2:return t.sent(),[2]}}))})),function(){f.current=!1}}),[d,_,y,g,m]),d?t.createElement(e,u({},l)):t.createElement("div",null,"Loading...")}}var it=function(){return""+window.location.pathname+window.location.search};export{rt as CrossidAuthProvider,tt as useAuth,et as useAuthActions,ot as withAuth}; | ||
***************************************************************************** */var l=function(t,e){return(l=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])})(t,e)};function p(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function n(){this.constructor=t}l(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}var d=function(){return(d=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var o in e=arguments[n])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t}).apply(this,arguments)};function h(t,e){var n={};for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&e.indexOf(r)<0&&(n[r]=t[r]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(r=Object.getOwnPropertySymbols(t);o<r.length;o++)e.indexOf(r[o])<0&&Object.prototype.propertyIsEnumerable.call(t,r[o])&&(n[r[o]]=t[r[o]])}return n}function f(t,e,n,r){return new(n||(n=Promise))((function(o,i){function a(t){try{s(r.next(t))}catch(t){i(t)}}function u(t){try{s(r.throw(t))}catch(t){i(t)}}function s(t){var e;t.done?o(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(a,u)}s((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:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function u(i){return function(u){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,u])}}}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,(u=e,s={timeout:n,method:"POST",body:t},void 0===s&&(s={}),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=s.timeout,e=void 0===t?5e3:t,n=new AbortController,r=setTimeout((function(){return n.abort()}),e),[4,fetch(u,d(d({},s),{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 u,s}))}))}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=new O({ttl:300}),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,u;return v(this,(function(s){switch(s.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=s.sent(),(a=L(i.id_token)).payload.__bearer=i.id_token,u=L(i.access_token),this.state.remove(this.loginStateKey),this._assertAccessToken(u,o.audience),this._assertIDToken(a,o.nonce),u.payload._raw=i.access_token,this._cacheTokens(a,u,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(u){switch(u.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=u.sent(),s=new Uint8Array(o),i=D(C(String.fromCharCode.apply(String,Array.from(s)))),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 s,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._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 u=[r[0]],s=this.cache.get(w);s||(s={}),u.forEach((function(t){s[t]||(s[t]={});var e=s[t];o.forEach((function(t){e[t]||(e[t]=[]),e[t].push(a)}))})),this.cache.set(w,s)},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:[]}},u=0,s=n;u<s.length;u++){var c=a(s[u]);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++)u.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]},u=this,s=0,c=Object.entries(i);s<c.length;s++){var l=c[s];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(u){switch(u.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=u.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 s(void 0,void 0,void 0,(function(){var e,n,r,o;return c(this,(function(i){switch(i.label){case 0:return r=e=t,o=e,(n=e).tenant_id?[4,Q(n)]:[3,2];case 1:return[2,i.sent()];case 2:return r.wellknown_endpoint?[4,X(r)]:[3,4];case 3:return[2,i.sent()];case 4:return o.authorization_endpoint&&o.token_endpoint&&o.issuer?[4,Y(o)]:[3,6];case 5:return[2,i.sent()];case 6:throw new Error("invalid props")}}))}))},ot=function(e){var n=r(),a=n[0],u=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=e.goTo;o((function(){s(void 0,void 0,void 0,(function(){var t,n,r,o,i,s,l,p,h;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(),u(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,s=o.pathname,l=new URLSearchParams(window.location.search),a&&i+s===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)&&(w?w(h.state):window.history.replaceState({},document.title,h.state)),[3,12];case 9:return a&&i+s===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)&&(w?w(h.state):window.history.replaceState({},document.title,h.state)),[3,12];case 11:v(!1),c.label=12;case 12:return[2]}}))}))}),[a,w,e]);var m=i((function(t,e){void 0===t&&(t={}),t.state=e,null==a||a.loginWithRedirect(t)}),[a]),b=i((function(t){void 0===t&&(t={}),t.post_logout_redirect_uri||(t.post_logout_redirect_uri=e.post_logout_redirect_uri),null==a||a.logoutWithRedirect(t)}),[a]),k=i((function(t){return void 0===t&&(t={}),s(void 0,void 0,void 0,(function(){return c(this,(function(e){switch(e.label){case 0:return[4,null==a?void 0:a.getAccessToken(t)];case 1:return[2,e.sent()||""]}}))}))}),[a]);return t.createElement($.Provider,{value:{user:y,error:p,loading:f,client:a}},t.createElement(tt.Provider,{value:{loginWithRedirect:m,logoutWithRedirect:b,getAccessToken:k}},e.children))};function it(e,n){var i=this;void 0===n&&(n={});return function(l){var p=a(!1),d=r(!1),h=d[0],f=d[1],v=et(),_=v.loading,y=v.client,g=nt().loginWithRedirect,w=n.returnTo,m=void 0===w?at():w;return o((function(){if(p.current=!0,!_&&!h)return s(i,void 0,void 0,(function(){return c(this,(function(t){switch(t.label){case 0:return[4,null==y?void 0:y.getUser()];case 1:return t.sent()&&p?(f(!0),[2]):[4,g({},m)];case 2:return t.sent(),[2]}}))})),function(){p.current=!1}}),[h,y,_,g,m]),h?t.createElement(e,u({},l)):t.createElement("div",null,"Loading...")}}var at=function(){return""+window.location.pathname+window.location.search};export{ot as CrossidAuthProvider,et as useAuth,nt as useAuthActions,it as withAuth}; | ||
//# sourceMappingURL=index.js.map |
import React from 'react'; | ||
import { Client, IDToken, ClientOpts, ClientDiscoveryOpts, ClientCrossidOpts, AuthorizationOpts, GetAccessTokenOpts } from '@crossid/crossid-spa-js'; | ||
import { Client, IDToken, ClientOpts, ClientDiscoveryOpts, ClientCrossidOpts, AuthorizationOpts, LogoutOpts, GetAccessTokenOpts } from '@crossid/crossid-spa-js'; | ||
/** | ||
@@ -15,2 +15,3 @@ * The state which is returned when using the auth hook. | ||
loginWithRedirect: (opts: AuthorizationOpts, returnTo: string) => void; | ||
logoutWithRedirect: (opts: LogoutOpts) => void; | ||
getAccessToken: (opts?: GetAccessTokenOpts) => Promise<string>; | ||
@@ -22,2 +23,3 @@ } | ||
goTo?: (url: string) => void; | ||
post_logout_redirect_uri?: string; | ||
} | ||
@@ -24,0 +26,0 @@ interface AuthProviderClientCrossidOpts extends ClientCrossidOpts, AuthProviderOptsBase { |
{ | ||
"author": "Crossid", | ||
"name": "@crossid/crossid-react", | ||
"version": "0.0.2", | ||
"version": "0.1.0", | ||
"description": "Openid authentication hooks for React", | ||
@@ -37,3 +37,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"@crossid/crossid-spa-js": "^0.0.5" | ||
"@crossid/crossid-spa-js": "^0.1.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
195332
276
+ Added@crossid/crossid-spa-js@0.1.1(transitive)
- Removed@crossid/crossid-spa-js@0.0.5(transitive)