@machinemetrics/mm-react-embeddable 2.0.0-4 → 2.0.0-6
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.
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("react"),Ee=require("prop-types"),ge=require("styled-components"),ee=r=>r&&r.__esModule?r:{default:r},L=ee(t),k=ee(Ee),re=ee(ge),Y=t.createContext(null),N=t.createContext({pending:[],authorize:()=>{},approve:()=>{},complete:()=>{},dismiss:()=>{},originEpochs:{},credentials:{},unsupported:{}}),ue="access_token",Q=r=>r?r.kind?r:r.accessToken?{kind:ue,accessToken:r.accessToken,expiresAt:r.expiresAt||null}:null:null,Re=60,ke=120,Ce=new Set(["host_not_allowed","unbound_host_bound_guest","invalid_token","provider_unavailable"]),J=typeof window!="undefined"&&window.location.protocol==="https:",Te=8e3,G=r=>{try{return new URL(r).origin}catch{return null}},ne=(r,u)=>{const i=u==null?void 0:u.origin;return!i||G(r==null?void 0:r.authUrl)!==i?null:i},se=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(".")},oe=(r,u)=>{try{const i=new URL(r),f=new URL(u);return i.protocol!==f.protocol?!0:se(i.hostname)!==se(f.hostname)}catch{return!0}},ce=(r,{onBlocked:u,onClosed:i})=>{const f=window.open(r.authUrl,"mm-embed-auth","popup,width=600,height=700");if(!f)return u(),()=>{};const m=setInterval(()=>{f.closed&&(clearInterval(m),i())},1e3);return()=>clearInterval(m)},ie=({children:r,launcher:u,broker:i})=>{const f=t.useContext(Y),[m,E]=t.useState({}),[B,C]=t.useState({}),[x,A]=t.useState({}),[P,O]=t.useState({}),v=t.useRef(x);v.current=x;const g=t.useRef(m);g.current=m;const z=t.useRef(null),K=t.useRef({}),M=t.useRef({}),D=t.useRef({}),$=t.useRef(i);$.current=i;const _=t.useCallback(e=>{const n=M.current[e];if(!n)return;const s=a=>!a[e]||a[e].clientId?a:{...a,[e]:{...a[e],clientId:n}};E(s),O(s)},[]),c=t.useCallback((e,n)=>{e&&(O(s=>s[e]?s:{...s,[e]:{origin:e,reason:n,clientId:M.current[e]||null}}),_(e))},[_]),l=t.useCallback((e,{v2:n=!1,origin:s=null}={})=>{s&&(n&&v.current[s]||(E(a=>a[s]?a:{...a,[s]:{origin:s,authUrl:e.authUrl,silentUrl:e.silentUrl||null,status:n&&$.current?"broker":n&&e.silentUrl&&J?"silent":"interactive",v2:n,scope:e.scope||null,acceptsCredentials:D.current[s]||null,consent:null,error:null,clientId:e.clientId||M.current[s]||null,popupBlocked:!1}}),_(s)))},[_]),o=t.useRef({}),d=t.useCallback(e=>{o.current[e]&&(clearTimeout(o.current[e]),delete o.current[e])},[]),p=t.useCallback(e=>{d(e),E(n=>{if(!n[e])return n;const s={...n};return delete s[e],s})},[d]),h=t.useCallback(e=>{e&&(p(e),C(n=>({...n,[e]:(n[e]||0)+1})))},[p]),S=t.useCallback(e=>{E(n=>!n[e]||n[e].status==="interactive"?n:{...n,[e]:{...n[e],status:"interactive"}})},[]),y=t.useCallback(e=>{p(e.origin)},[p]),R=t.useCallback((e,n)=>{const s=ne(e,n);if(s){if(oe(s,window.location.origin)){c(s,"cross-site-legacy");return}l(e,{origin:s})}},[l,c]);t.useEffect(()=>{if(!f)return;const e=[f.addListener("*","init",(n,s)=>{s!=null&&s.origin&&(n!=null&&n.clientId)&&(M.current[s.origin]=n.clientId,_(s.origin)),s!=null&&s.origin&&Array.isArray(n==null?void 0:n.acceptsCredentials)&&(D.current[s.origin]=n.acceptsCredentials)}),f.addListener("*","authorize",R),f.addListener("*","login",R),f.addListener("*","credential-request",(n,s)=>l(n,{v2:!0,origin:ne(n,s)}))];return()=>e.forEach(n=>n())},[f,R,l,_]),t.useEffect(()=>{const e=n=>{const s=n==null?void 0:n.data;if(!s||s.type!=="authorized"&&s.type!=="authorize-failed")return;const a=G(s.authUrl);if(!(!a||n.origin!==a)){if(s.type==="authorized"){if(s.accessToken){if(!g.current[a])return;A(b=>({...b,[a]:Q({accessToken:s.accessToken})})),p(a)}else h(a);return}S(a)}};return window.addEventListener("message",e),()=>window.removeEventListener("message",e)},[p,h,S]);const F=t.useCallback((e,n)=>{const s=e==null?void 0:e.origin,a=Q(n);!s||!a||(A(b=>({...b,[s]:a})),p(s))},[p]),U=t.useCallback((e,n)=>{E(s=>s[e]?{...s,[e]:{...s[e],...n}}:s)},[]),w=t.useRef({}),I=t.useCallback(async(e,{approve:n}={})=>{const s=$.current,{origin:a}=e;let b;try{b=await s({origin:a,clientId:e.clientId,scope:e.scope,authUrl:e.authUrl},{approve:n})}catch{n?U(a,{error:{reason:"broker_unavailable"}}):U(a,{status:e.silentUrl&&J?"silent":"interactive"});return}if((b==null?void 0:b.status)===200&&b.credential){F(e,b.credential);return}if((b==null?void 0:b.status)===409&&b.consentRequired){U(a,{status:"interactive",consent:{client:b.client||null,stepUp:!!b.stepUp,scope:b.scope||e.scope},error:null});return}const V=(b==null?void 0:b.error)||"broker_error";if(!n&&Ce.has(V)){U(a,{status:e.silentUrl&&J?"silent":"interactive"});return}if(n&&V==="consent_denied"){U(a,{error:null});return}if(V==="rate_limited"&&!e.rateLimitRetried){const X=Math.min(Number(b==null?void 0:b.retryAfter)>0?Number(b.retryAfter):Re,ke),he={...e,rateLimitRetried:!0};d(a),o.current[a]=setTimeout(()=>{delete o.current[a],w.current[a]=!0,Z.current(he,{approve:n}).finally(()=>{delete w.current[a]})},X*1e3),U(a,{rateLimitRetried:!0});return}p(a),O(X=>({...X,[a]:{origin:a,reason:`broker:${V}`,detail:b||null,clientId:e.clientId||M.current[a]||null}}))},[F,U,p,d]),Z=t.useRef(I);Z.current=I,t.useEffect(()=>{Object.values(m).forEach(e=>{e.status!=="broker"||w.current[e.origin]||o.current[e.origin]||(w.current[e.origin]=!0,I(e).finally(()=>{delete w.current[e.origin]}))})},[m,I]);const W=t.useCallback(e=>!$.current||!(e!=null&&e.consent)?Promise.resolve():I(e,{approve:!0}),[I]);t.useEffect(()=>{const e=K.current;Object.values(m).forEach(n=>{n.status==="silent"&&!e[n.origin]&&(e[n.origin]=setTimeout(()=>{delete e[n.origin],S(n.origin)},Te))}),Object.keys(e).forEach(n=>{(!m[n]||m[n].status!=="silent")&&(clearTimeout(e[n]),delete e[n])})},[m,S]),t.useEffect(()=>()=>{z.current&&z.current(),Object.values(K.current).forEach(clearTimeout),Object.values(o.current).forEach(clearTimeout)},[]);const j=t.useCallback(e=>{z.current&&(z.current(),z.current=null);const n=u||ce;z.current=n(e,{onBlocked:()=>{E(s=>s[e.origin]?{...s,[e.origin]:{...s[e.origin],popupBlocked:!0}}:s)},onClosed:()=>{if(e.v2){J&&E(s=>!s[e.origin]||!s[e.origin].silentUrl?s:{...s,[e.origin]:{...s[e.origin],status:"silent"}});return}h(e.origin)}})},[u,h]),T={pending:Object.values(m).filter(e=>e.status==="interactive"),authorize:j,approve:W,complete:F,dismiss:y,originEpochs:B,credentials:x,unsupported:P},q=Object.values(m).filter(e=>e.status==="silent"&&e.silentUrl);return L.default.createElement(N.Provider,{value:T},r,q.map(e=>L.default.createElement("iframe",{key:`mm-silent-auth-${e.origin}`,src:e.silentUrl,title:`MachineMetrics silent authorization (${e.origin})`,"aria-hidden":"true",tabIndex:-1,style:{display:"none"}})))};ie.propTypes={children:k.default.node,launcher:k.default.func,broker:k.default.func};const _e=()=>t.useContext(N),ae=r=>{const{originEpochs:u}=t.useContext(N),i=G(r);return i&&u[i]||0},le=r=>{const{credentials:u}=t.useContext(N),i=G(r);return i&&u[i]||null},de=r=>{const{unsupported:u}=t.useContext(N),i=G(r);return i&&u[i]||null},fe=({children:r,authorizationLauncher:u,credentialBroker:i})=>{const[f,m]=t.useState({}),[E,B]=t.useState({}),C=t.useRef({}),x=t.useRef({}),A=t.useRef({}),P=t.useCallback((c,l)=>{var o;return!!((o=A.current[c])!=null&&o.has(l))},[]),O=t.useCallback((c,l,o)=>{m(d=>({...d,[c]:{origin:l,frameRef:o}}))},[]),v=t.useCallback(c=>{m(l=>{const o={...l};return delete o[c],o}),B(l=>{const o={...l};return delete o[c],o}),delete A.current[c]},[]);t.useEffect(()=>{const c=(o=[],d,p)=>{o.forEach(h=>h(d,p))},l=o=>{var F,U,w,I,Z,W,j;if(!((F=o==null?void 0:o.data)!=null&&F.instanceId))return;const{instanceId:d,type:p}=o.data,h=f[d],S=(w=(U=h==null?void 0:h.frameRef)==null?void 0:U.current)==null?void 0:w.contentWindow;if(!S||o.source!==S||o.origin!==h.origin)return;const y={origin:o.origin},R=C.current;if(p==="response"){const{callbackId:T,response:q,error:e}=o.data,n=x.current[T];n&&(n(q,e),delete x.current[T]);return}if(p==="mark-registered"){const{requestType:T}=o.data;A.current[d]||(A.current[d]=new Set),A.current[d].add(T);return}p==="ready"&&B(T=>({...T,[d]:!0})),c((I=R[d])==null?void 0:I[p],o.data,y),c((Z=R["*"])==null?void 0:Z[p],o.data,y),c((W=R[d])==null?void 0:W["*"],o.data,y),c((j=R["*"])==null?void 0:j["*"],o.data,y)};return window.addEventListener("message",l),()=>{window.removeEventListener("message",l)}},[f]);const g=t.useCallback((c,l,o)=>{var p,h;const d=f[c];if(!d){console.warn(`Origin not set for id: ${c}. Unable to send message.`);return}(h=(p=d==null?void 0:d.frameRef)==null?void 0:p.current)!=null&&h.contentWindow&&d.frameRef.current.contentWindow.postMessage({type:l,...o,instanceId:c},d.origin)},[f]),z=t.useRef(0),K=t.useCallback(()=>`${Date.now()}-${++z.current}`,[]),M=t.useCallback(async(c,l,o={})=>new Promise((d,p)=>{const h=K(),S=(y,R)=>{delete x.current[h],R?p(R):d(y)};x.current[h]=S,g(c,"request",{callbackId:h,requestType:l,data:o})}),[g]),D=t.useCallback((c,l)=>{g(c,"load-options",l)},[g]),$=t.useCallback((c,l)=>{g(c,"validate",l)},[g]),_=t.useMemo(()=>({registerFrame:O,unregisterFrame:v,sendMessage:g,sendRequest:M,loadOptions:D,validate:$,hasHandler:P,readyFrames:E,addListener:(c,l,o)=>(C.current[c]||(C.current[c]={}),C.current[c][l]||(C.current[c][l]=[]),C.current[c][l].push(o),()=>{C.current[c][l]=C.current[c][l].filter(d=>d!==o)})}),[O,v,g,M,D,$,P,E]);return L.default.createElement(Y.Provider,{value:_},L.default.createElement(ie,{launcher:u,broker:i},r))},H=(r,u,i)=>{const f=t.useContext(Y);if(f===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const m=t.useRef(i);t.useEffect(()=>{m.current=i},[i]),t.useEffect(()=>{if(!(!r||!u)){if(u.startsWith("key:")){const E=u.substring(4);return f.readyFrames[r]&&f.sendMessage(r,"subscribe-key",{keyChord:E}),f.addListener(r,`key-pressed:${E}`,(...C)=>m.current(...C))}return f.addListener(r,u,(...E)=>m.current(...E))}},[f,r,u])};fe.propTypes={children:k.default.node.isRequired,authorizationLauncher:k.default.func,credentialBroker:k.default.func};const te=re.default.default||re.default,we=1e4,Ie="credentials",Me=(r,u)=>`${r}/mm-embedded-interop?id=${encodeURIComponent(u)}&origin=${encodeURIComponent(window.location.origin)}&caps=${Ie}`,Se=te.iframe`
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("react"),Re=require("prop-types"),ke=require("styled-components"),re=r=>r&&r.__esModule?r:{default:r},L=re(t),R=re(Re),oe=re(ke),G=t.createContext(null),Y=t.createContext({pending:[],authorize:()=>{},approve:()=>{},complete:()=>{},dismiss:()=>{},originEpochs:{},credentials:{},unsupported:{}}),ie="access_token",ee=r=>r?r.kind?r:r.accessToken?{kind:ie,accessToken:r.accessToken,expiresAt:r.expiresAt||null}:null:null,Ce=60,Te=120,_e=new Set(["host_not_allowed","unbound_host_bound_guest","invalid_token","provider_unavailable"]),Q=typeof window!="undefined"&&window.location.protocol==="https:",we=8e3,V=r=>{try{return new URL(r).origin}catch{return null}},ue=(r,o)=>{const i=o==null?void 0:o.origin;return!i||V(r==null?void 0:r.authUrl)!==i?null:i},ce=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(".")},te=(r,o)=>{try{const i=new URL(r),f=new URL(o);return i.protocol!==f.protocol?!0:ce(i.hostname)!==ce(f.hostname)}catch{return!0}},ae=(r,{onBlocked:o,onClosed:i})=>{const f=window.open(r.authUrl,"mm-embed-auth","popup,width=600,height=700");if(!f)return o(),()=>{};const b=setInterval(()=>{f.closed&&(clearInterval(b),i())},1e3);return()=>clearInterval(b)},le=({children:r,launcher:o,broker:i})=>{const f=t.useContext(G),[b,m]=t.useState({}),[D,k]=t.useState({}),[x,A]=t.useState({}),[P,O]=t.useState({}),H=t.useRef(x);H.current=x;const E=t.useRef(b);E.current=b;const z=t.useRef(null),N=t.useRef({}),I=t.useRef({}),Z=t.useRef({}),$=t.useRef(i);$.current=i;const T=t.useCallback(e=>{const n=I.current[e];if(!n)return;const s=a=>!a[e]||a[e].clientId?a:{...a,[e]:{...a[e],clientId:n}};m(s),O(s)},[]),c=t.useCallback((e,n)=>{e&&(O(s=>s[e]?s:{...s,[e]:{origin:e,reason:n,clientId:I.current[e]||null}}),T(e))},[T]),l=t.useCallback((e,{v2:n=!1,origin:s=null}={})=>{s&&(n&&H.current[s]||(m(a=>a[s]?a:{...a,[s]:{origin:s,authUrl:e.authUrl,silentUrl:e.silentUrl||null,status:n&&$.current?"broker":n&&e.silentUrl&&Q?"silent":"interactive",v2:n,scope:e.scope||null,acceptsCredentials:Z.current[s]||null,consent:null,error:null,clientId:e.clientId||I.current[s]||null,popupBlocked:!1}}),T(s)))},[T]),u=t.useRef({}),d=t.useCallback(e=>{u.current[e]&&(clearTimeout(u.current[e]),delete u.current[e])},[]),p=t.useCallback(e=>{d(e),m(n=>{if(!n[e])return n;const s={...n};return delete s[e],s})},[d]),h=t.useCallback(e=>{e&&(p(e),k(n=>({...n,[e]:(n[e]||0)+1})))},[p]),M=t.useCallback(e=>{m(n=>!n[e]||n[e].status==="interactive"?n:{...n,[e]:{...n[e],status:"interactive"}})},[]),y=t.useCallback(e=>{p(e.origin)},[p]),g=t.useCallback((e,n)=>{const s=ue(e,n);if(s){if(te(s,window.location.origin)){c(s,"cross-site-legacy");return}l(e,{origin:s})}},[l,c]);t.useEffect(()=>{if(!f)return;const e=[f.addListener("*","init",(n,s)=>{s!=null&&s.origin&&(n!=null&&n.clientId)&&(I.current[s.origin]=n.clientId,T(s.origin)),s!=null&&s.origin&&Array.isArray(n==null?void 0:n.acceptsCredentials)?Z.current[s.origin]=n.acceptsCredentials:s!=null&&s.origin&&te(s.origin,window.location.origin)&&c(s.origin,"cross-site-legacy")}),f.addListener("*","authorize",g),f.addListener("*","login",g),f.addListener("*","credential-request",(n,s)=>l(n,{v2:!0,origin:ue(n,s)}))];return()=>e.forEach(n=>n())},[f,g,l,T,c]),t.useEffect(()=>{const e=n=>{const s=n==null?void 0:n.data;if(!s||s.type!=="authorized"&&s.type!=="authorize-failed")return;const a=V(s.authUrl);if(!(!a||n.origin!==a)){if(s.type==="authorized"){if(s.accessToken){if(!E.current[a])return;A(U=>({...U,[a]:ee({accessToken:s.accessToken})})),p(a)}else h(a);return}M(a)}};return window.addEventListener("message",e),()=>window.removeEventListener("message",e)},[p,h,M]);const F=t.useCallback((e,n)=>{const s=e==null?void 0:e.origin,a=ee(n);!s||!a||(A(U=>({...U,[s]:a})),p(s))},[p]),S=t.useCallback((e,n)=>{m(s=>s[e]?{...s,[e]:{...s[e],...n}}:s)},[]),_=t.useRef({}),w=t.useCallback(async(e,{approve:n}={})=>{const s=$.current,{origin:a}=e;let U;try{U=await s({origin:a,clientId:e.clientId,scope:e.scope,authUrl:e.authUrl},{approve:n})}catch{n?S(a,{error:{reason:"broker_unavailable"}}):S(a,{status:e.silentUrl&&Q?"silent":"interactive"});return}const se=U==null?void 0:U.status,B=(U==null?void 0:U.body)||{};if(se===200&&B.credential){F(e,B.credential);return}if(se===409&&B.consentRequired){S(a,{status:"interactive",consent:{client:B.client||null,stepUp:!!B.stepUp,scope:B.scope||e.scope},error:null});return}const X=B.error||"broker_error";if(!n&&_e.has(X)){S(a,{status:e.silentUrl&&Q?"silent":"interactive"});return}if(n&&X==="consent_denied"){S(a,{error:null});return}if(X==="rate_limited"&&!e.rateLimitRetried){const J=Math.min(Number(B.retryAfter)>0?Number(B.retryAfter):Ce,Te),ge={...e,rateLimitRetried:!0};d(a),u.current[a]=setTimeout(()=>{delete u.current[a],_.current[a]=!0,W.current(ge,{approve:n}).finally(()=>{delete _.current[a]})},J*1e3),S(a,{rateLimitRetried:!0});return}p(a),O(J=>({...J,[a]:{origin:a,reason:`broker:${X}`,detail:U||null,clientId:e.clientId||I.current[a]||null}}))},[F,S,p,d]),W=t.useRef(w);W.current=w,t.useEffect(()=>{Object.values(b).forEach(e=>{e.status!=="broker"||_.current[e.origin]||u.current[e.origin]||(_.current[e.origin]=!0,w(e).finally(()=>{delete _.current[e.origin]}))})},[b,w]);const v=t.useCallback(e=>!$.current||!(e!=null&&e.consent)?Promise.resolve():w(e,{approve:!0}),[w]);t.useEffect(()=>{const e=N.current;Object.values(b).forEach(n=>{n.status==="silent"&&!e[n.origin]&&(e[n.origin]=setTimeout(()=>{delete e[n.origin],M(n.origin)},we))}),Object.keys(e).forEach(n=>{(!b[n]||b[n].status!=="silent")&&(clearTimeout(e[n]),delete e[n])})},[b,M]),t.useEffect(()=>()=>{z.current&&z.current(),Object.values(N.current).forEach(clearTimeout),Object.values(u.current).forEach(clearTimeout)},[]);const j=t.useCallback(e=>{z.current&&(z.current(),z.current=null);const n=o||ae;z.current=n(e,{onBlocked:()=>{m(s=>s[e.origin]?{...s,[e.origin]:{...s[e.origin],popupBlocked:!0}}:s)},onClosed:()=>{if(e.v2){Q&&m(s=>!s[e.origin]||!s[e.origin].silentUrl?s:{...s,[e.origin]:{...s[e.origin],status:"silent"}});return}h(e.origin)}})},[o,h]),C={pending:Object.values(b).filter(e=>e.status==="interactive"),authorize:j,approve:v,complete:F,dismiss:y,originEpochs:D,credentials:x,unsupported:P},q=Object.values(b).filter(e=>e.status==="silent"&&e.silentUrl);return L.default.createElement(Y.Provider,{value:C},r,q.map(e=>L.default.createElement("iframe",{key:`mm-silent-auth-${e.origin}`,src:e.silentUrl,title:`MachineMetrics silent authorization (${e.origin})`,"aria-hidden":"true",tabIndex:-1,style:{display:"none"}})))};le.propTypes={children:R.default.node,launcher:R.default.func,broker:R.default.func};const Ie=()=>t.useContext(Y),de=r=>{const{originEpochs:o}=t.useContext(Y),i=V(r);return i&&o[i]||0},fe=r=>{const{credentials:o}=t.useContext(Y),i=V(r);return i&&o[i]||null},pe=r=>{const{unsupported:o}=t.useContext(Y),i=V(r);return i&&o[i]||null},be=({children:r,authorizationLauncher:o,credentialBroker:i})=>{const[f,b]=t.useState({}),[m,D]=t.useState({}),k=t.useRef({}),x=t.useRef({}),A=t.useRef({}),P=t.useCallback((c,l)=>{var u;return!!((u=A.current[c])!=null&&u.has(l))},[]),O=t.useCallback((c,l,u)=>{b(d=>({...d,[c]:{origin:l,frameRef:u}}))},[]),H=t.useCallback(c=>{b(l=>{const u={...l};return delete u[c],u}),D(l=>{const u={...l};return delete u[c],u}),delete A.current[c]},[]);t.useEffect(()=>{const c=(u=[],d,p)=>{u.forEach(h=>h(d,p))},l=u=>{var F,S,_,w,W,v,j;if(!((F=u==null?void 0:u.data)!=null&&F.instanceId))return;const{instanceId:d,type:p}=u.data,h=f[d],M=(_=(S=h==null?void 0:h.frameRef)==null?void 0:S.current)==null?void 0:_.contentWindow;if(!M||u.source!==M||u.origin!==h.origin)return;const y={origin:u.origin},g=k.current;if(p==="response"){const{callbackId:C,response:q,error:e}=u.data,n=x.current[C];n&&(n(q,e),delete x.current[C]);return}if(p==="mark-registered"){const{requestType:C}=u.data;A.current[d]||(A.current[d]=new Set),A.current[d].add(C);return}p==="ready"&&D(C=>({...C,[d]:!0})),c((w=g[d])==null?void 0:w[p],u.data,y),c((W=g["*"])==null?void 0:W[p],u.data,y),c((v=g[d])==null?void 0:v["*"],u.data,y),c((j=g["*"])==null?void 0:j["*"],u.data,y)};return window.addEventListener("message",l),()=>{window.removeEventListener("message",l)}},[f]);const E=t.useCallback((c,l,u)=>{var p,h;const d=f[c];if(!d){console.warn(`Origin not set for id: ${c}. Unable to send message.`);return}(h=(p=d==null?void 0:d.frameRef)==null?void 0:p.current)!=null&&h.contentWindow&&d.frameRef.current.contentWindow.postMessage({type:l,...u,instanceId:c},d.origin)},[f]),z=t.useRef(0),N=t.useCallback(()=>`${Date.now()}-${++z.current}`,[]),I=t.useCallback(async(c,l,u={})=>new Promise((d,p)=>{const h=N(),M=(y,g)=>{delete x.current[h],g?p(g):d(y)};x.current[h]=M,E(c,"request",{callbackId:h,requestType:l,data:u})}),[E]),Z=t.useCallback((c,l)=>{E(c,"load-options",l)},[E]),$=t.useCallback((c,l)=>{E(c,"validate",l)},[E]),T=t.useMemo(()=>({registerFrame:O,unregisterFrame:H,sendMessage:E,sendRequest:I,loadOptions:Z,validate:$,hasHandler:P,readyFrames:m,addListener:(c,l,u)=>(k.current[c]||(k.current[c]={}),k.current[c][l]||(k.current[c][l]=[]),k.current[c][l].push(u),()=>{k.current[c][l]=k.current[c][l].filter(d=>d!==u)})}),[O,H,E,I,Z,$,P,m]);return L.default.createElement(G.Provider,{value:T},L.default.createElement(le,{launcher:o,broker:i},r))},K=(r,o,i)=>{const f=t.useContext(G);if(f===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const b=t.useRef(i);t.useEffect(()=>{b.current=i},[i]),t.useEffect(()=>{if(!(!r||!o)){if(o.startsWith("key:")){const m=o.substring(4);return f.readyFrames[r]&&f.sendMessage(r,"subscribe-key",{keyChord:m}),f.addListener(r,`key-pressed:${m}`,(...k)=>b.current(...k))}return f.addListener(r,o,(...m)=>b.current(...m))}},[f,r,o])};be.propTypes={children:R.default.node.isRequired,authorizationLauncher:R.default.func,credentialBroker:R.default.func};const ne=oe.default.default||oe.default,Me=1e4,Se="credentials",Ue=(r,o)=>`${r}/mm-embedded-interop?id=${encodeURIComponent(o)}&origin=${encodeURIComponent(window.location.origin)}&caps=${Se}`,Le=ne.iframe`
|
|
2
2
|
width: 100%;
|
|
3
3
|
height: 100%;
|
|
4
4
|
border: none;
|
|
5
5
|
visibility: ${r=>r.$isReady?"visible":"hidden"};
|
|
6
|
-
`,
|
|
6
|
+
`,xe=ne.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
|
+
`,he=r=>{const o=r&&r.origin||"This app",i=r&&r.reason||"";switch(i){case"broker:scope_above_ceiling":return{title:`${o} 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:`${o} can’t be authorized here`,detail:"It isn’t registered to run at this address."};case"broker:cross_company_principal":return{title:`${o} can’t be authorized here`,detail:"It can’t be authorized for this account."};default:return i.startsWith("broker:")?{title:`${o} can’t be authorized here`,detail:"Authorization was refused."}:{title:`${o} can’t be embedded here`,detail:"It needs an update to support embedding from a different domain."}}},me=({entry:r})=>{const o=he(r);return L.default.createElement(xe,{"data-reason":r&&r.reason||void 0},L.default.createElement("div",{style:{fontWeight:600}},o.title),L.default.createElement("div",{style:{fontSize:12,color:"#888"}},o.detail))};me.propTypes={entry:R.default.shape({origin:R.default.string,name:R.default.string,reason:R.default.string,detail:R.default.object})};const Ae=ne.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=()=>L.default.createElement(Ae,null,L.default.createElement("div",{style:{textAlign:"center"}},"This app couldn’t be loaded as an embeddable widget.")),Ee=({id:r,src:o,path:i,onInit:f=()=>{},onTimeout:b,context:m={},params:D={},renderUnsupported:k,renderTimeout:x,...A})=>{if("isConfiguring"in A)return console.error("isConfiguring must be passed via the params prop to MMEmbeddableZone"),null;if("colorMode"in m)return console.error("colorMode must be passed via the params prop to MMEmbeddableZone"),null;if("isFullScreen"in m)return console.error("isFullScreen must be passed via the params prop to MMEmbeddableZone"),null;const P=t.useRef(),[O,H]=t.useState(),[E,z]=t.useState(0),[N,I]=t.useState(!1),[Z,$]=t.useState(!1),T=de(o),c=fe(o),l=pe(o),[u,d]=t.useState(0),[p,h]=t.useState(null),{registerFrame:M,unregisterFrame:y,sendMessage:g}=t.useContext(G),[F,S]=t.useState(),_=t.useRef(),w=t.useRef(b),W=t.useRef(f),v=t.useRef(!1),j=t.useCallback(()=>{v.current=!1,clearTimeout(_.current),z(0),I(!1),$(!1),h(null)},[]);t.useEffect(()=>{(T>0||u>0)&&j()},[T,u,j]),t.useEffect(()=>{w.current=b},[b]),t.useEffect(()=>{W.current=f},[f]),t.useEffect(()=>{E&&g(r,"set-context",{context:{...m}})},[m,r,E]),t.useEffect(()=>{E&&g(r,"set-params",{params:D})},[D,r,E]),t.useEffect(()=>{i!==void 0&&S((i==null?void 0:i.indexOf("/"))===0?i:`/${i}`)},[i]),t.useEffect(()=>{F===void 0||!E||g(r,"navigate",{path:F})},[F,E,r,g]),t.useEffect(()=>{if(!o||!r||!P)return;j();const e=new URL(o);return M(r,e.origin,P),W.current(),H(Ue(e.origin,r)),()=>{y(r)}},[o,r,M,P,j]),t.useEffect(()=>{if(!(!O||v.current))return _.current=setTimeout(()=>{I(!0),w.current&&w.current()},Me),()=>clearTimeout(_.current)},[O,o,T,u]);const C=t.useCallback(()=>{v.current=!0,clearTimeout(_.current),I(!1)},[]);K(r,"init",e=>{C(),Array.isArray(e==null?void 0:e.acceptsCredentials)&&h(e.acceptsCredentials)}),K(r,"authorize",C),K(r,"login",C),K(r,"credential-request",()=>{C(),$(!0)}),t.useEffect(()=>{if(!Z||!c)return;const e=p;if(Array.isArray(e)&&!e.includes(c.kind))return;const n=c.kind==="access_token"?{accessToken:c.accessToken}:{};g(r,"credential",{...n,credential:c})},[Z,c,p,r,g]),K(r,"ready",()=>{C(),z(e=>e+1)});const q=t.useRef();return K(r,"heartbeat",()=>{q.current&&clearTimeout(q.current),q.current=setTimeout(()=>{d(e=>e+1)},6e3)}),t.useEffect(()=>()=>{q.current&&clearTimeout(q.current)},[]),l?k?k(l):L.default.createElement(me,{entry:l}):N&&!E?x?x():L.default.createElement(ze,null):L.default.createElement(Le,{key:`auth-epoch-${T}-frame-${u}`,$isReady:E,ref:P,src:O,...A})};Ee.propTypes={id:R.default.string,src:R.default.string.isRequired,path:R.default.string,onInit:R.default.func,onTimeout:R.default.func,renderUnsupported:R.default.func,renderTimeout:R.default.func};exports.CREDENTIAL_KIND_ACCESS_TOKEN=ie;exports.EmbeddedAuthorizationContext=Y;exports.MMEmbeddableZone=Ee;exports.MMEmbeddableZoneContext=G;exports.MMEmbeddableZoneProvider=be;exports.isCrossSite=te;exports.normalizeCredential=ee;exports.popupAuthorizationLauncher=ae;exports.unsupportedCopy=he;exports.useEmbeddedAuthorizations=Ie;exports.useEmbeddedCredential=fe;exports.useEmbeddedEffect=K;exports.useEmbeddedOriginEpoch=de;exports.useEmbeddedUnsupported=pe;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import x, { createContext as
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
const
|
|
1
|
+
import x, { createContext as de, useContext as V, useState as M, useRef as g, useCallback as h, useEffect as R, useMemo as me } from "react";
|
|
2
|
+
import k from "prop-types";
|
|
3
|
+
import ie from "styled-components";
|
|
4
|
+
const ne = de(null), N = de({
|
|
5
5
|
pending: [],
|
|
6
6
|
authorize: () => {
|
|
7
7
|
},
|
|
@@ -14,37 +14,37 @@ const re = ue(null), J = ue({
|
|
|
14
14
|
originEpochs: {},
|
|
15
15
|
credentials: {},
|
|
16
16
|
unsupported: {}
|
|
17
|
-
}),
|
|
18
|
-
kind:
|
|
17
|
+
}), ge = "access_token", ce = (t) => t ? t.kind ? t : t.accessToken ? {
|
|
18
|
+
kind: ge,
|
|
19
19
|
accessToken: t.accessToken,
|
|
20
20
|
expiresAt: t.expiresAt || null
|
|
21
|
-
} : null : null,
|
|
21
|
+
} : null : null, be = 60, Ee = 120, Re = /* @__PURE__ */ new Set([
|
|
22
22
|
"host_not_allowed",
|
|
23
23
|
"unbound_host_bound_guest",
|
|
24
24
|
"invalid_token",
|
|
25
25
|
"provider_unavailable"
|
|
26
|
-
]),
|
|
26
|
+
]), re = typeof window != "undefined" && window.location.protocol === "https:", Te = 8e3, Q = (t) => {
|
|
27
27
|
try {
|
|
28
28
|
return new URL(t).origin;
|
|
29
29
|
} catch {
|
|
30
30
|
return null;
|
|
31
31
|
}
|
|
32
|
-
},
|
|
32
|
+
}, ue = (t, s) => {
|
|
33
33
|
const c = s == null ? void 0 : s.origin;
|
|
34
|
-
return !c ||
|
|
35
|
-
},
|
|
34
|
+
return !c || Q(t == null ? void 0 : t.authUrl) !== c ? null : c;
|
|
35
|
+
}, le = (t) => {
|
|
36
36
|
if (!t || /^\d{1,3}(?:\.\d{1,3}){3}$/.test(t) || t.includes(":"))
|
|
37
37
|
return t;
|
|
38
38
|
const s = t.split(".");
|
|
39
39
|
return s.length <= 2 ? t : s.slice(-2).join(".");
|
|
40
|
-
},
|
|
40
|
+
}, ae = (t, s) => {
|
|
41
41
|
try {
|
|
42
42
|
const c = new URL(t), d = new URL(s);
|
|
43
|
-
return c.protocol !== d.protocol ? !0 :
|
|
43
|
+
return c.protocol !== d.protocol ? !0 : le(c.hostname) !== le(d.hostname);
|
|
44
44
|
} catch {
|
|
45
45
|
return !0;
|
|
46
46
|
}
|
|
47
|
-
},
|
|
47
|
+
}, ke = (t, { onBlocked: s, onClosed: c }) => {
|
|
48
48
|
const d = window.open(
|
|
49
49
|
t.authUrl,
|
|
50
50
|
"mm-embed-auth",
|
|
@@ -53,34 +53,38 @@ const re = ue(null), J = ue({
|
|
|
53
53
|
if (!d)
|
|
54
54
|
return s(), () => {
|
|
55
55
|
};
|
|
56
|
-
const
|
|
57
|
-
d.closed && (clearInterval(
|
|
56
|
+
const p = setInterval(() => {
|
|
57
|
+
d.closed && (clearInterval(p), c());
|
|
58
58
|
}, 1e3);
|
|
59
|
-
return () => clearInterval(
|
|
60
|
-
},
|
|
61
|
-
const d =
|
|
62
|
-
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
const F =
|
|
66
|
-
|
|
67
|
-
const
|
|
68
|
-
const r =
|
|
59
|
+
return () => clearInterval(p);
|
|
60
|
+
}, fe = ({ children: t, launcher: s, broker: c }) => {
|
|
61
|
+
const d = V(ne), [p, b] = M({}), [Z, w] = M({}), [$, S] = M({}), [q, y] = M({}), G = g($);
|
|
62
|
+
G.current = $;
|
|
63
|
+
const E = g(p);
|
|
64
|
+
E.current = p;
|
|
65
|
+
const F = g(null), X = g({}), C = g({}), v = g({}), P = g(c);
|
|
66
|
+
P.current = c;
|
|
67
|
+
const _ = h((e) => {
|
|
68
|
+
const r = C.current[e];
|
|
69
69
|
if (!r)
|
|
70
70
|
return;
|
|
71
71
|
const n = (u) => !u[e] || u[e].clientId ? u : { ...u, [e]: { ...u[e], clientId: r } };
|
|
72
|
-
|
|
72
|
+
b(n), y(n);
|
|
73
73
|
}, []), i = h(
|
|
74
74
|
(e, r) => {
|
|
75
|
-
e && (
|
|
75
|
+
e && (y((n) => n[e] ? n : {
|
|
76
76
|
...n,
|
|
77
|
-
[e]: {
|
|
78
|
-
|
|
77
|
+
[e]: {
|
|
78
|
+
origin: e,
|
|
79
|
+
reason: r,
|
|
80
|
+
clientId: C.current[e] || null
|
|
81
|
+
}
|
|
82
|
+
}), _(e));
|
|
79
83
|
},
|
|
80
|
-
[
|
|
81
|
-
),
|
|
84
|
+
[_]
|
|
85
|
+
), l = h(
|
|
82
86
|
(e, { v2: r = !1, origin: n = null } = {}) => {
|
|
83
|
-
n && (r &&
|
|
87
|
+
n && (r && G.current[n] || (b((u) => u[n] ? u : {
|
|
84
88
|
...u,
|
|
85
89
|
[n]: {
|
|
86
90
|
origin: n,
|
|
@@ -89,40 +93,43 @@ const re = ue(null), J = ue({
|
|
|
89
93
|
// v2 entries try the invisible silent resolver first — but only on a host that can
|
|
90
94
|
// frame login (https). Otherwise (and for legacy signals) go straight to interactive,
|
|
91
95
|
// so an http host doesn't wait out a silent frame that can never load login.
|
|
92
|
-
status: r &&
|
|
96
|
+
status: r && P.current ? "broker" : r && e.silentUrl && re ? "silent" : "interactive",
|
|
93
97
|
v2: r,
|
|
94
98
|
// v1.9: the scope the guest asked for; forwarded verbatim to the broker.
|
|
95
99
|
scope: e.scope || null,
|
|
96
|
-
acceptsCredentials:
|
|
100
|
+
acceptsCredentials: v.current[n] || null,
|
|
97
101
|
consent: null,
|
|
98
102
|
error: null,
|
|
99
103
|
// clientId from the request payload (v2) or the cached init (legacy);
|
|
100
104
|
// the host resolves the display name from it.
|
|
101
|
-
clientId: e.clientId ||
|
|
105
|
+
clientId: e.clientId || C.current[n] || null,
|
|
102
106
|
popupBlocked: !1
|
|
103
107
|
}
|
|
104
|
-
}),
|
|
108
|
+
}), _(n)));
|
|
105
109
|
},
|
|
106
|
-
[
|
|
107
|
-
), o =
|
|
110
|
+
[_]
|
|
111
|
+
), o = g({}), a = h((e) => {
|
|
108
112
|
o.current[e] && (clearTimeout(o.current[e]), delete o.current[e]);
|
|
109
|
-
}, []), f = h(
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
113
|
+
}, []), f = h(
|
|
114
|
+
(e) => {
|
|
115
|
+
a(e), b((r) => {
|
|
116
|
+
if (!r[e])
|
|
117
|
+
return r;
|
|
118
|
+
const n = { ...r };
|
|
119
|
+
return delete n[e], n;
|
|
120
|
+
});
|
|
121
|
+
},
|
|
122
|
+
[a]
|
|
123
|
+
), m = h(
|
|
117
124
|
(e) => {
|
|
118
|
-
e && (f(e),
|
|
125
|
+
e && (f(e), w((r) => ({
|
|
119
126
|
...r,
|
|
120
127
|
[e]: (r[e] || 0) + 1
|
|
121
128
|
})));
|
|
122
129
|
},
|
|
123
130
|
[f]
|
|
124
|
-
),
|
|
125
|
-
|
|
131
|
+
), A = h((e) => {
|
|
132
|
+
b((r) => !r[e] || r[e].status === "interactive" ? r : {
|
|
126
133
|
...r,
|
|
127
134
|
[e]: { ...r[e], status: "interactive" }
|
|
128
135
|
});
|
|
@@ -131,20 +138,20 @@ const re = ue(null), J = ue({
|
|
|
131
138
|
f(e.origin);
|
|
132
139
|
},
|
|
133
140
|
[f]
|
|
134
|
-
),
|
|
141
|
+
), T = h(
|
|
135
142
|
(e, r) => {
|
|
136
|
-
const n =
|
|
143
|
+
const n = ue(e, r);
|
|
137
144
|
if (n) {
|
|
138
|
-
if (
|
|
145
|
+
if (ae(n, window.location.origin)) {
|
|
139
146
|
i(n, "cross-site-legacy");
|
|
140
147
|
return;
|
|
141
148
|
}
|
|
142
|
-
|
|
149
|
+
l(e, { origin: n });
|
|
143
150
|
}
|
|
144
151
|
},
|
|
145
|
-
[
|
|
152
|
+
[l, i]
|
|
146
153
|
);
|
|
147
|
-
|
|
154
|
+
R(() => {
|
|
148
155
|
if (!d)
|
|
149
156
|
return;
|
|
150
157
|
const e = [
|
|
@@ -155,179 +162,185 @@ const re = ue(null), J = ue({
|
|
|
155
162
|
// clientId is only a lookup key: the host resolves the display name from
|
|
156
163
|
// it, and a broker binds it to the attested origin server-side.
|
|
157
164
|
d.addListener("*", "init", (r, n) => {
|
|
158
|
-
n != null && n.origin && (r != null && r.clientId) && (
|
|
165
|
+
n != null && n.origin && (r != null && r.clientId) && (C.current[n.origin] = r.clientId, _(n.origin)), n != null && n.origin && Array.isArray(r == null ? void 0 : r.acceptsCredentials) ? v.current[n.origin] = r.acceptsCredentials : n != null && n.origin && ae(n.origin, window.location.origin) && i(n.origin, "cross-site-legacy");
|
|
159
166
|
}),
|
|
160
|
-
d.addListener("*", "authorize",
|
|
161
|
-
d.addListener("*", "login",
|
|
167
|
+
d.addListener("*", "authorize", T),
|
|
168
|
+
d.addListener("*", "login", T),
|
|
162
169
|
d.addListener(
|
|
163
170
|
"*",
|
|
164
171
|
"credential-request",
|
|
165
|
-
(r, n) =>
|
|
172
|
+
(r, n) => l(r, { v2: !0, origin: ue(r, n) })
|
|
166
173
|
)
|
|
167
174
|
];
|
|
168
175
|
return () => e.forEach((r) => r());
|
|
169
|
-
}, [d,
|
|
176
|
+
}, [d, T, l, _, i]), R(() => {
|
|
170
177
|
const e = (r) => {
|
|
171
178
|
const n = r == null ? void 0 : r.data;
|
|
172
179
|
if (!n || n.type !== "authorized" && n.type !== "authorize-failed")
|
|
173
180
|
return;
|
|
174
|
-
const u =
|
|
181
|
+
const u = Q(n.authUrl);
|
|
175
182
|
if (!(!u || r.origin !== u)) {
|
|
176
183
|
if (n.type === "authorized") {
|
|
177
184
|
if (n.accessToken) {
|
|
178
|
-
if (!
|
|
185
|
+
if (!E.current[u])
|
|
179
186
|
return;
|
|
180
|
-
S((
|
|
181
|
-
...
|
|
182
|
-
[u]:
|
|
187
|
+
S((z) => ({
|
|
188
|
+
...z,
|
|
189
|
+
[u]: ce({ accessToken: n.accessToken })
|
|
183
190
|
})), f(u);
|
|
184
191
|
} else
|
|
185
|
-
|
|
192
|
+
m(u);
|
|
186
193
|
return;
|
|
187
194
|
}
|
|
188
|
-
|
|
195
|
+
A(u);
|
|
189
196
|
}
|
|
190
197
|
};
|
|
191
198
|
return window.addEventListener("message", e), () => window.removeEventListener("message", e);
|
|
192
|
-
}, [f,
|
|
199
|
+
}, [f, m, A]);
|
|
193
200
|
const B = h(
|
|
194
201
|
(e, r) => {
|
|
195
|
-
const n = e == null ? void 0 : e.origin, u =
|
|
196
|
-
!n || !u || (S((
|
|
202
|
+
const n = e == null ? void 0 : e.origin, u = ce(r);
|
|
203
|
+
!n || !u || (S((z) => ({ ...z, [n]: u })), f(n));
|
|
197
204
|
},
|
|
198
205
|
[f]
|
|
199
|
-
),
|
|
200
|
-
|
|
201
|
-
}, []),
|
|
206
|
+
), O = h((e, r) => {
|
|
207
|
+
b((n) => n[e] ? { ...n, [e]: { ...n[e], ...r } } : n);
|
|
208
|
+
}, []), U = g({}), L = h(
|
|
202
209
|
async (e, { approve: r } = {}) => {
|
|
203
|
-
const n =
|
|
204
|
-
let
|
|
210
|
+
const n = P.current, { origin: u } = e;
|
|
211
|
+
let z;
|
|
205
212
|
try {
|
|
206
|
-
|
|
207
|
-
{
|
|
213
|
+
z = await n(
|
|
214
|
+
{
|
|
215
|
+
origin: u,
|
|
216
|
+
clientId: e.clientId,
|
|
217
|
+
scope: e.scope,
|
|
218
|
+
authUrl: e.authUrl
|
|
219
|
+
},
|
|
208
220
|
{ approve: r }
|
|
209
221
|
);
|
|
210
222
|
} catch {
|
|
211
|
-
r ?
|
|
212
|
-
status: e.silentUrl &&
|
|
223
|
+
r ? O(u, { error: { reason: "broker_unavailable" } }) : O(u, {
|
|
224
|
+
status: e.silentUrl && re ? "silent" : "interactive"
|
|
213
225
|
});
|
|
214
226
|
return;
|
|
215
227
|
}
|
|
216
|
-
|
|
217
|
-
|
|
228
|
+
const oe = z == null ? void 0 : z.status, H = (z == null ? void 0 : z.body) || {};
|
|
229
|
+
if (oe === 200 && H.credential) {
|
|
230
|
+
B(e, H.credential);
|
|
218
231
|
return;
|
|
219
232
|
}
|
|
220
|
-
if (
|
|
221
|
-
|
|
233
|
+
if (oe === 409 && H.consentRequired) {
|
|
234
|
+
O(u, {
|
|
222
235
|
status: "interactive",
|
|
223
236
|
consent: {
|
|
224
|
-
client:
|
|
225
|
-
stepUp: !!
|
|
226
|
-
scope:
|
|
237
|
+
client: H.client || null,
|
|
238
|
+
stepUp: !!H.stepUp,
|
|
239
|
+
scope: H.scope || e.scope
|
|
227
240
|
},
|
|
228
241
|
error: null
|
|
229
242
|
});
|
|
230
243
|
return;
|
|
231
244
|
}
|
|
232
|
-
const
|
|
233
|
-
if (!r &&
|
|
234
|
-
|
|
235
|
-
status: e.silentUrl &&
|
|
245
|
+
const ee = H.error || "broker_error";
|
|
246
|
+
if (!r && Re.has(ee)) {
|
|
247
|
+
O(u, {
|
|
248
|
+
status: e.silentUrl && re ? "silent" : "interactive"
|
|
236
249
|
});
|
|
237
250
|
return;
|
|
238
251
|
}
|
|
239
|
-
if (r &&
|
|
240
|
-
|
|
252
|
+
if (r && ee === "consent_denied") {
|
|
253
|
+
O(u, { error: null });
|
|
241
254
|
return;
|
|
242
255
|
}
|
|
243
|
-
if (
|
|
244
|
-
const
|
|
245
|
-
Number(
|
|
246
|
-
|
|
247
|
-
),
|
|
248
|
-
|
|
249
|
-
delete o.current[u],
|
|
250
|
-
delete
|
|
256
|
+
if (ee === "rate_limited" && !e.rateLimitRetried) {
|
|
257
|
+
const te = Math.min(
|
|
258
|
+
Number(H.retryAfter) > 0 ? Number(H.retryAfter) : be,
|
|
259
|
+
Ee
|
|
260
|
+
), he = { ...e, rateLimitRetried: !0 };
|
|
261
|
+
a(u), o.current[u] = setTimeout(() => {
|
|
262
|
+
delete o.current[u], U.current[u] = !0, K.current(he, { approve: r }).finally(() => {
|
|
263
|
+
delete U.current[u];
|
|
251
264
|
});
|
|
252
|
-
},
|
|
265
|
+
}, te * 1e3), O(u, { rateLimitRetried: !0 });
|
|
253
266
|
return;
|
|
254
267
|
}
|
|
255
|
-
f(u),
|
|
256
|
-
...
|
|
268
|
+
f(u), y((te) => ({
|
|
269
|
+
...te,
|
|
257
270
|
[u]: {
|
|
258
271
|
origin: u,
|
|
259
|
-
reason: `broker:${
|
|
260
|
-
detail:
|
|
261
|
-
clientId: e.clientId ||
|
|
272
|
+
reason: `broker:${ee}`,
|
|
273
|
+
detail: z || null,
|
|
274
|
+
clientId: e.clientId || C.current[u] || null
|
|
262
275
|
}
|
|
263
276
|
}));
|
|
264
277
|
},
|
|
265
|
-
[B,
|
|
266
|
-
),
|
|
267
|
-
|
|
268
|
-
Object.values(
|
|
269
|
-
e.status !== "broker" ||
|
|
270
|
-
delete
|
|
278
|
+
[B, O, f, a]
|
|
279
|
+
), K = g(L);
|
|
280
|
+
K.current = L, R(() => {
|
|
281
|
+
Object.values(p).forEach((e) => {
|
|
282
|
+
e.status !== "broker" || U.current[e.origin] || o.current[e.origin] || (U.current[e.origin] = !0, L(e).finally(() => {
|
|
283
|
+
delete U.current[e.origin];
|
|
271
284
|
}));
|
|
272
285
|
});
|
|
273
|
-
}, [
|
|
274
|
-
const
|
|
275
|
-
(e) => !
|
|
276
|
-
[
|
|
286
|
+
}, [p, L]);
|
|
287
|
+
const Y = h(
|
|
288
|
+
(e) => !P.current || !(e != null && e.consent) ? Promise.resolve() : L(e, { approve: !0 }),
|
|
289
|
+
[L]
|
|
277
290
|
);
|
|
278
|
-
|
|
279
|
-
const e =
|
|
280
|
-
Object.values(
|
|
291
|
+
R(() => {
|
|
292
|
+
const e = X.current;
|
|
293
|
+
Object.values(p).forEach((r) => {
|
|
281
294
|
r.status === "silent" && !e[r.origin] && (e[r.origin] = setTimeout(() => {
|
|
282
|
-
delete e[r.origin],
|
|
283
|
-
},
|
|
295
|
+
delete e[r.origin], A(r.origin);
|
|
296
|
+
}, Te));
|
|
284
297
|
}), Object.keys(e).forEach((r) => {
|
|
285
|
-
(!
|
|
298
|
+
(!p[r] || p[r].status !== "silent") && (clearTimeout(e[r]), delete e[r]);
|
|
286
299
|
});
|
|
287
|
-
}, [
|
|
288
|
-
F.current && F.current(), Object.values(
|
|
300
|
+
}, [p, A]), R(() => () => {
|
|
301
|
+
F.current && F.current(), Object.values(X.current).forEach(clearTimeout), Object.values(o.current).forEach(clearTimeout);
|
|
289
302
|
}, []);
|
|
290
303
|
const W = h(
|
|
291
304
|
(e) => {
|
|
292
305
|
F.current && (F.current(), F.current = null);
|
|
293
|
-
const r = s ||
|
|
306
|
+
const r = s || ke;
|
|
294
307
|
F.current = r(e, {
|
|
295
308
|
onBlocked: () => {
|
|
296
|
-
|
|
309
|
+
b((n) => n[e.origin] ? {
|
|
297
310
|
...n,
|
|
298
311
|
[e.origin]: { ...n[e.origin], popupBlocked: !0 }
|
|
299
312
|
} : n);
|
|
300
313
|
},
|
|
301
314
|
onClosed: () => {
|
|
302
315
|
if (e.v2) {
|
|
303
|
-
|
|
316
|
+
re && b((n) => !n[e.origin] || !n[e.origin].silentUrl ? n : {
|
|
304
317
|
...n,
|
|
305
318
|
[e.origin]: { ...n[e.origin], status: "silent" }
|
|
306
319
|
});
|
|
307
320
|
return;
|
|
308
321
|
}
|
|
309
|
-
|
|
322
|
+
m(e.origin);
|
|
310
323
|
}
|
|
311
324
|
});
|
|
312
325
|
},
|
|
313
|
-
[s,
|
|
314
|
-
),
|
|
326
|
+
[s, m]
|
|
327
|
+
), I = {
|
|
315
328
|
// Hosts render UI for interactive entries only; silent resolution is
|
|
316
329
|
// invisible by design.
|
|
317
|
-
pending: Object.values(
|
|
330
|
+
pending: Object.values(p).filter(
|
|
318
331
|
(e) => e.status === "interactive"
|
|
319
332
|
),
|
|
320
333
|
authorize: W,
|
|
321
|
-
approve:
|
|
334
|
+
approve: Y,
|
|
322
335
|
complete: B,
|
|
323
336
|
dismiss: j,
|
|
324
|
-
originEpochs:
|
|
337
|
+
originEpochs: Z,
|
|
325
338
|
credentials: $,
|
|
326
339
|
unsupported: q
|
|
327
|
-
}, D = Object.values(
|
|
340
|
+
}, D = Object.values(p).filter(
|
|
328
341
|
(e) => e.status === "silent" && e.silentUrl
|
|
329
342
|
);
|
|
330
|
-
return /* @__PURE__ */ x.createElement(
|
|
343
|
+
return /* @__PURE__ */ x.createElement(N.Provider, { value: I }, t, D.map((e) => /* @__PURE__ */ x.createElement(
|
|
331
344
|
"iframe",
|
|
332
345
|
{
|
|
333
346
|
key: `mm-silent-auth-${e.origin}`,
|
|
@@ -339,179 +352,180 @@ const re = ue(null), J = ue({
|
|
|
339
352
|
}
|
|
340
353
|
)));
|
|
341
354
|
};
|
|
342
|
-
|
|
343
|
-
children:
|
|
344
|
-
launcher:
|
|
355
|
+
fe.propTypes = {
|
|
356
|
+
children: k.node,
|
|
357
|
+
launcher: k.func,
|
|
345
358
|
/**
|
|
346
359
|
* `async (request, { approve }) => result` where request =
|
|
347
360
|
* `{ origin, clientId, scope, authUrl }` (origin is the zone-attested frame origin) and result
|
|
348
|
-
*
|
|
349
|
-
*
|
|
350
|
-
* `{
|
|
361
|
+
* is `{ status, body }`: the HTTP status and the authorization server's parsed JSON, kept
|
|
362
|
+
* apart so a body field can never shadow the status. Bodies: `{ credential, scope, client }`
|
|
363
|
+
* on 200, `{ consentRequired: true, client: { name, hostname }, stepUp }` on 409, `{ error,
|
|
364
|
+
* … }` otherwise. Provided only by hosts that are themselves first-party clients.
|
|
351
365
|
*/
|
|
352
|
-
broker:
|
|
366
|
+
broker: k.func
|
|
353
367
|
};
|
|
354
|
-
const
|
|
355
|
-
const { originEpochs: s } =
|
|
368
|
+
const je = () => V(N), we = (t) => {
|
|
369
|
+
const { originEpochs: s } = V(N), c = Q(t);
|
|
356
370
|
return c && s[c] || 0;
|
|
357
|
-
},
|
|
358
|
-
const { credentials: s } =
|
|
371
|
+
}, Ie = (t) => {
|
|
372
|
+
const { credentials: s } = V(N), c = Q(t);
|
|
359
373
|
return c && s[c] || null;
|
|
360
|
-
},
|
|
361
|
-
const { unsupported: s } =
|
|
374
|
+
}, _e = (t) => {
|
|
375
|
+
const { unsupported: s } = V(N), c = Q(t);
|
|
362
376
|
return c && s[c] || null;
|
|
363
|
-
},
|
|
364
|
-
const [d,
|
|
377
|
+
}, Ue = ({ children: t, authorizationLauncher: s, credentialBroker: c }) => {
|
|
378
|
+
const [d, p] = M({}), [b, Z] = M({}), w = g({}), $ = g({}), S = g({}), q = h((i, l) => {
|
|
365
379
|
var o;
|
|
366
|
-
return !!((o = S.current[i]) != null && o.has(
|
|
367
|
-
}, []),
|
|
368
|
-
|
|
369
|
-
}, []),
|
|
370
|
-
|
|
371
|
-
const o = { ...
|
|
380
|
+
return !!((o = S.current[i]) != null && o.has(l));
|
|
381
|
+
}, []), y = h((i, l, o) => {
|
|
382
|
+
p((a) => ({ ...a, [i]: { origin: l, frameRef: o } }));
|
|
383
|
+
}, []), G = h((i) => {
|
|
384
|
+
p((l) => {
|
|
385
|
+
const o = { ...l };
|
|
372
386
|
return delete o[i], o;
|
|
373
|
-
}),
|
|
374
|
-
const o = { ...
|
|
387
|
+
}), Z((l) => {
|
|
388
|
+
const o = { ...l };
|
|
375
389
|
return delete o[i], o;
|
|
376
390
|
}), delete S.current[i];
|
|
377
391
|
}, []);
|
|
378
|
-
|
|
379
|
-
const i = (o = [],
|
|
380
|
-
o.forEach((
|
|
381
|
-
},
|
|
382
|
-
var B,
|
|
392
|
+
R(() => {
|
|
393
|
+
const i = (o = [], a, f) => {
|
|
394
|
+
o.forEach((m) => m(a, f));
|
|
395
|
+
}, l = (o) => {
|
|
396
|
+
var B, O, U, L, K, Y, W;
|
|
383
397
|
if (!((B = o == null ? void 0 : o.data) != null && B.instanceId))
|
|
384
398
|
return;
|
|
385
|
-
const { instanceId:
|
|
386
|
-
if (!
|
|
399
|
+
const { instanceId: a, type: f } = o.data, m = d[a], A = (U = (O = m == null ? void 0 : m.frameRef) == null ? void 0 : O.current) == null ? void 0 : U.contentWindow;
|
|
400
|
+
if (!A || o.source !== A || o.origin !== m.origin)
|
|
387
401
|
return;
|
|
388
|
-
const j = { origin: o.origin },
|
|
402
|
+
const j = { origin: o.origin }, T = w.current;
|
|
389
403
|
if (f === "response") {
|
|
390
|
-
const { callbackId:
|
|
391
|
-
r && (r(D, e), delete $.current[
|
|
404
|
+
const { callbackId: I, response: D, error: e } = o.data, r = $.current[I];
|
|
405
|
+
r && (r(D, e), delete $.current[I]);
|
|
392
406
|
return;
|
|
393
407
|
}
|
|
394
408
|
if (f === "mark-registered") {
|
|
395
|
-
const { requestType:
|
|
396
|
-
S.current[
|
|
409
|
+
const { requestType: I } = o.data;
|
|
410
|
+
S.current[a] || (S.current[a] = /* @__PURE__ */ new Set()), S.current[a].add(I);
|
|
397
411
|
return;
|
|
398
412
|
}
|
|
399
|
-
f === "ready" &&
|
|
413
|
+
f === "ready" && Z((I) => ({ ...I, [a]: !0 })), i((L = T[a]) == null ? void 0 : L[f], o.data, j), i((K = T["*"]) == null ? void 0 : K[f], o.data, j), i((Y = T[a]) == null ? void 0 : Y["*"], o.data, j), i((W = T["*"]) == null ? void 0 : W["*"], o.data, j);
|
|
400
414
|
};
|
|
401
|
-
return window.addEventListener("message",
|
|
402
|
-
window.removeEventListener("message",
|
|
415
|
+
return window.addEventListener("message", l), () => {
|
|
416
|
+
window.removeEventListener("message", l);
|
|
403
417
|
};
|
|
404
418
|
}, [d]);
|
|
405
|
-
const
|
|
406
|
-
(i,
|
|
407
|
-
var f,
|
|
408
|
-
const
|
|
409
|
-
if (!
|
|
419
|
+
const E = h(
|
|
420
|
+
(i, l, o) => {
|
|
421
|
+
var f, m;
|
|
422
|
+
const a = d[i];
|
|
423
|
+
if (!a) {
|
|
410
424
|
console.warn(`Origin not set for id: ${i}. Unable to send message.`);
|
|
411
425
|
return;
|
|
412
426
|
}
|
|
413
|
-
(
|
|
414
|
-
{ type:
|
|
415
|
-
|
|
427
|
+
(m = (f = a == null ? void 0 : a.frameRef) == null ? void 0 : f.current) != null && m.contentWindow && a.frameRef.current.contentWindow.postMessage(
|
|
428
|
+
{ type: l, ...o, instanceId: i },
|
|
429
|
+
a.origin
|
|
416
430
|
);
|
|
417
431
|
},
|
|
418
432
|
[d]
|
|
419
|
-
), F =
|
|
420
|
-
async (i,
|
|
421
|
-
const
|
|
422
|
-
delete $.current[
|
|
433
|
+
), F = g(0), X = h(() => `${Date.now()}-${++F.current}`, []), C = h(
|
|
434
|
+
async (i, l, o = {}) => new Promise((a, f) => {
|
|
435
|
+
const m = X(), A = (j, T) => {
|
|
436
|
+
delete $.current[m], T ? f(T) : a(j);
|
|
423
437
|
};
|
|
424
|
-
$.current[
|
|
438
|
+
$.current[m] = A, E(i, "request", { callbackId: m, requestType: l, data: o });
|
|
425
439
|
}),
|
|
426
|
-
[
|
|
427
|
-
),
|
|
428
|
-
(i,
|
|
429
|
-
|
|
440
|
+
[E]
|
|
441
|
+
), v = h(
|
|
442
|
+
(i, l) => {
|
|
443
|
+
E(i, "load-options", l);
|
|
430
444
|
},
|
|
431
|
-
[
|
|
432
|
-
),
|
|
433
|
-
(i,
|
|
434
|
-
|
|
445
|
+
[E]
|
|
446
|
+
), P = h(
|
|
447
|
+
(i, l) => {
|
|
448
|
+
E(i, "validate", l);
|
|
435
449
|
},
|
|
436
|
-
[
|
|
437
|
-
),
|
|
450
|
+
[E]
|
|
451
|
+
), _ = me(
|
|
438
452
|
() => ({
|
|
439
|
-
registerFrame:
|
|
440
|
-
unregisterFrame:
|
|
441
|
-
sendMessage:
|
|
442
|
-
sendRequest:
|
|
443
|
-
loadOptions:
|
|
444
|
-
validate:
|
|
453
|
+
registerFrame: y,
|
|
454
|
+
unregisterFrame: G,
|
|
455
|
+
sendMessage: E,
|
|
456
|
+
sendRequest: C,
|
|
457
|
+
loadOptions: v,
|
|
458
|
+
validate: P,
|
|
445
459
|
hasHandler: q,
|
|
446
|
-
readyFrames:
|
|
447
|
-
addListener: (i,
|
|
448
|
-
|
|
460
|
+
readyFrames: b,
|
|
461
|
+
addListener: (i, l, o) => (w.current[i] || (w.current[i] = {}), w.current[i][l] || (w.current[i][l] = []), w.current[i][l].push(o), () => {
|
|
462
|
+
w.current[i][l] = w.current[i][l].filter((a) => a !== o);
|
|
449
463
|
})
|
|
450
464
|
}),
|
|
451
465
|
[
|
|
452
|
-
P,
|
|
453
|
-
Y,
|
|
454
|
-
R,
|
|
455
|
-
A,
|
|
456
|
-
Z,
|
|
457
466
|
y,
|
|
467
|
+
G,
|
|
468
|
+
E,
|
|
469
|
+
C,
|
|
470
|
+
v,
|
|
471
|
+
P,
|
|
458
472
|
q,
|
|
459
|
-
|
|
473
|
+
b
|
|
460
474
|
]
|
|
461
475
|
);
|
|
462
|
-
return /* @__PURE__ */ x.createElement(
|
|
463
|
-
},
|
|
464
|
-
const d =
|
|
476
|
+
return /* @__PURE__ */ x.createElement(ne.Provider, { value: _ }, /* @__PURE__ */ x.createElement(fe, { launcher: s, broker: c }, t));
|
|
477
|
+
}, J = (t, s, c) => {
|
|
478
|
+
const d = V(ne);
|
|
465
479
|
if (d === null)
|
|
466
480
|
throw new Error(
|
|
467
481
|
"useEmbeddedEffect must be used within a MMEmbeddableZoneProvider"
|
|
468
482
|
);
|
|
469
|
-
const
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
}, [c]),
|
|
483
|
+
const p = g(c);
|
|
484
|
+
R(() => {
|
|
485
|
+
p.current = c;
|
|
486
|
+
}, [c]), R(() => {
|
|
473
487
|
if (!(!t || !s)) {
|
|
474
488
|
if (s.startsWith("key:")) {
|
|
475
|
-
const
|
|
476
|
-
return d.readyFrames[t] && d.sendMessage(t, "subscribe-key", { keyChord:
|
|
489
|
+
const b = s.substring(4);
|
|
490
|
+
return d.readyFrames[t] && d.sendMessage(t, "subscribe-key", { keyChord: b }), d.addListener(
|
|
477
491
|
t,
|
|
478
|
-
`key-pressed:${
|
|
479
|
-
(...
|
|
492
|
+
`key-pressed:${b}`,
|
|
493
|
+
(...w) => p.current(...w)
|
|
480
494
|
);
|
|
481
495
|
}
|
|
482
496
|
return d.addListener(
|
|
483
497
|
t,
|
|
484
498
|
s,
|
|
485
|
-
(...
|
|
499
|
+
(...b) => p.current(...b)
|
|
486
500
|
);
|
|
487
501
|
}
|
|
488
502
|
}, [d, t, s]);
|
|
489
503
|
};
|
|
490
|
-
|
|
491
|
-
children:
|
|
504
|
+
Ue.propTypes = {
|
|
505
|
+
children: k.node.isRequired,
|
|
492
506
|
/**
|
|
493
507
|
* Optional interactive-step launcher for embedded authorizations,
|
|
494
508
|
* `(entry, { onBlocked, onClosed }) => cleanup`. Defaults to a top-level
|
|
495
509
|
* popup. Hosts that cannot open windows (Android WebView shells) drive
|
|
496
510
|
* their own UI from useEmbeddedAuthorizations() instead.
|
|
497
511
|
*/
|
|
498
|
-
authorizationLauncher:
|
|
512
|
+
authorizationLauncher: k.func,
|
|
499
513
|
/**
|
|
500
514
|
* Optional credential broker for hosts that are first-party MachineMetrics clients
|
|
501
515
|
* (`async (request, { approve }) => result`, see EmbeddedAuthorizationManager). When present,
|
|
502
516
|
* v2 credential requests are brokered server-side (no popup, no hidden frame) and consent is
|
|
503
517
|
* rendered natively by the host from the broker's `client` identity.
|
|
504
518
|
*/
|
|
505
|
-
credentialBroker:
|
|
519
|
+
credentialBroker: k.func
|
|
506
520
|
};
|
|
507
|
-
const
|
|
521
|
+
const se = ie.default || ie, Le = 1e4, Me = "credentials", Ce = (t, s) => `${t}/mm-embedded-interop?id=${encodeURIComponent(s)}&origin=${encodeURIComponent(
|
|
508
522
|
window.location.origin
|
|
509
|
-
)}&caps=${
|
|
523
|
+
)}&caps=${Me}`, Ae = se.iframe`
|
|
510
524
|
width: 100%;
|
|
511
525
|
height: 100%;
|
|
512
526
|
border: none;
|
|
513
527
|
visibility: ${(t) => t.$isReady ? "visible" : "hidden"};
|
|
514
|
-
`,
|
|
528
|
+
`, Oe = se.div`
|
|
515
529
|
width: 100%;
|
|
516
530
|
height: 100%;
|
|
517
531
|
display: flex;
|
|
@@ -524,7 +538,7 @@ const ne = se.default || se, _e = 1e4, Ue = "credentials", Le = (t, s) => `${t}/
|
|
|
524
538
|
text-align: center;
|
|
525
539
|
color: #555;
|
|
526
540
|
font-size: 14px;
|
|
527
|
-
`,
|
|
541
|
+
`, ze = (t) => {
|
|
528
542
|
const s = t && t.origin || "This app", c = t && t.reason || "";
|
|
529
543
|
switch (c) {
|
|
530
544
|
case "broker:scope_above_ceiling":
|
|
@@ -552,34 +566,34 @@ const ne = se.default || se, _e = 1e4, Ue = "credentials", Le = (t, s) => `${t}/
|
|
|
552
566
|
detail: "It needs an update to support embedding from a different domain."
|
|
553
567
|
};
|
|
554
568
|
}
|
|
555
|
-
},
|
|
556
|
-
const s =
|
|
557
|
-
return /* @__PURE__ */ x.createElement(
|
|
569
|
+
}, pe = ({ entry: t }) => {
|
|
570
|
+
const s = ze(t);
|
|
571
|
+
return /* @__PURE__ */ x.createElement(Oe, { "data-reason": t && t.reason || void 0 }, /* @__PURE__ */ x.createElement("div", { style: { fontWeight: 600 } }, s.title), /* @__PURE__ */ x.createElement("div", { style: { fontSize: 12, color: "#888" } }, s.detail));
|
|
558
572
|
};
|
|
559
|
-
|
|
560
|
-
entry:
|
|
561
|
-
origin:
|
|
562
|
-
name:
|
|
563
|
-
reason:
|
|
564
|
-
detail:
|
|
573
|
+
pe.propTypes = {
|
|
574
|
+
entry: k.shape({
|
|
575
|
+
origin: k.string,
|
|
576
|
+
name: k.string,
|
|
577
|
+
reason: k.string,
|
|
578
|
+
detail: k.object
|
|
565
579
|
})
|
|
566
580
|
};
|
|
567
|
-
const
|
|
581
|
+
const xe = se.div`
|
|
568
582
|
height: 100%;
|
|
569
583
|
display: flex;
|
|
570
584
|
flex: 1;
|
|
571
585
|
align-items: center;
|
|
572
586
|
justify-content: center;
|
|
573
|
-
`,
|
|
587
|
+
`, $e = () => /* @__PURE__ */ x.createElement(xe, null, /* @__PURE__ */ x.createElement("div", { style: { textAlign: "center" } }, "This app couldn’t be loaded as an embeddable widget.")), Se = ({
|
|
574
588
|
id: t,
|
|
575
589
|
src: s,
|
|
576
590
|
path: c,
|
|
577
591
|
onInit: d = () => {
|
|
578
592
|
},
|
|
579
|
-
onTimeout:
|
|
580
|
-
context:
|
|
581
|
-
params:
|
|
582
|
-
renderUnsupported:
|
|
593
|
+
onTimeout: p,
|
|
594
|
+
context: b = {},
|
|
595
|
+
params: Z = {},
|
|
596
|
+
renderUnsupported: w,
|
|
583
597
|
renderTimeout: $,
|
|
584
598
|
...S
|
|
585
599
|
}) => {
|
|
@@ -587,119 +601,119 @@ const Oe = ne.div`
|
|
|
587
601
|
return console.error(
|
|
588
602
|
"isConfiguring must be passed via the params prop to MMEmbeddableZone"
|
|
589
603
|
), null;
|
|
590
|
-
if ("colorMode" in
|
|
604
|
+
if ("colorMode" in b)
|
|
591
605
|
return console.error(
|
|
592
606
|
"colorMode must be passed via the params prop to MMEmbeddableZone"
|
|
593
607
|
), null;
|
|
594
|
-
if ("isFullScreen" in
|
|
608
|
+
if ("isFullScreen" in b)
|
|
595
609
|
return console.error(
|
|
596
610
|
"isFullScreen must be passed via the params prop to MMEmbeddableZone"
|
|
597
611
|
), null;
|
|
598
|
-
const q =
|
|
599
|
-
|
|
600
|
-
), [B,
|
|
601
|
-
|
|
612
|
+
const q = g(), [y, G] = M(), [E, F] = M(0), [X, C] = M(!1), [v, P] = M(!1), _ = we(s), i = Ie(s), l = _e(s), [o, a] = M(0), [f, m] = M(null), { registerFrame: A, unregisterFrame: j, sendMessage: T } = V(
|
|
613
|
+
ne
|
|
614
|
+
), [B, O] = M(), U = g(), L = g(p), K = g(d), Y = g(!1), W = h(() => {
|
|
615
|
+
Y.current = !1, clearTimeout(U.current), F(0), C(!1), P(!1), m(null);
|
|
602
616
|
}, []);
|
|
603
|
-
|
|
604
|
-
(
|
|
605
|
-
}, [
|
|
606
|
-
|
|
607
|
-
}, [
|
|
608
|
-
|
|
609
|
-
}, [d]),
|
|
610
|
-
|
|
617
|
+
R(() => {
|
|
618
|
+
(_ > 0 || o > 0) && W();
|
|
619
|
+
}, [_, o, W]), R(() => {
|
|
620
|
+
L.current = p;
|
|
621
|
+
}, [p]), R(() => {
|
|
622
|
+
K.current = d;
|
|
623
|
+
}, [d]), R(() => {
|
|
624
|
+
E && T(t, "set-context", {
|
|
611
625
|
context: {
|
|
612
|
-
...
|
|
626
|
+
...b
|
|
613
627
|
}
|
|
614
628
|
});
|
|
615
|
-
}, [
|
|
616
|
-
|
|
617
|
-
}, [
|
|
618
|
-
c !== void 0 &&
|
|
619
|
-
}, [c]),
|
|
620
|
-
B === void 0 || !
|
|
621
|
-
}, [B,
|
|
629
|
+
}, [b, t, E]), R(() => {
|
|
630
|
+
E && T(t, "set-params", { params: Z });
|
|
631
|
+
}, [Z, t, E]), R(() => {
|
|
632
|
+
c !== void 0 && O((c == null ? void 0 : c.indexOf("/")) === 0 ? c : `/${c}`);
|
|
633
|
+
}, [c]), R(() => {
|
|
634
|
+
B === void 0 || !E || T(t, "navigate", { path: B });
|
|
635
|
+
}, [B, E, t, T]), R(() => {
|
|
622
636
|
if (!s || !t || !q)
|
|
623
637
|
return;
|
|
624
638
|
W();
|
|
625
639
|
const e = new URL(s);
|
|
626
|
-
return
|
|
640
|
+
return A(t, e.origin, q), K.current(), G(Ce(e.origin, t)), () => {
|
|
627
641
|
j(t);
|
|
628
642
|
};
|
|
629
|
-
}, [s, t,
|
|
630
|
-
if (!(!
|
|
631
|
-
return
|
|
632
|
-
|
|
633
|
-
},
|
|
634
|
-
}, [
|
|
635
|
-
const
|
|
636
|
-
|
|
643
|
+
}, [s, t, A, q, W]), R(() => {
|
|
644
|
+
if (!(!y || Y.current))
|
|
645
|
+
return U.current = setTimeout(() => {
|
|
646
|
+
C(!0), L.current && L.current();
|
|
647
|
+
}, Le), () => clearTimeout(U.current);
|
|
648
|
+
}, [y, s, _, o]);
|
|
649
|
+
const I = h(() => {
|
|
650
|
+
Y.current = !0, clearTimeout(U.current), C(!1);
|
|
637
651
|
}, []);
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
}),
|
|
641
|
-
|
|
642
|
-
}),
|
|
643
|
-
if (!
|
|
652
|
+
J(t, "init", (e) => {
|
|
653
|
+
I(), Array.isArray(e == null ? void 0 : e.acceptsCredentials) && m(e.acceptsCredentials);
|
|
654
|
+
}), J(t, "authorize", I), J(t, "login", I), J(t, "credential-request", () => {
|
|
655
|
+
I(), P(!0);
|
|
656
|
+
}), R(() => {
|
|
657
|
+
if (!v || !i)
|
|
644
658
|
return;
|
|
645
659
|
const e = f;
|
|
646
660
|
if (Array.isArray(e) && !e.includes(i.kind))
|
|
647
661
|
return;
|
|
648
662
|
const r = i.kind === "access_token" ? { accessToken: i.accessToken } : {};
|
|
649
|
-
|
|
650
|
-
}, [
|
|
651
|
-
|
|
663
|
+
T(t, "credential", { ...r, credential: i });
|
|
664
|
+
}, [v, i, f, t, T]), J(t, "ready", () => {
|
|
665
|
+
I(), F((e) => e + 1);
|
|
652
666
|
});
|
|
653
|
-
const D =
|
|
654
|
-
return
|
|
667
|
+
const D = g();
|
|
668
|
+
return J(t, "heartbeat", () => {
|
|
655
669
|
D.current && clearTimeout(D.current), D.current = setTimeout(() => {
|
|
656
|
-
|
|
670
|
+
a((e) => e + 1);
|
|
657
671
|
}, 6e3);
|
|
658
|
-
}),
|
|
672
|
+
}), R(() => () => {
|
|
659
673
|
D.current && clearTimeout(D.current);
|
|
660
|
-
}, []),
|
|
661
|
-
|
|
674
|
+
}, []), l ? w ? w(l) : /* @__PURE__ */ x.createElement(pe, { entry: l }) : X && !E ? $ ? $() : /* @__PURE__ */ x.createElement($e, null) : /* @__PURE__ */ x.createElement(
|
|
675
|
+
Ae,
|
|
662
676
|
{
|
|
663
|
-
key: `auth-epoch-${
|
|
664
|
-
$isReady:
|
|
677
|
+
key: `auth-epoch-${_}-frame-${o}`,
|
|
678
|
+
$isReady: E,
|
|
665
679
|
ref: q,
|
|
666
|
-
src:
|
|
680
|
+
src: y,
|
|
667
681
|
...S
|
|
668
682
|
}
|
|
669
683
|
);
|
|
670
684
|
};
|
|
671
|
-
|
|
672
|
-
id:
|
|
673
|
-
src:
|
|
674
|
-
path:
|
|
675
|
-
onInit:
|
|
676
|
-
onTimeout:
|
|
685
|
+
Se.propTypes = {
|
|
686
|
+
id: k.string,
|
|
687
|
+
src: k.string.isRequired,
|
|
688
|
+
path: k.string,
|
|
689
|
+
onInit: k.func,
|
|
690
|
+
onTimeout: k.func,
|
|
677
691
|
/**
|
|
678
692
|
* Optional host-styled renderer for the structurally-unsupported embed
|
|
679
693
|
* state (legacy app on a cross-site origin), `(entry) => ReactNode`. Falls
|
|
680
694
|
* back to a minimal built-in placeholder.
|
|
681
695
|
*/
|
|
682
|
-
renderUnsupported:
|
|
696
|
+
renderUnsupported: k.func,
|
|
683
697
|
/**
|
|
684
698
|
* Optional host-styled renderer for the handshake-timeout failure state
|
|
685
699
|
* (the app is not embeddable / could not load), `() => ReactNode`. Falls
|
|
686
700
|
* back to a minimal centered message.
|
|
687
701
|
*/
|
|
688
|
-
renderTimeout:
|
|
702
|
+
renderTimeout: k.func
|
|
689
703
|
};
|
|
690
704
|
export {
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
+
ge as CREDENTIAL_KIND_ACCESS_TOKEN,
|
|
706
|
+
N as EmbeddedAuthorizationContext,
|
|
707
|
+
Se as MMEmbeddableZone,
|
|
708
|
+
ne as MMEmbeddableZoneContext,
|
|
709
|
+
Ue as MMEmbeddableZoneProvider,
|
|
710
|
+
ae as isCrossSite,
|
|
711
|
+
ce as normalizeCredential,
|
|
712
|
+
ke as popupAuthorizationLauncher,
|
|
713
|
+
ze as unsupportedCopy,
|
|
714
|
+
je as useEmbeddedAuthorizations,
|
|
715
|
+
Ie as useEmbeddedCredential,
|
|
716
|
+
J as useEmbeddedEffect,
|
|
717
|
+
we as useEmbeddedOriginEpoch,
|
|
718
|
+
_e as useEmbeddedUnsupported
|
|
705
719
|
};
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
(function(h,t){typeof exports=="object"&&typeof module!="undefined"?t(exports,require("react"),require("prop-types"),require("styled-components")):typeof define=="function"&&define.amd?define(["exports","react","prop-types","styled-components"],t):(h=typeof globalThis!="undefined"?globalThis:h||self,t(h["mm-react-embeddable"]={},h.React,h.PropTypes,h.styled))})(this,(function(h,t,Ee,ge){"use strict";const ee=r=>r&&r.__esModule?r:{default:r},U=ee(t),C=ee(Ee),ne=ee(ge),V=t.createContext(null),Y=t.createContext({pending:[],authorize:()=>{},approve:()=>{},complete:()=>{},dismiss:()=>{},originEpochs:{},credentials:{},unsupported:{}}),se="access_token",te=r=>r?r.kind?r:r.accessToken?{kind:se,accessToken:r.accessToken,expiresAt:r.expiresAt||null}:null:null,ke=60,Ce=120,Te=new Set(["host_not_allowed","unbound_host_bound_guest","invalid_token","provider_unavailable"]),J=typeof window!="undefined"&&window.location.protocol==="https:",_e=8e3,X=r=>{try{return new URL(r).origin}catch{return null}},ue=(r,u)=>{const c=u==null?void 0:u.origin;return!c||X(r==null?void 0:r.authUrl)!==c?null:c},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(".")},ie=(r,u)=>{try{const c=new URL(r),f=new URL(u);return c.protocol!==f.protocol?!0:oe(c.hostname)!==oe(f.hostname)}catch{return!0}},ce=(r,{onBlocked:u,onClosed:c})=>{const f=window.open(r.authUrl,"mm-embed-auth","popup,width=600,height=700");if(!f)return u(),()=>{};const m=setInterval(()=>{f.closed&&(clearInterval(m),c())},1e3);return()=>clearInterval(m)},le=({children:r,launcher:u,broker:c})=>{const f=t.useContext(V),[m,g]=t.useState({}),[Z,_]=t.useState({}),[O,y]=t.useState({}),[q,x]=t.useState({}),N=t.useRef(O);N.current=O;const k=t.useRef(m);k.current=m;const $=t.useRef(null),G=t.useRef({}),L=t.useRef({}),W=t.useRef({}),F=t.useRef(c);F.current=c;const I=t.useCallback(e=>{const n=L.current[e];if(!n)return;const s=l=>!l[e]||l[e].clientId?l:{...l,[e]:{...l[e],clientId:n}};g(s),x(s)},[]),i=t.useCallback((e,n)=>{e&&(x(s=>s[e]?s:{...s,[e]:{origin:e,reason:n,clientId:L.current[e]||null}}),I(e))},[I]),a=t.useCallback((e,{v2:n=!1,origin:s=null}={})=>{s&&(n&&N.current[s]||(g(l=>l[s]?l:{...l,[s]:{origin:s,authUrl:e.authUrl,silentUrl:e.silentUrl||null,status:n&&F.current?"broker":n&&e.silentUrl&&J?"silent":"interactive",v2:n,scope:e.scope||null,acceptsCredentials:W.current[s]||null,consent:null,error:null,clientId:e.clientId||L.current[s]||null,popupBlocked:!1}}),I(s)))},[I]),o=t.useRef({}),d=t.useCallback(e=>{o.current[e]&&(clearTimeout(o.current[e]),delete o.current[e])},[]),p=t.useCallback(e=>{d(e),g(n=>{if(!n[e])return n;const s={...n};return delete s[e],s})},[d]),E=t.useCallback(e=>{e&&(p(e),_(n=>({...n,[e]:(n[e]||0)+1})))},[p]),A=t.useCallback(e=>{g(n=>!n[e]||n[e].status==="interactive"?n:{...n,[e]:{...n[e],status:"interactive"}})},[]),P=t.useCallback(e=>{p(e.origin)},[p]),T=t.useCallback((e,n)=>{const s=ue(e,n);if(s){if(ie(s,window.location.origin)){i(s,"cross-site-legacy");return}a(e,{origin:s})}},[a,i]);t.useEffect(()=>{if(!f)return;const e=[f.addListener("*","init",(n,s)=>{s!=null&&s.origin&&(n!=null&&n.clientId)&&(L.current[s.origin]=n.clientId,I(s.origin)),s!=null&&s.origin&&Array.isArray(n==null?void 0:n.acceptsCredentials)&&(W.current[s.origin]=n.acceptsCredentials)}),f.addListener("*","authorize",T),f.addListener("*","login",T),f.addListener("*","credential-request",(n,s)=>a(n,{v2:!0,origin:ue(n,s)}))];return()=>e.forEach(n=>n())},[f,T,a,I]),t.useEffect(()=>{const e=n=>{const s=n==null?void 0:n.data;if(!s||s.type!=="authorized"&&s.type!=="authorize-failed")return;const l=X(s.authUrl);if(!(!l||n.origin!==l)){if(s.type==="authorized"){if(s.accessToken){if(!k.current[l])return;y(b=>({...b,[l]:te({accessToken:s.accessToken})})),p(l)}else E(l);return}A(l)}};return window.addEventListener("message",e),()=>window.removeEventListener("message",e)},[p,E,A]);const j=t.useCallback((e,n)=>{const s=e==null?void 0:e.origin,l=te(n);!s||!l||(y(b=>({...b,[s]:l})),p(s))},[p]),z=t.useCallback((e,n)=>{g(s=>s[e]?{...s,[e]:{...s[e],...n}}:s)},[]),M=t.useRef({}),S=t.useCallback(async(e,{approve:n}={})=>{const s=F.current,{origin:l}=e;let b;try{b=await s({origin:l,clientId:e.clientId,scope:e.scope,authUrl:e.authUrl},{approve:n})}catch{n?z(l,{error:{reason:"broker_unavailable"}}):z(l,{status:e.silentUrl&&J?"silent":"interactive"});return}if((b==null?void 0:b.status)===200&&b.credential){j(e,b.credential);return}if((b==null?void 0:b.status)===409&&b.consentRequired){z(l,{status:"interactive",consent:{client:b.client||null,stepUp:!!b.stepUp,scope:b.scope||e.scope},error:null});return}const Q=(b==null?void 0:b.error)||"broker_error";if(!n&&Te.has(Q)){z(l,{status:e.silentUrl&&J?"silent":"interactive"});return}if(n&&Q==="consent_denied"){z(l,{error:null});return}if(Q==="rate_limited"&&!e.rateLimitRetried){const R=Math.min(Number(b==null?void 0:b.retryAfter)>0?Number(b.retryAfter):ke,Ce),Oe={...e,rateLimitRetried:!0};d(l),o.current[l]=setTimeout(()=>{delete o.current[l],M.current[l]=!0,v.current(Oe,{approve:n}).finally(()=>{delete M.current[l]})},R*1e3),z(l,{rateLimitRetried:!0});return}p(l),x(R=>({...R,[l]:{origin:l,reason:`broker:${Q}`,detail:b||null,clientId:e.clientId||L.current[l]||null}}))},[j,z,p,d]),v=t.useRef(S);v.current=S,t.useEffect(()=>{Object.values(m).forEach(e=>{e.status!=="broker"||M.current[e.origin]||o.current[e.origin]||(M.current[e.origin]=!0,S(e).finally(()=>{delete M.current[e.origin]}))})},[m,S]);const H=t.useCallback(e=>!F.current||!(e!=null&&e.consent)?Promise.resolve():S(e,{approve:!0}),[S]);t.useEffect(()=>{const e=G.current;Object.values(m).forEach(n=>{n.status==="silent"&&!e[n.origin]&&(e[n.origin]=setTimeout(()=>{delete e[n.origin],A(n.origin)},_e))}),Object.keys(e).forEach(n=>{(!m[n]||m[n].status!=="silent")&&(clearTimeout(e[n]),delete e[n])})},[m,A]),t.useEffect(()=>()=>{$.current&&$.current(),Object.values(G.current).forEach(clearTimeout),Object.values(o.current).forEach(clearTimeout)},[]);const B=t.useCallback(e=>{$.current&&($.current(),$.current=null);const n=u||ce;$.current=n(e,{onBlocked:()=>{g(s=>s[e.origin]?{...s,[e.origin]:{...s[e.origin],popupBlocked:!0}}:s)},onClosed:()=>{if(e.v2){J&&g(s=>!s[e.origin]||!s[e.origin].silentUrl?s:{...s,[e.origin]:{...s[e.origin],status:"silent"}});return}E(e.origin)}})},[u,E]),w={pending:Object.values(m).filter(e=>e.status==="interactive"),authorize:B,approve:H,complete:j,dismiss:P,originEpochs:Z,credentials:O,unsupported:q},D=Object.values(m).filter(e=>e.status==="silent"&&e.silentUrl);return U.default.createElement(Y.Provider,{value:w},r,D.map(e=>U.default.createElement("iframe",{key:`mm-silent-auth-${e.origin}`,src:e.silentUrl,title:`MachineMetrics silent authorization (${e.origin})`,"aria-hidden":"true",tabIndex:-1,style:{display:"none"}})))};le.propTypes={children:C.default.node,launcher:C.default.func,broker:C.default.func};const we=()=>t.useContext(Y),ae=r=>{const{originEpochs:u}=t.useContext(Y),c=X(r);return c&&u[c]||0},de=r=>{const{credentials:u}=t.useContext(Y),c=X(r);return c&&u[c]||null},fe=r=>{const{unsupported:u}=t.useContext(Y),c=X(r);return c&&u[c]||null},pe=({children:r,authorizationLauncher:u,credentialBroker:c})=>{const[f,m]=t.useState({}),[g,Z]=t.useState({}),_=t.useRef({}),O=t.useRef({}),y=t.useRef({}),q=t.useCallback((i,a)=>{var o;return!!((o=y.current[i])!=null&&o.has(a))},[]),x=t.useCallback((i,a,o)=>{m(d=>({...d,[i]:{origin:a,frameRef:o}}))},[]),N=t.useCallback(i=>{m(a=>{const o={...a};return delete o[i],o}),Z(a=>{const o={...a};return delete o[i],o}),delete y.current[i]},[]);t.useEffect(()=>{const i=(o=[],d,p)=>{o.forEach(E=>E(d,p))},a=o=>{var j,z,M,S,v,H,B;if(!((j=o==null?void 0:o.data)!=null&&j.instanceId))return;const{instanceId:d,type:p}=o.data,E=f[d],A=(M=(z=E==null?void 0:E.frameRef)==null?void 0:z.current)==null?void 0:M.contentWindow;if(!A||o.source!==A||o.origin!==E.origin)return;const P={origin:o.origin},T=_.current;if(p==="response"){const{callbackId:w,response:D,error:e}=o.data,n=O.current[w];n&&(n(D,e),delete O.current[w]);return}if(p==="mark-registered"){const{requestType:w}=o.data;y.current[d]||(y.current[d]=new Set),y.current[d].add(w);return}p==="ready"&&Z(w=>({...w,[d]:!0})),i((S=T[d])==null?void 0:S[p],o.data,P),i((v=T["*"])==null?void 0:v[p],o.data,P),i((H=T[d])==null?void 0:H["*"],o.data,P),i((B=T["*"])==null?void 0:B["*"],o.data,P)};return window.addEventListener("message",a),()=>{window.removeEventListener("message",a)}},[f]);const k=t.useCallback((i,a,o)=>{var p,E;const d=f[i];if(!d){console.warn(`Origin not set for id: ${i}. Unable to send message.`);return}(E=(p=d==null?void 0:d.frameRef)==null?void 0:p.current)!=null&&E.contentWindow&&d.frameRef.current.contentWindow.postMessage({type:a,...o,instanceId:i},d.origin)},[f]),$=t.useRef(0),G=t.useCallback(()=>`${Date.now()}-${++$.current}`,[]),L=t.useCallback(async(i,a,o={})=>new Promise((d,p)=>{const E=G(),A=(P,T)=>{delete O.current[E],T?p(T):d(P)};O.current[E]=A,k(i,"request",{callbackId:E,requestType:a,data:o})}),[k]),W=t.useCallback((i,a)=>{k(i,"load-options",a)},[k]),F=t.useCallback((i,a)=>{k(i,"validate",a)},[k]),I=t.useMemo(()=>({registerFrame:x,unregisterFrame:N,sendMessage:k,sendRequest:L,loadOptions:W,validate:F,hasHandler:q,readyFrames:g,addListener:(i,a,o)=>(_.current[i]||(_.current[i]={}),_.current[i][a]||(_.current[i][a]=[]),_.current[i][a].push(o),()=>{_.current[i][a]=_.current[i][a].filter(d=>d!==o)})}),[x,N,k,L,W,F,q,g]);return U.default.createElement(V.Provider,{value:I},U.default.createElement(le,{launcher:u,broker:c},r))},K=(r,u,c)=>{const f=t.useContext(V);if(f===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const m=t.useRef(c);t.useEffect(()=>{m.current=c},[c]),t.useEffect(()=>{if(!(!r||!u)){if(u.startsWith("key:")){const g=u.substring(4);return f.readyFrames[r]&&f.sendMessage(r,"subscribe-key",{keyChord:g}),f.addListener(r,`key-pressed:${g}`,(..._)=>m.current(..._))}return f.addListener(r,u,(...g)=>m.current(...g))}},[f,r,u])};pe.propTypes={children:C.default.node.isRequired,authorizationLauncher:C.default.func,credentialBroker:C.default.func};const re=ne.default.default||ne.default,Ie=1e4,Me="credentials",Se=(r,u)=>`${r}/mm-embedded-interop?id=${encodeURIComponent(u)}&origin=${encodeURIComponent(window.location.origin)}&caps=${Me}`,Ue=re.iframe`
|
|
1
|
+
(function(m,t){typeof exports=="object"&&typeof module!="undefined"?t(exports,require("react"),require("prop-types"),require("styled-components")):typeof define=="function"&&define.amd?define(["exports","react","prop-types","styled-components"],t):(m=typeof globalThis!="undefined"?globalThis:m||self,t(m["mm-react-embeddable"]={},m.React,m.PropTypes,m.styled))})(this,(function(m,t,ke,Ce){"use strict";const te=r=>r&&r.__esModule?r:{default:r},S=te(t),k=te(ke),ue=te(Ce),X=t.createContext(null),G=t.createContext({pending:[],authorize:()=>{},approve:()=>{},complete:()=>{},dismiss:()=>{},originEpochs:{},credentials:{},unsupported:{}}),oe="access_token",re=r=>r?r.kind?r:r.accessToken?{kind:oe,accessToken:r.accessToken,expiresAt:r.expiresAt||null}:null:null,Te=60,_e=120,we=new Set(["host_not_allowed","unbound_host_bound_guest","invalid_token","provider_unavailable"]),Q=typeof window!="undefined"&&window.location.protocol==="https:",Ie=8e3,J=r=>{try{return new URL(r).origin}catch{return null}},ie=(r,u)=>{const c=u==null?void 0:u.origin;return!c||J(r==null?void 0:r.authUrl)!==c?null:c},ce=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(".")},ne=(r,u)=>{try{const c=new URL(r),f=new URL(u);return c.protocol!==f.protocol?!0:ce(c.hostname)!==ce(f.hostname)}catch{return!0}},le=(r,{onBlocked:u,onClosed:c})=>{const f=window.open(r.authUrl,"mm-embed-auth","popup,width=600,height=700");if(!f)return u(),()=>{};const b=setInterval(()=>{f.closed&&(clearInterval(b),c())},1e3);return()=>clearInterval(b)},ae=({children:r,launcher:u,broker:c})=>{const f=t.useContext(X),[b,E]=t.useState({}),[W,T]=t.useState({}),[O,y]=t.useState({}),[q,x]=t.useState({}),Y=t.useRef(O);Y.current=O;const g=t.useRef(b);g.current=b;const $=t.useRef(null),V=t.useRef({}),U=t.useRef({}),v=t.useRef({}),F=t.useRef(c);F.current=c;const w=t.useCallback(e=>{const n=U.current[e];if(!n)return;const s=l=>!l[e]||l[e].clientId?l:{...l,[e]:{...l[e],clientId:n}};E(s),x(s)},[]),i=t.useCallback((e,n)=>{e&&(x(s=>s[e]?s:{...s,[e]:{origin:e,reason:n,clientId:U.current[e]||null}}),w(e))},[w]),a=t.useCallback((e,{v2:n=!1,origin:s=null}={})=>{s&&(n&&Y.current[s]||(E(l=>l[s]?l:{...l,[s]:{origin:s,authUrl:e.authUrl,silentUrl:e.silentUrl||null,status:n&&F.current?"broker":n&&e.silentUrl&&Q?"silent":"interactive",v2:n,scope:e.scope||null,acceptsCredentials:v.current[s]||null,consent:null,error:null,clientId:e.clientId||U.current[s]||null,popupBlocked:!1}}),w(s)))},[w]),o=t.useRef({}),d=t.useCallback(e=>{o.current[e]&&(clearTimeout(o.current[e]),delete o.current[e])},[]),p=t.useCallback(e=>{d(e),E(n=>{if(!n[e])return n;const s={...n};return delete s[e],s})},[d]),h=t.useCallback(e=>{e&&(p(e),T(n=>({...n,[e]:(n[e]||0)+1})))},[p]),L=t.useCallback(e=>{E(n=>!n[e]||n[e].status==="interactive"?n:{...n,[e]:{...n[e],status:"interactive"}})},[]),P=t.useCallback(e=>{p(e.origin)},[p]),C=t.useCallback((e,n)=>{const s=ie(e,n);if(s){if(ne(s,window.location.origin)){i(s,"cross-site-legacy");return}a(e,{origin:s})}},[a,i]);t.useEffect(()=>{if(!f)return;const e=[f.addListener("*","init",(n,s)=>{s!=null&&s.origin&&(n!=null&&n.clientId)&&(U.current[s.origin]=n.clientId,w(s.origin)),s!=null&&s.origin&&Array.isArray(n==null?void 0:n.acceptsCredentials)?v.current[s.origin]=n.acceptsCredentials:s!=null&&s.origin&&ne(s.origin,window.location.origin)&&i(s.origin,"cross-site-legacy")}),f.addListener("*","authorize",C),f.addListener("*","login",C),f.addListener("*","credential-request",(n,s)=>a(n,{v2:!0,origin:ie(n,s)}))];return()=>e.forEach(n=>n())},[f,C,a,w,i]),t.useEffect(()=>{const e=n=>{const s=n==null?void 0:n.data;if(!s||s.type!=="authorized"&&s.type!=="authorize-failed")return;const l=J(s.authUrl);if(!(!l||n.origin!==l)){if(s.type==="authorized"){if(s.accessToken){if(!g.current[l])return;y(z=>({...z,[l]:re({accessToken:s.accessToken})})),p(l)}else h(l);return}L(l)}};return window.addEventListener("message",e),()=>window.removeEventListener("message",e)},[p,h,L]);const j=t.useCallback((e,n)=>{const s=e==null?void 0:e.origin,l=re(n);!s||!l||(y(z=>({...z,[s]:l})),p(s))},[p]),A=t.useCallback((e,n)=>{E(s=>s[e]?{...s,[e]:{...s[e],...n}}:s)},[]),I=t.useRef({}),M=t.useCallback(async(e,{approve:n}={})=>{const s=F.current,{origin:l}=e;let z;try{z=await s({origin:l,clientId:e.clientId,scope:e.scope,authUrl:e.authUrl},{approve:n})}catch{n?A(l,{error:{reason:"broker_unavailable"}}):A(l,{status:e.silentUrl&&Q?"silent":"interactive"});return}const ge=z==null?void 0:z.status,Z=(z==null?void 0:z.body)||{};if(ge===200&&Z.credential){j(e,Z.credential);return}if(ge===409&&Z.consentRequired){A(l,{status:"interactive",consent:{client:Z.client||null,stepUp:!!Z.stepUp,scope:Z.scope||e.scope},error:null});return}const R=Z.error||"broker_error";if(!n&&we.has(R)){A(l,{status:e.silentUrl&&Q?"silent":"interactive"});return}if(n&&R==="consent_denied"){A(l,{error:null});return}if(R==="rate_limited"&&!e.rateLimitRetried){const ee=Math.min(Number(Z.retryAfter)>0?Number(Z.retryAfter):Te,_e),$e={...e,rateLimitRetried:!0};d(l),o.current[l]=setTimeout(()=>{delete o.current[l],I.current[l]=!0,H.current($e,{approve:n}).finally(()=>{delete I.current[l]})},ee*1e3),A(l,{rateLimitRetried:!0});return}p(l),x(ee=>({...ee,[l]:{origin:l,reason:`broker:${R}`,detail:z||null,clientId:e.clientId||U.current[l]||null}}))},[j,A,p,d]),H=t.useRef(M);H.current=M,t.useEffect(()=>{Object.values(b).forEach(e=>{e.status!=="broker"||I.current[e.origin]||o.current[e.origin]||(I.current[e.origin]=!0,M(e).finally(()=>{delete I.current[e.origin]}))})},[b,M]);const K=t.useCallback(e=>!F.current||!(e!=null&&e.consent)?Promise.resolve():M(e,{approve:!0}),[M]);t.useEffect(()=>{const e=V.current;Object.values(b).forEach(n=>{n.status==="silent"&&!e[n.origin]&&(e[n.origin]=setTimeout(()=>{delete e[n.origin],L(n.origin)},Ie))}),Object.keys(e).forEach(n=>{(!b[n]||b[n].status!=="silent")&&(clearTimeout(e[n]),delete e[n])})},[b,L]),t.useEffect(()=>()=>{$.current&&$.current(),Object.values(V.current).forEach(clearTimeout),Object.values(o.current).forEach(clearTimeout)},[]);const B=t.useCallback(e=>{$.current&&($.current(),$.current=null);const n=u||le;$.current=n(e,{onBlocked:()=>{E(s=>s[e.origin]?{...s,[e.origin]:{...s[e.origin],popupBlocked:!0}}:s)},onClosed:()=>{if(e.v2){Q&&E(s=>!s[e.origin]||!s[e.origin].silentUrl?s:{...s,[e.origin]:{...s[e.origin],status:"silent"}});return}h(e.origin)}})},[u,h]),_={pending:Object.values(b).filter(e=>e.status==="interactive"),authorize:B,approve:K,complete:j,dismiss:P,originEpochs:W,credentials:O,unsupported:q},D=Object.values(b).filter(e=>e.status==="silent"&&e.silentUrl);return S.default.createElement(G.Provider,{value:_},r,D.map(e=>S.default.createElement("iframe",{key:`mm-silent-auth-${e.origin}`,src:e.silentUrl,title:`MachineMetrics silent authorization (${e.origin})`,"aria-hidden":"true",tabIndex:-1,style:{display:"none"}})))};ae.propTypes={children:k.default.node,launcher:k.default.func,broker:k.default.func};const Me=()=>t.useContext(G),de=r=>{const{originEpochs:u}=t.useContext(G),c=J(r);return c&&u[c]||0},fe=r=>{const{credentials:u}=t.useContext(G),c=J(r);return c&&u[c]||null},pe=r=>{const{unsupported:u}=t.useContext(G),c=J(r);return c&&u[c]||null},be=({children:r,authorizationLauncher:u,credentialBroker:c})=>{const[f,b]=t.useState({}),[E,W]=t.useState({}),T=t.useRef({}),O=t.useRef({}),y=t.useRef({}),q=t.useCallback((i,a)=>{var o;return!!((o=y.current[i])!=null&&o.has(a))},[]),x=t.useCallback((i,a,o)=>{b(d=>({...d,[i]:{origin:a,frameRef:o}}))},[]),Y=t.useCallback(i=>{b(a=>{const o={...a};return delete o[i],o}),W(a=>{const o={...a};return delete o[i],o}),delete y.current[i]},[]);t.useEffect(()=>{const i=(o=[],d,p)=>{o.forEach(h=>h(d,p))},a=o=>{var j,A,I,M,H,K,B;if(!((j=o==null?void 0:o.data)!=null&&j.instanceId))return;const{instanceId:d,type:p}=o.data,h=f[d],L=(I=(A=h==null?void 0:h.frameRef)==null?void 0:A.current)==null?void 0:I.contentWindow;if(!L||o.source!==L||o.origin!==h.origin)return;const P={origin:o.origin},C=T.current;if(p==="response"){const{callbackId:_,response:D,error:e}=o.data,n=O.current[_];n&&(n(D,e),delete O.current[_]);return}if(p==="mark-registered"){const{requestType:_}=o.data;y.current[d]||(y.current[d]=new Set),y.current[d].add(_);return}p==="ready"&&W(_=>({..._,[d]:!0})),i((M=C[d])==null?void 0:M[p],o.data,P),i((H=C["*"])==null?void 0:H[p],o.data,P),i((K=C[d])==null?void 0:K["*"],o.data,P),i((B=C["*"])==null?void 0:B["*"],o.data,P)};return window.addEventListener("message",a),()=>{window.removeEventListener("message",a)}},[f]);const g=t.useCallback((i,a,o)=>{var p,h;const d=f[i];if(!d){console.warn(`Origin not set for id: ${i}. Unable to send message.`);return}(h=(p=d==null?void 0:d.frameRef)==null?void 0:p.current)!=null&&h.contentWindow&&d.frameRef.current.contentWindow.postMessage({type:a,...o,instanceId:i},d.origin)},[f]),$=t.useRef(0),V=t.useCallback(()=>`${Date.now()}-${++$.current}`,[]),U=t.useCallback(async(i,a,o={})=>new Promise((d,p)=>{const h=V(),L=(P,C)=>{delete O.current[h],C?p(C):d(P)};O.current[h]=L,g(i,"request",{callbackId:h,requestType:a,data:o})}),[g]),v=t.useCallback((i,a)=>{g(i,"load-options",a)},[g]),F=t.useCallback((i,a)=>{g(i,"validate",a)},[g]),w=t.useMemo(()=>({registerFrame:x,unregisterFrame:Y,sendMessage:g,sendRequest:U,loadOptions:v,validate:F,hasHandler:q,readyFrames:E,addListener:(i,a,o)=>(T.current[i]||(T.current[i]={}),T.current[i][a]||(T.current[i][a]=[]),T.current[i][a].push(o),()=>{T.current[i][a]=T.current[i][a].filter(d=>d!==o)})}),[x,Y,g,U,v,F,q,E]);return S.default.createElement(X.Provider,{value:w},S.default.createElement(ae,{launcher:u,broker:c},r))},N=(r,u,c)=>{const f=t.useContext(X);if(f===null)throw new Error("useEmbeddedEffect must be used within a MMEmbeddableZoneProvider");const b=t.useRef(c);t.useEffect(()=>{b.current=c},[c]),t.useEffect(()=>{if(!(!r||!u)){if(u.startsWith("key:")){const E=u.substring(4);return f.readyFrames[r]&&f.sendMessage(r,"subscribe-key",{keyChord:E}),f.addListener(r,`key-pressed:${E}`,(...T)=>b.current(...T))}return f.addListener(r,u,(...E)=>b.current(...E))}},[f,r,u])};be.propTypes={children:k.default.node.isRequired,authorizationLauncher:k.default.func,credentialBroker:k.default.func};const se=ue.default.default||ue.default,Se=1e4,Ue="credentials",Le=(r,u)=>`${r}/mm-embedded-interop?id=${encodeURIComponent(u)}&origin=${encodeURIComponent(window.location.origin)}&caps=${Ue}`,Ae=se.iframe`
|
|
2
2
|
width: 100%;
|
|
3
3
|
height: 100%;
|
|
4
4
|
border: none;
|
|
5
5
|
visibility: ${r=>r.$isReady?"visible":"hidden"};
|
|
6
|
-
`,
|
|
6
|
+
`,ze=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",c=r&&r.reason||"";switch(c){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 c.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."}}},he=({entry:r})=>{const u=me(r);return S.default.createElement(ze,{"data-reason":r&&r.reason||void 0},S.default.createElement("div",{style:{fontWeight:600}},u.title),S.default.createElement("div",{style:{fontSize:12,color:"#888"}},u.detail))};he.propTypes={entry:k.default.shape({origin:k.default.string,name:k.default.string,reason:k.default.string,detail:k.default.object})};const Oe=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
|
+
`,ye=()=>S.default.createElement(Oe,null,S.default.createElement("div",{style:{textAlign:"center"}},"This app couldn’t be loaded as an embeddable widget.")),Ee=({id:r,src:u,path:c,onInit:f=()=>{},onTimeout:b,context:E={},params:W={},renderUnsupported:T,renderTimeout:O,...y})=>{if("isConfiguring"in y)return console.error("isConfiguring must be passed via the params prop to MMEmbeddableZone"),null;if("colorMode"in E)return console.error("colorMode must be passed via the params prop to MMEmbeddableZone"),null;if("isFullScreen"in E)return console.error("isFullScreen must be passed via the params prop to MMEmbeddableZone"),null;const q=t.useRef(),[x,Y]=t.useState(),[g,$]=t.useState(0),[V,U]=t.useState(!1),[v,F]=t.useState(!1),w=de(u),i=fe(u),a=pe(u),[o,d]=t.useState(0),[p,h]=t.useState(null),{registerFrame:L,unregisterFrame:P,sendMessage:C}=t.useContext(X),[j,A]=t.useState(),I=t.useRef(),M=t.useRef(b),H=t.useRef(f),K=t.useRef(!1),B=t.useCallback(()=>{K.current=!1,clearTimeout(I.current),$(0),U(!1),F(!1),h(null)},[]);t.useEffect(()=>{(w>0||o>0)&&B()},[w,o,B]),t.useEffect(()=>{M.current=b},[b]),t.useEffect(()=>{H.current=f},[f]),t.useEffect(()=>{g&&C(r,"set-context",{context:{...E}})},[E,r,g]),t.useEffect(()=>{g&&C(r,"set-params",{params:W})},[W,r,g]),t.useEffect(()=>{c!==void 0&&A((c==null?void 0:c.indexOf("/"))===0?c:`/${c}`)},[c]),t.useEffect(()=>{j===void 0||!g||C(r,"navigate",{path:j})},[j,g,r,C]),t.useEffect(()=>{if(!u||!r||!q)return;B();const e=new URL(u);return L(r,e.origin,q),H.current(),Y(Le(e.origin,r)),()=>{P(r)}},[u,r,L,q,B]),t.useEffect(()=>{if(!(!x||K.current))return I.current=setTimeout(()=>{U(!0),M.current&&M.current()},Se),()=>clearTimeout(I.current)},[x,u,w,o]);const _=t.useCallback(()=>{K.current=!0,clearTimeout(I.current),U(!1)},[]);N(r,"init",e=>{_(),Array.isArray(e==null?void 0:e.acceptsCredentials)&&h(e.acceptsCredentials)}),N(r,"authorize",_),N(r,"login",_),N(r,"credential-request",()=>{_(),F(!0)}),t.useEffect(()=>{if(!v||!i)return;const e=p;if(Array.isArray(e)&&!e.includes(i.kind))return;const n=i.kind==="access_token"?{accessToken:i.accessToken}:{};C(r,"credential",{...n,credential:i})},[v,i,p,r,C]),N(r,"ready",()=>{_(),$(e=>e+1)});const D=t.useRef();return N(r,"heartbeat",()=>{D.current&&clearTimeout(D.current),D.current=setTimeout(()=>{d(e=>e+1)},6e3)}),t.useEffect(()=>()=>{D.current&&clearTimeout(D.current)},[]),a?T?T(a):S.default.createElement(he,{entry:a}):V&&!g?O?O():S.default.createElement(ye,null):S.default.createElement(Ae,{key:`auth-epoch-${w}-frame-${o}`,$isReady:g,ref:q,src:x,...y})};Ee.propTypes={id:k.default.string,src:k.default.string.isRequired,path:k.default.string,onInit:k.default.func,onTimeout:k.default.func,renderUnsupported:k.default.func,renderTimeout:k.default.func},m.CREDENTIAL_KIND_ACCESS_TOKEN=oe,m.EmbeddedAuthorizationContext=G,m.MMEmbeddableZone=Ee,m.MMEmbeddableZoneContext=X,m.MMEmbeddableZoneProvider=be,m.isCrossSite=ne,m.normalizeCredential=re,m.popupAuthorizationLauncher=le,m.unsupportedCopy=me,m.useEmbeddedAuthorizations=Me,m.useEmbeddedCredential=fe,m.useEmbeddedEffect=N,m.useEmbeddedOriginEpoch=de,m.useEmbeddedUnsupported=pe,Object.defineProperty(m,Symbol.toStringTag,{value:"Module"})}));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@machinemetrics/mm-react-embeddable",
|
|
3
|
-
"version": "2.0.0-
|
|
3
|
+
"version": "2.0.0-6",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"module": "dist/mm-react-embeddable.es.js",
|
|
6
6
|
"main": "dist/mm-react-embeddable.cjs.js",
|
|
@@ -44,4 +44,4 @@
|
|
|
44
44
|
"vite": "^7.2.2",
|
|
45
45
|
"vitest": "^4.0.10"
|
|
46
46
|
}
|
|
47
|
-
}
|
|
47
|
+
}
|