diff --git a/android/src/main/kotlin/ru/innim/flutter_login_vk/MethodCallHandler.kt b/android/src/main/kotlin/ru/innim/flutter_login_vk/MethodCallHandler.kt index bbf2e19..ecc58b3 100644 --- a/android/src/main/kotlin/ru/innim/flutter_login_vk/MethodCallHandler.kt +++ b/android/src/main/kotlin/ru/innim/flutter_login_vk/MethodCallHandler.kt @@ -12,6 +12,7 @@ import com.vk.api.sdk.VK.login import com.vk.api.sdk.VK.logout import com.vk.api.sdk.auth.VKAccessToken import com.vk.api.sdk.auth.VKScope +import com.vk.api.sdk.utils.VKUtils import com.vk.sdk.api.account.AccountService import com.vk.sdk.api.users.UsersService import com.vk.sdk.api.users.dto.UsersUserFull @@ -27,6 +28,7 @@ class MethodCallHandler(private val context: Context, private val loginCallback: private const val getAccessToken = "getAccessToken" private const val getUserProfile = "getUserProfile" private const val getSdkVersion = "getSdkVersion" + private const val getCertificateFingerprint = "getCertificateFingerprint" private const val initSdk = "initSdk" private const val scopeLoginArg = "scope" private const val scopeInitArg = "scope" @@ -52,6 +54,7 @@ class MethodCallHandler(private val context: Context, private val loginCallback: getAccessToken -> sendResult(getAccessToken(), r) getUserProfile -> getUserProfile(r) getSdkVersion -> sendResult(getSdkVersion(), r) + getCertificateFingerprint -> sendResult(getCertificateFingerprint(), r) initSdk -> { val initScope = call.argument?>(scopeInitArg) initSdk(initScope, r) @@ -155,6 +158,11 @@ class MethodCallHandler(private val context: Context, private val loginCallback: return metaData.metaData["VKSdkVersion"].toString() } + private fun getCertificateFingerprint(): String? { + val fingerprints: Array? = VKUtils.getCertificateFingerprint(context, context.packageName) + return fingerprints?.joinToString() + } + private fun sendResult(data: Any?, r: MethodChannel.Result) { r.success(data) } diff --git a/lib/src/vk_login.dart b/lib/src/vk_login.dart index 534aa4c..b4a3b8c 100644 --- a/lib/src/vk_login.dart +++ b/lib/src/vk_login.dart @@ -1,3 +1,5 @@ +import 'dart:io'; + import 'package:async/async.dart'; import 'package:flutter/services.dart'; import 'package:flutter/widgets.dart'; @@ -12,6 +14,7 @@ class VKLogin { static const _methodGetAccessToken = 'getAccessToken'; static const _methodGetUserProfile = 'getUserProfile'; static const _methodGetSdkVersion = 'getSdkVersion'; + static const _methodGetCertificateFingerprint = 'getCertificateFingerprint'; // TODO: rename to `permissions`? static const _argLogInScope = 'scope'; @@ -35,16 +38,12 @@ class VKLogin { /// /// If user is now logged in, than returns `null`. Future get accessToken async { - assert(_initialized, - 'SDK is not initialized. You should call initSdk() first'); + assert(_initialized, 'SDK is not initialized. You should call initSdk() first'); if (!_initialized) return null; - final tokenResult = await _channel - .invokeMethod>(_methodGetAccessToken); + final tokenResult = await _channel.invokeMethod>(_methodGetAccessToken); - return tokenResult != null - ? VKAccessToken.fromMap(tokenResult.cast()) - : null; + return tokenResult != null ? VKAccessToken.fromMap(tokenResult.cast()) : null; } /// Returns currently used VK SDK. @@ -68,8 +67,7 @@ class VKLogin { /// You can pass [scope] (and/or [customScope], see [logIn]) /// to require listed permissions. If user logged in, /// but doesn't have all of this permissions - he will be logged out. - Future> initSdk( - {List? scope, List? customScope}) async { + Future> initSdk({List? scope, List? customScope}) async { final scopeArg = _getScope(scope: scope, customScope: customScope); if (debug) { @@ -108,14 +106,11 @@ class VKLogin { } try { - final result = await _channel - .invokeMethod>(_methodGetUserProfile); + final result = await _channel.invokeMethod>(_methodGetUserProfile); if (debug) _log('User profile: $result'); - return Result.value(result != null - ? VKUserProfile.fromMap(result.cast()) - : null); + return Result.value(result != null ? VKUserProfile.fromMap(result.cast()) : null); } on PlatformException catch (e) { if (debug) _log('Get profile error: $e'); return Result.error(e); @@ -151,10 +146,8 @@ class VKLogin { /// /// If error occure during log in process, than error result /// will be returned. And [Result.error] may - Future> logIn( - {List scope = const [], List? customScope}) async { - assert(_initialized, - 'SDK is not initialized. You should call initSdk() first'); + Future> logIn({List scope = const [], List? customScope}) async { + assert(_initialized, 'SDK is not initialized. You should call initSdk() first'); if (!_initialized) throw Exception('SDK is not initialized.'); final scopeArg = _getScope(scope: scope, customScope: customScope); @@ -162,8 +155,7 @@ class VKLogin { if (debug) _log('Log In with scope $scopeArg'); try { - final res = await _channel.invokeMethod>( - _methodLogIn, {_argLogInScope: scopeArg}); + final res = await _channel.invokeMethod>(_methodLogIn, {_argLogInScope: scopeArg}); if (res == null) { return Result.error('Invalid null result'); @@ -176,9 +168,22 @@ class VKLogin { } } + /// Get fingerprints for android + Future getCertificateFingerprint() async { + assert(_initialized, 'SDK is not initialized. You should call initSdk() first'); + assert(Platform.isAndroid, 'This method is only available for android.'); + if (!_initialized) throw Exception('SDK is not initialized.'); + if (!Platform.isAndroid) return null; + + try { + return await _channel.invokeMethod(_methodGetCertificateFingerprint); + } on PlatformException catch (e) { + rethrow; + } + } + Future logOut() async { - assert(_initialized, - 'SDK is not initialized. You should call initSdk() first'); + assert(_initialized, 'SDK is not initialized. You should call initSdk() first'); if (!_initialized) return; if (debug) _log('Log Out');