1const u="[nango]";function y({expectedWidth:a,expectedHeight:e}){const t=window.screen.width,s=window.screen.height,r=t/2-a/2,n=s/2-e/2,i=Math.min(a,t),o=Math.min(e,s);return{left:Math.max(r,0),top:Math.max(n,0),width:i,height:o}}function g(a){const e={...a,scrollbars:"yes",resizable:"yes",status:"no",toolbar:"no",location:"no",copyhistory:"no",menubar:"no",directories:"no"},t=[];for(const s in e)t.push(`${s}=${e[s]}`);return t.join(",")}class f{baseURL;debug;swClient;errorHandler;modal;isProcessingMessage=!1;constructor({baseUrl:e,debug:t,webSocketUrl:s,successHandler:r,errorHandler:n}){this.baseURL=e,this.debug=t,this.swClient=new WebSocket(s),this.errorHandler=n,this.swClient.onmessage=i=>{this.isProcessingMessage=!0,this.handleMessage(i,r),this.isProcessingMessage=!1}}setModal(e){this.modal=e}handleMessage(e,t){const s=JSON.parse(e.data);switch(s.message_type){case"connection_ack":{this.debug&&console.log(u,"Connection ack received. Opening modal..."),this.baseURL.searchParams.set("ws_client_id",s.ws_client_id),this.open();break}case"error":this.debug&&console.log(u,"Error received. Rejecting authorization..."),this.errorHandler(s.error_type,s.error_desc),this.swClient.close();break;case"success":this.debug&&console.log(u,"Success received. Resolving authorization..."),t({providerConfigKey:s.provider_config_key,connectionId:s.connection_id,isPending:s.is_pending}),this.swClient.close();break;default:this.debug&&console.log(u,"Unknown message type received from Nango server. Ignoring...");return}}open(){if(this.modal&&(this.debug&&console.log(u,"opening",this.baseURL.href),this.modal.location=this.baseURL.href,!this.modal||this.modal.closed||typeof this.modal.closed>"u")){this.errorHandler("blocked_by_browser","Modal blocked by browser");return}}close(){this.modal&&!this.modal.closed&&(this.modal.close(),delete this.modal),this.swClient.close(),delete this.swClient}}class b{iframe=null;isReady=!1;listener=null;sessionToken;baseURL;apiURL;onEvent;detectClosedAuthWindow;lang;themeOverride;constructor({sessionToken:e,baseURL:t="https://connect.nango.dev",apiURL:s="https://api.nango.dev",detectClosedAuthWindow:r,onEvent:n,lang:i,themeOverride:o}){this.sessionToken=e,this.baseURL=t,this.apiURL=s,this.onEvent=n,this.detectClosedAuthWindow=r,this.lang=i,this.themeOverride=o}open(){this.iframe?.isConnected||(this.iframe&&this.close(),this.iframe=this.createIframe(),document.body.append(this.iframe),document.body.style.overflow="hidden",this.setupEventListeners())}createIframe(){const e=new URL(this.baseURL);this.apiURL&&e.searchParams.append("apiURL",this.apiURL),this.detectClosedAuthWindow&&e.searchParams.append("detectClosedAuthWindow",String(this.detectClosedAuthWindow)),this.lang&&e.searchParams.append("lang",this.lang),this.themeOverride&&e.searchParams.append("theme",this.themeOverride);const t=document.createElement("iframe");return t.src=e.href,t.id="connect-ui",t.style.position="fixed",t.style.zIndex="9999",t.style.top="0",t.style.left="0",t.style.right="0",t.style.bottom="0",t.style.width="100vw",t.style.height="100vh",t.style.backgroundColor="transparent",t.allow="clipboard-write",t}setupEventListeners(){this.listener=e=>{if(e.origin!==new URL(this.baseURL).origin||typeof e.data!="object"||!e.data||!e.data.type)return;const t=e.data;switch(t.type){case"ready":{this.isReady=!0,this.sendSessionToken();break}case"close":{this.close();break}}this.onEvent&&this.onEvent(t)},window.addEventListener("message",this.listener,!1)}setSessionToken(e){this.sessionToken=e,this.isReady&&this.sendSessionToken()}close(){this.listener&&(window.removeEventListener("message",this.listener),this.listener=null),this.iframe&&(this.iframe.remove(),this.iframe=null,document.body.style.overflow="")}sendSessionToken(){if(!this.sessionToken)return;const e={type:"session_token",sessionToken:this.sessionToken};this.iframe?.contentWindow?.postMessage(e,"*")}}const k="https://api.nango.dev",w="NANGO DEBUG LOG: ";
1class h extends Error{type;constructor(e,t){super(e),this.type=t}}class U{hostBaseUrl;websocketsBaseUrl;publicKey;connectSessionToken;debug=!1;width=500;height=600;tm=null;win=null;constructor(e={}){e.host=e.host||k,e.websocketsPath=e.websocketsPath||"/",this.debug=e.debug||!1,this.debug&&(console.log(w,"Debug mode is enabled."),console.log(w,`Using host: ${e.host}.`)),e.width&&(this.width=e.width),e.height&&(this.height=e.height),this.hostBaseUrl=e.host.replace(/\/+$/,""),this.publicKey=e.publicKey,this.connectSessionToken=e.connectSessionToken;try{const t=new URL(this.hostBaseUrl),s=new URL(e.websocketsPath,t);this.websocketsBaseUrl=s.toString().replace("https://","wss://").replace("http://","ws://")}catch{throw new h("Invalid URL provided for the Nango host.","invalid_host_url")}}async create(e,t,s){this.ensureCredentials();let r=null,n=s;typeof t=="string"?r=t:n=t;const i=this.hostBaseUrl+`/auth/unauthenticated/${e}${this.toQueryString(r,n)}`;return await this.triggerAuth({authUrl:i})}auth(e,t,s){this.ensureCredentials();let r=null,n=s;if(typeof t=="string"?r=t:n={...n,...t},n&&("installation"in n&&n.installation==="outbound"||"credentials"in n&&("token_id"in n.credentials&&"token_secret"in n.credentials||!("oauth_client_id_override"in n.credentials)||!("oauth_client_secret_override"in n.credentials))&&Object.keys(n.credentials).length>0)){const o=n.credentials;if(!o)throw new h("Credentials are required for custom auth","missing_credentials");const{credentials:l,...d}=n;return this.customAuth(e,r,this.convertCredentialsToConfig(o),d,n.installation)}const i=window.open("","_blank",g(y({expectedWidth:this.width,expectedHeight:this.height})));return new Promise((o,l)=>{const d=p=>{o(p)},c=(p,m)=>{l(new h(m,p))};if(this.win&&this.clear(),!i||i.closed||typeof i.closed>"u"){c("blocked_by_browser","Modal blocked by browser");return}let _;try{_=new URL(`${this.hostBaseUrl}/oauth/connect/${e}${this.toQueryString(r,n)}`)}catch{c("invalid_host_url","Invalid URL provided for the Nango host.");return}this.win=new f({baseUrl:_,debug:this.debug,webSocketUrl:this.websocketsBaseUrl,successHandler:d,errorHandler:c}),this.win.setModal(i),this.tm=setInterval(()=>{!this.win||!this.win.modal||this.win.modal.closed&&(this.win.isProcessingMessage||n?.detectClosedAuthWindow&&(clearInterval(this.tm),this.win.close(),this.win=null,l(new h("The authorization window was closed before the authorization flow was completed","window_closed"))))},500)}).finally(()=>{this.clear()})}reconnect(e,t){if(!this.connectSessionToken)throw new h("Reconnect requires a session token","missing_connect_session_token");return this.auth(e,t)}clear(){if(this.tm&&clearInterval(this.tm),this.win){const e=this.win;this.win=null,setTimeout(()=>{try{e.close()}catch{}},500)}}openConnectUI(e){const t=new b({sessionToken:this.connectSessionToken,...e});return t.open(),t}convertCredentialsToConfig(e){const t={};if("type"in e&&"username"in e&&"password"in e&&e.type==="SIGNATURE")return{params:{type:e.type,username:e.username,password:e.password}};if("type"in e&&e.type==="AWS_SIGV4"){const s={type:e.type,role_arn:e.role_arn};return e.region&&(s.region=e.region),{params:s}}if("username"in e&&(t.username=e.username||""),"password"in e&&(t.password=e.password||""),"apiKey"in e&&(t.apiKey=e.apiKey||""),"role_arn"in e&&(t.role_arn=e.role_arn),"region"in e&&e.region&&(t.region=e.region),"privateKey"in e||"type"in e&&e.type==="JWT"){const{privateKey:s,...r}=e;return{...r},{params:e}}if("privateKeyId"in e&&"issuerId"in e&&"privateKey"in e){const s={params:{privateKeyId:e.privateKeyId,issuerId:e.issuerId,privateKey:e.privateKey}};return"scope"in e&&(typeof e.scope=="string"||Array.isArray(e.scope))&&(s.params.scope=e.scope),s}if("client_id"in e&&("client_secret"in e||"client_private_key"in e))return{params:{client_id:e.client_id,client_secret:e.client_secret,client_certificate:e.client_certificate,client_private_key:e.client_private_key}};if("token_id"in e&&"token_secret"in e){const s={token_id:e.token_id,token_secret:e.token_secret};return"oauth_client_id_override"in e&&(s.oauth_client_id_override=e.oauth_client_id_override),"oauth_client_secret_override"in e&&(s.oauth_client_secret_override=e.oauth_client_secret_override),{params:s}}return"username"in e&&"password"in e&&"organization_id"in e&&"dev_key"in e?{params:{username:e.username,password:e.password,organization_id:e.organization_id,dev_key:e.dev_key}}:"type"in e&&e.type==="TWO_STEP"?{params:{...e}}:{params:t}}async triggerAuth({authUrl:e,credentials:t,assertionOption:s}){const r={};t&&Object.assign(r,t),s&&Object.keys(s).length>0&&(r.assertionOption=s);const n=await fetch(e,{method:"POST",headers:{"Content-Type":"application/json"},...Object.keys(r).length>0?{body:JSON.stringify(r)}:{}});if(!n.ok){const i=await n.json();throw new h(i.error.message,i.error.code)}return n.json()}async customAuth(e,t,s,r,n){const{params:i}=s;if(!i)throw new h("You must specify credentials.","missing_credentials");if("type"in i&&i.type==="TWO_STEP"){const o=r?.assertionOption;return await this.triggerAuth({authUrl:this.hostBaseUrl+`/auth/two-step/${e}${this.toQueryString(t,r)}`,credentials:i,...o?{assertionOption:o}:{}})}return"type"in i&&i.type==="SIGNATURE"&&"username"in i&&"password"in i?await this.triggerAuth({authUrl:this.hostBaseUrl+`/auth/signature/${e}${this.toQueryString(t,r)}`,credentials:i}):"type"in i&&i.type==="AWS_SIGV4"&&"role_arn"in i?await this.triggerAuth({authUrl:this.hostBaseUrl+`/auth/aws-sigv4/${e}${this.toQueryString(t,r)}`,credentials:{role_arn:i.role_arn,region:i.region}}):"username"in i&&"password"in i&&"organization_id"in i&&"dev_key"in i?await this.triggerAuth({authUrl:this.hostBaseUrl+`/auth/bill/${e}${this.toQueryString(t,r)}`,credentials:i}):"apiKey"in i?await this.triggerAuth({authUrl:this.hostBaseUrl+`/api-auth/api-key/${e}${this.toQueryString(t,r)}`,credentials:i}):"username"in i||"password"in i?await this.triggerAuth({authUrl:this.hostBaseUrl+`/api-auth/basic/${e}${this.toQueryString(t,r)}`,credentials:i}):"privateKey"in i||"type"in i&&i.type==="JWT"?await this.triggerAuth({authUrl:this.hostBaseUrl+`/auth/jwt/${e}${this.toQueryString(t,r)}`,credentials:i}):"privateKeyId"in i&&"issuerId"in i&&"privateKey"in i?await this.triggerAuth({authUrl:this.hostBaseUrl+`/app-store-auth/${e}${this.toQueryString(t,r)}`,credentials:i}):"token_id"in i&&"token_secret"in i?await this.triggerAuth({authUrl:this.hostBaseUrl+`/auth/tba/${e}${this.toQueryString(t,r)}`,credentials:i}):"client_id"in i&&("client_secret"in i||"client_private_key"in i)?await this.triggerAuth({authUrl:this.hostBaseUrl+`/oauth2/auth/${e}${this.toQueryString(t,r)}`,credentials:i}):n==="outbound"?await this.triggerAuth({authUrl:this.hostBaseUrl+`/auth/oauth-outboun
1d/${e}${this.toQueryString(t,r)}`}):Promise.reject(new Error("Something went wrong with the authorization"))}toQueryString(e,t){const s=[];if(e&&s.push(`connection_id=${e}`),this.publicKey&&s.push(`public_key=${this.publicKey}`),this.connectSessionToken&&s.push(`connect_session_token=${this.connectSessionToken}`),t){for(const r in t.params){const n=t.params[r];typeof n=="string"&&s.push(`params[${encodeURIComponent(r)}]=${encodeURIComponent(n)}`)}if(t.hmac&&s.push(`hmac=${t.hmac}`),t.user_scope&&s.push(`user_scope=${t.user_scope.join(",")}`),t.credentials){const r=t.credentials;"oauth_client_id_override"in r&&s.push(`credentials[oauth_client_id_override]=${encodeURIComponent(r.oauth_client_id_override)}`),"oauth_client_secret_override"in r&&s.push(`credentials[oauth_client_secret_override]=${encodeURIComponent(r.oauth_client_secret_override)}`),"oauth_refresh_token_override"in r&&s.push(`credentials[oauth_refresh_token_override]=${encodeURIComponent(r.oauth_refresh_token_override)}`),"token_id"in r&&s.push(`token_id=${encodeURIComponent(r.token_id)}`),"token_secret"in r&&s.push(`token_secret=${encodeURIComponent(r.token_secret)}`)}for(const r in t.authorization_params){const n=t.authorization_params[r];typeof n=="string"?s.push(`authorization_params[${r}]=${n}`):n===void 0&&s.push(`authorization_params[${r}]=undefined`)}}return s.length===0?"":"?"+s.join("&")}ensureCredentials(){if(!this.publicKey&&!this.connectSessionToken)throw new h("You must specify a public key OR a connect session token (cf. documentation).","missing_auth_token")}}export{h as AuthError,b as ConnectUI,U as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.