diff --git a/src/commonMain/kotlin/LoxoneCommands.kt b/src/commonMain/kotlin/LoxoneCommands.kt index 1dca268..2157419 100644 --- a/src/commonMain/kotlin/LoxoneCommands.kt +++ b/src/commonMain/kotlin/LoxoneCommands.kt @@ -2,6 +2,8 @@ package cz.smarteon.loxone import cz.smarteon.loxone.message.EmptyLoxoneMsgVal import cz.smarteon.loxone.message.LoxoneMsg +import cz.smarteon.loxone.message.Token +import cz.smarteon.loxone.message.TokenPermission import cz.smarteon.loxone.message.sysCommand import kotlin.jvm.JvmStatic @@ -26,6 +28,32 @@ object LoxoneCommands { */ object Tokens { + /** + * Command to get new token. + * @param credentialsHash The credentials hash obtained by [LoxoneCrypto.loxoneHashing] + * from [LoxoneCredentials]. + * @param user The user to get the token for. + * @param permission The permission of the token. + * @param clientId Unique identifier of this client. + * @param clientInfo Information about this client. + */ + @JvmStatic + fun get( + credentialsHash: String, + user: String, + permission: TokenPermission, + clientId: String = LoxoneClientSettings.DEFAULT_CLIENT_ID, + clientInfo: String = LoxoneClientSettings.DEFAULT_CLIENT_INFO + ) = sysCommand( + "getjwt", + credentialsHash, + user, + permission.id.toString(), + clientId, + clientInfo, + authenticated = false + ) + /** * Command to kill token. * @param tokenHash The token hash to kill. diff --git a/src/commonMain/kotlin/LoxoneTokenAuthenticator.kt b/src/commonMain/kotlin/LoxoneTokenAuthenticator.kt index d088392..d23b041 100644 --- a/src/commonMain/kotlin/LoxoneTokenAuthenticator.kt +++ b/src/commonMain/kotlin/LoxoneTokenAuthenticator.kt @@ -5,7 +5,6 @@ import cz.smarteon.loxone.LoxoneCrypto.loxoneHashing import cz.smarteon.loxone.message.Hashing import cz.smarteon.loxone.message.Hashing.Companion.commandForUser import cz.smarteon.loxone.message.Token -import cz.smarteon.loxone.message.Token.Companion.commandGetToken import cz.smarteon.loxone.message.TokenState import io.github.oshai.kotlinlogging.KotlinLogging import kotlinx.coroutines.sync.Mutex @@ -48,7 +47,7 @@ class LoxoneTokenAuthenticator @JvmOverloads constructor( state.isExpired -> { logger.debug { "Token expired, requesting new one" } token = client.callForMsg( - commandGetToken( + Tokens.get( loxoneHashing(profile.credentials!!.password, checkNotNull(hashing), "getttoken", user), user, settings.tokenPermission, diff --git a/src/commonMain/kotlin/message/Token.kt b/src/commonMain/kotlin/message/Token.kt index 55714b5..c1242e3 100644 --- a/src/commonMain/kotlin/message/Token.kt +++ b/src/commonMain/kotlin/message/Token.kt @@ -4,7 +4,6 @@ import cz.smarteon.loxone.LoxoneTime import kotlinx.datetime.Clock import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable -import kotlin.jvm.JvmStatic /** * Represents Loxone authentication token. @@ -61,25 +60,4 @@ data class Token( result = 31 * result + filled.hashCode() return result } - - companion object { - - @JvmStatic - fun commandGetToken( - tokenHash: String, - user: String, - permission: TokenPermission, - clientId: String, - clientInfo: String - ) = - sysCommand( - "getjwt", - tokenHash, - user, - permission.id.toString(), - clientId, - clientInfo, - authenticated = false - ) - } } diff --git a/src/commonTest/kotlin/LoxoneCommandsTest.kt b/src/commonTest/kotlin/LoxoneCommandsTest.kt index a14075c..76a79d6 100644 --- a/src/commonTest/kotlin/LoxoneCommandsTest.kt +++ b/src/commonTest/kotlin/LoxoneCommandsTest.kt @@ -1,6 +1,8 @@ package cz.smarteon.loxone import cz.smarteon.loxone.message.EmptyLoxoneMsgVal +import cz.smarteon.loxone.message.Token +import cz.smarteon.loxone.message.TokenPermission import io.kotest.assertions.asClue import io.kotest.core.spec.style.ShouldSpec import io.kotest.matchers.shouldBe @@ -14,4 +16,22 @@ class LoxoneCommandsTest : ShouldSpec({ it.expectedCode shouldBe "401" } } + + should("create gettoken command") { + LoxoneCommands.Tokens.get("hash", "user", TokenPermission.WEB).asClue { + it.pathSegments shouldBe listOf( + "jdev", + "sys", + "getjwt", + "hash", + "user", + "2", + "df184362-73fc-5d3e-ab0ec7c1c3e5bb2e", + "loxoneKotlin" + ) + it.valueType shouldBe Token::class + it.authenticated shouldBe false + it.expectedCode shouldBe "200" + } + } })