openstack-uicore-foundation
Version:
ui reactjs components for openstack marketing site
2 lines • 14 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,t)=>{t.d(o,{SET_LOGGED_USER:()=>n});t(1116),t(6842),t(9087),t(9558),t(2183);const n="SET_LOGGED_USER"},3195:(e,o,t)=>{t.d(o,{AUTH_ERROR_ACCESS_TOKEN_EXPIRED:()=>i,AUTH_ERROR_ID_TOKEN_INVALID:()=>l,AUTH_ERROR_LOCK_ACQUIRE_ERROR:()=>s,AUTH_ERROR_MISSING_AUTH_INFO:()=>n,AUTH_ERROR_MISSING_NONCE_PARAM:()=>u,AUTH_ERROR_MISSING_OTP_PARAM:()=>_,AUTH_ERROR_MISSING_PKCE_PARAM:()=>R,AUTH_ERROR_MISSING_REFRESH_TOKEN:()=>r,AUTH_ERROR_REFRESH_TOKEN_NETWORK_ERROR:()=>c,AUTH_ERROR_REFRESH_TOKEN_REQUEST_ERROR:()=>a});const n="AUTH_ERROR_MISSING_AUTH_INFO",r="AUTH_ERROR_MISSING_REFRESH_TOKEN",i="AUTH_ERROR_ACCESS_TOKEN_EXPIRED",s="AUTH_ERROR_LOCK_ACQUIRE_ERROR",a="AUTH_ERROR_REFRESH_TOKEN_REQUEST_ERROR",c="AUTH_ERROR_REFRESH_TOKEN_NETWORK_ERROR",l="AUTH_ERROR_ID_TOKEN_INVALID",_="AUTH_ERROR_MISSING_OTP_PARAM",R="AUTH_ERROR_MISSING_PKCE_PARAM",u="AUTH_ERROR_MISSING_NONCE_PARAM"},2183:(e,o,t)=>{t.r(o),t.d(o,{ACCESS_TOKEN_SKEW_TIME:()=>g,BACKOFF_BASE_MS:()=>v,MAX_RETRIES:()=>M,REFRESH_TOKEN_FETCH_TIMEOUT_MS:()=>K,RESPONSE_TYPE_CODE:()=>w,RESPONSE_TYPE_IMPLICIT:()=>h,clearAccessToken:()=>D,clearAuthInfo:()=>W,doLogin:()=>C,doLoginBasicLogin:()=>P,emitAccessToken:()=>b,getAccessToken:()=>$,getAuthInfo:()=>B,getAuthUrl:()=>U,getIdToken:()=>Q,getLogoutUrl:()=>N,getOAuth2ClientId:()=>J,getOAuth2Flow:()=>V,getOAuth2IDPBaseUrl:()=>X,getOAuth2Scopes:()=>Y,initLogOut:()=>Z,isIdTokenAlive:()=>ne,passwordlessLogin:()=>te,passwordlessStart:()=>oe,refreshAccessToken:()=>j,retryWithBackoff:()=>x,storeAuthInfo:()=>q,useOAuth2RefreshToken:()=>z,validateIdToken:()=>ee});var n=t(9558),r=t(5812),i=t.n(r),s=t(806),a=t.n(s);const c=require("browser-tabs-lock");var l=t.n(c);const _=require("js-cookie");var R=t.n(_),u=t(8041),d=t.n(u),E=t(9891),p=t.n(E),O=t(5097),f=t(8853),T=t(3195);let S=a();const Lock=new(l()),GET_TOKEN_SILENTLY_LOCK_KEY="openstackuicore.lock.getTokenSilently",g=60,h="token id_token",w="code",A="authInfo",y="nonce",I="pkce",k="idToken",m="BackUrl",U=(e=null,o=null,t=null,r=null,i=null,s=null,a=null,c=m)=>{let l=J(),_=(0,n.getAuthCallback)(),R=X(),u=Y(),E=V();null!=e&&(_+=`?${c}=${encodeURIComponent(e)}`);let p=H(16);(0,n.putOnLocalStorage)(y,p);let O=d()(`${R}/oauth2/auth`),f={response_type:encodeURI(E),scope:encodeURI(u),nonce:p,response_mode:"fragment",client_id:encodeURI(l),redirect_uri:encodeURI(_)};if(E===w){const e=L();(0,n.putOnLocalStorage)(I,JSON.stringify(e)),f.code_challenge=e.codeChallenge,f.code_challenge_method="S256",f.approval_prompt="force"}return o&&(f.prompt=o),u&&u.includes("offline_access")&&(f.prompt="consent"),t&&(f.id_token_hint=t),r&&(f.provider=r),s&&(f.otp_login_hint=s),i&&(f.login_hint=encodeURI(i)),a&&(f.tenant=a),O=O.query(f),O},N=(e=null)=>{let o=X(),t=J(),r=d()(`${o}/oauth2/end-session`),i=H(16),s=`${(0,n.getOrigin)()}/auth/logout`;(0,n.putOnLocalStorage)("post_logout_state",i);const a={post_logout_redirect_uri:encodeURI(s),client_id:encodeURI(t),state:i};return e&&(a.id_token_hint=e),r.query(a)},H=e=>{let o="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",t="";for(let n=0;n<e;n++)t+=o.charAt(Math.floor(62*Math.random()));return t},C=(e=null,o=null,t=null,r=null,i=null,s=null)=>{let a=U(e,t,null,o,r,i,s);(0,n.getCurrentLocation)().replace(a.toString())},P=(e=null,o=null,t=null)=>{C(e,null,null,o,t)},L=()=>{const e=(0,n.base64URLEncode)((0,f.dz)(64));return{codeVerifier:e,codeChallenge:(0,f.gl)(e,"Base64url"),createdAt:new Date}},b=async(e,o=null,t=m)=>{let r=X(),i=J(),s=(0,n.getAuthCallback)(),a=JSON.parse((0,n.getFromLocalStorage)(I,!0));if(!a)throw Error(T.AUTH_ERROR_MISSING_PKCE_PARAM);null!=o&&(s+=`?${t}=${encodeURIComponent(o)}`);const c={code:e,grant_type:"authorization_code",code_verifier:a.codeVerifier,client_id:encodeURI(i),redirect_uri:encodeURI(s)};try{const e=await fetch(`${r}/oauth2/token`,{method:"POST",headers:{Accept:"application/json","Content-Type":"application/json"},body:JSON.stringify(c)}).catch((function(e){console.log("Request failed:",e.message)})),o=await e.json();let{access_token:t,refresh_token:n,id_token:i,expires_in:s,error:a,error_description:l}=o;return{access_token:t,refresh_token:n,id_token:i,expires_in:s,error:a,error_description:l}}catch(e){console.log(e)}},M=5,v=1e3,K=1e4,x=async(e,o=M,t=v)=>{for(let n=0;n<o;n++)try{return await e()}catch(e){if(!(e.message&&e.message.startsWith(T.AUTH_ERROR_REFRESH_TOKEN_NETWORK_ERROR))||n===o-1)throw e;const r=t*Math.pow(2,n);console.log(`retryWithBackoff retry ${n+1}/${o} in ${r}ms`),await new Promise((e=>setTimeout(e,r)))}},F=async()=>{console.log("openstack-uicore-foundation::Security::methods::_getAccessToken");let e=B();if(!e)throw console.log("openstack-uicore-foundation::Security::methods::_getAccessToken AUTH_ERROR_MISSING_AUTH_INFO"),Error(T.AUTH_ERROR_MISSING_AUTH_INFO);let{accessToken:o,expiresIn:t,accessTokenUpdatedAt:n,refreshToken:r}=e,s=V();const a=i()().unix();let c=a-n;return t-=g,console.log(`openstack-uicore-foundation::Security::methods::_getAccessToken now ${a} accessTokenUpdatedAt ${n} expiresIn ${t} timeElapsedSecs ${c}`),(c>=t||null==o)&&(console.log("openstack-uicore-foundation::Security::methods::_getAccessToken access token expired, refreshing it ..."),o=await(async(e,o)=>{if(e===w&&z()){if(!o)throw W(),Error(T.AUTH_ERROR_MISSING_REFRESH_TOKEN);let e=await x((()=>j(o))),{access_token:t,expires_in:n,refresh_token:r,id_token:i}=e;return void 0===r&&(r=null),q(t,n,r,i),t}throw W(),Error(T.AUTH_ERROR_ACCESS_TOKEN_EXPIRED)})(s,r)),o},$=async()=>{if("undefined"!=typeof navigator&&navigator.locks)return await navigator.locks.request(GET_TOKEN_SILENTLY_LOCK_KEY,(async e=>(console.log("openstack-uicore-foundation::Security::methods::getAccessToken web lock api",e),await F())));if(!await(0,n.retryPromise)((()=>Lock.acquireLock(GET_TOKEN_SILENTLY_LOCK_KEY,6e3)),10))throw Error(T.AUTH_ERROR_LOCK_ACQUIRE_ERROR);try{return await F()}finally{await Lock.releaseLock(GET_TOKEN_SILENTLY_LOCK_KEY)}},G=()=>{console.log("openstack-uicore-foundation::Security::methods::_clearAccessToken");let e=B();if(!e)throw console.log("openstack-uicore-foundation::Security::methods::_clearAccessToken AUTH_ERROR_MISSING_AUTH_INFO"),Error(T.AUTH_ERROR_MISSING_AUTH_INFO);let{accessToken:o,expiresIn:t,accessTokenUpdatedAt:n,refreshToken:r}=e;q(null,0,r)},D=async()=>{if("undefined"!=typeof navigator&&navigator.locks)await navigator.locks.request(GET_TOKEN_SILENTLY_LOCK_KEY,(async e=>{console.log("openstack-uicore-foundation::Security::methods::clearAccessToken web lock api",e),G()}));else{if(!await(0,n.retryPromise)((()=>Lock.acquireLock(GET_TOKEN_SILENTLY_LOCK_KEY,6e3)),10))throw Error(T.AUTH_ERROR_LOCK_ACQUIRE_ERROR);try{G()}finally{await Lock.releaseLock(GET_TOKEN_SILENTLY_LOCK_KEY)}}},j=async e=>{let o=X(),t=J();const r={grant_type:"refresh_token",client_id:encodeURI(t),refresh_token:e},i=new AbortController,s=setTimeout((()=>i.abort()),K);let a,c;try{a=await fetch(`${o}/oauth2/token`,{method:"POST",headers:{Accept:"application/json","Content-Type":"application/json"},body:JSON.stringify(r),signal:i.signal})}catch(e){throw console.log("refreshAccessToken network error:",e.message),Error(`${T.AUTH_ERROR_REFRESH_TOKEN_NETWORK_ERROR}: ${e.message}`)}finally{clearTimeout(s)}if(!a.ok){if(console.log(`refreshAccessToken server error: ${a.status} - ${a.statusText}`),a.status>=500||408===a.status||429===a.status)throw Error(`${T.AUTH_ERROR_REFRESH_TOKEN_NETWORK_ERROR}: ${a.status} - ${a.statusText}`);throw(0,n.setSessionClearingState)(!0),Error(`${T.AUTH_ERROR_REFRESH_TOKEN_REQUEST_ERROR}: ${a.status} - ${a.statusText}`)}try{c=await a.json()}catch(e){throw Error(`${T.AUTH_ERROR_REFRESH_TOKEN_NETWORK_ERROR}: invalid JSON response from IDP`)}let{access_token:l,refresh_token:_,expires_in:R,id_token:u}=c;if(!l)throw(0,n.setSessionClearingState)(!0),Error(`${T.AUTH_ERROR_REFRESH_TOKEN_REQUEST_ERROR}: missing access_token in refresh response`);return{access_token:l,refresh_token:_,expires_in:R,id_token:u}},q=(e,o,t=null,r=null)=>{let i=B(),s={accessToken:e,expiresIn:o,accessTokenUpdatedAt:Math.floor(Date.now()/1e3)};null==t&&i&&(t=i.refreshToken),null==r&&i&&(r=i.idToken),t&&(s.refreshToken=t),r?(s[k]=r,R().set(k,r,{secure:!0,sameSite:"Lax"})):R().remove(k),(0,n.putOnLocalStorage)(A,JSON.stringify(s))},B=()=>{try{let e=(0,n.getFromLocalStorage)(A,!1);return e?JSON.parse(e):null}catch(e){return null}},W=()=>{"undefined"!=typeof window&&((0,n.removeFromLocalStorage)(A),R().remove(k))},Q=()=>{if("undefined"!=typeof window){const e=B();return e?e.idToken:null}return null},J=()=>"undefined"!=typeof window?window.OAUTH2_CLIENT_ID:null,V=()=>"undefined"!=typeof window&&window.OAUTH2_FLOW||"token id_token",z=()=>"undefined"==typeof window||new Boolean(window.OAUTH2_USE_REFRESH_TOKEN||!0),X=()=>"undefined"!=typeof window?window.IDP_BASE_URL:null,Y=()=>"undefined"!=typeof window?window.SCOPES:null,Z=()=>{(0,n.getCurrentLocation)().replace(N(Q()).toString())},ee=(e,o,t)=>{let r=new(p())({issuer:o,audience:t}),i=(0,n.getFromLocalStorage)(y,!0);if(!i)throw Error(T.AUTH_ERROR_MISSING_NONCE_PARAM);let s=r.decode(e),a=(s.header.alg,s.header.kid,s.payload.aud),c=s.payload.iss;s.payload.exp,s.payload.nbf;return(s.payload.nonce||null)==i&&a==t&&c==o},oe=e=>{let o=J(),t=Y(),r=H(16);(0,n.putOnLocalStorage)(y,r);let i=X(),s=d()(`${i}/oauth2/auth`),a={response_type:"otp",scope:encodeURI(t),nonce:r,client_id:encodeURI(o),connection:e.connection||"email",send:e.send||"code"};return e.hasOwnProperty("email")&&(a.email=encodeURIComponent(e.email)),e.hasOwnProperty("phone_number")&&(a.phone_number=encodeURIComponent(e.phone_number)),e.hasOwnProperty("redirect_uri")&&(a.redirect_uri=encodeURIComponent(e.redirect_uri)),S.post(s.toString()).send(a).then((e=>{let o=e.body;return Promise.resolve({response:o})})).catch((e=>Promise.reject(e)))},te=e=>o=>{let t=J(),n=Y(),r=X(),i=d()(`${r}/oauth2/token`);if(!e.hasOwnProperty("otp"))throw Error(T.AUTH_ERROR_MISSING_OTP_PARAM);let s={grant_type:"passwordless",connection:e.connection||"email",scope:encodeURI(n),client_id:encodeURI(t),otp:e.otp};return e.hasOwnProperty("email")&&(s.email=encodeURIComponent(e.email)),e.hasOwnProperty("phone_number")&&(s.phone_number=encodeURIComponent(e.phone_number)),S.post(i.toString()).send(s).then((e=>{try{let n=e.body,{access_token:i,expires_in:s,refresh_token:a,id_token:c}=n;if(void 0===a&&(a=null),void 0===c&&(c=null),c&&!ee(c,r,t))throw Error(T.AUTH_ERROR_ID_TOKEN_INVALID);return q(i,s,a,c),o&&o({type:O.SET_LOGGED_USER,payload:{sessionState:null}}),Promise.resolve({response:n})}catch(e){return console.log(e),Promise.reject(e)}})).catch((e=>Promise.reject(e)))},ne=(e=null)=>()=>{e||(e=Math.floor(Date.now()/1e3));const o=Q();if(!o)throw Error("Id Token not set.");const t=X(),n=J();return new(p())({issuer:t,audience:n}).decode(o).payload.exp-(e+g)>0}},9087:(e,o,t)=>{t(2462),t(1116),t(806);var n=t(8041),r=t.n(n);t(9236),t(6842),t(9558),t(5097),t(2183),t(3209);r().escapeQuerySpace=!1;const i=e=>o=>({type:e,payload:o});i("RESET_LOADING"),i("START_LOADING"),i("STOP_LOADING")},3209:(e,o,t)=>{},8853:(e,o,t)=>{t.d(o,{dz:()=>u,gl:()=>d});require("spark-md5");const n=require("crypto-js/sha256");var r=t.n(n);const i=require("crypto-js/enc-base64url");var s=t.n(i);const c=require("crypto-js/enc-hex");var l=t.n(c);const _=65536,R="undefined"!=typeof window?window.crypto||window.msCrypto:null,u=e=>{if(e>4294967295)throw new RangeError("requested too many random bytes");const o=Buffer.allocUnsafe(e);if(!R)return a;if(e>0)if(e>_)for(let t=0;t<e;t+=_)R.getRandomValues(o.slice(t,t+_));else R.getRandomValues(o);return o},d=(e,o="hex")=>{let t=l();return"Base64url"===o&&(t=s()),r()(e).toString(t)}},9558:(e,o,t)=>{t.d(o,{base64URLEncode:()=>_,getAuthCallback:()=>n,getCurrentLocation:()=>r,getFromLocalStorage:()=>a,getOrigin:()=>i,putOnLocalStorage:()=>s,removeFromLocalStorage:()=>c,retryPromise:()=>R,setSessionClearingState:()=>l});t(5812),t(8041);const n=()=>"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.origin:null,s=(e,o)=>{"undefined"!=typeof window&&window.localStorage.setItem(e,o)},a=(e,o)=>{if("undefined"!=typeof window){let t=window.localStorage.getItem(e);return o&&(console.log(`getFromLocalStorage removing key ${e}`),c(e)),t}return null},c=e=>{"undefined"!=typeof window&&window.localStorage.removeItem(e)},l=e=>{"undefined"!=typeof window&&(window.clearing_session_state=e)},_=e=>e.toString("base64").replace(/\+/g,"-").replace(/\//g,"_").replace(/=/g,""),R=async(e,o=3)=>{for(let t=0;t<o;t++)if(await e())return!0;return!1}},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 t(n){var r=o[n];if(void 0!==r)return r.exports;var i=o[n]={exports:{}};return e[n](i,i.exports,t),i.exports}return(()=>{t.n=e=>{var o=e&&e.__esModule?()=>e.default:()=>e;return t.d(o,{a:o}),o}})(),(()=>{t.d=(e,o)=>{for(var n in o)t.o(o,n)&&!t.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:o[n]})}})(),(()=>{t.o=(e,o)=>Object.prototype.hasOwnProperty.call(e,o)})(),(()=>{t.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}})(),t(2183)})()));
//# sourceMappingURL=methods.js.map