@machinemetrics/mm-react-embeddable 2.0.1-2 → 2.0.2-0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
|
@@ -25,3 +25,7 @@ the user's click on that row).
|
|
|
25
25
|
|
|
26
26
|
A host with no `credentialBroker`, or one whose broker returns an answer it may not act on, gets
|
|
27
27
|
an interactive entry, and the host's UI opens the popup for it.
|
|
28
|
+
|
|
29
|
+
A frame that already received a credential and asks again is treated as holding a dead token: the
|
|
30
|
+
origin's cached credential is dropped and the broker check runs again, at most once per origin every
|
|
31
|
+
30 seconds. When `ready` goes false the cache is emptied.
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("react"),ge=require("prop-types"),me=require("styled-components"),V=r=>r&&r.__esModule?r:{default:r},O=V(e),k=V(ge),te=V(me),Q=e.createContext(null),Y=e.createContext({pending:[],authorize:()=>{},approve:()=>{},complete:()=>{},dismiss:()=>{},originEpochs:{},credentials:{},unsupported:{}}),oe="access_token",J=r=>r?r.kind?r:r.accessToken?{kind:oe,accessToken:r.accessToken,expiresAt:r.expiresAt||null}:null:null,Ee=60,Re=120,ke=new Set(["host_not_allowed","unbound_host_bound_guest","invalid_token","provider_unavailable"]),G=r=>{try{return new URL(r).origin}catch{return null}},re=(r,o)=>{const i=o==null?void 0:o.origin;return!i||G(r==null?void 0:r.authUrl)!==i?null:i},ne=r=>{if(!r||/^\d{1,3}(?:\.\d{1,3}){3}$/.test(r)||r.includes(":"))return r;const o=r.split(".");return o.length<=2?r:o.slice(-2).join(".")},N=(r,o)=>{try{const i=new URL(r),d=new URL(o);return i.protocol!==d.protocol?!0:ne(i.hostname)!==ne(d.hostname)}catch{return!0}},ue=(r,{onBlocked:o,onClosed:i})=>{const d=window.open(r.authUrl,"mm-embed-auth","popup,width=600,height=700");if(!d)return o(),()=>{};const R=setInterval(()=>{d.closed&&(clearInterval(R),i())},1e3);return()=>clearInterval(R)},ce=({children:r,launcher:o,broker:i})=>{const d=e.useContext(Q),[R,C]=e.useState({}),[$,q]=e.useState({}),[D,W]=e.useState({}),[I,E]=e.useState({}),S=e.useRef(D);S.current=D;const B=e.useRef(R);B.current=R;const w=e.useRef(null),x=e.useRef({}),y=e.useRef({}),P=e.useRef(i);P.current=i;const U=e.useCallback(t=>{const s=x.current[t];if(!s)return;const n=c=>!c[t]||c[t].clientId?c:{...c,[t]:{...c[t],clientId:s}};C(n),E(n)},[]),F=e.useCallback((t,s)=>{t&&(E(n=>n[t]?n:{...n,[t]:{origin:t,reason:s,clientId:x.current[t]||null}}),U(t))},[U]),A=e.useCallback((t,{v2:s=!1,origin:n=null}={})=>{n&&(s&&S.current[n]||(C(c=>c[n]?c:{...c,[n]:{origin:n,authUrl:t.authUrl,status:s&&P.current?"broker":"interactive",v2:s,scope:t.scope||null,acceptsCredentials:y.current[n]||null,consent:null,error:null,clientId:t.clientId||x.current[n]||null,popupBlocked:!1}}),U(n)))},[U]),T=e.useRef({}),v=e.useCallback(t=>{T.current[t]&&(clearTimeout(T.current[t]),delete T.current[t])},[]),u=e.useCallback(t=>{v(t),C(s=>{if(!s[t])return s;const n={...s};return delete n[t],n})},[v]),l=e.useCallback(t=>{t&&(u(t),q(s=>({...s,[t]:(s[t]||0)+1})))},[u]),a=e.useCallback(t=>{u(t.origin)},[u]),p=e.useCallback((t,s)=>{const n=re(t,s);if(n){if(N(n,window.location.origin)){F(n,"cross-site-legacy");return}A(t,{origin:n})}},[A,F]);e.useEffect(()=>{if(!d)return;const t=[d.addListener("*","init",(s,n)=>{n!=null&&n.origin&&(s!=null&&s.clientId)&&(x.current[n.origin]=s.clientId,U(n.origin)),n!=null&&n.origin&&Array.isArray(s==null?void 0:s.acceptsCredentials)?y.current[n.origin]=s.acceptsCredentials:n!=null&&n.origin&&N(n.origin,window.location.origin)&&F(n.origin,"cross-site-legacy")}),d.addListener("*","authorize",p),d.addListener("*","login",p),d.addListener("*","credential-request",(s,n)=>A(s,{v2:!0,origin:re(s,n)}))];return()=>t.forEach(s=>s())},[d,p,A,U,F]),e.useEffect(()=>{const t=s=>{const n=s==null?void 0:s.data;if(!n||n.type!=="authorized")return;const c=G(n.authUrl);if(!c||s.origin!==c)return;const b=B.current[c];if(!(!b||b.status!=="interactive")){if(n.accessToken){W(L=>({...L,[c]:J({accessToken:n.accessToken})})),u(c);return}l(c)}};return window.addEventListener("message",t),()=>window.removeEventListener("message",t)},[u,l]);const h=e.useCallback((t,s)=>{const n=t==null?void 0:t.origin,c=J(s);!n||!c||(W(b=>({...b,[n]:c})),u(n))},[u]),g=e.useCallback((t,s)=>{C(n=>n[t]?{...n,[t]:{...n[t],...s}}:n)},[]),m=e.useRef({}),_=e.useCallback(async(t,{approve:s}={})=>{const n=P.current,{origin:c}=t;let b;try{b=await n({origin:c,clientId:t.clientId,scope:t.scope,authUrl:t.authUrl},{approve:s})}catch{s?g(c,{error:{reason:"broker_unavailable"}}):g(c,{status:"interactive"});return}const L=b==null?void 0:b.status,f=(b==null?void 0:b.body)||{};if(L===200&&f.credential){h(t,f.credential);return}if(L===409&&f.consentRequired){g(c,{status:"interactive",consent:{client:f.client||null,stepUp:!!f.stepUp,scope:f.scope||t.scope},error:null});return}const z=f.error||"broker_error";if(!s&&ke.has(z)){g(c,{status:"interactive"});return}if(s&&z==="consent_denied"){g(c,{error:null});return}if(z==="rate_limited"&&!t.rateLimitRetried){const X=Math.min(Number(f.retryAfter)>0?Number(f.retryAfter):Ee,Re),he={...t,rateLimitRetried:!0};v(c),T.current[c]=setTimeout(()=>{delete T.current[c],m.current[c]=!0,M.current(he,{approve:s}).finally(()=>{delete m.current[c]})},X*1e3),g(c,{rateLimitRetried:!0});return}u(c),E(X=>({...X,[c]:{origin:c,reason:`broker:${z}`,detail:{status:L!=null?L:null,reason:z},clientId:t.clientId||x.current[c]||null}}))},[h,g,u,v]),M=e.useRef(_);M.current=_,e.useEffect(()=>{Object.values(R).forEach(t=>{t.status!=="broker"||m.current[t.origin]||T.current[t.origin]||(m.current[t.origin]=!0,_(t).finally(()=>{delete m.current[t.origin]}))})},[R,_]);const K=e.useCallback(t=>!P.current||!(t!=null&&t.consent)||m.current[t.origin]?Promise.resolve():(m.current[t.origin]=!0,_(t,{approve:!0}).finally(()=>{delete m.current[t.origin]})),[_]);e.useEffect(()=>()=>{w.current&&w.current(),Object.values(T.current).forEach(clearTimeout)},[]);const Z=e.useCallback(t=>{w.current&&(w.current(),w.current=null);const s=o||ue;w.current=s(t,{onBlocked:()=>{C(n=>n[t.origin]?{...n,[t.origin]:{...n[t.origin],popupBlocked:!0}}:n)},onClosed:()=>{const n=B.current[t.origin];!n||n.v2||l(t.origin)}})},[o,l]),j={pending:Object.values(R).filter(t=>t.status==="interactive"),authorize:Z,approve:K,complete:h,dismiss:a,originEpochs:$,credentials:D,unsupported:I};return O.default.createElement(Y.Provider,{value:j},r)};ce.propTypes={children:k.default.node,launcher:k.default.func,broker:k.default.func};const Ce=()=>e.useContext(Y),ie=r=>{const{originEpochs:o}=e.useContext(Y),i=G(r);return i&&o[i]||0},ae=r=>{const{credentials:o}=e.useContext(Y),i=G(r);return i&&o[i]||null},le=r=>{const{unsupported:o}=e.useContext(Y),i=G(r);return i&&o[i]||null},se="MMEmbeddableZoneProvider requires a boolean `ready` prop (true once the host holds the bearer its credentialBroker needs). Embedded apps will not load until it is provided.",de=({children:r,authorizationLauncher:o,credentialBroker:i,ready:d})=>{const R=d===!0,C=e.useRef(!1);e.useEffect(()=>{d!==void 0||C.current||(C.current=!0,console.error(se),typeof window!="undefined"&&typeof window.alert=="function"&&window.alert(se))},[d]);const[$,q]=e.useState({}),[D,W]=e.useState({}),I=e.useRef({}),E=e.useRef({}),S=e.useRef({}),B=e.useCallback((u,l)=>{var a;return!!((a=S.current[u])!=null&&a.has(l))},[]),w=e.useCallback((u,l,a)=>{q(p=>({...p,[u]:{origin:l,frameRef:a}}))},[]),x=e.useCallback(u=>{q(l=>{const a={...l};return delete a[u],a}),W(l=>{const a={...l};return delete a[u],a}),delete S.current[u],Object.keys(E.current).forEach(l=>{E.current[l].instanceId===u&&delete E.current[l]})},[]);e.useEffect(()=>()=>{E.current={}},[]),e.useEffect(()=>{const u=(a=[],p,h)=>{a.forEach(g=>g(p,h))},l=a=>{var K,Z,j,t,s,n,c;if(!((K=a==null?void 0:a.data)!=null&&K.instanceId))return;const{instanceId:p,type:h}=a.data,g=$[p],m=(j=(Z=g==null?void 0:g.frameRef)==null?void 0:Z.current)==null?void 0:j.contentWindow;if(!m||a.source!==m||a.origin!==g.origin)return;const _={origin:a.origin},M=I.current;if(h==="response"){const{callbackId:b,response:L,error:f}=a.data,z=E.current[b];z&&z.instanceId===p&&(z.callback(L,f),delete E.current[b]);return}if(h==="mark-registered"){const{requestType:b}=a.data;S.current[p]||(S.current[p]=new Set),S.current[p].add(b);return}h==="ready"&&W(b=>({...b,[p]:!0})),u((t=M[p])==null?void 0:t[h],a.data,_),u((s=M["*"])==null?void 0:s[h],a.data,_),u((n=M[p])==null?void 0:n["*"],a.data,_),u((c=M["*"])==null?void 0:c["*"],a.data,_)};return window.addEventListener("message",l),()=>{window.removeEventListener("message",l)}},[$]);const y=e.useCallback((u,l,a,p)=>{var g,m;const h=$[u];if(!h){console.warn(`Origin not set for id: ${u}. Unable to send message.`);return}(m=(g=h==null?void 0:h.frameRef)==null?void 0:g.current)!=null&&m.contentWindow&&h.frameRef.current.contentWindow.postMessage({type:l,...a,instanceId:u},p||h.origin)},[$]),P=e.useRef(0),U=e.useCallback(()=>`${Date.now()}-${++P.current}`,[]),F=e.useCallback(async(u,l,a={})=>new Promise((p,h)=>{const g=U(),m=(_,M)=>{delete E.current[g],M?h(M):p(_)};E.current[g]={instanceId:u,callback:m},y(u,"request",{callbackId:g,requestType:l,data:a})}),[y]),A=e.useCallback((u,l)=>{y(u,"load-options",l)},[y]),T=e.useCallback((u,l)=>{y(u,"validate",l)},[y]),v=e.useMemo(()=>({registerFrame:w,unregisterFrame:x,sendMessage:y,sendRequest:F,loadOptions:A,validate:T,hasHandler:B,readyFrames:D,ready:R,addListener:(u,l,a)=>(I.current[u]||(I.current[u]={}),I.current[u][l]||(I.current[u][l]=[]),I.current[u][l].push(a),()=>{I.current[u][l]=I.current[u][l].filter(p=>p!==a)})}),[w,x,y,F,A,T,B,D,R]);return O.default.createElement(Q.Provider,{value:v},O.default.createElement(ce,{launcher:o,broker:i},r))},H=(r,o,i)=>{const d=e.useContext(Q);if(d===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const R=e.useRef(i);e.useEffect(()=>{R.current=i},[i]),e.useEffect(()=>{if(!(!r||!o)){if(o.startsWith("key:")){const C=o.substring(4);return d.readyFrames[r]&&d.sendMessage(r,"subscribe-key",{keyChord:C}),d.addListener(r,`key-pressed:${C}`,(...q)=>R.current(...q))}return d.addListener(r,o,(...C)=>R.current(...C))}},[d,r,o])};de.propTypes={children:k.default.node.isRequired,ready:k.default.bool.isRequired,authorizationLauncher:k.default.func,credentialBroker:k.default.func};const ee=te.default.default||te.default,we=1e4,Te="credentials",_e=(r,o)=>`${r}/mm-embedded-interop?id=${encodeURIComponent(o)}&origin=${encodeURIComponent(window.location.origin)}&caps=${Te}`,Ie=ee.iframe`
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("react"),Ce=require("prop-types"),we=require("styled-components"),ne=r=>r&&r.__esModule?r:{default:r},F=ne(t),g=ne(Ce),ce=ne(we),J=t.createContext(null),Q=t.createContext({pending:[],authorize:()=>{},approve:()=>{},complete:()=>{},dismiss:()=>{},originEpochs:{},credentials:{},unsupported:{},setDelivered:()=>{}}),ae="access_token",te=r=>r?r.kind?r:r.accessToken?{kind:ae,accessToken:r.accessToken,expiresAt:r.expiresAt||null}:null:null,Te=60,_e=120,Ie=3e4,Me=new Set(["host_not_allowed","unbound_host_bound_guest","invalid_token","provider_unavailable"]),V=r=>{try{return new URL(r).origin}catch{return null}},ue=(r,u)=>{const a=u==null?void 0:u.origin;return!a||V(r==null?void 0:r.authUrl)!==a?null:a},oe=r=>{if(!r||/^\d{1,3}(?:\.\d{1,3}){3}$/.test(r)||r.includes(":"))return r;const u=r.split(".");return u.length<=2?r:u.slice(-2).join(".")},re=(r,u)=>{try{const a=new URL(r),p=new URL(u);return a.protocol!==p.protocol?!0:oe(a.hostname)!==oe(p.hostname)}catch{return!0}},le=(r,{onBlocked:u,onClosed:a})=>{const p=window.open(r.authUrl,"mm-embed-auth","popup,width=600,height=700");if(!p)return u(),()=>{};const h=setInterval(()=>{p.closed&&(clearInterval(h),a())},1e3);return()=>clearInterval(h)},de=({children:r,launcher:u,broker:a,ready:p})=>{const h=t.useContext(J),[R,T]=t.useState({}),[q,Y]=t.useState({}),[W,k]=t.useState({}),[b,I]=t.useState({}),z=t.useRef(W);z.current=W;const _=t.useRef(R);_.current=R;const x=t.useRef(null),C=t.useRef({}),B=t.useRef({}),Z=t.useRef(a);Z.current=a;const L=t.useRef({}),O=t.useRef({}),v=t.useCallback((e,s,n)=>{!e||!s||(L.current[e]||(L.current[e]=new Set),n?L.current[e].add(s):L.current[e].delete(s))},[]),U=t.useCallback(e=>{const s=C.current[e];if(!s)return;const n=c=>!c[e]||c[e].clientId?c:{...c,[e]:{...c[e],clientId:s}};T(n),I(n)},[]),i=t.useCallback((e,s)=>{e&&(I(n=>n[e]?n:{...n,[e]:{origin:e,reason:s,clientId:C.current[e]||null}}),U(e))},[U]),l=t.useCallback((e,{v2:s=!1,origin:n=null}={})=>{var c;if(n){if(s&&z.current[n]){if(!((c=L.current[n])!=null&&c.has(e.instanceId)))return;const m=Date.now();if(m-(O.current[n]||0)<Ie)return;O.current[n]=m,L.current[n].clear();const D={...z.current};delete D[n],z.current=D,k(D)}T(m=>m[n]?m:{...m,[n]:{origin:n,authUrl:e.authUrl,status:s&&Z.current?"broker":"interactive",v2:s,scope:e.scope||null,acceptsCredentials:B.current[n]||null,consent:null,error:null,clientId:e.clientId||C.current[n]||null,popupBlocked:!1}}),U(n)}},[U]),o=t.useRef({}),f=t.useCallback(e=>{o.current[e]&&(clearTimeout(o.current[e]),delete o.current[e])},[]),d=t.useCallback(e=>{f(e),T(s=>{if(!s[e])return s;const n={...s};return delete n[e],n})},[f]),E=t.useCallback(e=>{e&&(d(e),Y(s=>({...s,[e]:(s[e]||0)+1})))},[d]),$=t.useCallback(e=>{d(e.origin)},[d]),w=t.useCallback((e,s)=>{const n=ue(e,s);if(n){if(re(n,window.location.origin)){i(n,"cross-site-legacy");return}l(e,{origin:n})}},[l,i]);t.useEffect(()=>{if(!h)return;const e=[h.addListener("*","init",(s,n)=>{n!=null&&n.origin&&(s!=null&&s.clientId)&&(C.current[n.origin]=s.clientId,U(n.origin)),n!=null&&n.origin&&Array.isArray(s==null?void 0:s.acceptsCredentials)?B.current[n.origin]=s.acceptsCredentials:n!=null&&n.origin&&re(n.origin,window.location.origin)&&i(n.origin,"cross-site-legacy")}),h.addListener("*","authorize",w),h.addListener("*","login",w),h.addListener("*","credential-request",(s,n)=>l(s,{v2:!0,origin:ue(s,n)}))];return()=>e.forEach(s=>s())},[h,w,l,U,i]),t.useEffect(()=>{const e=s=>{const n=s==null?void 0:s.data;if(!n||n.type!=="authorized")return;const c=V(n.authUrl);if(!c||s.origin!==c)return;const m=_.current[c];if(!(!m||m.status!=="interactive")){if(n.accessToken){k(D=>({...D,[c]:te({accessToken:n.accessToken})})),d(c);return}E(c)}};return window.addEventListener("message",e),()=>window.removeEventListener("message",e)},[d,E]);const M=t.useCallback((e,s)=>{const n=e==null?void 0:e.origin,c=te(s);!n||!c||(k(m=>({...m,[n]:c})),d(n))},[d]),A=t.useCallback((e,s)=>{T(n=>n[e]?{...n,[e]:{...n[e],...s}}:n)},[]),S=t.useRef({}),y=t.useCallback(async(e,{approve:s}={})=>{const n=Z.current,{origin:c}=e;let m;try{m=await n({origin:c,clientId:e.clientId,scope:e.scope,authUrl:e.authUrl},{approve:s})}catch{s?A(c,{error:{reason:"broker_unavailable"}}):A(c,{status:"interactive"});return}const D=m==null?void 0:m.status,H=(m==null?void 0:m.body)||{};if(D===200&&H.credential){M(e,H.credential);return}if(D===409&&H.consentRequired){A(c,{status:"interactive",consent:{client:H.client||null,stepUp:!!H.stepUp,scope:H.scope||e.scope},error:null});return}const X=H.error||"broker_error";if(!s&&Me.has(X)){A(c,{status:"interactive"});return}if(s&&X==="consent_denied"){A(c,{error:null});return}if(X==="rate_limited"&&!e.rateLimitRetried){const ee=Math.min(Number(H.retryAfter)>0?Number(H.retryAfter):Te,_e),ke={...e,rateLimitRetried:!0};f(c),o.current[c]=setTimeout(()=>{delete o.current[c],S.current[c]=!0,j.current(ke,{approve:s}).finally(()=>{delete S.current[c]})},ee*1e3),A(c,{rateLimitRetried:!0});return}d(c),I(ee=>({...ee,[c]:{origin:c,reason:`broker:${X}`,detail:{status:D!=null?D:null,reason:X},clientId:e.clientId||C.current[c]||null}}))},[M,A,d,f]),j=t.useRef(y);j.current=y,t.useEffect(()=>{Object.values(R).forEach(e=>{e.status!=="broker"||S.current[e.origin]||o.current[e.origin]||(S.current[e.origin]=!0,y(e).finally(()=>{delete S.current[e.origin]}))})},[R,y]);const G=t.useCallback(e=>!Z.current||!(e!=null&&e.consent)||S.current[e.origin]?Promise.resolve():(S.current[e.origin]=!0,y(e,{approve:!0}).finally(()=>{delete S.current[e.origin]})),[y]);t.useEffect(()=>()=>{x.current&&x.current(),Object.values(o.current).forEach(clearTimeout)},[]),t.useEffect(()=>{p===!1&&(Object.values(o.current).forEach(clearTimeout),o.current={},S.current={},C.current={},B.current={},L.current={},O.current={},z.current={},k({}),T({}),I({}))},[p]);const K=t.useCallback(e=>{x.current&&(x.current(),x.current=null);const s=u||le;x.current=s(e,{onBlocked:()=>{T(n=>n[e.origin]?{...n,[e.origin]:{...n[e.origin],popupBlocked:!0}}:n)},onClosed:()=>{const n=_.current[e.origin];!n||n.v2||E(e.origin)}})},[u,E]),P={pending:Object.values(R).filter(e=>e.status==="interactive"),authorize:K,approve:G,complete:M,dismiss:$,originEpochs:q,credentials:W,unsupported:b,setDelivered:v};return F.default.createElement(Q.Provider,{value:P},r)};de.propTypes={children:g.default.node,launcher:g.default.func,ready:g.default.bool,broker:g.default.func};const fe=()=>t.useContext(Q),pe=r=>{const{originEpochs:u}=t.useContext(Q),a=V(r);return a&&u[a]||0},be=r=>{const{credentials:u}=t.useContext(Q),a=V(r);return a&&u[a]||null},he=r=>{const{unsupported:u}=t.useContext(Q),a=V(r);return a&&u[a]||null},ie="MMEmbeddableZoneProvider requires a boolean `ready` prop (true once the host holds the bearer its credentialBroker needs). Embedded apps will not load until it is provided.",Ee=({children:r,authorizationLauncher:u,credentialBroker:a,ready:p})=>{const h=p===!0,R=t.useRef(!1);t.useEffect(()=>{p!==void 0||R.current||(R.current=!0,console.error(ie),typeof window!="undefined"&&typeof window.alert=="function"&&window.alert(ie))},[p]);const[T,q]=t.useState({}),[Y,W]=t.useState({}),k=t.useRef({}),b=t.useRef({}),I=t.useRef({}),z=t.useCallback((i,l)=>{var o;return!!((o=I.current[i])!=null&&o.has(l))},[]),_=t.useCallback((i,l,o)=>{q(f=>({...f,[i]:{origin:l,frameRef:o}}))},[]),x=t.useCallback(i=>{q(l=>{const o={...l};return delete o[i],o}),W(l=>{const o={...l};return delete o[i],o}),delete I.current[i],Object.keys(b.current).forEach(l=>{b.current[l].instanceId===i&&delete b.current[l]})},[]);t.useEffect(()=>()=>{b.current={}},[]),t.useEffect(()=>{const i=(o=[],f,d)=>{o.forEach(E=>E(f,d))},l=o=>{var A,S,y,j,G,K,P;if(!((A=o==null?void 0:o.data)!=null&&A.instanceId))return;const{instanceId:f,type:d}=o.data,E=T[f],$=(y=(S=E==null?void 0:E.frameRef)==null?void 0:S.current)==null?void 0:y.contentWindow;if(!$||o.source!==$||o.origin!==E.origin)return;const w={origin:o.origin},M=k.current;if(d==="response"){const{callbackId:e,response:s,error:n}=o.data,c=b.current[e];c&&c.instanceId===f&&(c.callback(s,n),delete b.current[e]);return}if(d==="mark-registered"){const{requestType:e}=o.data;I.current[f]||(I.current[f]=new Set),I.current[f].add(e);return}d==="ready"&&W(e=>({...e,[f]:!0})),i((j=M[f])==null?void 0:j[d],o.data,w),i((G=M["*"])==null?void 0:G[d],o.data,w),i((K=M[f])==null?void 0:K["*"],o.data,w),i((P=M["*"])==null?void 0:P["*"],o.data,w)};return window.addEventListener("message",l),()=>{window.removeEventListener("message",l)}},[T]);const C=t.useCallback((i,l,o,f)=>{var E,$;const d=T[i];if(!d){console.warn(`Origin not set for id: ${i}. Unable to send message.`);return}($=(E=d==null?void 0:d.frameRef)==null?void 0:E.current)!=null&&$.contentWindow&&d.frameRef.current.contentWindow.postMessage({type:l,...o,instanceId:i},f||d.origin)},[T]),B=t.useRef(0),Z=t.useCallback(()=>`${Date.now()}-${++B.current}`,[]),L=t.useCallback(async(i,l,o={})=>new Promise((f,d)=>{const E=Z(),$=(w,M)=>{delete b.current[E],M?d(M):f(w)};b.current[E]={instanceId:i,callback:$},C(i,"request",{callbackId:E,requestType:l,data:o})}),[C]),O=t.useCallback((i,l)=>{C(i,"load-options",l)},[C]),v=t.useCallback((i,l)=>{C(i,"validate",l)},[C]),U=t.useMemo(()=>({registerFrame:_,unregisterFrame:x,sendMessage:C,sendRequest:L,loadOptions:O,validate:v,hasHandler:z,readyFrames:Y,ready:h,addListener:(i,l,o)=>(k.current[i]||(k.current[i]={}),k.current[i][l]||(k.current[i][l]=[]),k.current[i][l].push(o),()=>{k.current[i][l]=k.current[i][l].filter(f=>f!==o)})}),[_,x,C,L,O,v,z,Y,h]);return F.default.createElement(J.Provider,{value:U},F.default.createElement(de,{launcher:u,broker:a,ready:h},r))},N=(r,u,a)=>{const p=t.useContext(J);if(p===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const h=t.useRef(a);t.useEffect(()=>{h.current=a},[a]),t.useEffect(()=>{if(!(!r||!u)){if(u.startsWith("key:")){const R=u.substring(4);return p.readyFrames[r]&&p.sendMessage(r,"subscribe-key",{keyChord:R}),p.addListener(r,`key-pressed:${R}`,(...q)=>h.current(...q))}return p.addListener(r,u,(...R)=>h.current(...R))}},[p,r,u])};Ee.propTypes={children:g.default.node.isRequired,ready:g.default.bool.isRequired,authorizationLauncher:g.default.func,credentialBroker:g.default.func};const se=ce.default.default||ce.default,Ae=1e4,Se="credentials",ye=(r,u)=>`${r}/mm-embedded-interop?id=${encodeURIComponent(u)}&origin=${encodeURIComponent(window.location.origin)}&caps=${Se}`,Le=se.iframe`
|
|
2
2
|
width: 100%;
|
|
3
3
|
height: 100%;
|
|
4
4
|
border: none;
|
|
5
5
|
visibility: ${r=>r.$isReady?"visible":"hidden"};
|
|
6
|
-
`,
|
|
6
|
+
`,xe=se.div`
|
|
7
7
|
width: 100%;
|
|
8
8
|
height: 100%;
|
|
9
9
|
display: flex;
|
|
@@ -16,10 +16,10 @@
|
|
|
16
16
|
text-align: center;
|
|
17
17
|
color: #555;
|
|
18
18
|
font-size: 14px;
|
|
19
|
-
`,
|
|
19
|
+
`,me=r=>{const u=r&&r.origin||"This app",a=r&&r.reason||"";switch(a){case"broker:scope_above_ceiling":return{title:`${u} can’t be authorized here`,detail:"It asks for permissions your role doesn’t have."};case"broker:origin_mismatch":case"broker:unknown_client":return{title:`${u} can’t be authorized here`,detail:"It isn’t registered to run at this address."};case"broker:cross_company_principal":return{title:`${u} can’t be authorized here`,detail:"It can’t be authorized for this account."};default:return a.startsWith("broker:")?{title:`${u} can’t be authorized here`,detail:"Authorization was refused."}:{title:`${u} can’t be embedded here`,detail:"It needs an update to support embedding from a different domain."}}},ge=({entry:r})=>{const u=me(r);return F.default.createElement(xe,{"data-reason":r&&r.reason||void 0},F.default.createElement("div",{style:{fontWeight:600}},u.title),F.default.createElement("div",{style:{fontSize:12,color:"#888"}},u.detail))};ge.propTypes={entry:g.default.shape({origin:g.default.string,name:g.default.string,reason:g.default.string,detail:g.default.object})};const Ue=se.div`
|
|
20
20
|
height: 100%;
|
|
21
21
|
display: flex;
|
|
22
22
|
flex: 1;
|
|
23
23
|
align-items: center;
|
|
24
24
|
justify-content: center;
|
|
25
|
-
`,
|
|
25
|
+
`,ze=()=>F.default.createElement(Ue,null,F.default.createElement("div",{style:{textAlign:"center"}},"This app couldn’t be loaded as an embeddable widget.")),Re=({id:r,src:u,path:a,onInit:p=()=>{},onTimeout:h,context:R={},params:T={},renderUnsupported:q,renderTimeout:Y,...W})=>{if("isConfiguring"in W)return console.error("isConfiguring must be passed via the params prop to MMEmbeddableZone"),null;if("colorMode"in R)return console.error("colorMode must be passed via the params prop to MMEmbeddableZone"),null;if("isFullScreen"in R)return console.error("isFullScreen must be passed via the params prop to MMEmbeddableZone"),null;const k=t.useRef(),b=V(u),[I,z]=t.useState(),[_,x]=t.useState(0),[C,B]=t.useState(!1),[Z,L]=t.useState(!1),O=pe(u),v=be(u),U=he(u),{setDelivered:i}=fe(),[l,o]=t.useState(0),[f,d]=t.useState(null),{registerFrame:E,unregisterFrame:$,sendMessage:w,ready:M}=t.useContext(J),[A,S]=t.useState(),y=t.useRef(),j=t.useRef(h),G=t.useRef(p),K=t.useRef(!1),P=t.useRef(),e=t.useCallback(()=>{K.current=!1,clearTimeout(y.current),clearTimeout(P.current),x(0),B(!1),L(!1),d(null),i(b,r,!1)},[i,b,r]);t.useEffect(()=>{(O>0||l>0)&&e()},[O,l,e]),t.useEffect(()=>{j.current=h},[h]),t.useEffect(()=>{G.current=p},[p]),t.useEffect(()=>{_&&w(r,"set-context",{context:{...R}},b)},[R,r,_,b]),t.useEffect(()=>{_&&w(r,"set-params",{params:T},b)},[T,r,_,b]),t.useEffect(()=>{a!==void 0&&S((a==null?void 0:a.indexOf("/"))===0?a:`/${a}`)},[a]),t.useEffect(()=>{A===void 0||!_||w(r,"navigate",{path:A},b)},[A,_,r,w,b]),t.useEffect(()=>{if(!u||!r||!k)return;if(!M){e(),z(void 0);return}e();const c=new URL(u);return E(r,c.origin,k),G.current(),z(ye(c.origin,r)),()=>{$(r)}},[u,r,M,E,k,e]),t.useEffect(()=>{if(!(!I||K.current))return y.current=setTimeout(()=>{B(!0),j.current&&j.current()},Ae),()=>clearTimeout(y.current)},[I,u,O,l]);const s=t.useCallback(()=>{K.current=!0,clearTimeout(y.current),B(!1)},[]);if(N(r,"init",c=>{s(),Array.isArray(c==null?void 0:c.acceptsCredentials)&&d(c.acceptsCredentials)}),N(r,"authorize",s),N(r,"login",s),N(r,"credential-request",()=>{s(),L(!0)}),t.useEffect(()=>{if(!Z||!v)return;const c=f;if(Array.isArray(c)&&!c.includes(v.kind))return;const m=v.kind==="access_token"?{accessToken:v.accessToken}:{};w(r,"credential",{...m,credential:v},b),i(b,r,!0)},[Z,v,f,r,w,b,i]),N(r,"ready",()=>{s(),x(c=>c+1)}),N(r,"heartbeat",()=>{P.current&&clearTimeout(P.current),P.current=setTimeout(()=>{o(c=>c+1)},6e3)}),t.useEffect(()=>()=>{P.current&&clearTimeout(P.current)},[]),U)return q?q(U):F.default.createElement(ge,{entry:U});if(C&&!_)return Y?Y():F.default.createElement(ze,null);const n={...W};return delete n.skipManifestCheck,F.default.createElement(Le,{key:`auth-epoch-${O}-frame-${l}`,$isReady:_,ref:k,src:I,...n})};Re.propTypes={id:g.default.string,src:g.default.string.isRequired,path:g.default.string,onInit:g.default.func,onTimeout:g.default.func,renderUnsupported:g.default.func,renderTimeout:g.default.func};exports.CREDENTIAL_KIND_ACCESS_TOKEN=ae;exports.EmbeddedAuthorizationContext=Q;exports.MMEmbeddableZone=Re;exports.MMEmbeddableZoneContext=J;exports.MMEmbeddableZoneProvider=Ee;exports.isCrossSite=re;exports.normalizeCredential=te;exports.popupAuthorizationLauncher=le;exports.unsupportedCopy=me;exports.useEmbeddedAuthorizations=fe;exports.useEmbeddedCredential=be;exports.useEmbeddedEffect=N;exports.useEmbeddedOriginEpoch=pe;exports.useEmbeddedUnsupported=he;
|