Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passcode activity enhanced with biometric authentication #1905

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ext {
// Sdk and tools
minSdkVersion = 15
targetSdkVersion = 32
compileSdkVersion = 32
compileSdkVersion = 33
buildToolsVersion = '28.0.3'

// App dependencies
Expand Down
5 changes: 5 additions & 0 deletions mifosng-android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@ dependencies {
implementation 'androidx.cardview:cardview:1.0.0'
//preferences
implementation "androidx.preference:preference:$preference"

implementation("androidx.biometric:biometric:1.2.0-alpha05")

implementation 'androidx.appcompat:appcompat:1.3.1'

}
/*
All direct/transitive dependencies shared between your test and production APKs need to be
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.mifos.mifosxdroid.passcode

import android.content.Intent
import androidx.biometric.BiometricManager
import android.os.Build
import android.provider.Settings
import android.provider.Settings.EXTRA_BIOMETRIC_AUTHENTICATORS_ALLOWED
import androidx.activity.result.ActivityResultLauncher
import androidx.biometric.BiometricPrompt
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentActivity
import com.mifos.mifosxdroid.HomeActivity
import com.mifos.mifosxdroid.R

open class BiometricAuthentication(
val context: FragmentActivity,
) {
private val executor = ContextCompat.getMainExecutor(context)
private val callback = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
object : BiometricPrompt.AuthenticationCallback() {
override fun onAuthenticationFailed() {
super.onAuthenticationFailed()
}

override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
super.onAuthenticationSucceeded(result)
val intent= Intent(context, HomeActivity::class.java)
context.startActivity(intent)
}

override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
super.onAuthenticationError(errorCode, errString)
}
}
} else {
TODO("VERSION.SDK_INT < P")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Aditya-gupta99
what is expected here?

}

private val biometricPrompt = BiometricPrompt(context, executor, callback)



fun launchBiometricEnrollment(resultLauncher: ActivityResultLauncher<Intent>) {
val intent: Intent = when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.R -> {
Intent(Settings.ACTION_BIOMETRIC_ENROLL).putExtra(
EXTRA_BIOMETRIC_AUTHENTICATORS_ALLOWED,
BiometricManager.Authenticators.BIOMETRIC_STRONG or BiometricManager.Authenticators.BIOMETRIC_WEAK
)
}
Build.VERSION.SDK_INT >= Build.VERSION_CODES.P -> {
Intent(Settings.ACTION_FINGERPRINT_ENROLL)
}
else -> {
Intent(Settings.ACTION_SECURITY_SETTINGS)
}
}
resultLauncher.launch(intent)
}

fun getBiometricCapabilities(): BiometricCapability {
val biometricManager = BiometricManager.from(context)
return when (biometricManager.canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_WEAK or BiometricManager.Authenticators.DEVICE_CREDENTIAL)) {
BiometricManager.BIOMETRIC_SUCCESS -> {
BiometricCapability.HAS_BIOMETRIC_AUTH
}
BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE -> {
BiometricCapability.NOT_SUPPORTED
}
BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE -> {
BiometricCapability.NOT_SUPPORTED
}
BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED -> {
BiometricCapability.NOT_ENROLLED
}
BiometricManager.BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED -> {
BiometricCapability.SECURITY_UPDATE_REQUIRED
}
BiometricManager.BIOMETRIC_ERROR_UNSUPPORTED -> {
BiometricCapability.NOT_SUPPORTED
}
BiometricManager.BIOMETRIC_STATUS_UNKNOWN -> {
BiometricCapability.NOT_SUPPORTED
}
else -> {
BiometricCapability.NOT_SUPPORTED
}
}
}

fun authenticateWithBiometrics() {
val promptInfo = BiometricPrompt.PromptInfo.Builder().apply {
setTitle(context.getString(R.string.unlocked_mifos))
setDescription(context.getString(R.string.text_description_biometrics_dialog))
setAllowedAuthenticators(BiometricManager.Authenticators.BIOMETRIC_WEAK or BiometricManager.Authenticators.DEVICE_CREDENTIAL)
}.build()

biometricPrompt.authenticate(promptInfo)
}
}

enum class BiometricCapability {
HAS_BIOMETRIC_AUTH, NOT_ENROLLED, SECURITY_UPDATE_REQUIRED, NOT_SUPPORTED
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.mifos.mifosxdroid.passcode;
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.appcompat.widget.AppCompatButton
import androidx.core.widget.NestedScrollView
import butterknife.BindView
import butterknife.ButterKnife
Expand Down Expand Up @@ -34,6 +35,20 @@ class PassCodeActivity : MifosPassCodeActivity() {
currPassCode = it.getStringExtra(Constants.CURR_PASSWORD)
isToUpdatePassCode = it.getBooleanExtra(Constants.IS_TO_UPDATE_PASS_CODE, false)
}
if(findViewById<AppCompatButton>(R.id.btn_save).text.equals("Login with biometric")){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Aditya-gupta99 ,
Please use butter knife currently

BiometricAuthentication(this).authenticateWithBiometrics()
}
if(BiometricAuthentication(this).getBiometricCapabilities()==BiometricCapability.HAS_BIOMETRIC_AUTH){
val btn=findViewById<AppCompatButton>(R.id.btn_save)
Copy link
Collaborator

@avivijay19 avivijay19 Jun 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

butterknife
please use proper naming variable

if (btn.visibility==View.GONE){
btn.visibility=View.VISIBLE
btn.text="Login with biometric"
btn.setOnClickListener {
BiometricAuthentication(this).authenticateWithBiometrics()
}
}
}

}

override fun showToaster(view: View?, msg: Int) {
Expand Down Expand Up @@ -66,4 +81,11 @@ class PassCodeActivity : MifosPassCodeActivity() {
}
finish()
}

override fun onResume() {
if(findViewById<AppCompatButton>(R.id.btn_save).text.equals("Login with biometric")){
BiometricAuthentication(this).authenticateWithBiometrics()
}
super.onResume()
}
}
2 changes: 2 additions & 0 deletions mifosng-android/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,8 @@
<string name="passcode">Passcode</string>
<string name="change_passcode">Change Passcode</string>
<string name="change_app_passcode">Change App Passcode</string>
<string name="unlocked_mifos">Unlock Mifos</string>
<string name="text_description_biometrics_dialog">Use your with PIN, Pattern, Password Face or fingerprint</string>

<string name="currency">Currency</string>
<string name="account_number">Account Number</string>
Expand Down