@@ -37,6 +37,7 @@ import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3';
3737// non-state variables that may be mutated outside of store
3838// set via initializers at run time
3939let awsCredentials ;
40+ let refreshCredentials = true ;
4041let pollyClient ;
4142let lexClient ;
4243let audio ;
@@ -675,7 +676,7 @@ export default {
675676 } ,
676677 deleteSession ( context ) {
677678 context . commit ( 'setIsLexProcessing' , true ) ;
678- return context . dispatch ( 'refreshAuthTokens ' )
679+ return context . dispatch ( 'checkCredentialsForRefresh ' )
679680 . then ( ( ) => context . dispatch ( 'getCredentials' , context . state . config ) )
680681 . then ( ( ) => lexClient . deleteSession ( ) )
681682 . then ( ( data ) => {
@@ -690,7 +691,7 @@ export default {
690691 } ,
691692 startNewSession ( context ) {
692693 context . commit ( 'setIsLexProcessing' , true ) ;
693- return context . dispatch ( 'refreshAuthTokens ' )
694+ return context . dispatch ( 'checkCredentialsForRefresh ' )
694695 . then ( ( ) => context . dispatch ( 'getCredentials' , context . state . config ) )
695696 . then ( ( ) => lexClient . startNewSession ( ) )
696697 . then ( ( data ) => {
@@ -712,7 +713,7 @@ export default {
712713 ? context . state . config . lex . v2BotLocaleId . split ( ',' ) [ 0 ]
713714 : undefined ;
714715 const sessionId = lexClient . userId ;
715- return context . dispatch ( 'refreshAuthTokens ' )
716+ return context . dispatch ( 'checkCredentialsForRefresh ' )
716717 . then ( ( ) => context . dispatch ( 'getCredentials' , context . state . config ) )
717718 . then ( ( ) => {
718719 // TODO: Need to handle if the error occurred. typing would be broke since lexClient.postText throw error
@@ -762,7 +763,7 @@ export default {
762763 console . info ( 'audio blob size:' , audioBlob . size ) ;
763764 let timeStart ;
764765
765- return context . dispatch ( 'refreshAuthTokens ' )
766+ return context . dispatch ( 'checkCredentialsForRefresh ' )
766767 . then ( ( ) => context . dispatch ( 'getCredentials' , context . state . config ) )
767768 . then ( ( ) => {
768769 const localeId = context . state . config . lex . v2BotLocaleId
@@ -1106,7 +1107,7 @@ export default {
11061107 *
11071108 **********************************************************************/
11081109
1109- getCredentialsFromParent ( context ) {
1110+ getCredentialsFromParent ( context , region ) {
11101111 const expireTime = ( awsCredentials && awsCredentials . expireTime ) ?
11111112 awsCredentials . expireTime : 0 ;
11121113 const credsExpirationDate = new Date ( expireTime ) . getTime ( ) ;
@@ -1124,75 +1125,102 @@ export default {
11241125 return Promise . reject ( error ) ;
11251126 } )
11261127 . then ( ( creds ) => {
1127- const { accessKeyId, identityId, secretAccessKey, sessionToken } = creds ;
1128+ const { accessKeyId, identityId, secretAccessKey, sessionToken, expiration } = creds ;
11281129 // recreate as a static credential
1129- awsCredentials = {
1130+ awsCredentials = Promise . resolve ( {
11301131 accessKeyId : accessKeyId ,
11311132 secretAccessKey : secretAccessKey ,
11321133 sessionToken : sessionToken ,
11331134 identityId : identityId ,
1134- expired : false ,
1135- } ;
1135+ expiration : expiration ,
1136+ } ) ;
1137+
1138+ if ( lexClient ) {
1139+ lexClient . refreshClient ( region , awsCredentials ) ;
1140+ }
11361141
11371142 return awsCredentials ;
11381143 } ) ;
11391144 } ,
11401145 async getCredentials ( context , config ) {
1141- if ( context . state . awsCreds . provider === 'parentWindow' ) {
1142- return context . dispatch ( 'getCredentialsFromParent' ) ;
1143- }
1144-
1145- if ( awsCredentials ) {
1146- return awsCredentials ;
1147- }
1148-
1149- const region = config . cognito . region || config . region || 'us-east-1' ;
1150- const poolId = config . cognito . poolId || localStorage . getItem ( 'poolId' ) ;
1151- const appUserPoolName = config . cognito . appUserPoolName || localStorage . getItem ( 'appUserPoolName' ) ;
1152- const appUserPoolClientId = config . cognito . appUserPoolClientId || localStorage . getItem ( 'appUserPoolClientId' ) ;
1153- const idToken = config . lex . sessionAttributes . idtokenjwt || localStorage . getItem ( `${ appUserPoolClientId } idtokenjwt` ) ;
1154-
1155- if ( idToken ) {
1156- logins = { } ;
1157- logins [ `cognito-idp.${ region } .amazonaws.com/${ appUserPoolName } ` ] = idToken ;
1158- const client = new CognitoIdentityClient ( { region } ) ;
1159- const getIdentityId = new GetIdCommand ( {
1160- IdentityPoolId : poolId ,
1161- Logins : logins ? logins : { }
1162- } )
1163- let getCreds ;
1164- try {
1165- await client . send ( getIdentityId )
1166- . then ( ( res ) => {
1167- identityId = res . IdentityId ;
1168- getCreds = new GetCredentialsForIdentityCommand ( {
1169- IdentityId : identityId ,
1170- Logins : logins ? logins : { }
1146+ if ( refreshCredentials ) {
1147+ const region = config . cognito . region || config . region || 'us-east-1' ;
1148+
1149+ if ( context . state . awsCreds . provider === 'parentWindow' ) {
1150+ return context . dispatch ( 'getCredentialsFromParent' , region ) ;
1151+ }
1152+
1153+ const poolId = config . cognito . poolId || localStorage . getItem ( 'poolId' ) ;
1154+ const appUserPoolName = config . cognito . appUserPoolName || localStorage . getItem ( 'appUserPoolName' ) ;
1155+ const appUserPoolClientId = config . cognito . appUserPoolClientId || localStorage . getItem ( 'appUserPoolClientId' ) ;
1156+ const idToken = config . lex . sessionAttributes . idtokenjwt || localStorage . getItem ( `${ appUserPoolClientId } idtokenjwt` ) ;
1157+
1158+ if ( idToken ) {
1159+ logins = { } ;
1160+ logins [ `cognito-idp.${ region } .amazonaws.com/${ appUserPoolName } ` ] = idToken ;
1161+ const client = new CognitoIdentityClient ( { region } ) ;
1162+ const getIdentityId = new GetIdCommand ( {
1163+ IdentityPoolId : poolId ,
1164+ Logins : logins ? logins : { }
1165+ } )
1166+ let getCreds ;
1167+ try {
1168+ await client . send ( getIdentityId )
1169+ . then ( ( res ) => {
1170+ identityId = res . IdentityId ;
1171+ getCreds = new GetCredentialsForIdentityCommand ( {
1172+ IdentityId : identityId ,
1173+ Logins : logins ? logins : { }
1174+ } )
11711175 } )
1172- } )
1173- const res = await client . send ( getCreds ) ;
1174- const creds = res . Credentials ;
1175- const credentials = {
1176- accessKeyId : creds . AccessKeyId ,
1177- identityId,
1178- secretAccessKey : creds . SecretKey ,
1179- sessionToken : creds . SessionToken ,
1180- expiration : creds . Expiration ,
1181- } ;
1182- return credentials ;
1183- } catch ( err ) {
1184- console . log ( err )
1176+ const res = await client . send ( getCreds ) ;
1177+ const creds = res . Credentials ;
1178+ const credentials = {
1179+ accessKeyId : creds . AccessKeyId ,
1180+ identityId,
1181+ secretAccessKey : creds . SecretKey ,
1182+ sessionToken : creds . SessionToken ,
1183+ expiration : creds . Expiration ,
1184+ } ;
1185+ if ( lexClient ) {
1186+ lexClient . refreshClient ( region , credentials ) ;
1187+ }
1188+ return credentials ;
1189+ } catch ( err ) {
1190+ console . log ( err )
1191+ }
1192+ } else {
1193+ const credentialProvider = fromCognitoIdentityPool ( {
1194+ identityPoolId : poolId ,
1195+ clientConfig : { region } ,
1196+ } )
1197+ awsCredentials = credentialProvider ( ) ;
1198+ if ( lexClient ) {
1199+ lexClient . refreshClient ( region , awsCredentials ) ;
1200+ }
1201+ return awsCredentials ;
11851202 }
1186- } else {
1187- const credentialProvider = fromCognitoIdentityPool ( {
1188- identityPoolId : poolId ,
1189- clientConfig : { region } ,
1190- } )
1191- const credentials = credentialProvider ( ) ;
1192- return credentials ;
11931203 }
11941204 } ,
1195-
1205+ checkCredentialsForRefresh ( ) {
1206+ if ( awsCredentials ) {
1207+ awsCredentials . then ( ( res ) => {
1208+ if ( res . expiration ) {
1209+ const expiration = new Date ( res . expiration ) . getTime ( ) ;
1210+ const now = Date . now ( ) ;
1211+ // calculate and expiration time 5 minutes sooner and adjust to milliseconds
1212+ // to compare with now.
1213+ const expirationTime = ( expiration - ( 5 * 60 * 1000 ) ) ;
1214+ if ( now > expirationTime ) {
1215+ refreshCredentials = true ;
1216+ return Promise . resolve ( ) ;
1217+ }
1218+ }
1219+ } ) ;
1220+ }
1221+ refreshCredentials = false ;
1222+ return Promise . resolve ( ) ;
1223+ } ,
11961224 /***********************************************************************
11971225 *
11981226 * Auth Token Actions
@@ -1219,7 +1247,7 @@ export default {
12191247 return Promise . resolve ( ) ;
12201248 } ) ;
12211249 } ,
1222- refreshAuthTokens ( context ) {
1250+ async refreshAuthTokens ( context ) {
12231251 function isExpired ( token ) {
12241252 if ( token ) {
12251253 const decoded = jwtDecode ( token ) ;
@@ -1242,6 +1270,7 @@ export default {
12421270 console . info ( 'starting auth token refresh' ) ;
12431271 return context . dispatch ( 'refreshAuthTokensFromParent' ) ;
12441272 }
1273+
12451274 return Promise . resolve ( ) ;
12461275 } ,
12471276
0 commit comments