UNPKG

@azure/msal-browser

Version:
3 lines (2 loc) • 3.65 kB
/*! @azure/msal-browser v5.25.0 2026-10-06 */ "use strict";!function(o,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((o="undefined"!=typeof globalThis?globalThis:o||self).msalPopupRelay={})}(this,function(o){class t extends Error{constructor(o,e,r,n){const i=r||(o?`See https://aka.ms/msal.js.errors#${o} for details`:"");super(i?`${o}: ${i}`:o),Object.setPrototypeOf(this,t.prototype),this.errorCode=o||"",this.errorMessage=i||"",this.subError=n||"",this.correlationId=e,this.name="AuthError"}}const e="popup_relay_unsupported_flow";class r extends t{constructor(o,t,e){super(o,t,`See https://aka.ms/msal.js.errors#${o} for details`,e),Object.setPrototypeOf(this,r.prototype),this.name="BrowserAuthError"}}function n(o,t,e){return new r(o,t,e)}var i;!function(o){o.Redirect="redirect",o.Popup="popup",o.Silent="silent",o.None="none"}(i||(i={}));const s="popup_relay_bad_request",p="popup_relay_invalid_allowed_origin";function a(o){throw n(e,"",o)}function l(o){try{return new URL(o)}catch(o){return null}}function u(o,t){"string"==typeof o&&o||a(s);const e=l(o);(!e||"https:"!==e.protocol||e.username||e.password)&&a("popup_relay_unsafe_url"),void 0===t||Array.isArray(t)||a(p);const r=void 0===t?void 0:function(o){return o.map(o=>{const t="string"==typeof o?l(o):null;return(!t||"https:"!==t.protocol||t.username||t.password)&&a(p),t.origin})}(t);return r&&r.indexOf(e.origin)<0&&a("popup_relay_untrusted_authority"),o}o.runPopupRelay=function(o){const t=window.opener,r=window.location.origin,i=new URLSearchParams(window.location.hash.replace(/^#/,"")).get("req")||"";if("function"==typeof window.history.replaceState&&window.history.replaceState(null,"",window.location.origin+window.location.pathname),!t)throw n(e,"","popup_relay_no_opener");const p=function(o,t){let e;try{e=JSON.parse(o)}catch(o){e=null}"object"==typeof e&&null!==e||a(s);const r=e,n=r.id;if("string"==typeof n&&n||a(s),"GET"===r.method)return{id:n,method:"GET",url:u(r.url,t)};if("POST"===r.method){const o=u(r.action,t),e=r.fields;("object"!=typeof e||null===e||Array.isArray(e))&&a(s);const i={};return Object.keys(e).forEach(o=>{const t=e[o];"string"!=typeof t&&a(s),i[o]=t}),{id:n,method:"POST",action:o,fields:i}}return a(s)}(i,o?.allowedAuthorityOrigins),l=p.id,c=new BroadcastChannel(l);let d=!1;const h={},f=o=>{if(!d){d=!0;try{c.close()}catch(o){}clearInterval(h.closedPoll),clearTimeout(h.timeoutId),t.postMessage({type:"msal:popup-relay-response:v1",id:l,...o},r),setTimeout(()=>{try{window.close()}catch(o){}},0)}};c.onmessage=o=>{const t=o.data&&o.data.payload;t&&f({payload:t})};const y=function(o){const t=[`width=${o?.popupSize?.width||520}`,`height=${o?.popupSize?.height||640}`];return void 0!==o?.popupPosition?.top&&t.push(`top=${o.popupPosition.top}`),void 0!==o?.popupPosition?.left&&t.push(`left=${o.popupPosition.left}`),t.push("scrollbars=yes"),t.join(",")}(o?.popupWindowAttributes),w="POST"===p.method?window.open("about:blank","msalPopupRelayChild",y):window.open(p.url,"msalPopupRelayChild",y);w?("POST"===p.method&&function(o,t,e){const r=o.createElement("form");return r.method="post",r.action=t,Object.keys(e).forEach(t=>{const n=o.createElement("input");n.hidden=!0,n.name=t,n.value=e[t],r.appendChild(n)}),o.body.appendChild(r),r}(w.document,p.action,p.fields).submit(),h.closedPoll=window.setInterval(()=>{w.closed&&!d&&(clearTimeout(h.timeoutId),f({error:"user_cancelled"}))},500),h.timeoutId=window.setTimeout(()=>{if(!d){clearInterval(h.closedPoll);try{w.close()}catch(o){}f({error:"timed_out"})}},o?.timeoutMs||3e5)):f({error:"popup_window_error"})}});