1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2025-12-22 22:26:01 +00:00

"TelegramAPIUrlsKeeper" now have two new things: properties "webAppDataSecretKey" and fun "checkWebAppLink"

This commit is contained in:
2022-04-29 19:11:38 +06:00
parent c70484076d
commit 4799617ced
8 changed files with 78 additions and 3 deletions

View File

@@ -0,0 +1,7 @@
package dev.inmo.tgbotapi.utils
import dev.inmo.micro_utils.crypto.SourceString
internal expect fun SourceString.hmacSha256(key: String): String
internal expect fun SourceString.hex(): String

View File

@@ -18,6 +18,10 @@ class TelegramAPIUrlsKeeper(
token: String,
hostUrl: String = telegramBotAPIDefaultUrl
) {
val webAppDataSecretKey by lazy {
token.hmacSha256("WebAppData")
}
val commonAPIUrl: String
val fileBaseUrl: String
@@ -28,4 +32,10 @@ class TelegramAPIUrlsKeeper(
}
fun createFileLinkUrl(filePath: String) = "${fileBaseUrl}/$filePath"
/**
* @param rawData Data from [dev.inmo.tgbotapi.webapps.WebApp.initData]
* @param hash Data from [dev.inmo.tgbotapi.webapps.WebApp.initDataUnsafe] from the field [dev.inmo.tgbotapi.webapps.WebAppInitData.hash]
*/
fun checkWebAppLink(rawData: String, hash: String) = rawData.hmacSha256(webAppDataSecretKey).hex() == hash
}