11import { CognitoIdentityClient } from './CognitoIdentityClient' ;
22import { Config } from '../orchestration/Orchestration' ;
3- import { Credentials } from '@aws-sdk/types' ;
3+ import { AwsCredentialIdentity } from '@aws-sdk/types' ;
44import { FetchHttpHandler } from '@aws-sdk/fetch-http-handler' ;
5- import { StsClient } from './StsClient' ;
65import { CRED_KEY , CRED_RENEW_MS } from '../utils/constants' ;
76
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 ;
1311
1412 constructor ( config : Config ) {
1513 const region : string = config . identityPoolId ! . split ( ':' ) [ 0 ] ;
1614 this . config = config ;
17- this . stsClient = new StsClient ( {
18- fetchRequestHandler : new FetchHttpHandler ( ) ,
19- region
20- } ) ;
2115 this . cognitoIdentityClient = new CognitoIdentityClient ( {
2216 fetchRequestHandler : new FetchHttpHandler ( ) ,
2317 region
@@ -33,7 +27,7 @@ export class Authentication {
3327 * re-authenticate every time the client loads, which (1) improves the performance of the RUM web client and (2)
3428 * reduces the load on AWS services Cognito and STS.
3529 *
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
3731 * XSS attack, there is no impact if the credentials were to be leaked. This is because (1) the identity pool ID
3832 * and role ARN are public and (2) the credentials are for an anonymous (guest) user.
3933 *
@@ -51,10 +45,10 @@ export class Authentication {
5145 * Taken together, (1) and (2) mean that if these temporary credentials were to be leaked, the leaked credentials
5246 * would not allow a bad actor to gain access to anything which they did not already have public access to.
5347 *
54- * Implements CredentialsProvider = Provider<Credentials >
48+ * Implements AwsCredentialIdentityProvider = Provider<AwsCredentialIdentity >
5549 */
5650 public ChainAnonymousCredentialsProvider =
57- async ( ) : Promise < Credentials > => {
51+ async ( ) : Promise < AwsCredentialIdentity > => {
5852 return this . AnonymousCredentialsProvider ( )
5953 . catch ( this . AnonymousStorageCredentialsProvider )
6054 . catch ( this . AnonymousCognitoCredentialsProvider ) ;
@@ -63,27 +57,28 @@ export class Authentication {
6357 /**
6458 * Provides credentials for an anonymous (guest) user. These credentials are read from a member variable.
6559 *
66- * Implements CredentialsProvider = Provider<Credentials >
60+ * Implements AwsCredentialIdentityProvider = Provider<AwsCredentialIdentity >
6761 */
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+ } ;
7772
7873 /**
7974 * Provides credentials for an anonymous (guest) user. These credentials are read from localStorage.
8075 *
81- * Implements CredentialsProvider = Provider<Credentials >
76+ * Implements AwsCredentialIdentityProvider = Provider<AwsCredentialIdentity >
8277 */
8378 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 ;
8782 try {
8883 credentials = JSON . parse ( localStorage . getItem ( CRED_KEY ) ! ) ;
8984 } catch ( e ) {
@@ -106,44 +101,18 @@ export class Authentication {
106101 } ;
107102
108103 /**
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.
111106 *
112107 * See https://docs.aws.amazon.com/cognito/latest/developerguide/authentication-flow.html
113108 *
114- * Implements CredentialsProvider = Provider<Credentials >
109+ * Implements AwsCredentialIdentityProvider = Provider<AwsCredentialIdentity >
115110 */
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 > ;
146112
113+ /**
114+ * Returns {@code true} when the credentials need to be renewed.
115+ */
147116 private renewCredentials ( ) : boolean {
148117 if ( ! this . credentials || ! this . credentials . expiration ) {
149118 return true ;
0 commit comments