Skip to content

Commit

Permalink
fix: try to get hashing key every time 2
Browse files Browse the repository at this point in the history
  • Loading branch information
jimirocks committed Dec 6, 2024
1 parent c4d62ce commit a2899f6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
10 changes: 7 additions & 3 deletions src/commonMain/kotlin/LoxoneTokenAuthenticator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class LoxoneTokenAuthenticator @JvmOverloads constructor(

else -> {
logger.debug { "Authenticating websocket with token $token" }
val authResponse = client.callForMsg(Tokens.auth(tokenHash("authenticate"), user))
val authResponse = client.callForMsg(Tokens.auth(tokenHash(client, "authenticate"), user))
token = token!!.merge(authResponse)
authWebsockets.add(client)
}
Expand All @@ -85,7 +85,7 @@ class LoxoneTokenAuthenticator @JvmOverloads constructor(

suspend fun killToken(client: LoxoneClient) = execConditionalWithMutex({ TokenState(token).isUsable }) {
logger.debug { "Going to kill token $token" }
client.callForMsg(Tokens.kill(tokenHash("killtoken"), user))
client.callForMsg(Tokens.kill(tokenHash(client, "killtoken"), user))
logger.info { "Token killed" }
token = null
authWebsockets.remove(client)
Expand All @@ -97,7 +97,11 @@ class LoxoneTokenAuthenticator @JvmOverloads constructor(
}
}

fun tokenHash(operation: String) = LoxoneCrypto.loxoneHmac(checkNotNull(token), operation)
suspend fun tokenHash(client: LoxoneClient, operation: String) = if (client is HttpLoxoneClient) {
LoxoneCrypto.loxoneHmac(checkNotNull(token?.token), client.callForMsg(commandForUser(user)), operation)
} else {
LoxoneCrypto.loxoneHmac(checkNotNull(token), operation)
}

private suspend fun execConditionalWithMutex(condition: () -> Boolean, block: suspend () -> Unit) {
if (condition()) {
Expand Down
15 changes: 10 additions & 5 deletions src/commonMain/kotlin/ktor/KtorHttpLoxoneClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,22 @@ class KtorHttpLoxoneClient internal constructor(
httpClient.close()
}

private fun HttpRequestBuilder.commandRequest(addAuth: Boolean = true, pathBuilder: URLBuilder.() -> Unit) {
private suspend fun HttpRequestBuilder.commandRequest(addAuth: Boolean = true, pathBuilder: URLBuilder.() -> Unit) {
val tokenHash = if (addAuth && authentication is LoxoneAuth.Token) {
authentication.authenticator.tokenHash(this@KtorHttpLoxoneClient, "http-autht")
} else {
null
}

url {
protocol = if (endpoint.useSsl) URLProtocol.HTTPS else URLProtocol.HTTP
host = endpoint.host
port = endpoint.port
appendEncodedPathSegments(endpoint.path)
pathBuilder()
if (addAuth && authentication is LoxoneAuth.Token) {
val authenticator = authentication.authenticator
parameters.append("autht", authenticator.tokenHash("http-autht"))
parameters.append("user", authenticator.user)
if (tokenHash != null) {
parameters.append("autht", tokenHash)
parameters.append("user", authentication.tokenAuthenticator!!.user)
}
}
}
Expand Down

0 comments on commit a2899f6

Please sign in to comment.