Skip to content

Commit

Permalink
fix: Save deserialized credential object to Authentication member (#436)
Browse files Browse the repository at this point in the history
* fix: Save deserialized credential object to Authentication member
---------

Co-authored-by: Quinn Hanam <hanquinn@amazon.com>
  • Loading branch information
adebayor123 and qhanam authored Aug 23, 2023
1 parent 8b95de0 commit 6120c61
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 7 deletions.
3 changes: 1 addition & 2 deletions src/dispatch/Authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ export class Authentication {
// The credentials have expired.
return reject();
}
this.credentials = credentials;
resolve(credentials);
resolve(this.credentials);
});
};

Expand Down
12 changes: 7 additions & 5 deletions src/dispatch/EnhancedAuthentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,25 @@ export class EnhancedAuthentication {
private AnonymousStorageCredentialsProvider =
async (): Promise<Credentials> => {
return new Promise<Credentials>((resolve, reject) => {
let credentials;
let credentials: Credentials;
try {
credentials = JSON.parse(localStorage.getItem(CRED_KEY)!);
} catch (e) {
// Error decoding or parsing the cookie -- abort
reject();
return reject();
}
// The expiration property of Credentials has a date type. Because the date was serialized as a string,
// we need to convert it back into a date, otherwise the AWS SDK signing middleware
// (@aws-sdk/middleware-signing) will throw an exception and no credentials will be returned.
credentials.expiration = new Date(credentials.expiration);
this.credentials = credentials;
this.credentials = {
...credentials,
expiration: new Date(credentials.expiration as Date)
};
if (this.renewCredentials()) {
// The credentials have expired.
return reject();
}
resolve(credentials);
resolve(this.credentials);
});
};

Expand Down
65 changes: 65 additions & 0 deletions src/dispatch/__tests__/Authentication.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,4 +336,69 @@ describe('Authentication tests', () => {
})
);
});

test('when credentials are read from storage then a new date object is created', async () => {
// Init
const storageExpiration = new Date(Date.now() + 3600 * 1000);

localStorage.setItem(
CRED_KEY,
JSON.stringify({
accessKeyId: 'a',
secretAccessKey: 'b',
sessionToken: 'c',
expiration: storageExpiration
})
);

const config = {
...DEFAULT_CONFIG,
...{
identityPoolId: IDENTITY_POOL_ID,
guestRoleArn: GUEST_ROLE_ARN
}
};
const auth = new Authentication(config);

// Run
const credentials = await auth.ChainAnonymousCredentialsProvider();

// Assert
expect(credentials.expiration!.getTime()).toEqual(
storageExpiration.getTime()
);
});

test('when credentials are read from storage then the member variable stores the expiration as a date object', async () => {
// Init
const storageExpiration = new Date(Date.now() + 3600 * 1000);

localStorage.setItem(
CRED_KEY,
JSON.stringify({
accessKeyId: 'a',
secretAccessKey: 'b',
sessionToken: 'c',
expiration: storageExpiration
})
);

const config = {
...DEFAULT_CONFIG,
...{
identityPoolId: IDENTITY_POOL_ID,
guestRoleArn: GUEST_ROLE_ARN
}
};
const auth = new Authentication(config);

// Run
await auth.ChainAnonymousCredentialsProvider();
const credentials = await auth.ChainAnonymousCredentialsProvider();

// Assert
expect(credentials.expiration!.getTime()).toEqual(
storageExpiration.getTime()
);
});
});
75 changes: 75 additions & 0 deletions src/dispatch/__tests__/EnhancedAuthentication.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,4 +305,79 @@ describe('EnhancedAuthentication tests', () => {
})
);
});

test('when credentials are read from storage then a new date object is created', async () => {
// Init
const fetchExpiration = new Date(0);
const storageExpiration = new Date(Date.now() + 3600 * 1000);
getCredentials.mockResolvedValue({
accessKeyId: 'x',
secretAccessKey: 'y',
sessionToken: 'z',
expiration: fetchExpiration
});

localStorage.setItem(
CRED_KEY,
JSON.stringify({
accessKeyId: 'a',
secretAccessKey: 'b',
sessionToken: 'c',
expiration: storageExpiration
})
);

const auth = new EnhancedAuthentication({
...DEFAULT_CONFIG,
...{
identityPoolId: IDENTITY_POOL_ID
}
});

// Run
const credentials = await auth.ChainAnonymousCredentialsProvider();

// Assert
expect(credentials.expiration!.getTime()).toEqual(
storageExpiration.getTime()
);
});

test('when credentials are read from storage then the member variable stores the expiration as a date object', async () => {
// Init
const fetchExpiration = new Date(0);
const storageExpiration = new Date(Date.now() + 3600 * 1000);
getCredentials.mockResolvedValue({
accessKeyId: 'x',
secretAccessKey: 'y',
sessionToken: 'z',
expiration: fetchExpiration
});

localStorage.setItem(
CRED_KEY,
JSON.stringify({
accessKeyId: 'a',
secretAccessKey: 'b',
sessionToken: 'c',
expiration: storageExpiration
})
);

const auth = new EnhancedAuthentication({
...DEFAULT_CONFIG,
...{
identityPoolId: IDENTITY_POOL_ID
}
});

// Run
await auth.ChainAnonymousCredentialsProvider();
const credentials = await auth.ChainAnonymousCredentialsProvider();

// Assert
expect(credentials.expiration!.getTime()).toEqual(
storageExpiration.getTime()
);
});
});

0 comments on commit 6120c61

Please sign in to comment.