Skip to content

Commit 8da7fce

Browse files
committed
handle followupAuth
1 parent ee32a9d commit 8da7fce

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/webview/BrowserConnector.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import kotlinx.coroutines.flow.distinctUntilChanged
1818
import kotlinx.coroutines.flow.launchIn
1919
import kotlinx.coroutines.flow.merge
2020
import kotlinx.coroutines.flow.onEach
21-
import kotlinx.coroutines.future.await
2221
import kotlinx.coroutines.launch
2322
import org.cef.browser.CefBrowser
2423
import org.eclipse.lsp4j.Position
@@ -33,6 +32,8 @@ import software.aws.toolkits.jetbrains.services.amazonq.lsp.encryption.JwtEncryp
3332
import software.aws.toolkits.jetbrains.services.amazonq.lsp.flareChat.AwsServerCapabilitiesProvider
3433
import software.aws.toolkits.jetbrains.services.amazonq.lsp.flareChat.ChatCommunicationManager
3534
import software.aws.toolkits.jetbrains.services.amazonq.lsp.flareChat.getTextDocumentIdentifier
35+
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.AUTH_FOLLOW_UP_CLICKED
36+
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.AuthFollowUpClickNotification
3637
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.ButtonClickNotification
3738
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.ButtonClickParams
3839
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.ButtonClickResult
@@ -384,6 +385,13 @@ class BrowserConnector(
384385
}
385386
}
386387
}
388+
AUTH_FOLLOW_UP_CLICKED -> {
389+
val message = serializer.deserializeChatMessages<AuthFollowUpClickNotification>(node)
390+
chatCommunicationManager.handleAuthFollowUpClicked(
391+
project,
392+
message.params
393+
)
394+
}
387395
CHAT_COPY_CODE_TO_CLIPBOARD -> {
388396
handleChatNotification<CopyCodeToClipboardNotification, CopyCodeToClipboardParams>(node) { server, params ->
389397
server.copyCodeToClipboard(params)

plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/flareChat/ChatCommunicationManager.kt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,18 @@ import com.intellij.openapi.components.Service
88
import com.intellij.openapi.components.service
99
import com.intellij.openapi.project.Project
1010
import org.eclipse.lsp4j.ProgressParams
11+
import software.aws.toolkits.core.utils.getLogger
12+
import software.aws.toolkits.core.utils.warn
13+
import software.aws.toolkits.jetbrains.core.credentials.ToolkitConnectionManager
14+
import software.aws.toolkits.jetbrains.core.credentials.logoutFromSsoConnection
15+
import software.aws.toolkits.jetbrains.core.credentials.pinning.QConnection
16+
import software.aws.toolkits.jetbrains.core.credentials.reauthConnectionIfNeeded
17+
import software.aws.toolkits.jetbrains.core.gettingstarted.editor.BearerTokenFeatureSet
18+
import software.aws.toolkits.jetbrains.core.gettingstarted.editor.checkBearerConnectionValidity
1119
import software.aws.toolkits.jetbrains.services.amazonq.lsp.AmazonQLspService
1220
import software.aws.toolkits.jetbrains.services.amazonq.lsp.flareChat.ProgressNotificationUtils.getObject
21+
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.AuthFollowUpClickedParams
22+
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.AuthFollowUpType
1323
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.GetSerializedChatResult
1424
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.OpenTabResult
1525
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.SEND_CHAT_COMMAND_PROMPT
@@ -58,9 +68,44 @@ class ChatCommunicationManager {
5868
}
5969
}
6070

71+
fun handleAuthFollowUpClicked(project: Project, params: AuthFollowUpClickedParams) {
72+
val incomingType = params.authFollowUpType
73+
val connectionManager = ToolkitConnectionManager.getInstance(project)
74+
try {
75+
when (incomingType) {
76+
AuthFollowUpType.RE_AUTH.value, AuthFollowUpType.MISSING_SCOPES.value -> {
77+
connectionManager.activeConnectionForFeature(QConnection.getInstance())?.let {
78+
reauthConnectionIfNeeded(project, it, isReAuth = true)
79+
}
80+
return
81+
}
82+
AuthFollowUpType.FULL_AUTH.value, AuthFollowUpType.USE_SUPPORTED_AUTH.value -> {
83+
// Logout by deleting token credentials
84+
val validConnection = checkBearerConnectionValidity(project, BearerTokenFeatureSet.Q)
85+
val connection = validConnection.activeConnectionBearer
86+
if (connection != null) {
87+
logoutFromSsoConnection(project, connection)
88+
} else {
89+
LOG.warn { "No valid connection found for logout" }
90+
}
91+
return
92+
}
93+
else -> {
94+
LOG.warn { "Unknown auth follow up type: $incomingType" }
95+
throw IllegalStateException("Error occurred while attempting to handle auth follow up: Unknown AuthFollowUpType $incomingType")
96+
}
97+
}
98+
} catch (ex: Exception) {
99+
LOG.warn(ex) { "Failed to handle authentication when auth follow up clicked" }
100+
throw ex
101+
}
102+
}
103+
61104
companion object {
62105
fun getInstance(project: Project) = project.service<ChatCommunicationManager>()
63106

107+
val LOG = getLogger<ChatCommunicationManager>()
108+
64109
val pendingSerializedChatRequests = ConcurrentHashMap<String, CompletableFuture<GetSerializedChatResult>>()
65110
fun completeSerializedChatResponse(requestId: String, content: String) {
66111
pendingSerializedChatRequests.remove(requestId)?.complete(GetSerializedChatResult((content)))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat
5+
6+
data class AuthFollowUpClickNotification(
7+
override val command: String,
8+
override val params: AuthFollowUpClickedParams,
9+
) : ChatNotification<AuthFollowUpClickedParams>
10+
11+
data class AuthFollowUpClickedParams(
12+
val tabId: String,
13+
val messageId: String,
14+
val authFollowUpType: String,
15+
) {
16+
companion object {
17+
fun create(tabId: String, messageId: String, authType: AuthFollowUpType): AuthFollowUpClickedParams =
18+
AuthFollowUpClickedParams(tabId, messageId, authType.value)
19+
}
20+
}
21+
22+
enum class AuthFollowUpType(val value: String) {
23+
FULL_AUTH("full-auth"),
24+
RE_AUTH("re-auth"),
25+
MISSING_SCOPES("missing_scopes"),
26+
USE_SUPPORTED_AUTH("use-supported-auth"),
27+
;
28+
29+
override fun toString(): String =
30+
name.lowercase()
31+
}

plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/model/aws/chat/FlareChatCommands.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const val PROMPT_INPUT_OPTIONS_CHANGE = "aws/chat/promptInputOptionChange"
1313
const val CHAT_PROMPT_OPTION_ACKNOWLEDGED = "chatPromptOptionAcknowledged"
1414
const val CHAT_FEEDBACK = "aws/chat/feedback"
1515
const val CHAT_FOLLOW_UP_CLICK = "aws/chat/followUpClick"
16+
const val AUTH_FOLLOW_UP_CLICKED = "authFollowUpClicked"
1617
const val CHAT_LIST_CONVERSATIONS = "aws/chat/listConversations"
1718
const val CHAT_CONVERSATION_CLICK = "aws/chat/conversationClick"
1819
const val CHAT_FILE_CLICK = "aws/chat/fileClick"

0 commit comments

Comments
 (0)