diff options
author | Tiem Song <tiem@google.com> | 2017-11-17 18:02:48 +0000 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2017-11-17 18:02:48 +0000 |
commit | 344223f9f41ea28ea9f09f81686d5665260f3918 (patch) | |
tree | 648d92a37214a46d946cfce0fa40746df144c179 | |
parent | 6cb485a0c2cbe9be5f447c7d375725b7f087edfd (diff) | |
parent | 6fd99d6913f1019717bfb143189921d3444d23cb (diff) | |
download | android-344223f9f41ea28ea9f09f81686d5665260f3918.tar.gz |
Prepare FingerprintDialog Kotlin sample for publishing
am: 6fd99d6913
Change-Id: I96c302097408e6b5081ad861656a4b9d793090d9
5 files changed, 130 insertions, 9 deletions
diff --git a/security/FingerprintDialog/kotlinApp/README.md b/security/FingerprintDialog/kotlinApp/README.md new file mode 100644 index 00000000..0e6bbb0a --- /dev/null +++ b/security/FingerprintDialog/kotlinApp/README.md @@ -0,0 +1,86 @@ + +Android FingerprintDialog Sample (Kotlin) +========================================= + +A sample that demonstrates to use registered fingerprints to authenticate the user in your app + +Introduction +------------ + +This sample demonstrates how you can use registered fingerprints in your app to authenticate the +user before proceeding some actions such as purchasing an item. + +First you need to create a symmetric key in the Android Key Store using [KeyGenerator][1] +which can be only be used after the user has authenticated with fingerprint and pass +a [KeyGenParameterSpec][2]. + +By setting [KeyGenParameterSpec.Builder.setUserAuthenticationRequired][3] to true, you can permit +the use of the key only after the user authenticate it including when authenticated with the user's +fingerprint. + +Then start listening to a fingerprint on the fingerprint sensor by calling +[FingerprintManager.authenticate][4] with a [Cipher][5] initialized with the symmetric key created. +Or alternatively you can fall back to server-side verified password as an authenticator. + +Once the fingerprint (or password) is verified, the +[FingerprintManager.AuthenticationCallback#onAuthenticationSucceeded()][6] callback is called. + +[1]: https://developer.android.com/reference/javax/crypto/KeyGenerator.html +[2]: https://developer.android.com/reference/android/security/keystore/KeyGenParameterSpec.html +[3]: https://developer.android.com/reference/android/security/keystore/KeyGenParameterSpec.Builder.html#setUserAuthenticationRequired%28boolean%29 +[4]: https://developer.android.com/reference/android/hardware/fingerprint/FingerprintManager.html#authenticate%28android.hardware.fingerprint.FingerprintManager.CryptoObject,%20android.os.CancellationSignal,%20int,%20android.hardware.fingerprint.FingerprintManager.AuthenticationCallback,%20android.os.Handler%29 +[5]: https://developer.android.com/reference/javax/crypto/Cipher.html +[6]: https://developer.android.com/reference/android/hardware/fingerprint/FingerprintManager.AuthenticationCallback.html#onAuthenticationSucceeded%28android.hardware.fingerprint.FingerprintManager.AuthenticationResult%29 + +Pre-requisites +-------------- + +- Android SDK 26 +- Android Build Tools v26.0.1 +- Android Support Repository + +Screenshots +------------- + +<img src="screenshots/1-purchase-screen.png" height="400" alt="Screenshot"/> +<img src="screenshots/2-fingerprint-dialog.png" height="400" alt="Screenshot"/> +<img src="screenshots/3-fingerprint-authenticated.png" height="400" alt="Screenshot"/> +<img src="screenshots/4-new-fingerprint-enrolled.png" height="400" alt="Screenshot"/> + +Getting Started +--------------- + +This sample uses the Gradle build system. To build this project, use the +"gradlew build" command or use "Import Project" in Android Studio. + +Support +------- + +- Google+ Community: https://plus.google.com/communities/105153134372062985968 +- Stack Overflow: http://stackoverflow.com/questions/tagged/android + +If you've found an error in this sample, please file an issue: +https://github.com/googlesamples/android-FingerprintDialog + +Patches are encouraged, and may be submitted by forking this project and +submitting a pull request through GitHub. Please see CONTRIBUTING.md for more details. + +License +------- + +Copyright 2017 The Android Open Source Project, Inc. + +Licensed to the Apache Software Foundation (ASF) under one or more contributor +license agreements. See the NOTICE file distributed with this work for +additional information regarding copyright ownership. The ASF licenses this +file to you under the Apache License, Version 2.0 (the "License"); you may not +use this file except in compliance with the License. You may obtain a copy of +the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +License for the specific language governing permissions and limitations under +the License. diff --git a/security/FingerprintDialog/kotlinApp/app/src/main/java/com/example/android/fingerprintdialog/FingerprintAuthenticationDialogFragment.kt b/security/FingerprintDialog/kotlinApp/app/src/main/java/com/example/android/fingerprintdialog/FingerprintAuthenticationDialogFragment.kt index 8e0fe0bd..f095e100 100644 --- a/security/FingerprintDialog/kotlinApp/app/src/main/java/com/example/android/fingerprintdialog/FingerprintAuthenticationDialogFragment.kt +++ b/security/FingerprintDialog/kotlinApp/app/src/main/java/com/example/android/fingerprintdialog/FingerprintAuthenticationDialogFragment.kt @@ -50,13 +50,13 @@ class FingerprintAuthenticationDialogFragment : DialogFragment(), private lateinit var secondDialogButton: Button private lateinit var useFingerprintFutureCheckBox: CheckBox - internal lateinit var callback: Callback - internal lateinit var cryptoObject: FingerprintManager.CryptoObject + private lateinit var callback: Callback + private lateinit var cryptoObject: FingerprintManager.CryptoObject private lateinit var fingerprintUiHelper: FingerprintUiHelper private lateinit var inputMethodManager: InputMethodManager private lateinit var sharedPreferences: SharedPreferences - internal var stage = Stage.FINGERPRINT + private var stage = Stage.FINGERPRINT private val showKeyboardRunnable = Runnable { inputMethodManager.showSoftInput(passwordEditText, 0) @@ -128,6 +128,18 @@ class FingerprintAuthenticationDialogFragment : DialogFragment(), sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) } + fun setCallback(callback: Callback) { + this.callback = callback + } + + fun setCryptoObject(cryptoObject: FingerprintManager.CryptoObject) { + this.cryptoObject = cryptoObject + } + + fun setStage(stage: Stage) { + this.stage = stage + } + /** * Switches to backup (password) screen. This either can happen when fingerprint is not * available or the user chooses to use the password authentication method by pressing the diff --git a/security/FingerprintDialog/kotlinApp/app/src/main/java/com/example/android/fingerprintdialog/MainActivity.kt b/security/FingerprintDialog/kotlinApp/app/src/main/java/com/example/android/fingerprintdialog/MainActivity.kt index 27bcd250..0a4732dd 100644 --- a/security/FingerprintDialog/kotlinApp/app/src/main/java/com/example/android/fingerprintdialog/MainActivity.kt +++ b/security/FingerprintDialog/kotlinApp/app/src/main/java/com/example/android/fingerprintdialog/MainActivity.kt @@ -297,8 +297,8 @@ class MainActivity : AppCompatActivity(), findViewById<View>(R.id.encrypted_message).visibility = View.GONE val fragment = FingerprintAuthenticationDialogFragment() - fragment.cryptoObject = FingerprintManager.CryptoObject(cipher) - fragment.callback = this@MainActivity + fragment.setCryptoObject(FingerprintManager.CryptoObject(cipher)) + fragment.setCallback(this@MainActivity) // Set up the crypto object for later, which will be authenticated by fingerprint usage. if (initCipher(cipher, keyName)) { @@ -308,15 +308,15 @@ class MainActivity : AppCompatActivity(), val useFingerprintPreference = sharedPreferences .getBoolean(getString(R.string.use_fingerprint_to_authenticate_key), true) if (useFingerprintPreference) { - fragment.stage = Stage.FINGERPRINT + fragment.setStage(Stage.FINGERPRINT) } else { - fragment.stage = Stage.PASSWORD + fragment.setStage(Stage.PASSWORD) } } else { // This happens if the lock screen has been disabled or or a fingerprint was // enrolled. Thus, show the dialog to authenticate with their password first and ask // the user if they want to authenticate with a fingerprint in the future. - fragment.stage = Stage.NEW_FINGERPRINT_ENROLLED + fragment.setStage(Stage.NEW_FINGERPRINT_ENROLLED) } fragment.show(fragmentManager, DIALOG_FRAGMENT_TAG) } diff --git a/security/FingerprintDialog/kotlinApp/build.gradle b/security/FingerprintDialog/kotlinApp/build.gradle index 624b2312..ccd6eb2b 100644 --- a/security/FingerprintDialog/kotlinApp/build.gradle +++ b/security/FingerprintDialog/kotlinApp/build.gradle @@ -1,5 +1,5 @@ buildscript { - ext.kotlin_version = '1.1.51' + ext.kotlin_version = '1.1.60' repositories { google() jcenter() diff --git a/security/FingerprintDialog/kotlinApp/packaging.yaml b/security/FingerprintDialog/kotlinApp/packaging.yaml new file mode 100644 index 00000000..18464868 --- /dev/null +++ b/security/FingerprintDialog/kotlinApp/packaging.yaml @@ -0,0 +1,23 @@ + +# GOOGLE SAMPLE PACKAGING DATA +# +# This file is used by Google as part of our samples packaging process. +# End users may safely ignore this file. It has no relevance to other systems. +--- +status: PUBLISHED +technologies: [Android] +categories: [security] +languages: [Kotlin] +solutions: [Mobile] +github: android-FingerprintDialog +level: INTERMEDIATE +icon: screenshots/big-icon.png +apiRefs: + - android:android.hardware.fingerprint.FingerprintManager + - android:android.hardware.fingerprint.FingerprintManager.AuthenticationCallback + - android:android.hardware.fingerprint.FingerprintManager.CryptoObject + - android:android.security.KeyGenParameterSpec + - android:java.security.KeyStore + - android:javax.crypto.Cipher + - android:javax.crypto.KeyGenerator +license: apache2 |