UNPKG

@ew-did-registry/did-s3-store

Version:

S3-based implementation of IDidStore

1 lines 6.07 kB
"use strict";exports.id=978,exports.ids=[978],exports.modules={3978:(e,s,o)=>{o.d(s,{fromSSO:()=>p});var n=o(3631),t=o(7073),i=o(8455);const r="To refresh this SSO session run 'aws sso login' with the corresponding profile.",a=e=>{if(e.expiration&&e.expiration.getTime()<Date.now())throw new n.Jh(`Token is expired. ${r}`,!1)},c=(e,s,o=!1)=>{if(void 0===s)throw new n.Jh(`Value not present for '${e}' in SSO Token${o?". Cannot refresh":""}. ${r}`,!1)};var l=o(9896);const{writeFile:g}=l.promises,f=new Date(0),w=(e={})=>async({callerClientConfig:s}={})=>{const i={...e,parentClientConfig:{...s,...e.parentClientConfig}};i.logger?.debug("@aws-sdk/token-providers - fromSso");const l=await(0,t.YU)(i),w=(0,t.Bz)({profile:i.profile??s?.profile}),d=l[w];if(!d)throw new n.Jh(`Profile '${w}' could not be found in shared credentials file.`,!1);if(!d.sso_session)throw new n.Jh(`Profile '${w}' is missing required property 'sso_session'.`);const h=d.sso_session,p=(await(0,t.qw)(i))[h];if(!p)throw new n.Jh(`Sso session '${h}' could not be found in shared credentials file.`,!1);for(const e of["sso_start_url","sso_region"])if(!p[e])throw new n.Jh(`Sso session '${h}' is missing required property '${e}'.`,!1);p.sso_start_url;const u=p.sso_region;let S;try{S=await(0,t.vf)(h)}catch(e){throw new n.Jh(`The SSO session token associated with profile=${w} was not found or is invalid. ${r}`,!1)}c("accessToken",S.accessToken),c("expiresAt",S.expiresAt);const{accessToken:C,expiresAt:_}=S,k={token:C,expiration:new Date(_)};if(k.expiration.getTime()-Date.now()>3e5)return k;if(Date.now()-f.getTime()<3e4)return a(k),k;c("clientId",S.clientId,!0),c("clientSecret",S.clientSecret,!0),c("refreshToken",S.refreshToken,!0);try{f.setTime(Date.now());const e=await(async(e,s,n={})=>{const{CreateTokenCommand:t}=await o.e(240).then(o.bind(o,6240)),i=await(async(e,s={})=>{const{SSOOIDCClient:n}=await o.e(240).then(o.bind(o,6240));return new n(Object.assign({},s.clientConfig??{},{region:e??s.clientConfig?.region,logger:s.clientConfig?.logger??s.parentClientConfig?.logger}))})(s,n);return i.send(new t({clientId:e.clientId,clientSecret:e.clientSecret,refreshToken:e.refreshToken,grantType:"refresh_token"}))})(S,u,i);c("accessToken",e.accessToken),c("expiresIn",e.expiresIn);const s=new Date(Date.now()+1e3*e.expiresIn);try{await((e,s)=>{const o=(0,t.C9)(e),n=JSON.stringify(s,null,2);return g(o,n)})(h,{...S,accessToken:e.accessToken,expiresAt:s.toISOString(),refreshToken:e.refreshToken})}catch(e){}return{token:e.accessToken,expiration:s}}catch(e){return a(k),k}},d=!1,h=async({ssoStartUrl:e,ssoSession:s,ssoAccountId:r,ssoRegion:a,ssoRoleName:c,ssoClient:l,clientConfig:g,parentClientConfig:f,profile:h,logger:p})=>{let u;const S="To refresh this SSO session run aws sso login with the corresponding profile.";if(s)try{const e=await w({profile:h})();u={accessToken:e.token,expiresAt:new Date(e.expiration).toISOString()}}catch(e){throw new n.C1(e.message,{tryNextLink:d,logger:p})}else try{u=await(0,t.vf)(e)}catch(e){throw new n.C1(`The SSO session associated with this profile is invalid. ${S}`,{tryNextLink:d,logger:p})}if(new Date(u.expiresAt).getTime()-Date.now()<=0)throw new n.C1(`The SSO session associated with this profile has expired. ${S}`,{tryNextLink:d,logger:p});const{accessToken:C}=u,{SSOClient:_,GetRoleCredentialsCommand:k}=await o.e(534).then(o.bind(o,8534)),m=l||new _(Object.assign({},g??{},{logger:g?.logger??f?.logger,region:g?.region??a}));let T;try{T=await m.send(new k({accountId:r,roleName:c,accessToken:C}))}catch(e){throw new n.C1(e,{tryNextLink:d,logger:p})}const{roleCredentials:{accessKeyId:y,secretAccessKey:x,sessionToken:O,expiration:I,credentialScope:N,accountId:$}={}}=T;if(!(y&&x&&O&&I))throw new n.C1("SSO returns an invalid temporary credential.",{tryNextLink:d,logger:p});const A={accessKeyId:y,secretAccessKey:x,sessionToken:O,expiration:new Date(I),...N&&{credentialScope:N},...$&&{accountId:$}};return s?(0,i.g)(A,"CREDENTIALS_SSO","s"):(0,i.g)(A,"CREDENTIALS_SSO_LEGACY","u"),A},p=(e={})=>async({callerClientConfig:s}={})=>{e.logger?.debug("@aws-sdk/credential-provider-sso - fromSSO");const{ssoStartUrl:o,ssoAccountId:i,ssoRegion:r,ssoRoleName:a,ssoSession:c}=e,{ssoClient:l}=e,g=(0,t.Bz)({profile:e.profile??s?.profile});if(o||i||r||a||c){if(o&&i&&r&&a)return h({ssoStartUrl:o,ssoSession:c,ssoAccountId:i,ssoRegion:r,ssoRoleName:a,ssoClient:l,clientConfig:e.clientConfig,parentClientConfig:e.parentClientConfig,profile:g});throw new n.C1('Incomplete configuration. The fromSSO() argument hash must include "ssoStartUrl", "ssoAccountId", "ssoRegion", "ssoRoleName"',{tryNextLink:!1,logger:e.logger})}{const s=(await(0,t.YU)(e))[g];if(!s)throw new n.C1(`Profile ${g} was not found.`,{logger:e.logger});if(!(f=s)||"string"!=typeof f.sso_start_url&&"string"!=typeof f.sso_account_id&&"string"!=typeof f.sso_session&&"string"!=typeof f.sso_region&&"string"!=typeof f.sso_role_name)throw new n.C1(`Profile ${g} is not configured with SSO credentials.`,{logger:e.logger});if(s?.sso_session){const i=(await(0,t.qw)(e))[s.sso_session],a=` configurations in profile ${g} and sso-session ${s.sso_session}`;if(r&&r!==i.sso_region)throw new n.C1("Conflicting SSO region"+a,{tryNextLink:!1,logger:e.logger});if(o&&o!==i.sso_start_url)throw new n.C1("Conflicting SSO start_url"+a,{tryNextLink:!1,logger:e.logger});s.sso_region=i.sso_region,s.sso_start_url=i.sso_start_url}const{sso_start_url:i,sso_account_id:a,sso_region:c,sso_role_name:w,sso_session:d}=((e,s)=>{const{sso_start_url:o,sso_account_id:t,sso_region:i,sso_role_name:r}=e;if(!(o&&t&&i&&r))throw new n.C1(`Profile is configured with invalid SSO credentials. Required parameters "sso_account_id", "sso_region", "sso_role_name", "sso_start_url". Got ${Object.keys(e).join(", ")}\nReference: https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-sso.html`,{tryNextLink:!1,logger:s});return e})(s,e.logger);return h({ssoStartUrl:i,ssoSession:d,ssoAccountId:a,ssoRegion:c,ssoRoleName:w,ssoClient:l,clientConfig:e.clientConfig,parentClientConfig:e.parentClientConfig,profile:g})}var f}}};