Selaa lähdekoodia

Fix finger auth

Signed-off-by: Mario Danic <mario@lovelyhq.com>
Mario Danic 6 vuotta sitten
vanhempi
commit
0f7b004023

+ 2 - 2
app/src/main/java/com/nextcloud/talk/activities/MainActivity.java

@@ -116,8 +116,8 @@ public final class MainActivity extends BaseActivity implements ActionBarProvide
     }
 
     @Override
-    public void onResume() {
-        super.onResume();
+    public void onStart() {
+        super.onStart();
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
             checkIfWeAreSecure();
         }

+ 17 - 4
app/src/main/java/com/nextcloud/talk/controllers/LockedController.java

@@ -25,6 +25,8 @@ import android.app.KeyguardManager;
 import android.content.Context;
 import android.content.Intent;
 import android.os.Build;
+import android.os.Handler;
+import android.os.Looper;
 import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
@@ -58,7 +60,6 @@ public class LockedController extends BaseController {
         return inflater.inflate(R.layout.controller_locked, container, false);
     }
 
-    @RequiresApi(api = Build.VERSION_CODES.M)
     @Override
     protected void onViewBound(@NonNull View view) {
         super.onViewBound(view);
@@ -66,8 +67,13 @@ public class LockedController extends BaseController {
         if (getActionBar() != null) {
             getActionBar().hide();
         }
+    }
 
-        showBiometricDialog();
+    @RequiresApi(api = Build.VERSION_CODES.M)
+    @Override
+    protected void onAttach(@NonNull View view) {
+        super.onAttach(view);
+        checkIfWeAreSecure();
     }
 
     @RequiresApi(api = Build.VERSION_CODES.M)
@@ -94,7 +100,7 @@ public class LockedController extends BaseController {
                         public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {
                             super.onAuthenticationSucceeded(result);
                             Log.d(TAG, "Fingerprint recognised successfully");
-                            getRouter().popCurrentController();
+                            new Handler(Looper.getMainLooper()).post(() -> getRouter().popCurrentController());
                         }
 
                         @Override
@@ -111,7 +117,12 @@ public class LockedController extends BaseController {
                     }
             );
 
-            biometricPrompt.authenticate(promptInfo, SecurityUtils.getCryptoObject());
+            BiometricPrompt.CryptoObject cryptoObject = SecurityUtils.getCryptoObject();
+            if (cryptoObject != null) {
+                biometricPrompt.authenticate(promptInfo, cryptoObject);
+            } else {
+                biometricPrompt.authenticate(promptInfo);
+            }
         }
     }
 
@@ -122,6 +133,8 @@ public class LockedController extends BaseController {
             if (keyguardManager != null && keyguardManager.isKeyguardSecure() && appPreferences.getIsScreenLocked()) {
                 if (!SecurityUtils.checkIfWeAreAuthenticated(appPreferences.getScreenLockTimeout())) {
                     showBiometricDialog();
+                } else {
+                    getRouter().popCurrentController();
                 }
             }
         }

+ 4 - 14
app/src/main/java/com/nextcloud/talk/utils/SecurityUtils.java

@@ -44,6 +44,8 @@ public class SecurityUtils {
     private static final String CREDENTIALS_KEY = "KEY_CREDENTIALS";
     private static final byte[] SECRET_BYTE_ARRAY = new byte[]{1, 2, 3, 4, 5, 6};
 
+    private static BiometricPrompt.CryptoObject cryptoObject;
+
     @RequiresApi(api = Build.VERSION_CODES.M)
     public static boolean checkIfWeAreAuthenticated(String screenLockTimeout) {
         try {
@@ -58,6 +60,7 @@ public class SecurityUtils {
             cipher.init(Cipher.ENCRYPT_MODE, secretKey);
             cipher.doFinal(SECRET_BYTE_ARRAY);
 
+            cryptoObject = new BiometricPrompt.CryptoObject(cipher);
             // If the user has recently authenticated, we will reach here
             return true;
         } catch (UserNotAuthenticatedException e) {
@@ -80,22 +83,9 @@ public class SecurityUtils {
 
     @RequiresApi(api = Build.VERSION_CODES.M)
     public static BiometricPrompt.CryptoObject getCryptoObject() {
-        Cipher cipher = null;
-        try {
-            cipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/" + KeyProperties.BLOCK_MODE_GCM + "/" + KeyProperties.ENCRYPTION_PADDING_NONE);
-        } catch (NoSuchAlgorithmException e) {
-            Log.w(TAG, e.getLocalizedMessage());
-        } catch (NoSuchPaddingException e) {
-            Log.w(TAG, e.getLocalizedMessage());
-        }
-
-        BiometricPrompt.CryptoObject cryptoObject = null;
-        if (cipher != null) {
-            cryptoObject = new BiometricPrompt.CryptoObject(cipher);
-        }
-
         return cryptoObject;
     }
+
     @RequiresApi(api = Build.VERSION_CODES.M)
     public static void createKey(String validity) {
         try {