openstack-uicore-foundation
Version:
ui reactjs components for openstack marketing site
2 lines • 9.39 kB
JavaScript
!function(e,o){"object"==typeof exports&&"object"==typeof module?module.exports=o():"function"==typeof define&&define.amd?define("openstack-uicore-foundation",[],o):"object"==typeof exports?exports["openstack-uicore-foundation"]=o():e["openstack-uicore-foundation"]=o()}(this,(()=>(()=>{"use strict";var e={5097:(e,o,n)=>{n(1116),n(6842),n(9087),n(9558),n(2183)},3195:(e,o,n)=>{n.d(o,{AUTH_ERROR_MISSING_NONCE_PARAM:()=>r,AUTH_ERROR_MISSING_PKCE_PARAM:()=>t});const t="AUTH_ERROR_MISSING_PKCE_PARAM",r="AUTH_ERROR_MISSING_NONCE_PARAM"},2183:(e,o,n)=>{n.d(o,{RESPONSE_TYPE_CODE:()=>_,RESPONSE_TYPE_IMPLICIT:()=>p,doLogin:()=>y,emitAccessToken:()=>S,getAuthInfo:()=>R,getOAuth2Flow:()=>A,validateIdToken:()=>T});var t=n(9558);n(5812),n(806);const r=require("browser-tabs-lock");var i=n.n(r);require("js-cookie");var a=n(8041),l=n.n(a),c=n(9891),s=n.n(c),d=(n(5097),n(8853)),u=n(3195);new(i());const p="token id_token",_="code",f="authInfo",w="nonce",g="pkce",h=e=>{let o="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",n="";for(let t=0;t<e;t++)n+=o.charAt(Math.floor(62*Math.random()));return n},y=(e=null,o=null,n=null,r=null,i=null,a=null)=>{let c=((e=null,o=null,n=null,r=null,i=null,a=null,c=null)=>{let s=m(),d=(0,t.getAuthCallback)(),u=I(),p=E(),f=A();null!=e&&(d+=`?BackUrl=${encodeURI(e)}`);let y=h(16);(0,t.putOnLocalStorage)(w,y);let S=l()(`${u}/oauth2/auth`),R={response_type:encodeURI(f),scope:encodeURI(p),nonce:y,response_mode:"fragment",client_id:encodeURI(s),redirect_uri:encodeURI(d)};if(f===_){const e=k();(0,t.putOnLocalStorage)(g,JSON.stringify(e)),R.code_challenge=e.codeChallenge,R.code_challenge_method="S256",R.approval_prompt="force"}return o&&(R.prompt=o),p&&p.includes("offline_access")&&(R.prompt="consent"),n&&(R.id_token_hint=n),r&&(R.provider=r),a&&(R.otp_login_hint=a),i&&(R.login_hint=encodeURI(i)),c&&(R.tenant=c),S=S.query(R),S})(e,n,null,o,r,i,a);(0,t.getCurrentLocation)().replace(c.toString())},k=()=>{const e=(0,t.base64URLEncode)((0,d.dz)(64));return{codeVerifier:e,codeChallenge:(0,d.gl)(e,"Base64url"),createdAt:new Date}},S=async(e,o=null)=>{let n=I(),r=m(),i=(0,t.getAuthCallback)(),a=JSON.parse((0,t.getFromLocalStorage)(g,!0));if(!a)throw Error(u.AUTH_ERROR_MISSING_PKCE_PARAM);null!=o&&(i+=`?BackUrl=${encodeURI(o)}`);const l={code:e,grant_type:"authorization_code",code_verifier:a.codeVerifier,client_id:encodeURI(r),redirect_uri:encodeURI(i)};try{const e=await fetch(`${n}/oauth2/token`,{method:"POST",headers:{Accept:"application/json","Content-Type":"application/json"},body:JSON.stringify(l)}).catch((function(e){console.log("Request failed:",e.message)})),o=await e.json();let{access_token:t,refresh_token:r,id_token:i,expires_in:a,error:c,error_description:s}=o;return{access_token:t,refresh_token:r,id_token:i,expires_in:a,error:c,error_description:s}}catch(e){console.log(e)}},R=()=>{try{let e=(0,t.getFromLocalStorage)(f,!1);return e?JSON.parse(e):null}catch(e){return null}},m=()=>"undefined"!=typeof window?window.OAUTH2_CLIENT_ID:null,A=()=>"undefined"!=typeof window&&window.OAUTH2_FLOW||"token id_token",I=()=>"undefined"!=typeof window?window.IDP_BASE_URL:null,E=()=>"undefined"!=typeof window?window.SCOPES:null,T=(e,o,n)=>{let r=new(s())({issuer:o,audience:n}),i=(0,t.getFromLocalStorage)(w,!0);if(!i)throw Error(u.AUTH_ERROR_MISSING_NONCE_PARAM);let a=r.decode(e),l=(a.header.alg,a.header.kid,a.payload.aud),c=a.payload.iss;a.payload.exp,a.payload.nbf;return(a.payload.nonce||null)==i&&l==n&&c==o}},9087:(e,o,n)=>{n(2462),n(806);var t=n(8041),r=n.n(t);n(9236),n(6842),n(9558),n(5097),n(2183);r().escapeQuerySpace=!1;const i=e=>o=>({type:e,payload:o});i("RESET_LOADING"),i("START_LOADING"),i("STOP_LOADING")},8853:(e,o,n)=>{n.d(o,{dz:()=>p,gl:()=>_});require("spark-md5");const t=require("crypto-js/sha256");var r=n.n(t);const i=require("crypto-js/enc-base64url");var l=n.n(i);const c=require("crypto-js/enc-hex");var s=n.n(c);const d=65536,u="undefined"!=typeof window?window.crypto||window.msCrypto:null,p=e=>{if(e>4294967295)throw new RangeError("requested too many random bytes");const o=Buffer.allocUnsafe(e);if(!u)return a;if(e>0)if(e>d)for(let n=0;n<e;n+=d)u.getRandomValues(o.slice(n,n+d));else u.getRandomValues(o);return o},_=(e,o="hex")=>{let n=s();return"Base64url"===o&&(n=l()),r()(e).toString(n)}},9558:(e,o,n)=>{n.d(o,{base64URLEncode:()=>d,getAuthCallback:()=>t,getCurrentHref:()=>a,getCurrentLocation:()=>r,getCurrentPathName:()=>i,getFromLocalStorage:()=>c,putOnLocalStorage:()=>l});n(5812),n(8041);const t=()=>"undefined"!=typeof window?`${window.location.origin}/auth/callback`:null,r=()=>{let e="";return"undefined"!=typeof window&&(e=window.location,window.top&&(e=window.top.location)),e},i=()=>"undefined"!=typeof window?window.location.pathname:null,a=()=>"undefined"!=typeof window?window.location.href:null,l=(e,o)=>{"undefined"!=typeof window&&window.localStorage.setItem(e,o)},c=(e,o)=>{if("undefined"!=typeof window){let n=window.localStorage.getItem(e);return o&&(console.log(`getFromLocalStorage removing key ${e}`),s(e)),n}return null},s=e=>{"undefined"!=typeof window&&window.localStorage.removeItem(e)},d=e=>e.toString("base64").replace(/\+/g,"-").replace(/\//g,"_").replace(/=/g,"")},1116:e=>{e.exports=require("@babel/runtime/helpers/defineProperty")},2462:e=>{e.exports=require("@babel/runtime/helpers/objectWithoutProperties")},6842:e=>{e.exports=require("i18n-react/dist/i18n-react")},9891:e=>{e.exports=require("idtoken-verifier")},5812:e=>{e.exports=require("moment-timezone")},806:e=>{e.exports=require("superagent/lib/client")},9236:e=>{e.exports=require("sweetalert2")},8041:e=>{e.exports=require("urijs")}},o={};function n(t){var r=o[t];if(void 0!==r)return r.exports;var i=o[t]={exports:{}};return e[t](i,i.exports,n),i.exports}(()=>{n.n=e=>{var o=e&&e.__esModule?()=>e.default:()=>e;return n.d(o,{a:o}),o}})(),(()=>{n.d=(e,o)=>{for(var t in o)n.o(o,t)&&!n.o(e,t)&&Object.defineProperty(e,t,{enumerable:!0,get:o[t]})}})(),(()=>{n.o=(e,o)=>Object.prototype.hasOwnProperty.call(e,o)})(),(()=>{n.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}})();var t={};n.r(t),n.d(t,{default:()=>_});const r=require("react"),i=require("prop-types");var l=n.n(i),c=n(8041),s=n.n(c),d=n(2183),u=n(9558);const p=({issuer:e,audience:o,location:n,callback:t,redirectToError:i,onUserAuth:a})=>{const[l,c]=(0,r.useState)(!0),[p,_]=(0,r.useState)({error:null,description:null}),[f,w]=(0,r.useState)(null),g=(0,d.getOAuth2Flow)();(0,r.useEffect)((()=>{g===d.RESPONSE_TYPE_IMPLICIT?y():g===d.RESPONSE_TYPE_CODE&&k()}),[]),(0,r.useEffect)((()=>{if(console.log("accessToken",f),f){const e=s()((0,u.getCurrentHref)()),o=e.search(!0),n=s().parseQuery(e.fragment());delete n.code,delete n.access_token,delete n.expires_in,delete n.token_type,delete n.scope,delete n.id_token,delete n.session_state;let r=o.hasOwnProperty("BackUrl")?o.BackUrl:"/app";n.length>0&&(r+=`#${s().buildQuery(n)}`),t(r)}}),[f]);const h=()=>s().parseQuery(n.hash.substr(1)),y=async()=>{const{access_token:n,id_token:t,session_state:r,error:i,error_description:l,expires_in:s}=h();if(i)return void _({error:i,description:l});if(!n)return void(0,d.doLogin)((0,u.getCurrentPathName)());const p=!!t&&(0,d.validateIdToken)(t,e,o);c(p),_({error:p?null:"Invalid Token",description:p?null:"Invalid Token"}),n&&p&&(await a(n,t,r,s),w(n),"undefined"!=typeof window&&window.location!==window.parent.location&&(console.log("AbstractAuthorizationCallbackRouteV2::_implicitFlow running inside iframe, sending auth state to parent"),window.parent.postMessage(JSON.stringify({action:"SET_AUTH_INFO_SILENTLY",access_token:n,id_token:t,session_state:r,expires_in:s}),window.location.origin)))},k=()=>{const{code:n,session_state:t,error:r,error_description:i}=h();if(r)return void _({error:r,description:i});if(!n)return void(0,d.doLogin)((0,u.getCurrentPathName)());let l=s()((0,u.getCurrentHref)()).search(!0),p=l.hasOwnProperty("BackUrl")?l.BackUrl:null;(0,d.emitAccessToken)(n,p).then((async n=>{const{id_token:r,access_token:i,refresh_token:l,expires_in:s,error:u,error_description:p}=n;if(u)return void _({error:u,description:p});const f=!!r&&(0,d.validateIdToken)(r,e,o);c(f),_({error:f?null:"Invalid Token",description:f?null:"Invalid Token"}),i&&f&&(console.log("AbstractAuthorizationCallbackRouteV2::_codeFlow [ASYNC] onUserAuth"),await a(i,r,t,s,l),console.log("AUTH",i,(0,d.getAuthInfo)()),w(i),"undefined"!=typeof window&&window.location!==window.parent.location&&(console.log("AbstractAuthorizationCallbackRouteV2::_codeFlow running inside iframe, sending auth state to parent"),window.parent.postMessage(JSON.stringify({action:"SET_AUTH_INFO_SILENTLY",access_token:i,id_token:r,session_state:t,expires_in:s,refresh_token:l}),window.location.origin)))})).catch((e=>{console.log(e),(0,d.doLogin)((0,u.getCurrentPathName)())}))};return null!=p&&p.error?i(`${null==p?void 0:p.error} - ${null==p?void 0:p.description}.`):l?null:i("Token Validation Error - Token is invalid.")};p.propTypes={issuer:l().string.isRequired,audience:l().string.isRequired,location:l().object.isRequired,callback:l().func,redirectToError:l().func,onUserAuth:l().func.isRequired},p.defaultProps={callback:()=>{},redirectToError:()=>{}};const _=(0,r.memo)(p);return t})()));
//# sourceMappingURL=abstract-auth-callback-route-v2.js.map