1
1
import { CognitoIdentityClient } from './CognitoIdentityClient' ;
2
2
import { Config } from '../orchestration/Orchestration' ;
3
- import { Credentials } from '@aws-sdk/types' ;
3
+ import { AwsCredentialIdentity } from '@aws-sdk/types' ;
4
4
import { FetchHttpHandler } from '@aws-sdk/fetch-http-handler' ;
5
- import { StsClient } from './StsClient' ;
6
5
import { CRED_KEY , CRED_RENEW_MS } from '../utils/constants' ;
7
6
8
- export class Authentication {
9
- private cognitoIdentityClient : CognitoIdentityClient ;
10
- private stsClient : StsClient ;
11
- private config : Config ;
12
- private credentials : Credentials | undefined ;
7
+ export abstract class Authentication {
8
+ protected cognitoIdentityClient : CognitoIdentityClient ;
9
+ protected config : Config ;
10
+ protected credentials : AwsCredentialIdentity | undefined ;
13
11
14
12
constructor ( config : Config ) {
15
13
const region : string = config . identityPoolId ! . split ( ':' ) [ 0 ] ;
16
14
this . config = config ;
17
- this . stsClient = new StsClient ( {
18
- fetchRequestHandler : new FetchHttpHandler ( ) ,
19
- region
20
- } ) ;
21
15
this . cognitoIdentityClient = new CognitoIdentityClient ( {
22
16
fetchRequestHandler : new FetchHttpHandler ( ) ,
23
17
region
@@ -33,7 +27,7 @@ export class Authentication {
33
27
* re-authenticate every time the client loads, which (1) improves the performance of the RUM web client and (2)
34
28
* reduces the load on AWS services Cognito and STS.
35
29
*
36
- * While storing credentials in localStorage puts the cookie at greater risk of being leaked through an
30
+ * While storing credentials in localStorage puts the credential at greater risk of being leaked through an
37
31
* XSS attack, there is no impact if the credentials were to be leaked. This is because (1) the identity pool ID
38
32
* and role ARN are public and (2) the credentials are for an anonymous (guest) user.
39
33
*
@@ -51,10 +45,10 @@ export class Authentication {
51
45
* Taken together, (1) and (2) mean that if these temporary credentials were to be leaked, the leaked credentials
52
46
* would not allow a bad actor to gain access to anything which they did not already have public access to.
53
47
*
54
- * Implements CredentialsProvider = Provider<Credentials >
48
+ * Implements AwsCredentialIdentityProvider = Provider<AwsCredentialIdentity >
55
49
*/
56
50
public ChainAnonymousCredentialsProvider =
57
- async ( ) : Promise < Credentials > => {
51
+ async ( ) : Promise < AwsCredentialIdentity > => {
58
52
return this . AnonymousCredentialsProvider ( )
59
53
. catch ( this . AnonymousStorageCredentialsProvider )
60
54
. catch ( this . AnonymousCognitoCredentialsProvider ) ;
@@ -63,27 +57,28 @@ export class Authentication {
63
57
/**
64
58
* Provides credentials for an anonymous (guest) user. These credentials are read from a member variable.
65
59
*
66
- * Implements CredentialsProvider = Provider<Credentials >
60
+ * Implements AwsCredentialIdentityProvider = Provider<AwsCredentialIdentity >
67
61
*/
68
- private AnonymousCredentialsProvider = async ( ) : Promise < Credentials > => {
69
- return new Promise < Credentials > ( ( resolve , reject ) => {
70
- if ( this . renewCredentials ( ) ) {
71
- // The credentials have expired.
72
- return reject ( ) ;
73
- }
74
- resolve ( this . credentials ! ) ;
75
- } ) ;
76
- } ;
62
+ private AnonymousCredentialsProvider =
63
+ async ( ) : Promise < AwsCredentialIdentity > => {
64
+ return new Promise < AwsCredentialIdentity > ( ( resolve , reject ) => {
65
+ if ( this . renewCredentials ( ) ) {
66
+ // The credentials have expired.
67
+ return reject ( ) ;
68
+ }
69
+ resolve ( this . credentials ! ) ;
70
+ } ) ;
71
+ } ;
77
72
78
73
/**
79
74
* Provides credentials for an anonymous (guest) user. These credentials are read from localStorage.
80
75
*
81
- * Implements CredentialsProvider = Provider<Credentials >
76
+ * Implements AwsCredentialIdentityProvider = Provider<AwsCredentialIdentity >
82
77
*/
83
78
private AnonymousStorageCredentialsProvider =
84
- async ( ) : Promise < Credentials > => {
85
- return new Promise < Credentials > ( ( resolve , reject ) => {
86
- let credentials : Credentials ;
79
+ async ( ) : Promise < AwsCredentialIdentity > => {
80
+ return new Promise < AwsCredentialIdentity > ( ( resolve , reject ) => {
81
+ let credentials : AwsCredentialIdentity ;
87
82
try {
88
83
credentials = JSON . parse ( localStorage . getItem ( CRED_KEY ) ! ) ;
89
84
} catch ( e ) {
@@ -106,44 +101,18 @@ export class Authentication {
106
101
} ;
107
102
108
103
/**
109
- * Provides credentials for an anonymous (guest) user. These credentials are retrieved from Cognito's basic
110
- * (classic) authflow.
104
+ * Provides credentials for an anonymous (guest) user. These credentials are retrieved from Cognito's enhanced
105
+ * authflow.
111
106
*
112
107
* See https://docs.aws.amazon.com/cognito/latest/developerguide/authentication-flow.html
113
108
*
114
- * Implements CredentialsProvider = Provider<Credentials >
109
+ * Implements AwsCredentialIdentityProvider = Provider<AwsCredentialIdentity >
115
110
*/
116
- private AnonymousCognitoCredentialsProvider =
117
- async ( ) : Promise < Credentials > => {
118
- return this . cognitoIdentityClient
119
- . getId ( {
120
- IdentityPoolId : this . config . identityPoolId as string
121
- } )
122
- . then ( ( getIdResponse ) =>
123
- this . cognitoIdentityClient . getOpenIdToken ( getIdResponse )
124
- )
125
- . then ( ( getOpenIdTokenResponse ) =>
126
- this . stsClient . assumeRoleWithWebIdentity ( {
127
- RoleArn : this . config . guestRoleArn as string ,
128
- RoleSessionName : 'cwr' ,
129
- WebIdentityToken : getOpenIdTokenResponse . Token
130
- } )
131
- )
132
- . then ( ( credentials : Credentials ) => {
133
- this . credentials = credentials ;
134
- try {
135
- localStorage . setItem (
136
- CRED_KEY ,
137
- JSON . stringify ( credentials )
138
- ) ;
139
- } catch ( e ) {
140
- // Ignore
141
- }
142
-
143
- return credentials ;
144
- } ) ;
145
- } ;
111
+ protected abstract AnonymousCognitoCredentialsProvider : ( ) => Promise < AwsCredentialIdentity > ;
146
112
113
+ /**
114
+ * Returns {@code true} when the credentials need to be renewed.
115
+ */
147
116
private renewCredentials ( ) : boolean {
148
117
if ( ! this . credentials || ! this . credentials . expiration ) {
149
118
return true ;
0 commit comments