Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuajylin committed Apr 26, 2023
0 parents commit 96ce839
Show file tree
Hide file tree
Showing 86 changed files with 2,626 additions and 0 deletions.
31 changes: 31 additions & 0 deletions .gitignore
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
33 changes: 33 additions & 0 deletions .metadata
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'
7 changes: 7 additions & 0 deletions CHANGELOG.md
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
21 changes: 21 additions & 0 deletions LICENSE
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.
37 changes: 37 additions & 0 deletions README.md
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.
4 changes: 4 additions & 0 deletions analysis_options.yaml
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
9 changes: 9 additions & 0 deletions android/.gitignore
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
55 changes: 55 additions & 0 deletions android/build.gradle
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"
}
1 change: 1 addition & 0 deletions android/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = 'flutter_passkey'
3 changes: 3 additions & 0 deletions android/src/main/AndroidManifest.xml
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>
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()
}
}
Loading

0 comments on commit 96ce839

Please sign in to comment.