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

Migrated to Androidx, created support for onscreen finger print and latest android sdk #114

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
12 changes: 7 additions & 5 deletions dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ ext.versions = [
name : '2.2.1',

// Android Versions
compileSdk : 25,
targetSdk : 25,
compileSdk : 29,
targetSdk : 29,
minSdk : 15,
buildTools : '26.0.2',
buildTools : '29.0.3',

// Gradle Versions
androidGradlePlugin : '3.0.1',
androidGradlePlugin : '3.6.3',
mavenGradlePlugin : '1.4.1',
bintrayGradlePlugin : '1.7.1',

Expand All @@ -38,7 +38,8 @@ ext.versions = [
jUnit : '4.12',
mockito : '1.10.19',
powermock : '1.6.4',
testRunner : '0.5'
testRunner : '0.5',
androidBiometric : '1.0.1'
]

ext.gradlePlugins = [
Expand All @@ -60,5 +61,6 @@ ext.libraries = [
mockito : "org.mockito:mockito-core:$versions.mockito",
powermockJUnit : "org.powermock:powermock-api-mockito:$versions.powermock",
powermockMockito : "org.powermock:powermock-module-junit4:$versions.powermock",
androidBiometric : "androidx.biometric:biometric:$versions.androidBiometric",

]
4 changes: 3 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
# org.gradle.parallel=true
android.useAndroidX = true
android.enableJetifier=true
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.3.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
1 change: 1 addition & 0 deletions rxfingerprint/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ dependencies {
testImplementation libraries.mockito
testImplementation libraries.powermockJUnit
testImplementation libraries.powermockMockito
api libraries.androidBiometric
}

//apply from: 'publish.gradle'
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import android.content.Context;
import android.os.Build;
import android.security.keystore.KeyProperties;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@

import android.annotation.SuppressLint;
import android.content.Context;
import android.hardware.fingerprint.FingerprintManager.AuthenticationResult;
import android.hardware.fingerprint.FingerprintManager.CryptoObject;
import android.support.annotation.Nullable;

import androidx.biometric.BiometricPrompt;
import androidx.core.hardware.fingerprint.FingerprintManagerCompat.AuthenticationResult;
import androidx.core.hardware.fingerprint.FingerprintManagerCompat.CryptoObject;
import androidx.annotation.Nullable;

import com.mtramin.rxfingerprint.data.FingerprintDecryptionResult;
import com.mtramin.rxfingerprint.data.FingerprintEncryptionResult;
Expand Down Expand Up @@ -88,6 +90,19 @@ protected CryptoObject initCryptoObject(ObservableEmitter<FingerprintDecryptionR
}
}

@Nullable
@Override
protected BiometricPrompt.CryptoObject initBiometricCryptoObject(ObservableEmitter<FingerprintDecryptionResult> subscriber) {
try {
CryptoData cryptoData = CryptoData.fromString(encodingProvider, encryptedString);
Cipher cipher = cipherProvider.getCipherForDecryption(cryptoData.getIv());
return new BiometricPrompt.CryptoObject(cipher);
} catch (Exception e) {
subscriber.onError(e);
return null;
}
}

@Override
protected void onAuthenticationSucceeded(ObservableEmitter<FingerprintDecryptionResult> emitter, AuthenticationResult result) {
try {
Expand All @@ -103,6 +118,20 @@ protected void onAuthenticationSucceeded(ObservableEmitter<FingerprintDecryption

}

@Override
protected void onAuthenticationSucceeded(ObservableEmitter<FingerprintDecryptionResult> emitter, BiometricPrompt.AuthenticationResult result) {
try {
CryptoData cryptoData = CryptoData.fromString(encodingProvider, encryptedString);
Cipher cipher = result.getCryptoObject().getCipher();
byte[] bytes = cipher.doFinal(cryptoData.getMessage());

emitter.onNext(new FingerprintDecryptionResult(FingerprintResult.AUTHENTICATED, null, ConversionUtils.toChars(bytes)));
emitter.onComplete();
} catch (Exception e) {
emitter.onError(cipherProvider.mapCipherFinalOperationException(e));
}
}

@Override
protected void onAuthenticationHelp(ObservableEmitter<FingerprintDecryptionResult> emitter, int helpMessageId, String helpString) {
emitter.onNext(new FingerprintDecryptionResult(FingerprintResult.HELP, helpString, null));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@

import android.annotation.SuppressLint;
import android.content.Context;
import android.hardware.fingerprint.FingerprintManager.AuthenticationResult;
import android.hardware.fingerprint.FingerprintManager.CryptoObject;
import android.support.annotation.Nullable;

import androidx.biometric.BiometricPrompt;
import androidx.core.hardware.fingerprint.FingerprintManagerCompat.AuthenticationResult;
import androidx.core.hardware.fingerprint.FingerprintManagerCompat.CryptoObject;
import androidx.annotation.Nullable;

import com.mtramin.rxfingerprint.data.FingerprintEncryptionResult;
import com.mtramin.rxfingerprint.data.FingerprintResult;
Expand Down Expand Up @@ -92,10 +94,42 @@ protected CryptoObject initCryptoObject(ObservableEmitter<FingerprintEncryptionR
}
}

@Nullable
@Override
protected BiometricPrompt.CryptoObject initBiometricCryptoObject(ObservableEmitter<FingerprintEncryptionResult> emitter) {
try {
Cipher cipher = cipherProvider.getCipherForEncryption();
return new BiometricPrompt.CryptoObject(cipher);
} catch (Exception e) {
emitter.onError(e);
return null;
}
}

@Override
@Deprecated
protected void onAuthenticationSucceeded(ObservableEmitter<FingerprintEncryptionResult> emitter, AuthenticationResult result) {
authenticationSucceed(result.getCryptoObject().getCipher(), emitter);
}

@Override
protected void onAuthenticationSucceeded(ObservableEmitter<FingerprintEncryptionResult> emitter, BiometricPrompt.AuthenticationResult result) {
authenticationSucceed(result.getCryptoObject().getCipher(), emitter);
}

@Override
protected void onAuthenticationHelp(ObservableEmitter<FingerprintEncryptionResult> emitter, int helpMessageId, String helpString) {
emitter.onNext(new FingerprintEncryptionResult(FingerprintResult.HELP, helpString, null));
}

@Override
protected void onAuthenticationFailed(ObservableEmitter<FingerprintEncryptionResult> emitter) {
emitter.onNext(new FingerprintEncryptionResult(FingerprintResult.FAILED, null, null));
}

private void authenticationSucceed(Cipher cipher,
ObservableEmitter<FingerprintEncryptionResult> emitter ){
try {
Cipher cipher = result.getCryptoObject().getCipher();
byte[] encryptedBytes = cipher.doFinal(ConversionUtils.toBytes(toEncrypt));
byte[] ivBytes = cipher.getParameters().getParameterSpec(IvParameterSpec.class).getIV();

Expand All @@ -109,13 +143,4 @@ protected void onAuthenticationSucceeded(ObservableEmitter<FingerprintEncryption
}
}

@Override
protected void onAuthenticationHelp(ObservableEmitter<FingerprintEncryptionResult> emitter, int helpMessageId, String helpString) {
emitter.onNext(new FingerprintEncryptionResult(FingerprintResult.HELP, helpString, null));
}

@Override
protected void onAuthenticationFailed(ObservableEmitter<FingerprintEncryptionResult> emitter) {
emitter.onNext(new FingerprintEncryptionResult(FingerprintResult.FAILED, null, null));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.mtramin.rxfingerprint;

import android.os.Build;

import androidx.biometric.BiometricPrompt;
import androidx.core.hardware.fingerprint.FingerprintManagerCompat;

/**
* Created by Gbenga Oladipupo on 08/05/2020.
*/
public class AuthenticationCallbackHelper<T> {

private T authenticationCallback;

private AuthenticationCallbackHelper(T authenticationCallback){
this.authenticationCallback = authenticationCallback;
}


@SuppressWarnings("unchecked")
protected static <T> AuthenticationCallbackHelper<T> set(T authCallback){

return new AuthenticationCallbackHelper(authCallback);
}


public T getAuthenticationCallback() {
return authenticationCallback;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.mtramin.rxfingerprint;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;

import androidx.biometric.BiometricPrompt;
import androidx.core.hardware.fingerprint.FingerprintManagerCompat.CryptoObject;
import androidx.fragment.app.FragmentActivity;

/**
* Created by Gbenga Oladipupo on 08/05/2020.
*/
public class BiometricHelper<T> {

private T cryptoObject;


public BiometricHelper(T cryptoObject1){
this.cryptoObject = cryptoObject1;
}

public T getCryptoObject() {
return cryptoObject;
}

@SuppressWarnings("unchecked")
public static <T> BiometricHelper<T> set(T cryptObj){
return new BiometricHelper(cryptObj);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.mtramin.rxfingerprint;

import android.content.Context;
import android.hardware.biometrics.BiometricManager;
import android.util.Log;

import com.mtramin.rxfingerprint.data.FingerprintAuthenticationException;

import java.lang.ref.WeakReference;
import java.security.Signature;

import javax.crypto.Cipher;

import androidx.annotation.NonNull;
import androidx.biometric.BiometricPrompt;
import androidx.core.hardware.fingerprint.FingerprintManagerCompat;
import androidx.fragment.app.FragmentActivity;
import io.reactivex.ObservableEmitter;

/**
* Created by Gbenga Oladipupo on 08/05/2020.
*/
public class BiometricPromptProvider {

private static final String TAG ="BiometricPromptProvider";

private FingerprintObservable fingerprintObservable;

BiometricPromptProvider biometricPromptProvider = null;

public static BiometricPromptProvider with(FingerprintObservable fingerprintObservable){
return new BiometricPromptProvider(fingerprintObservable);
}

public BiometricPromptProvider(FingerprintObservable fingerprintObservable){
this.fingerprintObservable = fingerprintObservable;
}

protected <T> void authenticate(Context context, BiometricPrompt.CryptoObject cryptoObject,
final ObservableEmitter<T> emitter) {

if(context instanceof FragmentActivity) {
BiometricPrompt mBiometricPrompt = new BiometricPrompt(((FragmentActivity)context),
UIThreadExecutor.get(),
createAuthenticationCallback(emitter));

// Set prompt info
BiometricPrompt.PromptInfo promptInfo = new BiometricPrompt.PromptInfo.Builder()
.setDescription("Description")
.setTitle("Title")
.setSubtitle("Subtitle")
.setNegativeButtonText("Cancel")
.build();

// Show biometric prompt
if (cryptoObject != null) {
Log.i(TAG, "Show biometric prompt");
mBiometricPrompt.authenticate(promptInfo,cryptoObject);
}
}
}


private <T> BiometricPrompt.AuthenticationCallback createAuthenticationCallback(final ObservableEmitter<T> emitter) {
return new BiometricPrompt.AuthenticationCallback() {
@Override
public void onAuthenticationError(int errMsgId, CharSequence errString) {
if (!emitter.isDisposed()) {
emitter.onError(new FingerprintAuthenticationException(errString));
}
}

@Override
public void onAuthenticationFailed() {
fingerprintObservable.onAuthenticationFailed(emitter);
}

@Override
public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {
fingerprintObservable.onAuthenticationSucceeded(emitter, result);
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import android.security.keystore.KeyGenParameterSpec;
import android.security.keystore.KeyPermanentlyInvalidatedException;
import android.security.keystore.KeyProperties;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.io.IOException;
import java.security.GeneralSecurityException;
Expand Down
Loading