Skip to content

Commit

Permalink
Merge pull request #5 from eduardomourar/feature/support-typescript
Browse files Browse the repository at this point in the history
Instantiate SSO service only once
  • Loading branch information
ljacobsson authored Mar 20, 2021
2 parents 3bae083 + 2c64845 commit f27c18c
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ exports.SingleSignOnCredentials = AWS.SingleSignOnCredentials = AWS.util.inherit
AWS.Credentials.call(this);

options = options || {};
this.errorCode = 'SingleSignOnCredentialsProviderFailure';
this.expired = true;

this.filename = options.filename;
this.profile = options.profile || process.env.AWS_PROFILE || AWS.util.defaultProfile;
this.service = new AWS.SSO();
this.get(options.callback || AWS.util.fn.noop);
},

Expand All @@ -40,13 +43,12 @@ exports.SingleSignOnCredentials = AWS.SingleSignOnCredentials = AWS.util.inherit
var profile = profiles[this.profile] || {};

if (Object.keys(profile).length === 0) {
callback(AWS.util.error(Error(`Profile ${this.profile} not found`), { code: self.errorCode }), null);
throw Error(`Profile ${this.profile} not found`);
}
if (!profile.sso_start_url) {
throw Error(`No sso_start_url set for profile ${this.profile}`);
}
AWS.config.update({ region: profile.sso_region });
const sso = new AWS.SSO();

const fileName = `${sha1(profile.sso_start_url)}.json`;

Expand All @@ -69,17 +71,24 @@ exports.SingleSignOnCredentials = AWS.SingleSignOnCredentials = AWS.util.inherit
);
}

const region = profile.sso_region;
const endpoint = `portal.sso.${region}.amazonaws.com`;
// The endpoint configuration is not automatically updated as per issue:
// https://github.com/aws/aws-sdk-js/issues/3558
self.service.config.update({ region, endpoint });
self.service.endpoint = new AWS.Endpoint(endpoint, self.service.config);

const request = {
accessToken: cacheObj.accessToken,
accountId: profile.sso_account_id,
roleName: profile.sso_role_name,
};
sso.getRoleCredentials(request, (err, c) => {
self.service.getRoleCredentials(request, (err, c) => {
if (err || !c) {
console.log(err)
console.log(err, { accountId: request.accountId, roleName: request.roleName })
callback(AWS.util.error(
new Error(err?.message || 'Please log in using "aws sso login"'),
{ code: 'SingleSignOnCredentialsProviderFailure'}
Error(err ? err.message : 'Please log in using "aws sso login"'),
{ code: self.errorCode }
), null);
return;
}
Expand All @@ -94,10 +103,7 @@ exports.SingleSignOnCredentials = AWS.SingleSignOnCredentials = AWS.util.inherit
});
} catch (err) {
console.log(err);
callback(AWS.util.error(
new Error(err.message),
{ code: 'SingleSignOnCredentialsProviderFailure'}
), null);
callback(AWS.util.error(err, { code: self.errorCode }), null);
}
},

Expand Down

0 comments on commit f27c18c

Please sign in to comment.