Browse Source

Update calls to secure check

Signed-off-by: Mario Danic <mario@lovelyhq.com>
Mario Danic 6 years ago
parent
commit
feafb0c357

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

@@ -92,7 +92,7 @@ public class BaseActivity extends AppCompatActivity {
     private void checkIfWeAreSecure() {
         keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
         if (keyguardManager != null && keyguardManager.isKeyguardSecure() && appPreferences.getIsScreenLocked()) {
-            if (!SecurityUtils.checkIfWeAreAuthenticated()) {
+            if (!SecurityUtils.checkIfWeAreAuthenticated(appPreferences.getScreenLockTimeout())) {
                 showAuthenticationScreen();
             }
         }
@@ -110,7 +110,7 @@ public class BaseActivity extends AppCompatActivity {
         if (requestCode == REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS) {
             if (resultCode == RESULT_OK) {
                 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
-                    if (SecurityUtils.checkIfWeAreAuthenticated()) {
+                    if (SecurityUtils.checkIfWeAreAuthenticated(appPreferences.getScreenLockTimeout())) {
                         // all went well
                     }
                 }

+ 8 - 2
app/src/main/java/com/nextcloud/talk/utils/SecurityUtils.java

@@ -44,7 +44,7 @@ public class SecurityUtils {
     private static final byte[] SECRET_BYTE_ARRAY = new byte[]{1, 2, 3, 4, 5, 6};
 
     @RequiresApi(api = Build.VERSION_CODES.M)
-    public static boolean checkIfWeAreAuthenticated() {
+    public static boolean checkIfWeAreAuthenticated(String screenLockTimeout) {
         try {
             KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
             keyStore.load(null);
@@ -63,11 +63,17 @@ public class SecurityUtils {
             // User is not authenticated, let's authenticate with device credentials.
             return false;
         } catch (KeyPermanentlyInvalidatedException e) {
+            // This happens if the lock screen has been disabled or reset after the key was
+            // generated after the key was generated.
+            // Shouldnt really happen because we regenerate the key every time an activity
+            // is created, but oh well
+            // Create key, and attempt again
+            createKey(screenLockTimeout);
             return false;
         } catch (BadPaddingException | IllegalBlockSizeException | KeyStoreException |
                 CertificateException | UnrecoverableKeyException | IOException
                 | NoSuchPaddingException | NoSuchAlgorithmException | InvalidKeyException e) {
-            return false;
+            throw new RuntimeException(e);
         }
     }