Skip to content

Commit 7ab1d2f

Browse files
committed
chore: refactor EnhancedAuthentication to extend Authentication
1 parent 7c8f816 commit 7ab1d2f

File tree

2 files changed

+4
-108
lines changed

2 files changed

+4
-108
lines changed

src/dispatch/BasicAuthentication.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ export class BasicAuthentication extends Authentication {
5151
} catch (e) {
5252
// Ignore
5353
}
54-
5554
return credentials;
5655
});
5756
};
Lines changed: 4 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,12 @@
1-
import { CognitoIdentityClient } from './CognitoIdentityClient';
21
import { Config } from '../orchestration/Orchestration';
32
import { Credentials } from '@aws-sdk/types';
4-
import { FetchHttpHandler } from '@aws-sdk/fetch-http-handler';
5-
import { CRED_KEY, CRED_RENEW_MS } from '../utils/constants';
6-
7-
export class EnhancedAuthentication {
8-
protected cognitoIdentityClient: CognitoIdentityClient;
9-
protected config: Config;
10-
protected credentials: Credentials | undefined;
3+
import { CRED_KEY } from '../utils/constants';
4+
import { Authentication } from './Authentication';
115

6+
export class EnhancedAuthentication extends Authentication {
127
constructor(config: Config) {
13-
const region: string = config.identityPoolId!.split(':')[0];
14-
this.config = config;
15-
this.cognitoIdentityClient = new CognitoIdentityClient({
16-
fetchRequestHandler: new FetchHttpHandler(),
17-
region
18-
});
8+
super(config);
199
}
20-
21-
/**
22-
* A credential provider which provides AWS credentials for an anonymous
23-
* (guest) user. These credentials are retrieved from the first successful
24-
* provider in a chain.
25-
*
26-
* Credentials are stored in and retrieved from localStorage. This prevents the client from having to
27-
* re-authenticate every time the client loads, which (1) improves the performance of the RUM web client and (2)
28-
* reduces the load on AWS services Cognito and STS.
29-
*
30-
* While storing credentials in localStorage puts the credential at greater risk of being leaked through an
31-
* XSS attack, there is no impact if the credentials were to be leaked. This is because (1) the identity pool ID
32-
* and role ARN are public and (2) the credentials are for an anonymous (guest) user.
33-
*
34-
* Regarding (1), the identity pool ID and role ARN are, by necessity, public. These identifiers are shipped with
35-
* each application as part of Cognito's Basic (Classic) authentication flow. The identity pool ID and role ARN
36-
* are not secret.
37-
*
38-
* Regarding (2), the authentication chain implemented in this file only supports anonymous (guest)
39-
* authentication. When the Cognito authentication flow is executed, {@code AnonymousCognitoCredentialsProvider}
40-
* does not communicate with a login provider such as Amazon, Facebook or Google. Instead, it relies on (a) the
41-
* identity pool supporting unauthenticated identities and (b) the IAM role policy enabling login through the
42-
* identity pool. If the identity pool does not support unauthenticated identities, this authentication chain
43-
* will not succeed.
44-
*
45-
* Taken together, (1) and (2) mean that if these temporary credentials were to be leaked, the leaked credentials
46-
* would not allow a bad actor to gain access to anything which they did not already have public access to.
47-
*
48-
* Implements CredentialsProvider = Provider<Credentials>
49-
*/
50-
public ChainAnonymousCredentialsProvider =
51-
async (): Promise<Credentials> => {
52-
return this.AnonymousCredentialsProvider()
53-
.catch(this.AnonymousStorageCredentialsProvider)
54-
.catch(this.AnonymousCognitoCredentialsProvider);
55-
};
56-
57-
/**
58-
* Provides credentials for an anonymous (guest) user. These credentials are read from a member variable.
59-
*
60-
* Implements CredentialsProvider = Provider<Credentials>
61-
*/
62-
private AnonymousCredentialsProvider = async (): Promise<Credentials> => {
63-
return new Promise<Credentials>((resolve, reject) => {
64-
if (this.renewCredentials()) {
65-
// The credentials have expired.
66-
return reject();
67-
}
68-
resolve(this.credentials!);
69-
});
70-
};
71-
72-
/**
73-
* Provides credentials for an anonymous (guest) user. These credentials are read from localStorage.
74-
*
75-
* Implements CredentialsProvider = Provider<Credentials>
76-
*/
77-
private AnonymousStorageCredentialsProvider =
78-
async (): Promise<Credentials> => {
79-
return new Promise<Credentials>((resolve, reject) => {
80-
let credentials: Credentials;
81-
try {
82-
credentials = JSON.parse(localStorage.getItem(CRED_KEY)!);
83-
} catch (e) {
84-
// Error decoding or parsing the cookie -- abort
85-
return reject();
86-
}
87-
// The expiration property of Credentials has a date type. Because the date was serialized as a string,
88-
// we need to convert it back into a date, otherwise the AWS SDK signing middleware
89-
// (@aws-sdk/middleware-signing) will throw an exception and no credentials will be returned.
90-
this.credentials = {
91-
...credentials,
92-
expiration: new Date(credentials.expiration as Date)
93-
};
94-
if (this.renewCredentials()) {
95-
// The credentials have expired.
96-
return reject();
97-
}
98-
resolve(this.credentials);
99-
});
100-
};
101-
10210
/**
10311
* Provides credentials for an anonymous (guest) user. These credentials are retrieved from Cognito's enhanced
10412
* authflow.
@@ -126,18 +34,7 @@ export class EnhancedAuthentication {
12634
} catch (e) {
12735
// Ignore
12836
}
129-
13037
return credentials;
13138
});
13239
};
133-
134-
private renewCredentials(): boolean {
135-
if (!this.credentials || !this.credentials.expiration) {
136-
return true;
137-
}
138-
const renewalTime: Date = new Date(
139-
this.credentials.expiration.getTime() - CRED_RENEW_MS
140-
);
141-
return new Date() > renewalTime;
142-
}
14340
}

0 commit comments

Comments
 (0)