Skip to content

Commit 713d658

Browse files
author
Austin Johnson
committed
initial commit from qnabot
1 parent c466a21 commit 713d658

File tree

12 files changed

+11264
-5377
lines changed

12 files changed

+11264
-5377
lines changed

lex-web-ui/package-lock.json

Lines changed: 9555 additions & 5014 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lex-web-ui/package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@
2020
"dependencies": {
2121
"aws-amplify": "^5.3.26",
2222
"amazon-connect-chatjs": "^2.3.0",
23-
"aws-sdk": "^2.1354.0",
23+
"@aws-sdk/client-cognito-identity": "^3.511.0",
24+
"@aws-sdk/client-connect": "^3.511.0",
25+
"@aws-sdk/client-lex-runtime-service": "^3.525.0",
26+
"@aws-sdk/client-lex-runtime-v2": "^3.514.0",
27+
"@aws-sdk/client-polly": "^3.525.0",
28+
"@aws-sdk/client-s3": "^3.525.0",
29+
"@aws-sdk/credential-providers": "^3.511.0",
2430
"browserify-zlib": "^0.2.0",
2531
"buffer": "^6.0.3",
2632
"core-js": "^3.6.5",

lex-web-ui/src/components/LexWeb.vue

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,12 @@ import MinButton from '@/components/MinButton';
7373
import ToolbarContainer from '@/components/ToolbarContainer';
7474
import MessageList from '@/components/MessageList';
7575
import InputContainer from '@/components/InputContainer';
76-
import LexRuntime from 'aws-sdk/clients/lexruntime';
77-
import LexRuntimeV2 from 'aws-sdk/clients/lexruntimev2';
76+
// import { fromCognitoIdentityPool } from '@aws-sdk/credential-providers';
77+
import { LexRuntimeServiceClient } from '@aws-sdk/client-lex-runtime-service';
78+
import { LexRuntimeV2Client } from '@aws-sdk/client-lex-runtime-v2';
79+
import { PollyClient } from '@aws-sdk/client-polly';
80+
7881
79-
import { Config as AWSConfig, CognitoIdentityCredentials }
80-
from 'aws-sdk/global';
8182
8283
export default {
8384
name: 'lex-web',
@@ -156,7 +157,7 @@ export default {
156157
if (!this.isMobile) {
157158
document.documentElement.style.overflowY = 'hidden';
158159
}
159-
160+
160161
this.initConfig()
161162
.then(() => Promise.all([
162163
this.$store.dispatch(
@@ -191,44 +192,25 @@ export default {
191192
if (!poolId) {
192193
return Promise.reject(new Error('no cognito.poolId found in config'))
193194
}
195+
194196
195-
const AWSConfigConstructor = (window.AWS && window.AWS.Config) ?
196-
window.AWS.Config :
197-
AWSConfig;
198-
199-
const CognitoConstructor =
200-
(window.AWS && window.AWS.CognitoIdentityCredentials) ?
201-
window.AWS.CognitoIdentityCredentials :
202-
CognitoIdentityCredentials;
203-
204-
const LexRuntimeConstructor = (window.AWS && window.AWS.LexRuntime) ?
205-
window.AWS.LexRuntime :
206-
LexRuntime;
207-
208-
const LexRuntimeConstructorV2 = (window.AWS && window.AWS.LexRuntimeV2) ?
209-
window.AWS.LexRuntimeV2 :
210-
LexRuntimeV2;
211-
212-
const credentials = new CognitoConstructor(
213-
{ IdentityPoolId: poolId },
214-
{ region: region },
215-
);
216-
217-
const awsConfig = new AWSConfigConstructor({
197+
const awsConfig = {
218198
region: region,
219-
credentials,
220-
});
199+
credentials: this.$lexWebUi.awsConfig.credentials,
200+
};
221201
222-
this.$lexWebUi.lexRuntimeClient = new LexRuntimeConstructor(awsConfig);
223-
this.$lexWebUi.lexRuntimeV2Client = new LexRuntimeConstructorV2(awsConfig);
202+
this.$lexWebUi.lexRuntimeClient = new LexRuntimeServiceClient(awsConfig);
203+
this.$lexWebUi.lexRuntimeV2Client = new LexRuntimeV2Client(awsConfig);
204+
this.$lexWebUi.pollyClient = new PollyClient(awsConfig)
224205
/* eslint-disable no-console */
225-
console.log(`lexRuntimeV2Client : ${JSON.stringify(this.$lexWebUi.lexRuntimeV2Client)}`);
206+
// console.log('this.$store.state', this.$store.state);
207+
// console.log(`lexRuntimeV2Client : ${JSON.stringify(this.$lexWebUi.lexRuntimeV2Client)}`);
226208
227209
const promises = [
228210
this.$store.dispatch('initMessageList'),
229-
this.$store.dispatch('initPollyClient', this.$lexWebUi.pollyClient),
211+
this.$store.dispatch('initPollyClient', this.$lexWebUi.pollyClient, this.$lexWebUi.awsConfig.credentials),
230212
this.$store.dispatch('initLexClient', {
231-
v1client: this.$lexWebUi.lexRuntimeClient, v2client: this.$lexWebUi.lexRuntimeV2Client,
213+
v1client: this.$lexWebUi.lexRuntimeClient, v2client: this.$lexWebUi.lexRuntimeV2Client, credentials: this.$lexWebUi.awsConfig.credentials
232214
}),
233215
];
234216
console.info('CONFIG : ', this.$store.state.config);
@@ -589,4 +571,4 @@ NOTE: not using var() for different heights due to IE11 compatibility
589571
590572
html { font-size: 14px !important; }
591573
592-
</style>
574+
</style>

lex-web-ui/src/lex-web-ui.js

Lines changed: 68 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -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';
2926
import LexWeb from '@/components/LexWeb';
3027
import 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
221242
if(process.env.NODE_ENV === "development")
222243
{
223244
const lexWeb = new Loader();

0 commit comments

Comments
 (0)