@azure/msal-browser
Version:
Microsoft Authentication Library for js
3 lines (2 loc) • 5.63 kB
JavaScript
/*! @azure/msal-browser v5.30.0 2026-11-18 */
;!function(e,o){"object"==typeof exports&&"undefined"!=typeof module?o(exports):"function"==typeof define&&define.amd?define(["exports"],o):o((e="undefined"!=typeof globalThis?globalThis:e||self).msalPopupRelay={})}(this,function(e){class o extends Error{constructor(e,t,r,n){const i=r||(e?`See https://aka.ms/msal.js.errors#${e} for details`:"");super(i?`${e}: ${i}`:e),Object.setPrototypeOf(this,o.prototype),this.errorCode=e||"",this.errorMessage=i||"",this.subError=n||"",this.correlationId=t,this.name="AuthError"}}const t="popup_window_error",r="popup_relay_unsupported_flow";class n extends o{constructor(e,o,t){super(e,o,`See https://aka.ms/msal.js.errors#${e} for details`,t),Object.setPrototypeOf(this,n.prototype),this.name="BrowserAuthError"}}function i(e,o,t){return new n(e,o,t)}const s="msal:popup-relay-response:v1",a="msal:popup-relay-response:v2";var p;!function(e){e.Redirect="redirect",e.Popup="popup",e.Silent="silent",e.None="none"}(p||(p={}));const l="0123456789abcdef",c=new Uint32Array(1);function u(){return window.crypto.getRandomValues(c),c[0]}function d(e,o,t){const r=e.createElement("form");return r.method="post",r.action=o,Object.keys(t).forEach(o=>{const n=e.createElement("input");n.hidden=!0,n.name=o,n.value=t[o],r.appendChild(n)}),e.body.appendChild(r),r}const h="popup_relay_bad_request",f="popup_relay_invalid_allowed_origin";function y(e){throw i(r,"",e)}function w(e){try{return new URL(e)}catch(e){return null}}function m(e,o){"string"==typeof e&&e||y(h);const t=w(e);(!t||"https:"!==t.protocol||t.username||t.password)&&y("popup_relay_unsafe_url"),void 0===o||Array.isArray(o)||y(f);const r=void 0===o?void 0:function(e){return e.map(e=>{const o="string"==typeof e?w(e):null;return(!o||"https:"!==o.protocol||o.username||o.password)&&y(f),o.origin})}(o);return r&&r.indexOf(t.origin)<0&&y("popup_relay_untrusted_authority"),e}function _(e,o){"object"==typeof e&&null!==e||y(h);const t=e,r=t.id;if("string"==typeof r&&r||y(h),"GET"===t.method)return{id:r,method:"GET",url:m(t.url,o)};if("POST"===t.method){const e=m(t.action,o),n=t.fields;("object"!=typeof n||null===n||Array.isArray(n))&&y(h);const i={};return Object.keys(n).forEach(e=>{const o=n[e];"string"!=typeof o&&y(h),i[e]=o}),{id:r,method:"POST",action:e,fields:i}}return y(h)}e.runPopupRelay=function(e){const o=function(e,o=""){if(void 0===e)return 1;if(1!==e&&2!==e)throw i(r,o,"popup_relay_invalid_protocol");return e}(e?.protocolVersion),p=window.opener,c=window.location.origin,f=new URLSearchParams(window.location.hash.replace(/^#/,"")),w=f.has("id")?2:1;let m;if(1===w)try{m=JSON.parse(f.get("req")||"")}catch{m=null}let g=f.get("id")||"";if(1===w&&m&&"object"==typeof m&&"id"in m&&(g=m.id),"function"==typeof window.history.replaceState&&window.history.replaceState(null,"",window.location.origin+window.location.pathname),!p)throw i(r,"","popup_relay_no_opener");if("string"==typeof g&&g||y(h),w!==o){const e=i(r,"","popup_relay_protocol_mismatch");throw p.postMessage({type:1===w?s:a,id:g,error:e.errorCode,subError:e.subError},c),e}const v=1===o?_(m,e?.allowedAuthorityOrigins):void 0,b=2===o?function(){const e=Date.now(),o=1024*u()+(1023&u()),t=new Uint8Array(16),r=Math.trunc(o/2**30),n=o&2**30-1,i=u();t[0]=e/2**40,t[1]=e/2**32,t[2]=e/2**24,t[3]=e/65536,t[4]=e/256,t[5]=e,t[6]=112|r>>>8,t[7]=r,t[8]=128|n>>>24,t[9]=n>>>16,t[10]=n>>>8,t[11]=n,t[12]=i>>>24,t[13]=i>>>16,t[14]=i>>>8,t[15]=i;let s="";for(let e=0;e<t.length;e++)s+=l.charAt(t[e]>>>4),s+=l.charAt(15&t[e]),3!==e&&5!==e&&7!==e&&9!==e||(s+="-");return s}():void 0,E=new BroadcastChannel(g);let P=!1,T=1===o;const A={},O=e=>{if(!P){P=!0;try{E.close()}catch(e){}clearInterval(A.closedPoll),clearTimeout(A.timeoutId),window.removeEventListener("message",S),window.removeEventListener("pagehide",L),p.postMessage({type:1===o?s:a,id:g,...2===o?{nonce:b}:{},...e},c),setTimeout(()=>{try{window.close()}catch(e){}},0)}};E.onmessage=e=>{const o=e.data&&e.data.payload;T&&o&&O({payload:o})};const S=r=>{const i=r.data;if(!P&&!T&&2===o&&I&&r.origin===c&&r.source===p&&i&&"msal:popup-relay-request:v2"===i.type&&i.id===g&&i.nonce===b){window.removeEventListener("message",S);try{const o=_(i.request,e?.allowedAuthorityOrigins);o.id!==g&&y(h),T=!0,"GET"===o.method?I.location.replace(o.url):d(I.document,o.action,o.fields).submit()}catch(e){O({error:e instanceof n?e.errorCode:t,subError:e instanceof n?e.subError:"popup_relay_navigation_failed"}),I.close()}}},j=function(e){const o=[`width=${e?.popupSize?.width||520}`,`height=${e?.popupSize?.height||640}`];return void 0!==e?.popupPosition?.top&&o.push(`top=${e.popupPosition.top}`),void 0!==e?.popupPosition?.left&&o.push(`left=${e.popupPosition.left}`),o.push("scrollbars=yes"),o.join(",")}(e?.popupWindowAttributes),I="GET"===v?.method?window.open(v.url,"msalPopupRelayChild",j):window.open("about:blank",1===o?"msalPopupRelayChild":"_blank",j);function L(){P=!0,window.removeEventListener("message",S),window.removeEventListener("pagehide",L),clearInterval(A.closedPoll),clearTimeout(A.timeoutId),E.close(),I?.close()}I?(2===o?window.addEventListener("message",S):"POST"===v?.method&&d(I.document,v.action,v.fields).submit(),window.addEventListener("pagehide",L),A.closedPoll=window.setInterval(()=>{I.closed&&!P&&(clearTimeout(A.timeoutId),O({error:"user_cancelled"}))},500),A.timeoutId=window.setTimeout(()=>{if(!P){clearInterval(A.closedPoll);try{I.close()}catch(e){}O({error:"timed_out",subError:T?void 0:"popup_relay_handshake_timeout"})}},e?.timeoutMs||3e5),2===o&&p.postMessage({type:"msal:popup-relay-ready:v2",id:g,nonce:b},c)):O({error:t})}});