Skip to content

Commit 9ab9910

Browse files
mikepitreclaude
andcommitted
fix(expo-biometrics): report missing secure key storage on Android
Report secureKeyStorageAvailable from getAvailability() on Android (false below API 28) and reject createKey() there with secure_key_storage_unavailable instead of biometry_not_available. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent c467279 commit 9ab9910

6 files changed

Lines changed: 66 additions & 5 deletions

File tree

‎packages/expo-biometrics/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ The key and record layout is shared with the Clerk iOS and Android SDKs, so cred
3737
- Expo SDK 54 or later, in a development build (the module is not available in Expo Go or on the web)
3838
- iOS: `NSFaceIDUsageDescription` in your `Info.plist`. The `@clerk/expo` config plugin sets it through its `faceIDPermission` option.
3939
- iOS: a device with a Secure Enclave. The iOS Simulator has none, so `createKey()` rejects there with `secure_key_storage_unavailable`.
40-
- Android 9 (API level 28) or later with a strong (Class 3) biometric. On older versions every key operation rejects with `biometry_not_available`.
40+
- Android 9 (API level 28) or later with a strong (Class 3) biometric. On older versions `getAvailability()` reports `secureKeyStorageAvailable: false`, `createKey()` rejects with `secure_key_storage_unavailable`, and `sign()` with `biometry_not_available`.
4141

4242
## Installation
4343

‎packages/expo-biometrics/android/src/main/java/expo/modules/clerk/biometrics/BiometricKeyManager.kt‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,16 @@ internal data class BiometricAvailability(
2424
val canEvaluateBiometrics: Boolean,
2525
val canEvaluateDeviceOwner: Boolean,
2626
val errorCode: BiometricsErrorCode?,
27+
val secureKeyStorageAvailable: Boolean,
2728
)
2829

2930
internal data class BiometricCredentialKey(val localKeyId: String, val publicKeyJwk: String)
3031

3132
/** Android Keystore keys laid out as clerk-android's `DefaultBiometricCredentialKeyManager` creates them. */
3233
internal class BiometricKeyManager {
3334
fun availability(context: Context): BiometricAvailability {
34-
if (Build.VERSION.SDK_INT < BiometricCredentialCoding.MIN_SDK) {
35-
return BiometricAvailability("none", false, false, BiometricsErrorCode.BIOMETRY_NOT_AVAILABLE)
35+
if (!secureKeyStorageAvailable()) {
36+
return BiometricAvailability("none", false, false, BiometricsErrorCode.BIOMETRY_NOT_AVAILABLE, false)
3637
}
3738
val manager = BiometricManager.from(context)
3839
val strong = manager.canAuthenticate(Authenticators.BIOMETRIC_STRONG)
@@ -50,11 +51,17 @@ internal class BiometricKeyManager {
5051
canEvaluateBiometrics = canEvaluateBiometrics,
5152
canEvaluateDeviceOwner = canEvaluateDeviceOwner,
5253
errorCode = if (canEvaluateBiometrics) null else BiometricsError.forCanAuthenticate(strong).code,
54+
secureKeyStorageAvailable = true,
5355
)
5456
}
5557

5658
fun createKey(context: Context, policy: BiometricCredentialPolicy): BiometricCredentialKey {
57-
requireSupportedSdk()
59+
if (!secureKeyStorageAvailable()) {
60+
throw BiometricsError(
61+
BiometricsErrorCode.SECURE_KEY_STORAGE_UNAVAILABLE,
62+
"Biometric credential keys require Android 9 (API 28) or later.",
63+
)
64+
}
5865
val status = BiometricManager.from(context).canAuthenticate(promptAuthenticators(policy))
5966
if (status != BiometricManager.BIOMETRIC_SUCCESS) {
6067
throw BiometricsError.forCanAuthenticate(status)
@@ -186,6 +193,8 @@ internal class BiometricKeyManager {
186193
companion object {
187194
private const val ANDROID_KEY_STORE = "AndroidKeyStore"
188195

196+
fun secureKeyStorageAvailable(sdkInt: Int = Build.VERSION.SDK_INT): Boolean = sdkInt >= BiometricCredentialCoding.MIN_SDK
197+
189198
/** Contract v2 section 2: device credentials only for biometry_or_device_passcode, and only on API 30+. */
190199
fun promptAuthenticators(policy: BiometricCredentialPolicy, sdkInt: Int = Build.VERSION.SDK_INT): Int =
191200
if (policy == BiometricCredentialPolicy.BIOMETRY_OR_DEVICE_PASSCODE && sdkInt >= Build.VERSION_CODES.R) {

‎packages/expo-biometrics/android/src/main/java/expo/modules/clerk/biometrics/BiometricsError.kt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ internal enum class BiometricsErrorCode(val value: String) {
1111
BIOMETRY_NOT_ENROLLED("biometry_not_enrolled"),
1212
BIOMETRY_LOCKOUT("biometry_lockout"),
1313
PASSCODE_NOT_SET("passcode_not_set"),
14+
SECURE_KEY_STORAGE_UNAVAILABLE("secure_key_storage_unavailable"),
1415
KEY_NOT_FOUND("key_not_found"),
1516
KEY_INVALIDATED("key_invalidated"),
1617
KEY_GENERATION_FAILED("key_generation_failed"),

‎packages/expo-biometrics/android/src/main/java/expo/modules/clerk/biometrics/ClerkExpoBiometricsModule.kt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class ClerkExpoBiometricsModule : Module() {
4545
"canEvaluateBiometrics" to availability.canEvaluateBiometrics,
4646
"canEvaluateDeviceOwner" to availability.canEvaluateDeviceOwner,
4747
"errorCode" to availability.errorCode?.value,
48+
"secureKeyStorageAvailable" to availability.secureKeyStorageAvailable,
4849
)
4950
}
5051

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package expo.modules.clerk.biometrics
2+
3+
import org.junit.Assert.assertEquals
4+
import org.junit.Assert.assertFalse
5+
import org.junit.Assert.assertThrows
6+
import org.junit.Assert.assertTrue
7+
import org.junit.Test
8+
import org.junit.runner.RunWith
9+
import org.robolectric.RobolectricTestRunner
10+
import org.robolectric.RuntimeEnvironment
11+
import org.robolectric.annotation.Config
12+
13+
@RunWith(RobolectricTestRunner::class)
14+
class BiometricKeyManagerTest {
15+
@Test
16+
fun `secure key storage requires Android 9`() {
17+
assertFalse(BiometricKeyManager.secureKeyStorageAvailable(sdkInt = 27))
18+
assertTrue(BiometricKeyManager.secureKeyStorageAvailable(sdkInt = 28))
19+
assertTrue(BiometricKeyManager.secureKeyStorageAvailable(sdkInt = 36))
20+
}
21+
22+
@Test
23+
@Config(sdk = [27])
24+
fun `availability reports no secure key storage before Android 9`() {
25+
val availability = BiometricKeyManager().availability(RuntimeEnvironment.getApplication())
26+
27+
assertFalse(availability.secureKeyStorageAvailable)
28+
assertFalse(availability.canEvaluateBiometrics)
29+
assertEquals(BiometricsErrorCode.BIOMETRY_NOT_AVAILABLE, availability.errorCode)
30+
}
31+
32+
@Test
33+
@Config(sdk = [28])
34+
fun `availability reports secure key storage on Android 9 and later`() {
35+
assertTrue(BiometricKeyManager().availability(RuntimeEnvironment.getApplication()).secureKeyStorageAvailable)
36+
}
37+
38+
@Test
39+
@Config(sdk = [27])
40+
fun `createKey rejects with secure_key_storage_unavailable before Android 9`() {
41+
for (policy in BiometricCredentialPolicy.entries) {
42+
val error =
43+
assertThrows(BiometricsError::class.java) {
44+
BiometricKeyManager().createKey(RuntimeEnvironment.getApplication(), policy)
45+
}
46+
assertEquals(BiometricsErrorCode.SECURE_KEY_STORAGE_UNAVAILABLE, error.code)
47+
}
48+
}
49+
}

‎packages/expo-biometrics/src/types.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ export interface BiometricAvailability {
2323
/** Why biometric authentication cannot be evaluated, or `null` when it can. */
2424
errorCode: BiometricsErrorCode | null;
2525
/**
26-
* Whether the device has hardware-backed key storage (the Secure Enclave on iOS). `false` on the iOS Simulator.
26+
* Whether the device has hardware-backed key storage: the Secure Enclave on iOS, which the iOS Simulator lacks, or the
27+
* Android Keystore on Android 9 (API level 28) and later.
2728
* `createKey()` rejects with `secure_key_storage_unavailable` when this is `false`.
2829
*/
2930
secureKeyStorageAvailable: boolean;

0 commit comments

Comments
 (0)