Skip to content

Commit

Permalink
refactor: move the gettoken command to LoxoneCommands
Browse files Browse the repository at this point in the history
  • Loading branch information
jimirocks committed Apr 8, 2024
1 parent c0a78d5 commit 4e7ce4e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 24 deletions.
28 changes: 28 additions & 0 deletions src/commonMain/kotlin/LoxoneCommands.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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<Token>(
"getjwt",
credentialsHash,
user,
permission.id.toString(),
clientId,
clientInfo,
authenticated = false
)

/**
* Command to kill token.
* @param tokenHash The token hash to kill.
Expand Down
3 changes: 1 addition & 2 deletions src/commonMain/kotlin/LoxoneTokenAuthenticator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
22 changes: 0 additions & 22 deletions src/commonMain/kotlin/message/Token.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<Token>(
"getjwt",
tokenHash,
user,
permission.id.toString(),
clientId,
clientInfo,
authenticated = false
)
}
}
20 changes: 20 additions & 0 deletions src/commonTest/kotlin/LoxoneCommandsTest.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"
}
}
})

0 comments on commit 4e7ce4e

Please sign in to comment.