react-native-keychain: Android: Wrapped error: User not authenticated
I get this error when trying to retrieve credentials stored in the keychain on Android devices. IMHO this is the effect of following exception being thrown: UserNotAuthenticatedException.
This is my code:
useEffect(() => {
if (!loginStarted) {
return;
}
Keychain.getSupportedBiometryType()
.then(data => {
console.log('Supported biometry: ' + data);
setBiometry(!!data);
if (!!data) {
// Try auto login
Keychain.getGenericPassword({service: service})
.then(credentials => {
if (!credentials) {
// no credentials stored, continue with normal login
console.log('No saved credentials.');
return;
}
// auto login with stored credentials
if (credentials.username && credentials.password) {
console.log('Trying auto login with saved credentials: ' + JSON.stringify(credentials));
login(credentials.username, credentials.password);
}
})
.catch(e => console.log('Error getting credentials: ' + e));
}
})
.catch(e => console.log('Error getting biometry: ' + e));
}, [loginStarted]);
The error is thrown at Keychain.getGenericPassword. On iOS the code works fine. Any idea or workaround for this?
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 8
- Comments: 58 (12 by maintainers)
Has anyone found a workaround to get this to work on devices that have both Fingerprint as well as Face authentication? Currently I just have biometrics disabled on Android until I can figure out a way to either make Face work on Android or force only Fingerprint for the time being.
7.0.0 was just released with the support of strong bio by default. Please try it out. It should remove all
User not authenticatederrors.I can confirm that the issue still happens as of react-native-keychain@8.0.0.
I just created a new React Native sample app from scratch (npx react-native init AwesomeProject --version 0.65.1) and installed the lib. Then I edited App.js to make it look like this:
To reproduce the error:
One more thing:
And apparently it will keep working until you restart the phone again. Then it will break, and to fix it, do set up fingerprint again on Android Settings.
Now, I have no experience programming with the native part on Android, so I hope these steps might help someone else come up with a fix.
Hi guys, i am also experiencing this issue with Android 8. Fingerprint works fine, so does Iris authentication. Face ID fails every single time. It would be great if there was a setting to force which type of biometric to use to unlock keychain as on Android it falls back to whatever user selected in the preference.
I’m also noticing this issue with Android. It works correctly until the device is restarted.
The weird thing is,
setUserAuthenticationValidityDurationSecondsseems to be related to the time since the user authenticated from the lock screen. If I callgetGenericPasswordafter unlocking the device withinsetUserAuthenticationValidityDurationSecondsvalue, it works.I am at the exact same point @ericrguimaraes - it works if I reset the fingerprint in the device settings, then stops if I restart. Did you find a solution to the issue?
I noticed similar behavior where the
onAuthenticationSucceededwas being called (inKeychainModule.javabut I was still getting theUserNotAuthenticatedException. Basically the decryption key’s authentication was expiring before the cipher could get initialized. (probably since I was running in a debug environment, but could also happen with slower devices)I was able to get it to work by extending the
UserAuthenticationValidityDurationattribute of the key from 1 second to 5. In my case, it was using the RSA key, so the change was in theCipherStorageKeystoreRsaEcb.javafile, around line 228This is still unresolved and I’ll have to disable the feature for Android because of this issue. Version: react-native-keychain@8.0.0 Emulator: Pixel 3 API 30
@AlphaJuliettOmega Thanks for the snippet. I was using accessControl: Keychain.ACCESS_CONTROL.BIOMETRY_ANY irrespective of Platform or android. I tried the code my android api level is 28 so below configuration was applied ``` { accessControl: Keychain.ACCESS_CONTROL.BIOMETRY_ANY_OR_DEVICE_PASSCODE, // https://github.com/oblador/react-native-keychain/issues/262 storage: Keychain.STORAGE_TYPE.AES } removed the warnings.
Hello @michaelgmcd right now the only available solution is using this fork. It has a bit different flow for Android and iOS, however it works for all devices
@Zo2m4bie sure, I’ll try to do in the next few days - been bogged down with wrapping up a release but want to PR this soon. I only had a Pixel 4 to test so if you can test this on a Samsung that would be great to see if it works or not. I’ll ping you when I’m ready.
Increasing
setUserAuthenticationValidityDurationSecondsto a larger value fixes this for me. Note that you have to ensure you have this value set before you store the secret into the keychain.With that in mind I think #339 is a reasonable fix for this.
@skicson regarding to the stack above, the onAuthenticationSucceeded is being called . But then some how the
public static final DecryptBytesHandler decrypt = (cipher, key, input) -> { cipher.init(Cipher.DECRYPT_MODE, key); };failed with theandroid.security.KeyStore.getInvalidKeyException