@@ -18,14 +18,11 @@ License for the specific language governing permissions and limitations under th
1818 * Exports Loader as the plugin constructor
1919 * and Store as store that can be used with Vuex.Store()
2020 */
21- import Vue from 'vue' ;
22- import Vuex from 'vuex' ;
23- import { Config as AWSConfig , CognitoIdentityCredentials }
24- from 'aws-sdk/global' ;
25- import LexRuntime from 'aws-sdk/clients/lexruntime' ;
26- import LexRuntimeV2 from 'aws-sdk/clients/lexruntimev2' ;
27- import Polly from 'aws-sdk/clients/polly' ;
28-
21+ import { fromCognitoIdentityPool } from '@aws-sdk/credential-providers' ;
22+ import { CognitoIdentityClient , GetIdCommand , GetCredentialsForIdentityCommand } from '@aws-sdk/client-cognito-identity' ;
23+ import { LexRuntimeServiceClient } from '@aws-sdk/client-lex-runtime-service' ;
24+ import { LexRuntimeV2Client } from '@aws-sdk/client-lex-runtime-v2' ;
25+ import { PollyClient } from '@aws-sdk/client-polly' ;
2926import LexWeb from '@/components/LexWeb' ;
3027import VuexStore from '@/store' ;
3128
@@ -162,62 +159,86 @@ export class Loader {
162159 this . app = app ;
163160
164161 const mergedConfig = mergeConfig ( defaultConfig , config ) ;
165-
166- const AWSConfigConstructor = ( window . AWS && window . AWS . Config ) ?
167- window . AWS . Config :
168- AWSConfig ;
169-
170- const CognitoConstructor =
171- ( window . AWS && window . AWS . CognitoIdentityCredentials ) ?
172- window . AWS . CognitoIdentityCredentials :
173- CognitoIdentityCredentials ;
174-
175- const PollyConstructor = ( window . AWS && window . AWS . Polly ) ?
176- window . AWS . Polly :
177- Polly ;
178-
179- const LexRuntimeConstructor = ( window . AWS && window . AWS . LexRuntime ) ?
180- window . AWS . LexRuntime :
181- LexRuntime ;
182-
183- const LexRuntimeConstructorV2 = ( window . AWS && window . AWS . LexRuntimeV2 ) ?
184- window . AWS . LexRuntimeV2 :
185- LexRuntimeV2 ;
186-
187- if ( ! AWSConfigConstructor || ! CognitoConstructor || ! PollyConstructor
188- || ! LexRuntimeConstructor || ! LexRuntimeConstructorV2 ) {
189- throw new Error ( 'unable to find AWS SDK' ) ;
162+ let credentials ;
163+ if ( mergedConfig . cognito . poolId != '' || localStorage . getItem ( 'poolId' ) ) {
164+ credentials = this . getCredentials ( mergedConfig ) . then ( ( creds ) => {
165+ return creds ;
166+ } ) ;
190167 }
191168
192- const credentials = new CognitoConstructor (
193- { IdentityPoolId : mergedConfig . cognito . poolId } ,
194- { region : mergedConfig . region || mergedConfig . cognito . poolId . split ( ':' ) [ 0 ] || 'us-east-1' } ,
195- ) ;
196169
197- const awsConfig = new AWSConfigConstructor ( {
170+ const awsConfig = {
198171 region : mergedConfig . region || mergedConfig . cognito . poolId . split ( ':' ) [ 0 ] || 'us-east-1' ,
199172 credentials,
200- } ) ;
173+ } ;
201174
202- const lexRuntimeClient = new LexRuntimeConstructor ( awsConfig ) ;
203- const lexRuntimeV2Client = new LexRuntimeConstructorV2 ( awsConfig ) ;
204- /* eslint-disable no-console */
205- const pollyClient = (
206- typeof mergedConfig . recorder === 'undefined' ||
207- ( mergedConfig . recorder && mergedConfig . recorder . enable !== false )
208- ) ? new PollyConstructor ( awsConfig ) : null ;
175+ const lexRuntimeClient = new LexRuntimeServiceClient ( awsConfig ) ;
176+ const lexRuntimeV2Client = new LexRuntimeV2Client ( awsConfig ) ;
177+ const pollyClient = new PollyClient ( awsConfig ) ;
209178
179+ // /* eslint-disable no-console */
210180 app . use ( Plugin , {
211181 config : mergedConfig ,
212182 awsConfig,
213183 lexRuntimeClient,
214184 lexRuntimeV2Client,
215- pollyClient,
185+ pollyClient
216186 } ) ;
217187 this . app = app ;
218188 }
189+
190+ async getCredentials ( context ) {
191+ const region = context . region || context . cognito . poolId . split ( ':' ) [ 0 ] || 'us-east-1' ;
192+ const poolId = context . cognito . poolId || localStorage . getItem ( 'poolId' ) ;
193+ const appUserPoolName = context . cognito . appUserPoolName || localStorage . getItem ( 'appUserPoolName' ) ;
194+ const poolName = `cognito-idp.${ region } .amazonaws.com/${ appUserPoolName } ` ;
195+ const appUserPoolClientId = context . cognito . appUserPoolClientId || localStorage . getItem ( 'appUserPoolClientId' )
196+ const idtoken = localStorage . getItem ( `${ appUserPoolClientId } idtokenjwt` ) ;
197+ let logins ;
198+ if ( idtoken ) {
199+ logins = { } ;
200+ logins [ poolName ] = idtoken ;
201+ const client = new CognitoIdentityClient ( { region } ) ;
202+ const getIdentityId = new GetIdCommand ( {
203+ IdentityPoolId : poolId ,
204+ Logins : logins ? logins : { }
205+ } )
206+ let identityId , getCreds ;
207+
208+ try {
209+ await client . send ( getIdentityId )
210+ . then ( ( res ) => {
211+ identityId = res . IdentityId ;
212+ getCreds = new GetCredentialsForIdentityCommand ( {
213+ IdentityId : identityId ,
214+ Logins : logins ? logins : { }
215+ } )
216+ } )
217+ const res = await client . send ( getCreds ) ;
218+ const creds = res . Credentials ;
219+ const credentials = {
220+ accessKeyId : creds . AccessKeyId ,
221+ identityId,
222+ secretAccessKey : creds . SecretKey ,
223+ sessionToken : creds . SessionToken ,
224+ expiration : creds . Expiration ,
225+ } ;
226+ return credentials ;
227+ } catch ( err ) {
228+ console . log ( err )
229+ }
230+ } else {
231+ const credentialProvider = fromCognitoIdentityPool ( {
232+ identityPoolId : poolId ,
233+ clientConfig : { region } ,
234+ } )
235+ const credentials = credentialProvider ( )
236+ return credentials
237+ }
238+ }
219239}
220240
241+ // comment out for prod build
221242if ( process . env . NODE_ENV === "development" )
222243{
223244 const lexWeb = new Loader ( ) ;
0 commit comments