openstack-uicore-foundation
Version:
ui reactjs components for openstack marketing site
2 lines • 11.8 kB
JavaScript
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("openstack-uicore-foundation",[],t):"object"==typeof exports?exports["openstack-uicore-foundation"]=t():e["openstack-uicore-foundation"]=t()}(this,(()=>(()=>{"use strict";var e={5097:(e,t,o)=>{o.d(t,{SESSION_STATE_STATUS_CHANGED:()=>r,SESSION_STATE_STATUS_UNCHANGED:()=>n,doLogout:()=>a,onUserAuth:()=>i,updateSessionStateStatus:()=>c});o(1116),o(6842),o(9087),o(9558);var s=o(2183);const n="unchanged",r="changed",i=(e,t,o,n=0,r=null)=>i=>{(0,s.storeAuthInfo)(e,n,r,t),i({type:"SET_LOGGED_USER",payload:{sessionState:o}})},a=e=>(t,o)=>{t({type:"LOGOUT_USER",payload:{backUrl:e}})},c=e=>(t,o)=>{t({type:"UPDATE_SESSION_STATE_STATUS",payload:{sessionStateStatus:e}})}},2183:(e,t,o)=>{o.d(t,{getAuthUrl:()=>f,initLogOut:()=>E,storeAuthInfo:()=>_});var s=o(9558);o(5812),o(806);const n=require("browser-tabs-lock");var r=o.n(n);const i=require("js-cookie");var a=o.n(i),c=o(8041),l=o.n(c),d=(o(9891),o(5097),o(8853));new(r());const u="code",p="authInfo",S="nonce",h="pkce",g="idToken",f=(e=null,t=null,o=null,n=null,r=null,i=null,a=null)=>{let c=v(),d=(0,s.getAuthCallback)(),p=k(),g=U(),f=T();null!=e&&(d+=`?BackUrl=${encodeURI(e)}`);let _=w(16);(0,s.putOnLocalStorage)(S,_);let y=l()(`${p}/oauth2/auth`),O={response_type:encodeURI(f),scope:encodeURI(g),nonce:_,response_mode:"fragment",client_id:encodeURI(c),redirect_uri:encodeURI(d)};if(f===u){const e=m();(0,s.putOnLocalStorage)(h,JSON.stringify(e)),O.code_challenge=e.codeChallenge,O.code_challenge_method="S256",O.approval_prompt="force"}return t&&(O.prompt=t),g&&g.includes("offline_access")&&(O.prompt="consent"),o&&(O.id_token_hint=o),n&&(O.provider=n),i&&(O.otp_login_hint=i),r&&(O.login_hint=encodeURI(r)),a&&(O.tenant=a),y=y.query(O),y},w=e=>{let t="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",o="";for(let s=0;s<e;s++)o+=t.charAt(Math.floor(62*Math.random()));return o},m=()=>{const e=(0,s.base64URLEncode)((0,d.dz)(64));return{codeVerifier:e,codeChallenge:(0,d.gl)(e,"Base64url"),createdAt:new Date}},_=(e,t,o=null,n=null)=>{let r=y(),i={accessToken:e,expiresIn:t,accessTokenUpdatedAt:Math.floor(Date.now()/1e3)};null==o&&r&&(o=r.refreshToken),null==n&&r&&(n=r.idToken),o&&(i.refreshToken=o),n?(i[g]=n,a().set(g,n,{secure:!0,sameSite:"Lax"})):a().remove(g),(0,s.putOnLocalStorage)(p,JSON.stringify(i))},y=()=>{try{let e=(0,s.getFromLocalStorage)(p,!1);return e?JSON.parse(e):null}catch(e){return null}},O=()=>{if("undefined"!=typeof window){const e=y();return e?e.idToken:null}return null},v=()=>"undefined"!=typeof window?window.OAUTH2_CLIENT_ID:null,T=()=>"undefined"!=typeof window&&window.OAUTH2_FLOW||"token id_token",k=()=>"undefined"!=typeof window?window.IDP_BASE_URL:null,U=()=>"undefined"!=typeof window?window.SCOPES:null,E=()=>{(0,s.getCurrentLocation)().replace(((e=null)=>{let t=k(),o=v(),n=l()(`${t}/oauth2/end-session`),r=w(16),i=`${(0,s.getOrigin)()}/auth/logout`;(0,s.putOnLocalStorage)("post_logout_state",r);const a={post_logout_redirect_uri:encodeURI(i),client_id:encodeURI(o),state:r};return e&&(a.id_token_hint=e),n.query(a)})(O()).toString())}},9087:(e,t,o)=>{o(2462),o(806);var s=o(8041),n=o.n(s);o(9236),o(6842),o(9558),o(5097),o(2183);n().escapeQuerySpace=!1;const r=e=>t=>({type:e,payload:t});r("RESET_LOADING"),r("START_LOADING"),r("STOP_LOADING")},8853:(e,t,o)=>{o.d(t,{dz:()=>p,gl:()=>S});require("spark-md5");const s=require("crypto-js/sha256");var n=o.n(s);const r=require("crypto-js/enc-base64url");var i=o.n(r);const c=require("crypto-js/enc-hex");var l=o.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 t=Buffer.allocUnsafe(e);if(!u)return a;if(e>0)if(e>d)for(let o=0;o<e;o+=d)u.getRandomValues(t.slice(o,o+d));else u.getRandomValues(t);return t},S=(e,t="hex")=>{let o=l();return"Base64url"===t&&(o=i()),n()(e).toString(o)}},9558:(e,t,o)=>{o.d(t,{base64URLEncode:()=>l,getAuthCallback:()=>s,getCurrentLocation:()=>n,getFromLocalStorage:()=>a,getOrigin:()=>r,putOnLocalStorage:()=>i});o(5812),o(8041);const s=()=>"undefined"!=typeof window?`${window.location.origin}/auth/callback`:null,n=()=>{let e="";return"undefined"!=typeof window&&(e=window.location,window.top&&(e=window.top.location)),e},r=()=>"undefined"!=typeof window?window.location.origin:null,i=(e,t)=>{"undefined"!=typeof window&&window.localStorage.setItem(e,t)},a=(e,t)=>{if("undefined"!=typeof window){let o=window.localStorage.getItem(e);return t&&(console.log(`getFromLocalStorage removing key ${e}`),c(e)),o}return null},c=e=>{"undefined"!=typeof window&&window.localStorage.removeItem(e)},l=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")}},t={};function o(s){var n=t[s];if(void 0!==n)return n.exports;var r=t[s]={exports:{}};return e[s](r,r.exports,o),r.exports}(()=>{o.n=e=>{var t=e&&e.__esModule?()=>e.default:()=>e;return o.d(t,{a:t}),t}})(),(()=>{o.d=(e,t)=>{for(var s in t)o.o(t,s)&&!o.o(e,s)&&Object.defineProperty(e,s,{enumerable:!0,get:t[s]})}})(),(()=>{o.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t)})(),(()=>{o.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}})();var s={};o.r(s),o.d(s,{default:()=>m});var n=o(1116),r=o.n(n);const i=require("react");var c=o.n(i);const l=require("react-redux");var d=o(5097),u=o(2183),p=o(8041),S=o.n(p);function h(e,t){var o=Object.keys(e);if(Object.getOwnPropertySymbols){var s=Object.getOwnPropertySymbols(e);t&&(s=s.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),o.push.apply(o,s)}return o}function g(e){for(var t=1;t<arguments.length;t++){var o=null!=arguments[t]?arguments[t]:{};t%2?h(Object(o),!0).forEach((function(t){r()(e,t,o[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(o)):h(Object(o)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(o,t))}))}return e}const f=18e5;class w extends c().Component{constructor(e){super(e),this.state={opFrame_src:"",rpCheckSessionStateFrame_src:""},this.receiveMessage=this.receiveMessage.bind(this),this.receiveMessageFromChild=this.receiveMessageFromChild.bind(this),this.setTimer=this.setTimer.bind(this),this.checkSession=this.checkSession.bind(this),this.onSetupCheckSessionRP=this.onSetupCheckSessionRP.bind(this),this.rpCheckSessionStateFrameOnLoad=this.rpCheckSessionStateFrameOnLoad.bind(this),this.opFrame=null,this.rpCheckSessionStateFrame=null,this.setOPFrameRef=e=>{this.opFrame=e},this.setCheckSessionStateFrameRef=e=>{this.rpCheckSessionStateFrame=e},this.interval=null}componentWillUnmount(){try{this.interval&&(window.clearInterval(this.interval),this.interval=null),window.removeEventListener("message",this.receiveMessageFromChild,!1),window.removeEventListener("message",this.receiveMessage,!1)}catch(e){console.log(e)}}componentDidMount(){if(this.props.isLoggedUser&&"undefined"!=typeof window){if(window.location!==window.parent.location)return;this.onSetupCheckSessionRP()}}rpCheckSessionStateFrameOnLoad(e){let t=e.target;if(""==this.state.rpCheckSessionStateFrame_src)return;if(!t.contentDocument)return;window.removeEventListener("message",this.receiveMessageFromChild,!1);let o=new(S())(t.contentDocument.URL);console.log("OPSessionChecker::rpCheckSessionStateFrameOnLoad - resultUrl "+o);let s=o.search(!0),n=S().parseQuery(o.fragment()),r=null;if(s.hasOwnProperty("error")&&(r=s.error),n.hasOwnProperty("error")&&(r=n.error),r)return console.log("OPSessionChecker::rpCheckSessionStateFrameOnLoad - error "+r),void(0,u.initLogOut)();this.props.updateSessionStateStatus(d.SESSION_STATE_STATUS_UNCHANGED)}componentDidUpdate(e){if(this.props.sessionStateStatus!==e.sessionStateStatus){if(this.props.sessionStateStatus===d.SESSION_STATE_STATUS_CHANGED){let e=(0,u.getAuthUrl)(null,"none");return void(""==this.state.rpCheckSessionStateFrame_src&&this.setState(g(g({},this.state),{},{opFrame_src:"",rpCheckSessionStateFrame_src:e.toString()})))}if(this.props.sessionStateStatus===d.SESSION_STATE_STATUS_UNCHANGED)return void this.onSetupCheckSessionRP()}this.props.sessionState!==e.sessionState&&(console.log(`OPSessionChecker::componentDidUpdate updated session state ${this.props.sessionState}`),this.onSetupCheckSessionRP())}checkSession(){let e=new Date;if(console.log(`OPSessionChecker::checkSession now ${e.toLocaleString()}`),null==this.opFrame)return;if(null==this.props.sessionState)return;if(null==this.props.clientId)return;let t=this.props.idpBaseUrl,o=this.opFrame.contentWindow,s=this.props.clientId+" "+this.props.sessionState;o.postMessage(s,t)}setTimer(e){e.target;""!=this.state.opFrame_src&&this.props.isLoggedUser&&this.props.sessionStateStatus===d.SESSION_STATE_STATUS_UNCHANGED&&"undefined"!=typeof window&&(console.log("OPSessionChecker::setTimer setting interval 1800000"),this.interval=window.setInterval(this.checkSession,f))}onSetupCheckSessionRP(){if(null==this.opFrame)return;window.addEventListener("message",this.receiveMessage,!1);const e=`${this.props.idpBaseUrl}/oauth2/check-session`;""==this.state.opFrame_src&&this.setState(g(g({},this.state),{},{opFrame_src:e,rpCheckSessionStateFrame_src:""}))}receiveMessageFromChild(e){if("string"==typeof e.data||e.data instanceof String)try{let t=JSON.parse(e.data);t.hasOwnProperty("action")&&"SET_AUTH_INFO_SILENTLY"==t.action&&(window.removeEventListener("message",this.receiveMessageFromChild,!1),this.props.onUserAuth(t.access_token,t.id_token,t.session_state,t.expires_in,t.hasOwnProperty("refresh_token")?t.refresh_token:null))}catch(e){console.log(e)}}receiveMessage(e){if(e.origin!==this.props.idpBaseUrl)return;let t=e.data;return this.props.sessionStateStatus===d.SESSION_STATE_STATUS_UNCHANGED&&this.props.isLoggedUser&&t===d.SESSION_STATE_STATUS_CHANGED?(console.log("OPSessionChecker::receiveMessage - session state has changed on OP"),"undefined"!=typeof window&&window.clearInterval(this.interval),this.interval=null,window.addEventListener("message",this.receiveMessageFromChild,!1),window.removeEventListener("message",this.receiveMessage,!1),void this.props.updateSessionStateStatus(d.SESSION_STATE_STATUS_CHANGED)):void 0}render(){return this.props.isLoggedUser&&this.props.sessionState?"undefined"!=typeof window&&window.location!==window.parent.location?null:c().createElement("div",{style:{height:"0px"}},c().createElement("iframe",{ref:this.setOPFrameRef,id:"OPFrame",onLoad:this.setTimer,src:this.state.opFrame_src,style:{visibility:"hidden",position:"absolute",left:0,top:0,border:"none",width:0,height:0}},">"),c().createElement("iframe",{ref:this.setCheckSessionStateFrameRef,id:"RPCHeckSessionStateFrame",src:this.state.rpCheckSessionStateFrame_src,onLoad:this.rpCheckSessionStateFrameOnLoad,style:{visibility:"hidden",position:"absolute",left:0,top:0,border:"none",width:0,height:0}},">")):null}}const m=(0,l.connect)((({loggedUserState:e})=>({sessionState:e.sessionState,isLoggedUser:e.isLoggedUser,sessionStateStatus:e.sessionStateStatus})),{doLogout:d.doLogout,updateSessionStateStatus:d.updateSessionStateStatus,onUserAuth:d.onUserAuth})(w);return s})()));
//# sourceMappingURL=session-checker.js.map