Skip to content

Commit

Permalink
Implemented biometric authentication
Browse files Browse the repository at this point in the history
* Upstream sdk version is needed to compile the imported libraries

Signed-off-by: aditya-gupta99 <[email protected]>
  • Loading branch information
Aditya-gupta99 committed Mar 11, 2023
1 parent c8c8cd0 commit cc9fb98
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 1 deletion.
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")
}

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 com.mifos.mifosxdroid.HomeActivity
import com.mifos.mifosxdroid.R
import com.mifos.mifosxdroid.SplashScreenActivity
Expand All @@ -23,6 +24,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")){
BiometricAuthentication(this).authenticateWithBiometrics()
}
if(BiometricAuthentication(this).getBiometricCapabilities()==BiometricCapability.HAS_BIOMETRIC_AUTH){
val btn=findViewById<AppCompatButton>(R.id.btn_save)
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 @@ -51,4 +66,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

0 comments on commit cc9fb98

Please sign in to comment.