@@ -18,11 +18,12 @@ import { DevEnvironmentId, getConnectedDevEnv, openDevEnv } from './model'
18
18
import { showConfigureDevEnv } from './vue/configure/backend'
19
19
import { showCreateDevEnv } from './vue/create/backend'
20
20
import { CancellationError } from '../shared/utilities/timeoutUtils'
21
- import { ToolkitError } from '../shared/errors'
21
+ import { ToolkitError , errorCode } from '../shared/errors'
22
22
import { telemetry } from '../shared/telemetry/telemetry'
23
23
import { showConfirmationMessage } from '../shared/utilities/messages'
24
24
import { AccountStatus } from '../shared/telemetry/telemetryClient'
25
25
import { CreateDevEnvironmentRequest , UpdateDevEnvironmentRequest } from 'aws-sdk/clients/codecatalyst'
26
+ import { Auth , SsoConnection } from '../credentials/auth'
26
27
27
28
/** "List CodeCatalyst Commands" command. */
28
29
export async function listCommands ( ) : Promise < void > {
@@ -135,13 +136,43 @@ function createClientInjector(authProvider: CodeCatalystAuthenticationProvider):
135
136
136
137
await authProvider . restore ( )
137
138
const conn = authProvider . activeConnection ?? ( await authProvider . promptNotConnected ( ) )
138
- const client = await createClient ( conn )
139
+ const validatedConn = await validateConnection ( conn , authProvider . auth )
140
+ const client = await createClient ( validatedConn )
139
141
telemetry . record ( { userId : client . identity . id } )
140
142
141
143
return command ( client , ...args )
142
144
}
143
145
}
144
146
147
+ /**
148
+ * Returns a connection that is ensured to be authenticated.
149
+ *
150
+ * Provides the user the ability to re-authenticate if needed,
151
+ * otherwise throwing an error.
152
+ */
153
+ async function validateConnection ( conn : SsoConnection , auth : Auth ) : Promise < SsoConnection > {
154
+ if ( auth . getConnectionState ( conn ) === 'valid' ) {
155
+ return conn
156
+ }
157
+
158
+ // Have user try to log in
159
+ const loginMessage = localize ( 'aws.auth.invalidConnection' , 'Connection is invalid or expired, login again?' )
160
+ const result = await vscode . window . showErrorMessage ( loginMessage , 'Login' )
161
+
162
+ if ( result !== 'Login' ) {
163
+ throw new ToolkitError ( 'User cancelled login.' , { cancelled : true , code : errorCode . invalidConnection } )
164
+ }
165
+
166
+ conn = await auth . reauthenticate ( conn )
167
+
168
+ // Log in attempt failed
169
+ if ( auth . getConnectionState ( conn ) !== 'valid' ) {
170
+ throw new ToolkitError ( 'Login failed.' , { code : errorCode . invalidConnection } )
171
+ }
172
+
173
+ return conn
174
+ }
175
+
145
176
function createCommandDecorator ( commands : CodeCatalystCommands ) : CommandDecorator {
146
177
return command =>
147
178
( ...args ) =>
0 commit comments