3
3
* SPDX-License-Identifier: Apache-2.0
4
4
*/
5
5
6
- import { AWSError , Service } from 'aws-sdk'
6
+ import { AWSError , CognitoIdentityCredentials , Service } from 'aws-sdk'
7
7
import apiConfig = require( './service-2.json' )
8
8
import globals from '../../shared/extensionGlobals'
9
9
import * as CodeWhispererClient from './codewhispererclient'
@@ -39,8 +39,23 @@ export type ListCodeScanFindingsRequest = Readonly<CodeWhispererClient.ListCodeS
39
39
export type ArtifactType = Readonly < CodeWhispererClient . ArtifactType >
40
40
export type ArtifactMap = Readonly < CodeWhispererClient . ArtifactMap >
41
41
export class DefaultCodeWhispererClient {
42
+ private credentials ?: CognitoIdentityCredentials
43
+
42
44
private async createSdkClient ( ) : Promise < CodeWhispererClient > {
43
- const credentials = ! isCloud9 ( ) ? await getCognitoCredentials ( ) : undefined
45
+ try {
46
+ if ( ! this . credentials && ! isCloud9 ( ) ) {
47
+ this . credentials = await getCognitoCredentials ( )
48
+ }
49
+ if ( this . credentials && this . credentials . needsRefresh ( ) ) {
50
+ await new Promise < void > ( ( resolve , reject ) =>
51
+ this . credentials ?. refresh ( e => ( e ? reject ( e ) : resolve ( ) ) )
52
+ )
53
+ }
54
+ } catch ( e ) {
55
+ getLogger ( ) . debug ( 'Error when getting or refreshing Cognito credentials' , e )
56
+ throw new CognitoCredentialsError ( 'Error when getting or refreshing Cognito credentials' )
57
+ }
58
+
44
59
const accessToken = globals . context . globalState . get < string | undefined > ( CodeWhispererConstants . accessToken )
45
60
const isOptedOut = CodeWhispererSettings . instance . isOptoutEnabled ( )
46
61
@@ -49,7 +64,7 @@ export class DefaultCodeWhispererClient {
49
64
{
50
65
apiConfig : apiConfig ,
51
66
region : CodeWhispererConstants . region ,
52
- credentials : credentials ,
67
+ credentials : this . credentials ,
53
68
endpoint : CodeWhispererConstants . endpoint ,
54
69
onRequestSetup : [
55
70
req => {
@@ -132,3 +147,5 @@ export class DefaultCodeWhispererClient {
132
147
return ( await this . createSdkClient ( ) ) . listCodeScanFindings ( request ) . promise ( )
133
148
}
134
149
}
150
+
151
+ export class CognitoCredentialsError extends Error { }
0 commit comments