UNPKG

@fntio/infra-client

Version:

Self-hosted AWS access portal: credential vending, target-account trust, and grant-driven private networking as CDK stacks and constructs.

2 lines (1 loc) • 6.74 kB
import{a as h}from"./chunk-KCJ5HNXP.js";import{F as f,I as m,qb as g}from"./chunk-FYA6QUPZ.js";import{e as l}from"./chunk-WFIHWVQO.js";function u(n){let e=n.replace(/-/g,"+").replace(/_/g,"/"),t=atob(e),r=new Uint8Array(new ArrayBuffer(t.length));for(let i=0;i<t.length;i++)r[i]=t.charCodeAt(i);return r}function s(n){let e="";for(let t of new Uint8Array(n))e+=String.fromCharCode(t);return btoa(e).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,"")}function A(){return typeof window<"u"&&"credentials"in navigator&&"PublicKeyCredential"in window}function y(n){return Promise.race([n,new Promise((e,t)=>setTimeout(()=>t(new DOMException("Passkey prompt timed out","NotAllowedError")),7e4))])}function T(n){return l(this,null,function*(){let e=n,t={rp:e.rp,user:{id:u(e.user.id),name:e.user.name,displayName:e.user.displayName},challenge:u(e.challenge),pubKeyCredParams:e.pubKeyCredParams,timeout:e.timeout,excludeCredentials:(e.excludeCredentials??[]).map(o=>({id:u(o.id),type:o.type,transports:o.transports})),authenticatorSelection:e.authenticatorSelection,attestation:e.attestation},r=yield y(navigator.credentials.create({publicKey:t}));if(!r)throw new Error("No credential returned");let i=r.response;return{id:r.id,rawId:s(r.rawId),type:r.type,authenticatorAttachment:r.authenticatorAttachment??void 0,response:{clientDataJSON:s(i.clientDataJSON),attestationObject:s(i.attestationObject),transports:i.getTransports?i.getTransports():void 0},clientExtensionResults:r.getClientExtensionResults()}})}function _(n){return(JSON.parse(n).allowCredentials??[]).map(t=>t.id)}function R(n){return l(this,null,function*(){let e=JSON.parse(n),t={challenge:u(e.challenge),rpId:e.rpId,timeout:e.timeout,userVerification:e.userVerification,allowCredentials:(e.allowCredentials??[]).map(a=>({id:u(a.id),type:a.type,transports:a.transports}))},r=yield y(navigator.credentials.get({publicKey:t}));if(!r)throw new Error("No credential returned");let i=r.response,o=JSON.stringify({id:r.id,rawId:s(r.rawId),type:r.type,authenticatorAttachment:r.authenticatorAttachment??void 0,response:{clientDataJSON:s(i.clientDataJSON),authenticatorData:s(i.authenticatorData),signature:s(i.signature),userHandle:i.userHandle?s(i.userHandle):null},clientExtensionResults:r.getClientExtensionResults()});return{credentialId:r.id,credential:o}})}var c={verifier:"sso.verifier",state:"sso.state",provider:"sso.provider",retried:"sso.retried"},d=class extends Error{constructor(t,r){super(r);this.verdict=t}verdict};function w(n=32){let e=new Uint8Array(n);return crypto.getRandomValues(e),s(e.buffer)}function b(n){return l(this,null,function*(){return s(yield crypto.subtle.digest("SHA-256",new TextEncoder().encode(n)))})}function C(n,e){let t=new URLSearchParams({identity_provider:e.provider,client_id:e.clientId,response_type:"code",scope:"openid email profile",redirect_uri:e.redirectUri,state:e.state,code_challenge:e.challenge,code_challenge_method:"S256"});return`${n.replace(/\/$/,"")}/oauth2/authorize?${t}`}function I(n){let e=new URLSearchParams(n);return{code:e.get("code")??void 0,state:e.get("state")??void 0,error:e.get("error")??void 0,errorDescription:e.get("error_description")??void 0}}function S(n,e){let t=e??"";return t.includes("FEDERATION_LINKED_RETRY")||/Already found an entry for username/i.test(t)?"linked-retry":t.includes("FEDERATION_NOT_INVITED")?"not-invited":t.includes("FEDERATION_NO_EMAIL")?"no-email":n==="access_denied"?"cancelled":"error"}function $(n){switch(n instanceof d?n.verdict:"error"){case"not-invited":return"Your account isn't set up here yet. Ask an admin to invite you with the email address your organization uses, then sign in again.";case"no-email":return"Your organization\u2019s sign-in did not share an email address, so this portal cannot match you to an invite. Ask your admin.";case"linked-retry":return"Your account was just connected \u2014 please sign in once more.";case"cancelled":return"The sign-in was cancelled.";default:return"The sign-in did not complete. Please try again."}}function k(n,e){return l(this,null,function*(){let t=yield fetch(`${n.replace(/\/$/,"")}/oauth2/token`,{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:new URLSearchParams({grant_type:"authorization_code",client_id:e.clientId,code:e.code,redirect_uri:e.redirectUri,code_verifier:e.verifier}),signal:AbortSignal.timeout(15e3)}),r=yield t.json().catch(()=>({}));if(!t.ok||typeof r.id_token!="string")throw new d("error",String(r.error??`token exchange failed (${t.status})`));return{IdToken:r.id_token,AccessToken:r.access_token,RefreshToken:r.refresh_token,ExpiresIn:Number(r.expires_in)||3600}})}function x(n,e){return n?Object.entries(n).filter(([,t])=>t).map(([t,r])=>({key:t,label:e.get(t)??t,value:r})).sort((t,r)=>t.label.localeCompare(r.label)):[]}var v=class n{configService=m(h);get federation(){try{return this.configService.config.federation}catch{return}}providers=g(()=>this.federation?.providers??[]);labels=g(()=>new Map((this.federation?.claims??[]).map(e=>[e.key,e.label])));claimRows(e){return x(e,this.labels())}redirectUri=`${location.origin}/auth/callback`;start(r){return l(this,arguments,function*(e,t={}){let i=this.federation;if(!i)throw new d("error","Federated sign-in is not configured.");let o=w(),a=w(16);sessionStorage.setItem(c.verifier,o),sessionStorage.setItem(c.state,a),sessionStorage.setItem(c.provider,e),t.retry?sessionStorage.setItem(c.retried,"1"):sessionStorage.removeItem(c.retried),location.assign(C(this.configService.config.hostedUiDomain,{clientId:i.clientId,provider:e,redirectUri:this.redirectUri,state:a,challenge:yield b(o)}))})}complete(e){return l(this,null,function*(){let t=I(e),r=sessionStorage.getItem(c.provider)??"";if(t.error){let p=S(t.error,t.errorDescription);if(p==="linked-retry"&&r&&!sessionStorage.getItem(c.retried))return yield this.start(r,{retry:!0}),null;throw this.clear(),console.warn("federated sign-in failed",t.error,t.errorDescription),new d(p,t.error)}if(!t.code)throw new d("error","No sign-in in progress.");let i=sessionStorage.getItem(c.verifier),o=sessionStorage.getItem(c.state);if(this.clear(),!i||!o||t.state!==o)throw new d("error","This sign-in started somewhere else \u2014 please start again.");let a=this.federation;if(!a)throw new d("error","Federated sign-in is not configured.");return{tokens:yield k(this.configService.config.hostedUiDomain,{clientId:a.clientId,code:t.code,redirectUri:this.redirectUri,verifier:i}),provider:r,clientId:a.clientId}})}clear(){for(let e of Object.values(c))sessionStorage.removeItem(e)}static \u0275fac=function(t){return new(t||n)};static \u0275prov=f({token:n,factory:n.\u0275fac,providedIn:"root"})};export{A as a,T as b,_ as c,R as d,$ as e,v as f};