diff --git a/idea-plugin/src/main/kotlin/fr/enedis/chutney/idea/settings/AuthMethodEnum.kt b/idea-plugin/src/main/kotlin/fr/enedis/chutney/idea/settings/AuthMethodEnum.kt new file mode 100644 index 000000000..e10bdb304 --- /dev/null +++ b/idea-plugin/src/main/kotlin/fr/enedis/chutney/idea/settings/AuthMethodEnum.kt @@ -0,0 +1,14 @@ +/* + * SPDX-FileCopyrightText: 2017-2026 Enedis + * + * SPDX-License-Identifier: Apache-2.0 + * + */ + +package fr.enedis.chutney.idea.settings + +enum class AuthMethodEnum { + BASIC, + BEARER, + API_KEY +} \ No newline at end of file diff --git a/idea-plugin/src/main/kotlin/fr/enedis/chutney/idea/settings/ChutneySettings.kt b/idea-plugin/src/main/kotlin/fr/enedis/chutney/idea/settings/ChutneySettings.kt index 1be996425..fdc864f69 100755 --- a/idea-plugin/src/main/kotlin/fr/enedis/chutney/idea/settings/ChutneySettings.kt +++ b/idea-plugin/src/main/kotlin/fr/enedis/chutney/idea/settings/ChutneySettings.kt @@ -24,7 +24,7 @@ class ChutneySettings : PersistentStateComponent AuthMethod.Basic(user.orEmpty(), password.orEmpty()) + AuthMethodEnum.BEARER -> AuthMethod.Bearer(token.orEmpty()) + AuthMethodEnum.API_KEY -> AuthMethod.ApiKey(token.orEmpty()) + }, proxyUrl = proxyUrl.takeIf { ! it.isNullOrBlank() }, proxyUser = proxyUser.takeIf { ! it.isNullOrBlank() }, proxyPassword = proxyPassword.takeIf { ! it.isNullOrBlank() } diff --git a/idea-plugin/src/main/kotlin/fr/enedis/chutney/idea/settings/ChutneySettingsConfigurable.kt b/idea-plugin/src/main/kotlin/fr/enedis/chutney/idea/settings/ChutneySettingsConfigurable.kt index 6163a52c7..0e7d85de1 100755 --- a/idea-plugin/src/main/kotlin/fr/enedis/chutney/idea/settings/ChutneySettingsConfigurable.kt +++ b/idea-plugin/src/main/kotlin/fr/enedis/chutney/idea/settings/ChutneySettingsConfigurable.kt @@ -21,6 +21,7 @@ import fr.enedis.chutney.kotlin.util.ChutneyServerInfo import fr.enedis.chutney.kotlin.util.HttpClient import java.awt.BorderLayout import java.awt.Color +import java.awt.GridLayout import javax.swing.* @@ -43,9 +44,12 @@ class ChutneySettingsConfigurable : val authModeLabel: JLabel = JLabel("Authentication mode") val basicAuthButton = JBRadioButton("Basic") - val tokenAuthButton = JBRadioButton("Token") + val bearerAuthButton = JBRadioButton("Bearer") + val apiKeyAuthButton = JBRadioButton("API-Key") val authButtonsGroup = ButtonGroup() + val checkConnectionButton = JButton("Check connection") + val chutneySettings: ChutneySettings = ChutneySettings.getInstance() override fun isModified(): Boolean { @@ -76,6 +80,10 @@ class ChutneySettingsConfigurable : updateAuthFields(isBasic = false, isToken = true) } + private fun apiKeyButtonSelected() { + updateAuthFields(isBasic = false, isToken = false) + } + private fun updateAuthFields(isBasic: Boolean, isToken: Boolean) { user.isVisible = isBasic userLabel.isVisible = isBasic @@ -84,11 +92,14 @@ class ChutneySettingsConfigurable : token.isVisible = isToken tokenLabel.isVisible = isToken + + checkConnectionButton.isVisible = isBasic || isToken } private fun stateFromFields() = ChutneySettings.ChutneySettingsState( url = url.text, - basicAuth = basicAuthButton.isSelected, + auth = if(basicAuthButton.isSelected) AuthMethodEnum.BASIC + else if(bearerAuthButton.isSelected) AuthMethodEnum.BEARER else AuthMethodEnum.API_KEY , user = user.text, password = String(password.password), token = token.text, @@ -100,22 +111,26 @@ class ChutneySettingsConfigurable : private fun initFields() { val serverInfo = chutneySettings.state.serverInfo() url.text = serverInfo?.url - basicAuthButton.isSelected = chutneySettings.state.basicAuth == true - tokenAuthButton.isSelected = chutneySettings.state.basicAuth == false + basicAuthButton.isSelected = chutneySettings.state.auth == AuthMethodEnum.BASIC + bearerAuthButton.isSelected = chutneySettings.state.auth == AuthMethodEnum.BEARER + apiKeyAuthButton.isSelected = chutneySettings.state.auth == AuthMethodEnum.API_KEY user.text = if(serverInfo?.auth is AuthMethod.Basic) (serverInfo.auth as AuthMethod.Basic).user else "" password.text = if(serverInfo?.auth is AuthMethod.Basic) (serverInfo.auth as AuthMethod.Basic).password else "" - token.text = if(serverInfo?.auth is AuthMethod.Bearer) (serverInfo.auth as AuthMethod.Bearer).token else "" + if(serverInfo?.auth is AuthMethod.Bearer) { + token.text = (serverInfo.auth as AuthMethod.Bearer).token + } else if(serverInfo?.auth is AuthMethod.ApiKey) { + token.text = (serverInfo.auth as AuthMethod.ApiKey).token + } proxyUrl.text = serverInfo?.proxyUrl proxyUser.text = serverInfo?.proxyUser proxyPassword.text = serverInfo?.proxyPassword - updateAuthFields(isBasic = basicAuthButton.isSelected, isToken = tokenAuthButton.isSelected) + updateAuthFields(isBasic = basicAuthButton.isSelected, isToken = bearerAuthButton.isSelected) } override fun createComponent(): JComponent { initFields() - val checkConnectionButton = JButton("Check connection") val checkLabel = JBLabel("").apply { isVisible = false } checkConnectionButton.addActionListener { @@ -123,7 +138,8 @@ class ChutneySettingsConfigurable : val serverInfo = ChutneyServerInfo( url.text, if(basicAuthButton.isSelected) AuthMethod.Basic(user.text, String(password.password)) - else AuthMethod.Bearer(token.text), + else if(bearerAuthButton.isSelected) AuthMethod.Bearer(token.text) + else AuthMethod.ApiKey(token.text), proxyUrl.text.ifBlank { null }, proxyUser.text.ifBlank { null }, String(proxyPassword.password).ifBlank { null } @@ -139,11 +155,13 @@ class ChutneySettingsConfigurable : } authButtonsGroup.add(basicAuthButton) - authButtonsGroup.add(tokenAuthButton) + authButtonsGroup.add(bearerAuthButton) + authButtonsGroup.add(apiKeyAuthButton) - val authButtonsPanel = JPanel(BorderLayout()) - authButtonsPanel.add(basicAuthButton, BorderLayout.WEST) - authButtonsPanel.add(tokenAuthButton, BorderLayout.CENTER) + val authButtonsPanel = JPanel(GridLayout(1, 3)) + authButtonsPanel.add(basicAuthButton) + authButtonsPanel.add(bearerAuthButton) + authButtonsPanel.add(apiKeyAuthButton) val myWrapper = JPanel(BorderLayout()) val centerPanel = @@ -164,7 +182,8 @@ class ChutneySettingsConfigurable : .panel basicAuthButton.addActionListener { basicButtonSelected() } - tokenAuthButton.addActionListener { tokenButtonSelected() } + bearerAuthButton.addActionListener { tokenButtonSelected() } + apiKeyAuthButton.addActionListener { apiKeyButtonSelected() } myWrapper.add(centerPanel, BorderLayout.NORTH) return myWrapper diff --git a/kotlin-dsl/src/main/kotlin/fr/enedis/chutney/kotlin/authentication/AuthMethod.kt b/kotlin-dsl/src/main/kotlin/fr/enedis/chutney/kotlin/authentication/AuthMethod.kt index 7fc3b7062..a28d36463 100644 --- a/kotlin-dsl/src/main/kotlin/fr/enedis/chutney/kotlin/authentication/AuthMethod.kt +++ b/kotlin-dsl/src/main/kotlin/fr/enedis/chutney/kotlin/authentication/AuthMethod.kt @@ -13,4 +13,6 @@ sealed interface AuthMethod { val password: String): AuthMethod data class Bearer(val token: String): AuthMethod + + data class ApiKey(val token: String): AuthMethod } diff --git a/kotlin-dsl/src/main/kotlin/fr/enedis/chutney/kotlin/util/ChutneyServerInfo.kt b/kotlin-dsl/src/main/kotlin/fr/enedis/chutney/kotlin/util/ChutneyServerInfo.kt index ad96744ab..ae975627a 100644 --- a/kotlin-dsl/src/main/kotlin/fr/enedis/chutney/kotlin/util/ChutneyServerInfo.kt +++ b/kotlin-dsl/src/main/kotlin/fr/enedis/chutney/kotlin/util/ChutneyServerInfo.kt @@ -70,6 +70,7 @@ data class ChutneyServerInfo( fun isBasicAuth(): Boolean = auth is AuthMethod.Basic fun isTokenAuth(): Boolean = auth is AuthMethod.Bearer + fun isApiKeyAuth(): Boolean = auth is AuthMethod.ApiKey } private enum class ProxyProtocol { http, https } diff --git a/kotlin-dsl/src/main/kotlin/fr/enedis/chutney/kotlin/util/HttpClient.kt b/kotlin-dsl/src/main/kotlin/fr/enedis/chutney/kotlin/util/HttpClient.kt index b9b76d8ea..d04869c8e 100644 --- a/kotlin-dsl/src/main/kotlin/fr/enedis/chutney/kotlin/util/HttpClient.kt +++ b/kotlin-dsl/src/main/kotlin/fr/enedis/chutney/kotlin/util/HttpClient.kt @@ -159,6 +159,9 @@ object HttpClient { if(serverInfo.isTokenAuth()) { httpRequest.addHeader("Authorization", "Bearer " + (serverInfo.auth as AuthMethod.Bearer).token) } + if(serverInfo.isApiKeyAuth()) { + httpRequest.addHeader("X-API-KEY", (serverInfo.auth as AuthMethod.ApiKey).token) + } return httpRequest }