-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 96ce839
Showing
86 changed files
with
2,626 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Miscellaneous | ||
*.class | ||
*.log | ||
*.pyc | ||
*.swp | ||
.DS_Store | ||
.atom/ | ||
.buildlog/ | ||
.history | ||
.svn/ | ||
migrate_working_dir/ | ||
|
||
# IntelliJ related | ||
*.iml | ||
*.ipr | ||
*.iws | ||
.idea/ | ||
|
||
# The .vscode folder contains launch configuration and tasks you configure in | ||
# VS Code which you may wish to be included in version control, so this line | ||
# is commented out by default. | ||
#.vscode/ | ||
|
||
# Flutter/Dart/Pub related | ||
# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock. | ||
/pubspec.lock | ||
**/doc/api/ | ||
.dart_tool/ | ||
.packages | ||
build/ | ||
local.properties |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# This file tracks properties of this Flutter project. | ||
# Used by Flutter tool to assess capabilities and perform upgrades etc. | ||
# | ||
# This file should be version controlled. | ||
|
||
version: | ||
revision: f72efea43c3013323d1b95cff571f3c1caa37583 | ||
channel: stable | ||
|
||
project_type: plugin | ||
|
||
# Tracks metadata for the flutter migrate command | ||
migration: | ||
platforms: | ||
- platform: root | ||
create_revision: f72efea43c3013323d1b95cff571f3c1caa37583 | ||
base_revision: f72efea43c3013323d1b95cff571f3c1caa37583 | ||
- platform: android | ||
create_revision: f72efea43c3013323d1b95cff571f3c1caa37583 | ||
base_revision: f72efea43c3013323d1b95cff571f3c1caa37583 | ||
- platform: ios | ||
create_revision: f72efea43c3013323d1b95cff571f3c1caa37583 | ||
base_revision: f72efea43c3013323d1b95cff571f3c1caa37583 | ||
|
||
# User provided section | ||
|
||
# List of Local paths (relative to this file) that should be | ||
# ignored by the migrate tool. | ||
# | ||
# Files that are not part of the templates will be ignored by default. | ||
unmanaged_files: | ||
- 'lib/main.dart' | ||
- 'ios/Runner.xcodeproj/project.pbxproj' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
## 1.0.1 | ||
|
||
* Update license | ||
|
||
## 1.0.0 | ||
|
||
* Initial Open Source release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 AuthenTrend Technology Inc. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# flutter_passkey | ||
|
||
A Flutter plugin for Passkey. | ||
|
||
| Android | iOS | MacOS | Web | Linux | Windows | | ||
| :-----: | :-: | :---: | :-: | :---: | :-----: | | ||
| V | V | X | X | X | X | | ||
|
||
## Usage | ||
|
||
To use this plugin, add `flutter_passkey` as a [dependency in your pubspec.yaml file](https://flutter.dev/platform-plugins/). | ||
|
||
### Example | ||
|
||
``` dart | ||
final flutterPasskeyPlugin = FlutterPasskey(); | ||
final isPasskeySupported = await flutterPasskeyPlugin.isSupported(); | ||
// Obtain creationOptions from the server | ||
final creationOptions = getCredentialCreationOptions(); | ||
flutterPasskeyPlugin.createCredential(creationOptions).then((response) { | ||
// Send response to the server | ||
}).catchError((error) { | ||
// Handle error | ||
}); | ||
// Obtain requestOptions from the server | ||
final requestOptions = getCredentialRequestOptions(); | ||
flutterPasskeyPlugin.getCredential(requestOptions).then((response) { | ||
// Send response to the server | ||
}).catchError((error) { | ||
// Handle error | ||
}); | ||
``` | ||
|
||
See the example app for more complex examples. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
include: package:flutter_lints/flutter.yaml | ||
|
||
# Additional information about this file can be found at | ||
# https://dart.dev/guides/language/analysis-options |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
*.iml | ||
.gradle | ||
/local.properties | ||
/.idea/workspace.xml | ||
/.idea/libraries | ||
.DS_Store | ||
/build | ||
/captures | ||
.cxx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
group 'com.authentrend.flutter_passkey' | ||
version '1.0-SNAPSHOT' | ||
|
||
buildscript { | ||
ext.kotlin_version = '1.7.10' | ||
repositories { | ||
google() | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
classpath 'com.android.tools.build:gradle:7.2.0' | ||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" | ||
} | ||
} | ||
|
||
allprojects { | ||
repositories { | ||
google() | ||
mavenCentral() | ||
} | ||
} | ||
|
||
apply plugin: 'com.android.library' | ||
apply plugin: 'kotlin-android' | ||
|
||
android { | ||
compileSdkVersion 31 | ||
|
||
compileOptions { | ||
sourceCompatibility JavaVersion.VERSION_1_8 | ||
targetCompatibility JavaVersion.VERSION_1_8 | ||
} | ||
|
||
kotlinOptions { | ||
jvmTarget = '1.8' | ||
} | ||
|
||
sourceSets { | ||
main.java.srcDirs += 'src/main/kotlin' | ||
} | ||
|
||
defaultConfig { | ||
minSdkVersion 16 | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.1" | ||
implementation "androidx.credentials:credentials:1.0.0-alpha03" | ||
|
||
// optional - needed for credentials support from play services, for devices running | ||
// Android 13 and below. | ||
implementation "androidx.credentials:credentials-play-services-auth:1.0.0-alpha03" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
rootProject.name = 'flutter_passkey' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.authentrend.flutter_passkey"> | ||
</manifest> |
161 changes: 161 additions & 0 deletions
161
android/src/main/kotlin/com/authentrend/flutter_passkey/FlutterPasskeyPlugin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
package com.authentrend.flutter_passkey | ||
|
||
import android.app.Activity | ||
import androidx.annotation.NonNull | ||
import androidx.credentials.CreatePublicKeyCredentialRequest | ||
import androidx.credentials.CreatePublicKeyCredentialResponse | ||
import androidx.credentials.CredentialManager | ||
import androidx.credentials.GetCredentialRequest | ||
import androidx.credentials.GetPublicKeyCredentialOption | ||
import androidx.credentials.PublicKeyCredential | ||
import androidx.credentials.exceptions.CreateCredentialException | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding | ||
import io.flutter.embedding.engine.plugins.activity.ActivityAware | ||
import io.flutter.embedding.engine.plugins.FlutterPlugin | ||
import io.flutter.plugin.common.MethodCall | ||
import io.flutter.plugin.common.MethodChannel | ||
import io.flutter.plugin.common.MethodChannel.MethodCallHandler | ||
import io.flutter.plugin.common.MethodChannel.Result | ||
import kotlinx.coroutines.launch | ||
import org.json.JSONObject | ||
import java.security.InvalidParameterException | ||
|
||
/** FlutterPasskeyPlugin */ | ||
class FlutterPasskeyPlugin: FlutterPlugin, MethodCallHandler, ActivityAware, ViewModel() { | ||
/// The MethodChannel that will the communication between Flutter and native Android | ||
/// | ||
/// This local reference serves to register the plugin with the Flutter Engine and unregister it | ||
/// when the Flutter Engine is detached from the Activity | ||
private lateinit var channel: MethodChannel | ||
private var activity: Activity? = null | ||
|
||
private fun getPlatformVersion(): String { | ||
return "Android ${android.os.Build.VERSION.RELEASE}" | ||
} | ||
|
||
private fun createCredential(@NonNull options: String, @NonNull callback: (credential: String?, e: Exception?) -> Unit) { | ||
if (activity == null) { | ||
throw IllegalStateException("Activity not found") | ||
} | ||
JSONObject(options) // check if options is a valid json string | ||
val createPublicKeyCredentialRequest = CreatePublicKeyCredentialRequest( | ||
requestJson = options, | ||
preferImmediatelyAvailableCredentials = false | ||
) | ||
viewModelScope.launch { | ||
try { | ||
val credentialManager = CredentialManager.create(activity!!) | ||
val result = credentialManager.createCredential( | ||
request = createPublicKeyCredentialRequest, | ||
activity = activity!!, | ||
) | ||
val credential = result as CreatePublicKeyCredentialResponse | ||
callback(credential.registrationResponseJson, null) | ||
} catch (e: Exception) { | ||
callback(null, e) | ||
} | ||
} | ||
} | ||
|
||
private fun getCredential(@NonNull options: String, @NonNull callback: (credential: String?, e: Exception?) -> Unit) { | ||
if (activity == null) { | ||
throw IllegalStateException("Activity not found") | ||
} | ||
JSONObject(options) // check if options is a valid json string | ||
val getPublicKeyCredentialOption = GetPublicKeyCredentialOption( | ||
requestJson = options, | ||
preferImmediatelyAvailableCredentials = false | ||
) | ||
viewModelScope.launch { | ||
try { | ||
val credentialManager = CredentialManager.create(activity!!) | ||
val result = credentialManager.getCredential( | ||
request = GetCredentialRequest(listOf(getPublicKeyCredentialOption)), | ||
activity = activity!!, | ||
) | ||
val credential = result.credential as PublicKeyCredential | ||
callback(credential.authenticationResponseJson, null) | ||
} catch (e: Exception) { | ||
callback(null, e) | ||
} | ||
} | ||
} | ||
|
||
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) { | ||
channel = MethodChannel(flutterPluginBinding.binaryMessenger, "flutter_passkey") | ||
channel.setMethodCallHandler(this) | ||
} | ||
|
||
override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) { | ||
when (call.method) { | ||
"getPlatformVersion" -> result.success(getPlatformVersion()) | ||
"createCredential" -> { | ||
val options = call.argument("options") as String? | ||
if (options == null) { | ||
result.error("InvalidParameterException", "Options not found", null) | ||
return | ||
} | ||
try { | ||
createCredential(options) { credential, e -> | ||
if (credential != null) { | ||
result.success(credential) | ||
} | ||
else if (e != null) { | ||
result.error(e.javaClass.kotlin.simpleName ?: "Exception", e.message ?: "Exception occurred", null) | ||
} | ||
else { | ||
result.error("Error", "Unknown error", null) | ||
} | ||
} | ||
} catch (e: Exception) { | ||
result.error(e.javaClass.kotlin.simpleName ?: "Exception", e.message ?: "Exception occurred", null) | ||
} | ||
} | ||
"getCredential" -> { | ||
val options = call.argument("options") as String? | ||
if (options == null) { | ||
result.error("InvalidParameterException", "Options not found", null) | ||
return | ||
} | ||
try { | ||
getCredential(options) { credential, e -> | ||
if (credential != null) { | ||
result.success(credential) | ||
} | ||
else if (e != null) { | ||
result.error(e.javaClass.kotlin.simpleName ?: "Exception", e.message ?: "Exception occurred", null) | ||
} | ||
else { | ||
result.error("Error", "Unknown error", null) | ||
} | ||
} | ||
} catch (e: Exception) { | ||
result.error(e.javaClass.kotlin.simpleName ?: "Exception", e.message ?: "Exception occurred", null) | ||
} | ||
} | ||
else -> result.notImplemented() | ||
} | ||
} | ||
|
||
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) { | ||
channel.setMethodCallHandler(null) | ||
} | ||
|
||
override fun onAttachedToActivity(binding: ActivityPluginBinding) { | ||
activity = binding.getActivity() | ||
} | ||
|
||
override fun onDetachedFromActivity() { | ||
activity = null | ||
} | ||
|
||
override fun onDetachedFromActivityForConfigChanges() { | ||
activity = null | ||
} | ||
|
||
override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) { | ||
activity = binding.getActivity() | ||
} | ||
} |
Oops, something went wrong.