1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-12-22 16:47:13 +00:00

update support of BiometricManager

This commit is contained in:
InsanusMokrassar 2024-04-18 16:40:59 +06:00
parent 28e031265c
commit db644c93ff
6 changed files with 114 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package dev.inmo.tgbotapi.webapps package dev.inmo.tgbotapi.webapps
import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper
import dev.inmo.tgbotapi.webapps.biometric.BiometricManager
import dev.inmo.tgbotapi.webapps.cloud.CloudStorage import dev.inmo.tgbotapi.webapps.cloud.CloudStorage
import dev.inmo.tgbotapi.webapps.haptic.HapticFeedback import dev.inmo.tgbotapi.webapps.haptic.HapticFeedback
import dev.inmo.tgbotapi.webapps.invoice.InvoiceClosedInfo import dev.inmo.tgbotapi.webapps.invoice.InvoiceClosedInfo
@ -54,6 +55,9 @@ external class WebApp {
@JsName("CloudStorage") @JsName("CloudStorage")
val cloudStorage: CloudStorage val cloudStorage: CloudStorage
@JsName("BiometricManager")
val biometricManager: BiometricManager
@JsName("SettingsButton") @JsName("SettingsButton")
val settingsButton: SettingsButton val settingsButton: SettingsButton

View File

@ -0,0 +1,15 @@
package dev.inmo.tgbotapi.webapps.biometric
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.buildJsonObject
import kotlin.js.json
external interface BiometricAuthenticateParams {
val reason: String?
}
fun BiometricAuthenticateParams(
reason: String? = null
) = buildJsonObject {
reason ?.let { put("reason", JsonPrimitive(it)) }
}.unsafeCast<BiometricAuthenticateParams>()

View File

@ -0,0 +1,51 @@
package dev.inmo.tgbotapi.webapps.biometric
import kotlinx.coroutines.CompletableDeferred
external interface BiometricManager {
val isInited: Boolean
val isBiometricAvailable: Boolean
val isAccessRequested: Boolean
val isAccessGranted: Boolean
val isBiometricTokenSaved: Boolean
val deviceId: DeviceId?
val biometricType: BiometricType
fun init(callback: (() -> Unit) = definedExternally): BiometricManager
fun requestAccess(params: BiometricRequestAccessParams, callback: ((Boolean) -> Unit) = definedExternally): BiometricManager
fun authenticate(params: BiometricAuthenticateParams, callback: ((Boolean, String?) -> Unit) = definedExternally): BiometricManager
fun updateBiometricToken(token: String, callback: ((Boolean) -> Unit) = definedExternally): BiometricManager
fun openSettings(): BiometricManager
}
private suspend inline fun <T> doWithAsyncJob(
action: (CompletableDeferred<T>) -> BiometricManager
): T {
val async = CompletableDeferred<T>()
action(async)
return async.await()
}
suspend fun BiometricManager.initSuspend() = doWithAsyncJob {
init {
it.complete(Unit)
}
}
suspend fun BiometricManager.requestAccessSuspend(params: BiometricRequestAccessParams) = doWithAsyncJob {
requestAccess(params) { success ->
it.complete(success)
}
}
suspend fun BiometricManager.authenticateSuspend(params: BiometricAuthenticateParams) = doWithAsyncJob {
authenticate(params) { _, token ->
it.complete(token)
}
}
suspend fun BiometricManager.updateBiometricTokenSuspend(token: String) = doWithAsyncJob {
updateBiometricToken(token) { success ->
it.complete(success)
}
}

View File

@ -0,0 +1,15 @@
package dev.inmo.tgbotapi.webapps.biometric
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.buildJsonObject
import kotlin.js.json
external interface BiometricRequestAccessParams {
val reason: String?
}
fun BiometricRequestAccessParams(
reason: String? = null
) = buildJsonObject {
reason ?.let { put("reason", JsonPrimitive(it)) }
}.unsafeCast<BiometricRequestAccessParams>()

View File

@ -0,0 +1,21 @@
package dev.inmo.tgbotapi.webapps.biometric
import kotlinx.serialization.Serializable
@Serializable
value class BiometricType(
val title: String
) {
val isFinger: Boolean
get() = title == FingerTypeTitle
val isFace: Boolean
get() = title == FaceTypeTitle
val isUnknown: Boolean
get() = title == UnknownTypeTitle
companion object {
const val FingerTypeTitle = "finger"
const val FaceTypeTitle = "face"
const val UnknownTypeTitle = "unknown"
}
}

View File

@ -0,0 +1,8 @@
package dev.inmo.tgbotapi.webapps.biometric
import kotlinx.serialization.Serializable
@Serializable
value class DeviceId(
val string: String
)