Skip to content

Commit 6120c61

Browse files
Reno Seoqhanam
andauthored
fix: Save deserialized credential object to Authentication member (#436)
* fix: Save deserialized credential object to Authentication member --------- Co-authored-by: Quinn Hanam <[email protected]>
1 parent 8b95de0 commit 6120c61

File tree

4 files changed

+148
-7
lines changed

4 files changed

+148
-7
lines changed

src/dispatch/Authentication.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@ export class Authentication {
101101
// The credentials have expired.
102102
return reject();
103103
}
104-
this.credentials = credentials;
105-
resolve(credentials);
104+
resolve(this.credentials);
106105
});
107106
};
108107

src/dispatch/EnhancedAuthentication.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,23 +77,25 @@ export class EnhancedAuthentication {
7777
private AnonymousStorageCredentialsProvider =
7878
async (): Promise<Credentials> => {
7979
return new Promise<Credentials>((resolve, reject) => {
80-
let credentials;
80+
let credentials: Credentials;
8181
try {
8282
credentials = JSON.parse(localStorage.getItem(CRED_KEY)!);
8383
} catch (e) {
8484
// Error decoding or parsing the cookie -- abort
85-
reject();
85+
return reject();
8686
}
8787
// The expiration property of Credentials has a date type. Because the date was serialized as a string,
8888
// we need to convert it back into a date, otherwise the AWS SDK signing middleware
8989
// (@aws-sdk/middleware-signing) will throw an exception and no credentials will be returned.
90-
credentials.expiration = new Date(credentials.expiration);
91-
this.credentials = credentials;
90+
this.credentials = {
91+
...credentials,
92+
expiration: new Date(credentials.expiration as Date)
93+
};
9294
if (this.renewCredentials()) {
9395
// The credentials have expired.
9496
return reject();
9597
}
96-
resolve(credentials);
98+
resolve(this.credentials);
9799
});
98100
};
99101

src/dispatch/__tests__/Authentication.test.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,4 +336,69 @@ describe('Authentication tests', () => {
336336
})
337337
);
338338
});
339+
340+
test('when credentials are read from storage then a new date object is created', async () => {
341+
// Init
342+
const storageExpiration = new Date(Date.now() + 3600 * 1000);
343+
344+
localStorage.setItem(
345+
CRED_KEY,
346+
JSON.stringify({
347+
accessKeyId: 'a',
348+
secretAccessKey: 'b',
349+
sessionToken: 'c',
350+
expiration: storageExpiration
351+
})
352+
);
353+
354+
const config = {
355+
...DEFAULT_CONFIG,
356+
...{
357+
identityPoolId: IDENTITY_POOL_ID,
358+
guestRoleArn: GUEST_ROLE_ARN
359+
}
360+
};
361+
const auth = new Authentication(config);
362+
363+
// Run
364+
const credentials = await auth.ChainAnonymousCredentialsProvider();
365+
366+
// Assert
367+
expect(credentials.expiration!.getTime()).toEqual(
368+
storageExpiration.getTime()
369+
);
370+
});
371+
372+
test('when credentials are read from storage then the member variable stores the expiration as a date object', async () => {
373+
// Init
374+
const storageExpiration = new Date(Date.now() + 3600 * 1000);
375+
376+
localStorage.setItem(
377+
CRED_KEY,
378+
JSON.stringify({
379+
accessKeyId: 'a',
380+
secretAccessKey: 'b',
381+
sessionToken: 'c',
382+
expiration: storageExpiration
383+
})
384+
);
385+
386+
const config = {
387+
...DEFAULT_CONFIG,
388+
...{
389+
identityPoolId: IDENTITY_POOL_ID,
390+
guestRoleArn: GUEST_ROLE_ARN
391+
}
392+
};
393+
const auth = new Authentication(config);
394+
395+
// Run
396+
await auth.ChainAnonymousCredentialsProvider();
397+
const credentials = await auth.ChainAnonymousCredentialsProvider();
398+
399+
// Assert
400+
expect(credentials.expiration!.getTime()).toEqual(
401+
storageExpiration.getTime()
402+
);
403+
});
339404
});

src/dispatch/__tests__/EnhancedAuthentication.test.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,4 +305,79 @@ describe('EnhancedAuthentication tests', () => {
305305
})
306306
);
307307
});
308+
309+
test('when credentials are read from storage then a new date object is created', async () => {
310+
// Init
311+
const fetchExpiration = new Date(0);
312+
const storageExpiration = new Date(Date.now() + 3600 * 1000);
313+
getCredentials.mockResolvedValue({
314+
accessKeyId: 'x',
315+
secretAccessKey: 'y',
316+
sessionToken: 'z',
317+
expiration: fetchExpiration
318+
});
319+
320+
localStorage.setItem(
321+
CRED_KEY,
322+
JSON.stringify({
323+
accessKeyId: 'a',
324+
secretAccessKey: 'b',
325+
sessionToken: 'c',
326+
expiration: storageExpiration
327+
})
328+
);
329+
330+
const auth = new EnhancedAuthentication({
331+
...DEFAULT_CONFIG,
332+
...{
333+
identityPoolId: IDENTITY_POOL_ID
334+
}
335+
});
336+
337+
// Run
338+
const credentials = await auth.ChainAnonymousCredentialsProvider();
339+
340+
// Assert
341+
expect(credentials.expiration!.getTime()).toEqual(
342+
storageExpiration.getTime()
343+
);
344+
});
345+
346+
test('when credentials are read from storage then the member variable stores the expiration as a date object', async () => {
347+
// Init
348+
const fetchExpiration = new Date(0);
349+
const storageExpiration = new Date(Date.now() + 3600 * 1000);
350+
getCredentials.mockResolvedValue({
351+
accessKeyId: 'x',
352+
secretAccessKey: 'y',
353+
sessionToken: 'z',
354+
expiration: fetchExpiration
355+
});
356+
357+
localStorage.setItem(
358+
CRED_KEY,
359+
JSON.stringify({
360+
accessKeyId: 'a',
361+
secretAccessKey: 'b',
362+
sessionToken: 'c',
363+
expiration: storageExpiration
364+
})
365+
);
366+
367+
const auth = new EnhancedAuthentication({
368+
...DEFAULT_CONFIG,
369+
...{
370+
identityPoolId: IDENTITY_POOL_ID
371+
}
372+
});
373+
374+
// Run
375+
await auth.ChainAnonymousCredentialsProvider();
376+
const credentials = await auth.ChainAnonymousCredentialsProvider();
377+
378+
// Assert
379+
expect(credentials.expiration!.getTime()).toEqual(
380+
storageExpiration.getTime()
381+
);
382+
});
308383
});

0 commit comments

Comments
 (0)