Skip to content

Commit 8b95de0

Browse files
authored
fix: Add getId to enhanced authflow (#433)
1 parent ac47136 commit 8b95de0

File tree

4 files changed

+38
-67
lines changed

4 files changed

+38
-67
lines changed

src/dispatch/CognitoIdentityClient.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ interface CognitoCredentials {
2929
AccessKeyId: string;
3030
Expiration: number;
3131
SecretAccessKey: string;
32+
SecretKey: string;
3233
SessionToken: string;
3334
}
3435

@@ -46,12 +47,6 @@ interface GetIdResponse {
4647
IdentityId: string;
4748
}
4849

49-
export const fromCognitoIdentityPool = (
50-
params: CognitoProviderParameters
51-
): (() => Promise<Credentials>) => {
52-
return () => params.client.getCredentialsForIdentity(params.identityPoolId);
53-
};
54-
5550
export declare type CognitoIdentityClientConfig = {
5651
fetchRequestHandler: HttpHandler;
5752
region?: string;
@@ -115,11 +110,11 @@ export class CognitoIdentityClient {
115110
const { Credentials } = (await responseToJson(
116111
response
117112
)) as CredentialsResponse;
118-
const { AccessKeyId, Expiration, SecretAccessKey, SessionToken } =
113+
const { AccessKeyId, Expiration, SecretKey, SessionToken } =
119114
Credentials;
120115
return {
121116
accessKeyId: AccessKeyId as string,
122-
secretAccessKey: SecretAccessKey as string,
117+
secretAccessKey: SecretKey as string,
123118
sessionToken: SessionToken as string,
124119
expiration: new Date(Expiration * 1000)
125120
};

src/dispatch/EnhancedAuthentication.ts

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
import {
2-
CognitoIdentityClient,
3-
fromCognitoIdentityPool
4-
} from './CognitoIdentityClient';
1+
import { CognitoIdentityClient } from './CognitoIdentityClient';
52
import { Config } from '../orchestration/Orchestration';
6-
import { CredentialProvider, Credentials } from '@aws-sdk/types';
3+
import { Credentials } from '@aws-sdk/types';
74
import { FetchHttpHandler } from '@aws-sdk/fetch-http-handler';
85
import { CRED_KEY, CRED_RENEW_MS } from '../utils/constants';
96

@@ -96,7 +93,6 @@ export class EnhancedAuthentication {
9693
// The credentials have expired.
9794
return reject();
9895
}
99-
this.credentials = credentials;
10096
resolve(credentials);
10197
});
10298
};
@@ -111,21 +107,26 @@ export class EnhancedAuthentication {
111107
*/
112108
private AnonymousCognitoCredentialsProvider =
113109
async (): Promise<Credentials> => {
114-
const credentialProvider: CredentialProvider =
115-
fromCognitoIdentityPool({
116-
client: this.cognitoIdentityClient,
117-
identityPoolId: this.config.identityPoolId as string
118-
});
110+
return this.cognitoIdentityClient
111+
.getId({ IdentityPoolId: this.config.identityPoolId as string })
112+
.then((getIdResponse) =>
113+
this.cognitoIdentityClient.getCredentialsForIdentity(
114+
getIdResponse.IdentityId
115+
)
116+
)
117+
.then((credentials: Credentials) => {
118+
this.credentials = credentials;
119+
try {
120+
localStorage.setItem(
121+
CRED_KEY,
122+
JSON.stringify(credentials)
123+
);
124+
} catch (e) {
125+
// Ignore
126+
}
119127

120-
return credentialProvider().then((credentials) => {
121-
this.credentials = credentials;
122-
try {
123-
localStorage.setItem(CRED_KEY, JSON.stringify(credentials));
124-
} catch (e) {
125-
// Ignore
126-
}
127-
return credentials;
128-
});
128+
return credentials;
129+
});
129130
};
130131

131132
private renewCredentials(): boolean {

src/dispatch/__tests__/CognitoIdentityClient.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Credentials } from '@aws-sdk/types';
66
import { getReadableStream } from '../../test-utils/test-utils';
77

88
const mockCredentials =
9-
'{ "IdentityId": "a", "Credentials": { "AccessKeyId": "x", "SecretAccessKey": "y", "SessionToken": "z" } }';
9+
'{ "IdentityId": "a", "Credentials": { "AccessKeyId": "x", "SecretKey": "y", "SessionToken": "z" } }';
1010
const mockToken = '{"IdentityId": "mockId", "Token": "mockToken"}';
1111
const mockIdCommand = '{"IdentityId": "mockId"}';
1212

src/dispatch/__tests__/EnhancedAuthentication.test.ts

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
import { CRED_KEY } from '../../utils/constants';
2-
import { Credentials } from '@aws-sdk/types';
32
import { EnhancedAuthentication } from '../EnhancedAuthentication';
4-
import { fromCognitoIdentityPool } from '../CognitoIdentityClient';
53
import { DEFAULT_CONFIG } from '../../test-utils/test-utils';
64

75
const mockGetId = jest.fn();
86
const getCredentials = jest.fn();
97

108
jest.mock('../CognitoIdentityClient', () => ({
11-
fromCognitoIdentityPool: jest.fn(),
129
CognitoIdentityClient: jest.fn().mockImplementation(() => ({
1310
getId: mockGetId,
1411
getCredentialsForIdentity: getCredentials
@@ -31,18 +28,6 @@ describe('EnhancedAuthentication tests', () => {
3128
sessionToken: 'z',
3229
expiration: new Date(Date.now() + 3600 * 1000)
3330
});
34-
(fromCognitoIdentityPool as any).mockReset();
35-
(fromCognitoIdentityPool as any).mockReturnValue(
36-
() =>
37-
new Promise<Credentials>((resolve) =>
38-
resolve({
39-
accessKeyId: 'x',
40-
secretAccessKey: 'y',
41-
sessionToken: 'z',
42-
expiration: new Date(Date.now() + 3600 * 1000)
43-
})
44-
)
45-
);
4631
localStorage.removeItem(CRED_KEY);
4732
});
4833

@@ -169,29 +154,19 @@ describe('EnhancedAuthentication tests', () => {
169154
test('when credential is retrieved from basic auth then next credential is retrieved from localStorage', async () => {
170155
// Init
171156
const expiration = new Date(Date.now() + 3600 * 1000);
172-
(fromCognitoIdentityPool as any)
173-
.mockReturnValueOnce(
174-
() =>
175-
new Promise<Credentials>((resolve) =>
176-
resolve({
177-
accessKeyId: 'a',
178-
secretAccessKey: 'b',
179-
sessionToken: 'c',
180-
expiration
181-
})
182-
)
183-
)
184-
.mockReturnValueOnce(
185-
() =>
186-
new Promise<Credentials>((resolve) =>
187-
resolve({
188-
accessKeyId: 'x',
189-
secretAccessKey: 'y',
190-
sessionToken: 'z',
191-
expiration
192-
})
193-
)
194-
);
157+
getCredentials
158+
.mockResolvedValueOnce({
159+
accessKeyId: 'a',
160+
expiration,
161+
secretAccessKey: 'b',
162+
sessionToken: 'c'
163+
})
164+
.mockResolvedValueOnce({
165+
accessKeyId: 'x',
166+
expiration,
167+
secretAccessKey: 'y',
168+
sessionToken: 'z'
169+
});
195170

196171
const auth = new EnhancedAuthentication({
197172
...DEFAULT_CONFIG,

0 commit comments

Comments
 (0)