androidx.biometric.BiometricPrompt Java Examples
The following examples show how to use
androidx.biometric.BiometricPrompt.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BiometricCallback.java From Android-Goldfinger with Apache License 2.0 | 7 votes |
@Override public void onAuthenticationSucceeded(@NonNull final BiometricPrompt.AuthenticationResult result) { if (!isAuthenticationActive) { return; } isAuthenticationActive = false; log("onAuthenticationSucceeded"); if (mode == Mode.AUTHENTICATION) { MAIN_HANDLER.post(new Runnable() { @Override public void run() { Goldfinger.Result goldfingerResult = new Goldfinger.Result( Goldfinger.Type.SUCCESS, Goldfinger.Reason.AUTHENTICATION_SUCCESS ); callback.onResult(goldfingerResult); } }); } else { cipherValue(result.getCryptoObject()); } }
Example #2
Source File: BiometricAuthHelper.java From Passbook with Apache License 2.0 | 6 votes |
@Override public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) { super.onAuthenticationSucceeded(result); Application app = Application.getInstance(); try { if (mIsFirstTime) { app.setFpData(mCryptoObject.getCipher().doFinal(app.getPassword().getBytes()), mCipher.getIV()); mListener.onConfirmed(true, null); } else { mListener.onConfirmed(false, mCryptoObject.getCipher().doFinal(app.getFpData())); } } catch (BadPaddingException | IllegalBlockSizeException e) { Log.e("Pb:FingerprintDialog", "Runtime error during encryption/decryption."); Log.e("Pb:FingerprintDialog", e.toString()); } }
Example #3
Source File: GoldfingerImpl.java From Android-Goldfinger with Apache License 2.0 | 6 votes |
private void initializeCryptoObject( @NonNull final PromptParams params, @NonNull final Mode mode, @NonNull final String key, @NonNull final String value, @NonNull final Callback callback ) { log("Creating CryptoObject"); asyncCryptoFactoryCallback = new AsyncCryptoObjectFactory.Callback() { @Override void onCryptoObjectCreated(@Nullable BiometricPrompt.CryptoObject cryptoObject) { creatingCryptoObject = false; if (cryptoObject != null) { startNativeFingerprintAuthentication(params, mode, key, value, callback, cryptoObject); } else { log("Failed to create CryptoObject"); callback.onError(new CryptoObjectInitException()); } } }; creatingCryptoObject = true; asyncCryptoFactory.createCryptoObject(mode, key, asyncCryptoFactoryCallback); }
Example #4
Source File: CrypterProxy.java From Android-Goldfinger with Apache License 2.0 | 6 votes |
@Nullable public String encrypt(@NonNull BiometricPrompt.CryptoObject cryptoObject, @NonNull String value) { Cipher cipher = cryptoObject.getCipher(); if (cipher != null && cipherCrypter != null) { return cipherCrypter.encrypt(cipher, value); } Mac mac = cryptoObject.getMac(); if (mac != null && macCrypter != null) { return macCrypter.encrypt(mac, value); } Signature signature = cryptoObject.getSignature(); if (signature != null && signatureCrypter != null) { return signatureCrypter.encrypt(signature, value); } return null; }
Example #5
Source File: CrypterProxy.java From Android-Goldfinger with Apache License 2.0 | 6 votes |
@Nullable public String decrypt(@NonNull BiometricPrompt.CryptoObject cryptoObject, @NonNull String value) { Cipher cipher = cryptoObject.getCipher(); if (cipher != null && cipherCrypter != null) { return cipherCrypter.decrypt(cipher, value); } Mac mac = cryptoObject.getMac(); if (mac != null && macCrypter != null) { return macCrypter.decrypt(mac, value); } Signature signature = cryptoObject.getSignature(); if (signature != null && signatureCrypter != null) { return signatureCrypter.decrypt(signature, value); } return null; }
Example #6
Source File: AuthActivity.java From Aegis with GNU General Public License v3.0 | 6 votes |
public void showBiometricPrompt() { Cipher cipher; try { cipher = _bioSlot.createDecryptCipher(_bioKey); } catch (SlotException e) { e.printStackTrace(); Dialogs.showErrorDialog(this, R.string.biometric_init_error, e); return; } BiometricPrompt.CryptoObject cryptoObj = new BiometricPrompt.CryptoObject(cipher); _bioPrompt = new BiometricPrompt(this, new UiThreadExecutor(), new BiometricPromptListener()); BiometricPrompt.PromptInfo info = new BiometricPrompt.PromptInfo.Builder() .setTitle(getString(R.string.authentication)) .setNegativeButtonText(getString(android.R.string.cancel)) .setConfirmationRequired(false) .build(); _bioPrompt.authenticate(info, cryptoObj); }
Example #7
Source File: Goldfinger.java From Android-Goldfinger with Apache License 2.0 | 6 votes |
/** * Create new {@link BiometricPrompt.PromptInfo} instance. Parameter * validation is done earlier in the code so we can trust the data at * this step. */ @SuppressWarnings("ConstantConditions") @NonNull BiometricPrompt.PromptInfo buildPromptInfo() { BiometricPrompt.PromptInfo.Builder builder = new BiometricPrompt.PromptInfo.Builder() .setTitle(title) .setSubtitle(subtitle) .setDescription(description) .setDeviceCredentialAllowed(deviceCredentialsAllowed) .setConfirmationRequired(confirmationRequired); if (!deviceCredentialsAllowed) { builder.setNegativeButtonText(negativeButtonText); } return builder.build(); }
Example #8
Source File: AuthActivity.java From Aegis with GNU General Public License v3.0 | 6 votes |
@Override public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) { super.onAuthenticationSucceeded(result); MasterKey key; BiometricSlot slot = _slots.find(BiometricSlot.class); try { key = slot.getKey(result.getCryptoObject().getCipher()); } catch (SlotException | SlotIntegrityException e) { e.printStackTrace(); Dialogs.showErrorDialog(AuthActivity.this, R.string.biometric_decrypt_error, e); return; } finish(key, false); }
Example #9
Source File: KeychainModule.java From react-native-keychain with MIT License | 6 votes |
/** Called when a biometric is recognized. */ @Override public void onAuthenticationSucceeded(@NonNull final BiometricPrompt.AuthenticationResult result) { try { if (null == context) throw new NullPointerException("Decrypt context is not assigned yet."); final DecryptionResult decrypted = new DecryptionResult( storage.decryptBytes(context.key, context.username), storage.decryptBytes(context.key, context.password) ); onDecrypt(decrypted, null); } catch (Throwable fail) { onDecrypt(null, fail); } }
Example #10
Source File: KeychainModule.java From react-native-keychain with MIT License | 6 votes |
/** trigger interactive authentication. */ public void startAuthentication() { final FragmentActivity activity = (FragmentActivity) getCurrentActivity(); if (null == activity) throw new NullPointerException("Not assigned current activity"); // code can be executed only from MAIN thread if (Thread.currentThread() != Looper.getMainLooper().getThread()) { activity.runOnUiThread(this::startAuthentication); waitResult(); return; } final BiometricPrompt prompt = new BiometricPrompt(activity, executor, this); prompt.authenticate(this.promptInfo); }
Example #11
Source File: MainActivity.java From android-biometricprompt with Apache License 2.0 | 6 votes |
private void showBiometricPrompt(Signature signature) { BiometricPrompt.AuthenticationCallback authenticationCallback = getAuthenticationCallback(); BiometricPrompt mBiometricPrompt = new BiometricPrompt(this, getMainThreadExecutor(), authenticationCallback); // Set prompt info BiometricPrompt.PromptInfo promptInfo = new BiometricPrompt.PromptInfo.Builder() .setDescription("Description") .setTitle("Title") .setSubtitle("Subtitle") .setNegativeButtonText("Cancel") .build(); // Show biometric prompt if (signature != null) { Log.i(TAG, "Show biometric prompt"); mBiometricPrompt.authenticate(promptInfo, new BiometricPrompt.CryptoObject(signature)); } }
Example #12
Source File: CreateSignatureCallback.java From react-native-biometrics with MIT License | 6 votes |
@Override public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) { super.onAuthenticationSucceeded(result); try { BiometricPrompt.CryptoObject cryptoObject = result.getCryptoObject(); Signature cryptoSignature = cryptoObject.getSignature(); cryptoSignature.update(this.payload.getBytes()); byte[] signed = cryptoSignature.sign(); String signedString = Base64.encodeToString(signed, Base64.DEFAULT); signedString = signedString.replaceAll("\r", "").replaceAll("\n", ""); WritableMap resultMap = new WritableNativeMap(); resultMap.putBoolean("success", true); resultMap.putString("signature", signedString); promise.resolve(resultMap); } catch (Exception e) { promise.reject("Error creating signature: " + e.getMessage(), "Error creating signature"); } }
Example #13
Source File: SecuritySetupSlide.java From Aegis with GNU General Public License v3.0 | 5 votes |
private void showBiometricPrompt() { BiometricSlotInitializer initializer = new BiometricSlotInitializer(this, new BiometricsListener()); BiometricPrompt.PromptInfo info = new BiometricPrompt.PromptInfo.Builder() .setTitle(getString(R.string.set_up_biometric)) .setNegativeButtonText(getString(android.R.string.cancel)) .build(); initializer.authenticate(info); }
Example #14
Source File: BiometricAuthHelper.java From Passbook with Apache License 2.0 | 5 votes |
public BiometricAuthHelper(boolean isFirstTime, BiometricListener listener, FragmentActivity context) { Executor executor = ContextCompat.getMainExecutor(context); mBiometricPrompt = new BiometricPrompt(context, executor, this); this.mIsFirstTime = isFirstTime; mTitle = context.getString(R.string.fp_title); initCipher(mIsFirstTime ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE); if (mIsFirstTime) { mDesc = context.getString(R.string.fp_ask); mNegativeButtonText = context.getString(android.R.string.cancel); } else { mDesc = context.getString(R.string.fp_desc); mNegativeButtonText = context.getString(R.string.fp_use_pwd); } this.mListener = listener; }
Example #15
Source File: BiometricAuthHelper.java From Passbook with Apache License 2.0 | 5 votes |
public void authenticate() { if (mCryptoObject == null) { return; } BiometricPrompt.PromptInfo promptInfo = new BiometricPrompt.PromptInfo.Builder() .setTitle(mTitle) .setDescription(mDesc) // Authenticate without requiring the user to press a "confirm" // button after satisfying the biometric check .setConfirmationRequired(false) .setNegativeButtonText(mNegativeButtonText) .build(); mBiometricPrompt.authenticate(promptInfo, mCryptoObject); }
Example #16
Source File: BiometricAuthHelper.java From Passbook with Apache License 2.0 | 5 votes |
@Override public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) { super.onAuthenticationError(errorCode, errString); if (errorCode == BiometricPrompt.ERROR_CANCELED && mListener != null) { mListener.onCanceled(mIsFirstTime); } }
Example #17
Source File: SimplePromptCallback.java From react-native-biometrics with MIT License | 5 votes |
@Override public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) { super.onAuthenticationSucceeded(result); WritableMap resultMap = new WritableNativeMap(); resultMap.putBoolean("success", true); this.promise.resolve(resultMap); }
Example #18
Source File: CryptoObjectFactory.java From Android-Goldfinger with Apache License 2.0 | 5 votes |
@Nullable @SuppressWarnings("ConstantConditions") private BiometricPrompt.CryptoObject createSignatureCryptoObject(@NonNull String key, @NonNull Mode mode) { Signature signature = Mode.ENCRYPTION == mode ? signatureFactory.createEncryptionCrypter(key) : signatureFactory.createDecryptionCrypter(key); return signature != null ? new BiometricPrompt.CryptoObject(signature) : null; }
Example #19
Source File: SimplePromptCallback.java From react-native-biometrics with MIT License | 5 votes |
@Override public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) { super.onAuthenticationError(errorCode, errString); if (errorCode == BiometricPrompt.ERROR_NEGATIVE_BUTTON || errorCode == BiometricPrompt.ERROR_USER_CANCELED) { WritableMap resultMap = new WritableNativeMap(); resultMap.putBoolean("success", false); resultMap.putString("error", "User cancellation"); this.promise.resolve(resultMap); } else { this.promise.reject(errString.toString(), errString.toString()); } }
Example #20
Source File: CryptoObjectFactory.java From Android-Goldfinger with Apache License 2.0 | 5 votes |
@Nullable BiometricPrompt.CryptoObject createCryptoObject(@NonNull String key, @NonNull Mode mode) { if (cipherFactory != null) { return createCipherCryptoObject(key, mode); } else if (macFactory != null) { return createMacCryptoObject(key, mode); } else if (signatureFactory != null) { return createSignatureCryptoObject(key, mode); } else { return null; } }
Example #21
Source File: ReactNativeBiometrics.java From react-native-biometrics with MIT License | 5 votes |
@ReactMethod public void simplePrompt(final ReadableMap params, final Promise promise) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { UiThreadUtil.runOnUiThread( new Runnable() { @Override public void run() { try { String cancelButtomText = params.getString("cancelButtonText"); String promptMessage = params.getString("promptMessage"); AuthenticationCallback authCallback = new SimplePromptCallback(promise); FragmentActivity fragmentActivity = (FragmentActivity) getCurrentActivity(); Executor executor = Executors.newSingleThreadExecutor(); BiometricPrompt biometricPrompt = new BiometricPrompt(fragmentActivity, executor, authCallback); PromptInfo promptInfo = new PromptInfo.Builder() .setDeviceCredentialAllowed(false) .setNegativeButtonText(cancelButtomText) .setTitle(promptMessage) .build(); biometricPrompt.authenticate(promptInfo); } catch (Exception e) { promise.reject("Error displaying local biometric prompt: " + e.getMessage(), "Error displaying local biometric prompt: " + e.getMessage()); } } }); } else { promise.reject("Cannot display biometric prompt on android versions below 6.0", "Cannot display biometric prompt on android versions below 6.0"); } }
Example #22
Source File: CreateSignatureCallback.java From react-native-biometrics with MIT License | 5 votes |
@Override public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) { super.onAuthenticationError(errorCode, errString); if (errorCode == BiometricPrompt.ERROR_NEGATIVE_BUTTON || errorCode == BiometricPrompt.ERROR_USER_CANCELED ) { WritableMap resultMap = new WritableNativeMap(); resultMap.putBoolean("success", false); resultMap.putString("error", "User cancellation"); this.promise.resolve(resultMap); } else { this.promise.reject(errString.toString(), errString.toString()); } }
Example #23
Source File: MainActivity.java From android-biometricprompt with Apache License 2.0 | 5 votes |
private BiometricPrompt.AuthenticationCallback getAuthenticationCallback() { // Callback for biometric authentication result return new BiometricPrompt.AuthenticationCallback() { @Override public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) { super.onAuthenticationError(errorCode, errString); } @Override public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) { Log.i(TAG, "onAuthenticationSucceeded"); super.onAuthenticationSucceeded(result); if (result.getCryptoObject() != null && result.getCryptoObject().getSignature() != null) { try { Signature signature = result.getCryptoObject().getSignature(); signature.update(mToBeSignedMessage.getBytes()); String signatureString = Base64.encodeToString(signature.sign(), Base64.URL_SAFE); // Normally, ToBeSignedMessage and Signature are sent to the server and then verified Log.i(TAG, "Message: " + mToBeSignedMessage); Log.i(TAG, "Signature (Base64 EncodeD): " + signatureString); Toast.makeText(getApplicationContext(), mToBeSignedMessage + ":" + signatureString, Toast.LENGTH_SHORT).show(); } catch (SignatureException e) { throw new RuntimeException(); } } else { // Error Toast.makeText(getApplicationContext(), "Something wrong", Toast.LENGTH_SHORT).show(); } } @Override public void onAuthenticationFailed() { super.onAuthenticationFailed(); } }; }
Example #24
Source File: CryptoObjectInitRunnable.java From Android-Goldfinger with Apache License 2.0 | 5 votes |
@Override public void run() { final BiometricPrompt.CryptoObject cryptoObject = cryptoObjectFactory.createCryptoObject(key, mode); if (!callback.canceled) { /* Return callback back to main thread as this is executed in the background */ MAIN_HANDLER.post(new Runnable() { @Override public void run() { callback.onCryptoObjectCreated(cryptoObject); } }); } }
Example #25
Source File: SplashActivity.java From Rucky with GNU General Public License v3.0 | 5 votes |
public void biometric() { Executor executor = ContextCompat.getMainExecutor(this); BiometricPrompt biometricPrompt = new BiometricPrompt(this, executor, new BiometricPrompt.AuthenticationCallback() { @Override public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) { super.onAuthenticationError(errorCode, errString); finishAffinity(); System.exit(0); } @Override public void onAuthenticationSucceeded( @NonNull BiometricPrompt.AuthenticationResult result) { super.onAuthenticationSucceeded(result); splash(); getKey(); } @Override public void onAuthenticationFailed() { super.onAuthenticationFailed(); finishAffinity(); System.exit(0); } }); BiometricPrompt.PromptInfo promptInfo = new BiometricPrompt.PromptInfo.Builder() .setTitle(getResources().getString(R.string.unlock)) .setSubtitle(getResources().getString(R.string.auth)) .setConfirmationRequired(false) .setDeviceCredentialAllowed(true) .build(); biometricPrompt.authenticate(promptInfo); }
Example #26
Source File: BiometricCallback.java From Android-Goldfinger with Apache License 2.0 | 5 votes |
/** * Cipher the value with unlocked {@link BiometricPrompt.CryptoObject}. * * @param cryptoObject unlocked {@link BiometricPrompt.CryptoObject} that is ready to use. */ private void cipherValue(BiometricPrompt.CryptoObject cryptoObject) { final String cipheredValue; if (mode == Mode.DECRYPTION) { cipheredValue = cryptoProxy.decrypt(cryptoObject, value); } else { cipheredValue = cryptoProxy.encrypt(cryptoObject, value); } MAIN_HANDLER.post(new Runnable() { @Override public void run() { if (cipheredValue != null) { log("Ciphered [%s] => [%s]", value, cipheredValue); callback.onResult(new Goldfinger.Result( Goldfinger.Type.SUCCESS, Goldfinger.Reason.AUTHENTICATION_SUCCESS, cipheredValue, null )); } else { Exception e = (mode == Mode.DECRYPTION) ? new DecryptionException() : new EncryptionException(); callback.onError(e); } } }); }
Example #27
Source File: CryptoObjectFactory.java From Android-Goldfinger with Apache License 2.0 | 4 votes |
@Nullable @SuppressWarnings("ConstantConditions") private BiometricPrompt.CryptoObject createCipherCryptoObject(String key, Mode mode) { Cipher cipher = Mode.ENCRYPTION == mode ? cipherFactory.createEncryptionCrypter(key) : cipherFactory.createDecryptionCrypter(key); return cipher != null ? new BiometricPrompt.CryptoObject(cipher) : null; }
Example #28
Source File: ReactNativeBiometrics.java From react-native-biometrics with MIT License | 4 votes |
@ReactMethod public void createSignature(final ReadableMap params, final Promise promise) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { UiThreadUtil.runOnUiThread( new Runnable() { @Override public void run() { try { String cancelButtomText = params.getString("cancelButtonText"); String promptMessage = params.getString("promptMessage"); String payload = params.getString("payload"); Signature signature = Signature.getInstance("SHA256withRSA"); KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore"); keyStore.load(null); PrivateKey privateKey = (PrivateKey) keyStore.getKey(biometricKeyAlias, null); signature.initSign(privateKey); BiometricPrompt.CryptoObject cryptoObject = new BiometricPrompt.CryptoObject(signature); AuthenticationCallback authCallback = new CreateSignatureCallback(promise, payload); FragmentActivity fragmentActivity = (FragmentActivity) getCurrentActivity(); Executor executor = Executors.newSingleThreadExecutor(); BiometricPrompt biometricPrompt = new BiometricPrompt(fragmentActivity, executor, authCallback); PromptInfo promptInfo = new PromptInfo.Builder() .setDeviceCredentialAllowed(false) .setNegativeButtonText(cancelButtomText) .setTitle(promptMessage) .build(); biometricPrompt.authenticate(promptInfo, cryptoObject); } catch (Exception e) { promise.reject("Error signing payload: " + e.getMessage(), "Error generating signature: " + e.getMessage()); } } }); } else { promise.reject("Cannot generate keys on android versions below 6.0", "Cannot generate keys on android versions below 6.0"); } }
Example #29
Source File: SlotManagerActivity.java From Aegis with GNU General Public License v3.0 | 4 votes |
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_slots); _edited = false; ActionBar bar = getSupportActionBar(); bar.setHomeAsUpIndicator(R.drawable.ic_close); bar.setDisplayHomeAsUpEnabled(true); findViewById(R.id.button_add_biometric).setOnClickListener(view -> { if (BiometricsHelper.isAvailable(this)) { BiometricSlotInitializer initializer = new BiometricSlotInitializer(SlotManagerActivity.this, new RegisterBiometricsListener()); BiometricPrompt.PromptInfo info = new BiometricPrompt.PromptInfo.Builder() .setTitle(getString(R.string.add_biometric_slot)) .setNegativeButtonText(getString(android.R.string.cancel)) .build(); initializer.authenticate(info); } }); findViewById(R.id.button_add_password).setOnClickListener(view -> { Dialogs.showSetPasswordDialog(this, new PasswordListener()); }); // set up the recycler view _adapter = new SlotAdapter(this); RecyclerView slotsView = findViewById(R.id.list_slots); LinearLayoutManager layoutManager = new LinearLayoutManager(this); slotsView.setLayoutManager(layoutManager); slotsView.setAdapter(_adapter); slotsView.setNestedScrollingEnabled(false); // load the slots and masterKey _creds = (VaultFileCredentials) getIntent().getSerializableExtra("creds"); for (Slot slot : _creds.getSlots()) { _adapter.addSlot(slot); } updateBiometricsButton(); }
Example #30
Source File: BiometricSlotInitializer.java From Aegis with GNU General Public License v3.0 | 4 votes |
@Override public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) { super.onAuthenticationSucceeded(result); _listener.onInitializeSlot(_slot, Objects.requireNonNull(result.getCryptoObject()).getCipher()); }