|
1 |
| -import { CognitoIdentityClient } from './CognitoIdentityClient'; |
2 | 1 | import { Config } from '../orchestration/Orchestration';
|
3 | 2 | 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'; |
11 | 5 |
|
| 6 | +export class EnhancedAuthentication extends Authentication { |
12 | 7 | 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); |
19 | 9 | }
|
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 |
| - |
102 | 10 | /**
|
103 | 11 | * Provides credentials for an anonymous (guest) user. These credentials are retrieved from Cognito's enhanced
|
104 | 12 | * authflow.
|
@@ -126,18 +34,7 @@ export class EnhancedAuthentication {
|
126 | 34 | } catch (e) {
|
127 | 35 | // Ignore
|
128 | 36 | }
|
129 |
| - |
130 | 37 | return credentials;
|
131 | 38 | });
|
132 | 39 | };
|
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 |
| - } |
143 | 40 | }
|
0 commit comments