From 3cdd8767c299b328f324304b93dfe8a1fe7b2907 Mon Sep 17 00:00:00 2001 From: andriikuliahin Date: Tue, 5 Mar 2024 15:07:39 +0200 Subject: [PATCH] Updated Android and iOS sdk-s, add new createFingerprint method, updated types, added event metadata --- package.json | 2 +- plugin.xml | 8 +-- .../com/keyri/cordova/plugin/CordovaKeyri.kt | 55 ++++++++++----- .../keyri/cordova/plugin/CordovaKeyri.swift | 17 ++++- www/index.ts | 12 ++-- www/types.ts | 70 ++++++++++++++++--- 6 files changed, 124 insertions(+), 40 deletions(-) diff --git a/package.json b/package.json index ac15673..8d0d302 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "version": "0.2.0", + "version": "0.3.0", "name": "@keyri/cordova-keyri", "cordova_name": "Cordova Keyri Plugin", "description": "Cordova Keyri SDK Plugin for QR login", diff --git a/plugin.xml b/plugin.xml index 3c24963..b0ac4da 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,7 +1,7 @@ + version="0.3.0"> CordovaKeyri Cordova Keyri SDK Plugin @@ -22,8 +22,8 @@ - - + + - + diff --git a/src/android/com/keyri/cordova/plugin/CordovaKeyri.kt b/src/android/com/keyri/cordova/plugin/CordovaKeyri.kt index 6a56c5e..53c4484 100644 --- a/src/android/com/keyri/cordova/plugin/CordovaKeyri.kt +++ b/src/android/com/keyri/cordova/plugin/CordovaKeyri.kt @@ -7,7 +7,7 @@ import android.content.Intent import androidx.fragment.app.FragmentActivity import com.keyrico.keyrisdk.Keyri import com.keyrico.keyrisdk.entity.session.Session -import com.keyrico.keyrisdk.sec.fraud.enums.EventType +import com.keyrico.keyrisdk.sec.fraud.event.EventType import com.google.gson.Gson import org.apache.cordova.CallbackContext import org.apache.cordova.CordovaPlugin @@ -89,11 +89,14 @@ class CordovaKeyri : CordovaPlugin() { "sendEvent" -> { val publicUserId = arguments?.getString(0) val eventType = arguments?.getString(1) - val success = arguments?.getBoolean(2) ?: true + val eventMetadata = arguments?.getString(2) + val success = arguments?.getBoolean(3) ?: true - sendEvent(publicUserId, eventType, success, callbackContext) + sendEvent(publicUserId, eventType, eventMetadata, success, callbackContext) } + "createFingerprint" -> createFingerprint(callbackContext) + "initiateQrSession" -> { val sessionId = arguments?.getString(0) val publicUserId = arguments?.getString(1) @@ -199,7 +202,16 @@ class CordovaKeyri : CordovaPlugin() { } else { cordova.getActivity()?.let { cordova.setActivityResultCallback(this) - easyKeyriAuth(it, AUTH_REQUEST_CODE, appKey, publicApiKey, serviceEncryptionKey, blockEmulatorDetection, payload, publicUserId) + easyKeyriAuth( + it, + AUTH_REQUEST_CODE, + appKey, + publicApiKey, + serviceEncryptionKey, + blockEmulatorDetection, + payload, + publicUserId + ) easyKeyriAuthCallback = callback } ?: callback.error("initializeDefaultScreen, can't get cordova.getActivity()") @@ -285,20 +297,26 @@ class CordovaKeyri : CordovaPlugin() { } } - private fun sendEvent(publicUserId: String?, eventType: String?, success: Boolean, callback: CallbackContext) { + private fun sendEvent(publicUserId: String?, eventType: String?, eventMetadata: String?, success: Boolean, callback: CallbackContext) { keyriCoroutineScope(callback).launch { - val type = EventType.values().firstOrNull { it.type == eventType } + val jsonMetadata = eventMetadata?.let(::JSONObject) + val type = EventType.custom(eventType ?: "visits", jsonMetadata) + val userId = publicUserId ?: "ANON" - if (type == null) { - callback.error("sendEvent, eventType must not be null") - } else { - val userId = publicUserId ?: "ANON" + keyri.sendEvent(userId, type, success).onSuccess { fingerprintResponse -> + callback.success(JSONObject(Gson().toJson(fingerprintResponse))) + }.onFailure { + callback.error("sendEvent, ${it.message}") + } + } + } - keyri.sendEvent(userId, type, success).onSuccess { - callback.success() - }.onFailure { - callback.error("sendEvent, ${it.message}") - } + private fun createFingerprint(callback: CallbackContext) { + keyriCoroutineScope(callback).launch { + keyri.createFingerprint().onSuccess { fingerprintRequest -> + callback.success(JSONObject(Gson().toJson(fingerprintRequest))) + }.onFailure { + callback.error("createFingerprint, ${it.message}") } } } @@ -347,9 +365,10 @@ class CordovaKeyri : CordovaPlugin() { callback.error("initializeDefaultConfirmationScreen, payload must not be null") } else { (cordova.getActivity() as? FragmentActivity)?.supportFragmentManager?.let { fm -> - keyri.initializeDefaultConfirmationScreen(fm, requireNotNull(activeSession), payload).onSuccess { authResult -> - callback.success() - }.onFailure { + keyri.initializeDefaultConfirmationScreen(fm, requireNotNull(activeSession), payload) + .onSuccess { authResult -> + callback.success() + }.onFailure { callback.error("initializeDefaultConfirmationScreen, ${it.message}") } } ?: callback.error("initializeDefaultConfirmationScreen, can't get supportFragmentManager") diff --git a/src/iOS/com/keyri/cordova/plugin/CordovaKeyri.swift b/src/iOS/com/keyri/cordova/plugin/CordovaKeyri.swift index a669920..a21c5d9 100644 --- a/src/iOS/com/keyri/cordova/plugin/CordovaKeyri.swift +++ b/src/iOS/com/keyri/cordova/plugin/CordovaKeyri.swift @@ -162,9 +162,10 @@ import Keyri return } - let success = command.arguments[2] as? String ?? "true" + let eventMetadata = command.arguments[2] as? String + let success = command.arguments[3] as? String ?? "true" - keyri?.sendEvent(publicUserId: publicUserId, eventType: EventType(rawValue: eventType) ?? .visits, success: Bool(success) ?? true) { result in + keyri?.sendEvent(publicUserId: publicUserId, eventType: EventType.custom(name: eventType, metadata: eventMetadata), success: Bool(success) ?? true) { result in switch result { case .success(let fingerprintResponse): self.processResult(message: fingerprintResponse.asDictionary(), command: command) @@ -174,6 +175,18 @@ import Keyri } ?? sendIsNotInitialized(methodName: "sendEvent", command: command) } + @objc(createFingerprint) + func createFingerprint(command: CDVInvokedUrlCommand) { + keyri?.createFingerprint() { result in + switch result { + case .success(let fingerprintRequest): + self.processResult(message: fingerprintRequest.asDictionary(), command: command) + case .failure(let error): + self.processError(error: error, command: command) + } + } ?? sendIsNotInitialized(methodName: "createFingerprint", command: command) + } + @objc(initiateQrSession:) func initiateQrSession(command: CDVInvokedUrlCommand) { guard let sessionId = command.arguments[0] as? String else { diff --git a/www/index.ts b/www/index.ts index 4a51458..d7c78d4 100644 --- a/www/index.ts +++ b/www/index.ts @@ -1,7 +1,7 @@ -import type { +import { ProcessLinkOptions, InitializeKeyriOptions, - SendEventOptions, LoginObject, RegisterObject, + SendEventOptions, LoginObject, RegisterObject, KeyriFingerprintRequest, } from './types'; import {KeyriFingerprintEventResponse, KeyriSession} from "./types"; @@ -48,11 +48,15 @@ export class CordovaKeyriPlugin { }; sendEvent(data: SendEventOptions): Promise { - return asPromise('sendEvent', [data.publicUserId, data.eventType, String(data.success)]); + return asPromise('sendEvent', [data.publicUserId, data.eventType.name, JSON.stringify(data.eventType.metadata), String(data.success)]); + }; + + createFingerprint(): Promise { + return asPromise('createFingerprint'); }; initiateQrSession(sessionId: string, publicUserId?: string): Promise { - return asPromise('initiateQrSession', [sessionId, publicUserId]); + return asPromise('initiateQrSession', [sessionId, publicUserId]); }; login(publicUserId?: string): Promise { diff --git a/www/types.ts b/www/types.ts index 44bc8af..82b3bec 100644 --- a/www/types.ts +++ b/www/types.ts @@ -30,6 +30,13 @@ export interface KeyriFingerprintEventResponse { salt: string; } +export interface KeyriFingerprintRequest { + clientEncryptionKey: string; + encryptedPayload: string; + salt: string; + iv: string; +} + export interface KeyriUserParameters { base64EncodedData?: string; } @@ -92,15 +99,56 @@ export interface SendEventOptions { success: boolean; } -export enum EventType { - Visits = 'visits', - Login = 'login', - Signup = 'signup', - AttachNewDevice = 'attach_new_device', - EmailChange = 'email_change', - ProfileUpdate = 'profile_update', - PasswordReset = 'password_reset', - Withdrawal = 'withdrawal', - Deposit = 'deposit', - Purchase = 'purchase', +export class EventType { + name: string; + metadata?: object; + + constructor(name: string, metadata?: object) { + this.name = name; + this.metadata = metadata; + } + + static visits(metadata?: object): EventType { + return new EventType('visits', metadata); + } + + static login(metadata?: object): EventType { + return new EventType('login', metadata); + } + + static signup(metadata?: object): EventType { + return new EventType('signup', metadata); + } + + static attachNewDevice(metadata?: object): EventType { + return new EventType('attach_new_device', metadata); + } + + static emailChange(metadata?: object): EventType { + return new EventType('email_change', metadata); + } + + static profileUpdate(metadata?: object): EventType { + return new EventType('profile_update', metadata); + } + + static passwordReset(metadata?: object): EventType { + return new EventType('password_reset', metadata); + } + + static withdrawal(metadata?: object): EventType { + return new EventType('withdrawal', metadata); + } + + static deposit(metadata?: object): EventType { + return new EventType('deposit', metadata); + } + + static purchase(metadata?: object): EventType { + return new EventType('purchase', metadata); + } + + static custom(name: string, metadata?: object): EventType { + return new EventType(name, metadata); + } }