Update calls to secure check

Signed-off-by: Mario Danic <mario@lovelyhq.com>
This commit is contained in:
Mario Danic 2019-02-15 02:07:08 +01:00
parent 455601b5a6
commit feafb0c357
2 changed files with 10 additions and 4 deletions

View File

@ -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
}
}

View File

@ -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);
}
}