-
Notifications
You must be signed in to change notification settings - Fork 275
fix(amazonq): lsp token/update emits for init, logins, and token refresh #5477
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,9 @@ | |
| import com.intellij.openapi.project.Project | ||
| import org.eclipse.lsp4j.jsonrpc.messages.ResponseMessage | ||
| import software.aws.toolkits.core.TokenConnectionSettings | ||
| import software.aws.toolkits.jetbrains.core.credentials.ToolkitConnection | ||
| import software.aws.toolkits.jetbrains.core.credentials.ToolkitConnectionManager | ||
| import software.aws.toolkits.jetbrains.core.credentials.ToolkitConnectionManagerListener | ||
| import software.aws.toolkits.jetbrains.core.credentials.pinning.QConnection | ||
| import software.aws.toolkits.jetbrains.core.credentials.sso.bearer.BearerTokenProvider | ||
| import software.aws.toolkits.jetbrains.core.credentials.sso.bearer.BearerTokenProviderListener | ||
|
|
@@ -16,38 +18,27 @@ | |
| import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.credentials.BearerCredentials | ||
| import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.credentials.UpdateCredentialsPayload | ||
| import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.credentials.UpdateCredentialsPayloadData | ||
| import software.aws.toolkits.jetbrains.utils.isQConnected | ||
| import software.aws.toolkits.jetbrains.utils.isQExpired | ||
| import java.util.concurrent.CompletableFuture | ||
|
|
||
| class DefaultAuthCredentialsService( | ||
| private val project: Project, | ||
| private val encryptionManager: JwtEncryptionManager, | ||
| serverInstance: Disposable, | ||
| ) : AuthCredentialsService, | ||
| BearerTokenProviderListener { | ||
| init { | ||
| project.messageBus.connect(serverInstance).subscribe(BearerTokenProviderListener.TOPIC, this) | ||
|
|
||
| onChange("init", null) | ||
| } | ||
|
|
||
| override fun onChange(providerId: String, newScopes: List<String>?) { | ||
| val connection = ToolkitConnectionManager.getInstance(project) | ||
| .activeConnectionForFeature(QConnection.getInstance()) | ||
| ?: return | ||
| BearerTokenProviderListener, | ||
| ToolkitConnectionManagerListener { | ||
|
|
||
| val provider = (connection.getConnectionSettings() as? TokenConnectionSettings) | ||
| ?.tokenProvider | ||
| ?.delegate as? BearerTokenProvider | ||
| ?: return | ||
|
|
||
| provider.currentToken()?.accessToken?.let { token -> | ||
| // assume encryption is always on | ||
| updateTokenCredentials(token, true) | ||
| init { | ||
| project.messageBus.connect(serverInstance).apply { | ||
| subscribe(BearerTokenProviderListener.TOPIC, this@DefaultAuthCredentialsService) | ||
| subscribe(ToolkitConnectionManagerListener.TOPIC, this@DefaultAuthCredentialsService) | ||
|
Check warning on line 36 in plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/auth/DefaultAuthCredentialsService.kt
|
||
| } | ||
| } | ||
|
|
||
| override fun invalidate(providerId: String) { | ||
| deleteTokenCredentials() | ||
| if (isQConnected(project) && !isQExpired(project)) { | ||
| updateTokenFromActiveConnection() | ||
|
Check warning on line 40 in plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/auth/DefaultAuthCredentialsService.kt
|
||
| } | ||
| } | ||
|
|
||
| override fun updateTokenCredentials(accessToken: String, encrypted: Boolean): CompletableFuture<ResponseMessage> { | ||
|
|
@@ -66,6 +57,41 @@ | |
| } ?: completableFuture.completeExceptionally(IllegalStateException("LSP Server not running")) | ||
| } | ||
|
|
||
| override fun onChange(providerId: String, newScopes: List<String>?) { | ||
| updateTokenFromActiveConnection() | ||
| } | ||
|
Check warning on line 62 in plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/auth/DefaultAuthCredentialsService.kt
|
||
|
|
||
| override fun activeConnectionChanged(newConnection: ToolkitConnection?) { | ||
| val qConnection = ToolkitConnectionManager.getInstance(project) | ||
| .activeConnectionForFeature(QConnection.getInstance()) | ||
| ?: return | ||
|
Check warning on line 67 in plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/auth/DefaultAuthCredentialsService.kt
|
||
| if (newConnection?.id != qConnection.id) return | ||
|
|
||
| updateTokenFromConnection(newConnection) | ||
| } | ||
|
Check warning on line 71 in plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/auth/DefaultAuthCredentialsService.kt
|
||
|
|
||
| private fun updateTokenFromActiveConnection() { | ||
| val connection = ToolkitConnectionManager.getInstance(project) | ||
| .activeConnectionForFeature(QConnection.getInstance()) | ||
| ?: return | ||
|
Check warning on line 76 in plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/auth/DefaultAuthCredentialsService.kt
|
||
|
|
||
| updateTokenFromConnection(connection) | ||
| } | ||
|
Check warning on line 79 in plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/auth/DefaultAuthCredentialsService.kt
|
||
|
|
||
| private fun updateTokenFromConnection(connection: ToolkitConnection) { | ||
| (connection.getConnectionSettings() as? TokenConnectionSettings) | ||
| ?.tokenProvider | ||
| ?.delegate | ||
| ?.let { it as? BearerTokenProvider } | ||
|
Comment on lines
+82
to
+85
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we move this to a util |
||
| ?.currentToken() | ||
| ?.accessToken | ||
| ?.let { token -> updateTokenCredentials(token, true) } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you please add the var name before the boolean? |
||
| } | ||
|
Check warning on line 89 in plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/auth/DefaultAuthCredentialsService.kt
|
||
|
|
||
| override fun invalidate(providerId: String) { | ||
| deleteTokenCredentials() | ||
| } | ||
|
Check warning on line 93 in plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/auth/DefaultAuthCredentialsService.kt
|
||
|
|
||
| private fun createUpdateCredentialsPayload(token: String, encrypted: Boolean): UpdateCredentialsPayload = | ||
| if (encrypted) { | ||
| UpdateCredentialsPayload( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
non-blocking, but are we sure these lines are not duplicated? I think they already exist somewhere in CW