From db644c93ffaace38c67f64134dd1369fec94cef8 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 18 Apr 2024 16:40:59 +0600 Subject: [PATCH] update support of BiometricManager --- .../dev/inmo/tgbotapi/webapps/WebApp.kt | 4 ++ .../biometric/BiometricAuthenticateParams.kt | 15 ++++++ .../webapps/biometric/BiometricManager.kt | 51 +++++++++++++++++++ .../biometric/BiometricRequestAccessParams.kt | 15 ++++++ .../webapps/biometric/BiometricType.kt | 21 ++++++++ .../tgbotapi/webapps/biometric/DeviceId.kt | 8 +++ 6 files changed, 114 insertions(+) create mode 100644 tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/biometric/BiometricAuthenticateParams.kt create mode 100644 tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/biometric/BiometricManager.kt create mode 100644 tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/biometric/BiometricRequestAccessParams.kt create mode 100644 tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/biometric/BiometricType.kt create mode 100644 tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/biometric/DeviceId.kt diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebApp.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebApp.kt index d6f4fe7f4e..fe8738feb9 100644 --- a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebApp.kt +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebApp.kt @@ -1,6 +1,7 @@ package dev.inmo.tgbotapi.webapps 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.haptic.HapticFeedback import dev.inmo.tgbotapi.webapps.invoice.InvoiceClosedInfo @@ -54,6 +55,9 @@ external class WebApp { @JsName("CloudStorage") val cloudStorage: CloudStorage + @JsName("BiometricManager") + val biometricManager: BiometricManager + @JsName("SettingsButton") val settingsButton: SettingsButton diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/biometric/BiometricAuthenticateParams.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/biometric/BiometricAuthenticateParams.kt new file mode 100644 index 0000000000..612479617f --- /dev/null +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/biometric/BiometricAuthenticateParams.kt @@ -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() diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/biometric/BiometricManager.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/biometric/BiometricManager.kt new file mode 100644 index 0000000000..41798ec126 --- /dev/null +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/biometric/BiometricManager.kt @@ -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 doWithAsyncJob( + action: (CompletableDeferred) -> BiometricManager +): T { + val async = CompletableDeferred() + 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) + } +} diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/biometric/BiometricRequestAccessParams.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/biometric/BiometricRequestAccessParams.kt new file mode 100644 index 0000000000..099dcb81e6 --- /dev/null +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/biometric/BiometricRequestAccessParams.kt @@ -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() diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/biometric/BiometricType.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/biometric/BiometricType.kt new file mode 100644 index 0000000000..2250ce3ed5 --- /dev/null +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/biometric/BiometricType.kt @@ -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" + } +} diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/biometric/DeviceId.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/biometric/DeviceId.kt new file mode 100644 index 0000000000..cddde7b69c --- /dev/null +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/biometric/DeviceId.kt @@ -0,0 +1,8 @@ +package dev.inmo.tgbotapi.webapps.biometric + +import kotlinx.serialization.Serializable + +@Serializable +value class DeviceId( + val string: String +) \ No newline at end of file