Skip to content

Commit

Permalink
[local-authentication] Give proper error when device is unsecured (#6962
Browse files Browse the repository at this point in the history
)

# Why

Follow up to #6846 to fix a wrongly returned `user_cancel`. Now returns `not_enrolled` if the device is not secured with any method.

# How

Before trying to initialise the BiometricPrompt we ask the KeyGuard if the device is "secured". If it isn't, we return the `not_enrolled` error right away.

# Test Plan

We intend to use this in a commercial product so I will test this on a number of devices by simply copying over the source into my node_modules, building the app, and testing on multiple devices.

I'm also hoping for some help from @tsapeta ❤️
  • Loading branch information
LinusU committed Feb 4, 2020
1 parent 67fbcc6 commit ad0ed4b
Showing 1 changed file with 14 additions and 0 deletions.
Expand Up @@ -3,6 +3,7 @@
package expo.modules.localauthentication;

import android.app.Activity;
import android.app.KeyguardManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
Expand Down Expand Up @@ -101,6 +102,15 @@ public void authenticateAsync(final Promise promise) {
return;
}

if (getKeyguardManager().isDeviceSecure() == false) {
Bundle errorResult = new Bundle();
errorResult.putBoolean("success", false);
errorResult.putString("error", "not_enrolled");
errorResult.putString("message", "KeyguardManager#isDeviceSecure() returned false");
promise.resolve(errorResult);
return;
}

// BiometricPrompt callbacks are invoked on the main thread so also run this there to avoid
// having to do locking.
mUIManager.runOnUiQueueThread(new Runnable() {
Expand Down Expand Up @@ -181,6 +191,10 @@ private static String convertErrorCode(int code) {
}
}

private KeyguardManager getKeyguardManager() {
return (KeyguardManager) getCurrentActivity().getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE);
}

private Activity getCurrentActivity() {
ActivityProvider activityProvider = mModuleRegistry.getModule(ActivityProvider.class);
return activityProvider != null ? activityProvider.getCurrentActivity() : null;
Expand Down

0 comments on commit ad0ed4b

Please sign in to comment.