@Test @Config(shadows = {AuthHelperShadow.class}) public void testVerifyPhoneNumberNoMsgException_showsAlertDialog() { reset(AuthHelperShadow.getPhoneAuthProvider()); mActivity.verifyPhoneNumber(PHONE, false); verify(AuthHelperShadow.getPhoneAuthProvider()).verifyPhoneNumber( eq(PHONE), eq(AUTO_RETRIEVAL_TIMEOUT_MILLIS), eq(TimeUnit.MILLISECONDS), eq(mActivity), callbacksArgumentCaptor.capture(), (PhoneAuthProvider.ForceResendingToken) isNull()); PhoneAuthProvider.OnVerificationStateChangedCallbacks onVerificationStateChangedCallbacks = callbacksArgumentCaptor.getValue(); onVerificationStateChangedCallbacks.onVerificationFailed( new FirebaseAuthException("some_code", "custom_message")); assertTrue(mActivity.getAlertDialog().isShowing()); assertEquals(FirebaseAuthError.ERROR_UNKNOWN.getDescription(), getAlertDialogMessage()); }
@Test public void verifyTokenCertificateError() throws Exception { FirebaseToken token = TestOnlyImplFirebaseAuthTrampolines.parseToken( FACTORY, createToken(createHeader(), createPayload())); MockHttpTransport mockTransport = new MockHttpTransport() { @Override public LowLevelHttpRequest buildRequest(String method, String url) throws IOException { throw new IOException("Expected error"); } }; FirebaseTokenVerifier verifier = new FirebaseTokenVerifier.Builder() .setClock(CLOCK) .setPublicKeysManager( new GooglePublicKeysManager.Builder(mockTransport, FACTORY) .setClock(CLOCK) .setPublicCertsEncodedUrl(FirebaseTokenVerifier.CLIENT_CERT_URL) .build()) .setProjectId(PROJECT_ID) .build(); try { verifier.verifyTokenAndSignature(TestOnlyImplFirebaseAuthTrampolines.getToken(token)); Assert.fail("No exception thrown"); } catch (FirebaseAuthException expected) { assertTrue(expected.getCause() instanceof IOException); assertEquals("Expected error", expected.getCause().getMessage()); } }
private void onVerificationFailed(@NonNull FirebaseException ex) { dismissLoadingDialog(); if (ex instanceof FirebaseAuthException) { FirebaseAuthError error = FirebaseAuthError.fromException((FirebaseAuthException) ex); switch (error) { case ERROR_INVALID_PHONE_NUMBER: VerifyPhoneNumberFragment verifyPhoneNumberFragment = (VerifyPhoneNumberFragment) getSupportFragmentManager().findFragmentByTag(VerifyPhoneNumberFragment.TAG); if (verifyPhoneNumberFragment != null) { verifyPhoneNumberFragment.showError( getString(R.string.fui_invalid_phone_number)); } break; case ERROR_TOO_MANY_REQUESTS: showAlertDialog(getString(R.string.fui_error_too_many_attempts), null); break; case ERROR_QUOTA_EXCEEDED: showAlertDialog(getString(R.string.fui_error_quota_exceeded), null); break; default: Log.w(PHONE_VERIFICATION_LOG_TAG, error.getDescription(), ex); showAlertDialog(error.getDescription(), null); } } else { Log.w(PHONE_VERIFICATION_LOG_TAG, ex.getLocalizedMessage()); showAlertDialog(ex.getLocalizedMessage(), null); } }
@Test @Config(shadows = {AuthHelperShadow.class}) public void testVerifyPhoneNumberInvalidPhoneException_showsInlineError() { reset(AuthHelperShadow.getPhoneAuthProvider()); mActivity.verifyPhoneNumber(PHONE, false); //was dialog displayed assertEquals( mActivity.getString(R.string.fui_verifying), mActivity.mProgressDialog.mMessageView.getText()); //was upstream method invoked verify(AuthHelperShadow.getPhoneAuthProvider()).verifyPhoneNumber( eq(PHONE), eq(AUTO_RETRIEVAL_TIMEOUT_MILLIS), eq(TimeUnit.MILLISECONDS), eq(mActivity), callbacksArgumentCaptor.capture(), (PhoneAuthProvider.ForceResendingToken) isNull()); PhoneAuthProvider.OnVerificationStateChangedCallbacks onVerificationStateChangedCallbacks = callbacksArgumentCaptor.getValue(); onVerificationStateChangedCallbacks.onVerificationFailed( new FirebaseAuthException( FirebaseAuthError.ERROR_INVALID_PHONE_NUMBER.toString(), "any_message")); //was error displayed assertEquals(mPhoneLayout.getError(), mActivity.getString(R.string.fui_invalid_phone_number)); }
/** * We are changing the semantics of the super-class method in order to provide more details on why * this is failing to the developer. */ public boolean verifyTokenAndSignature(IdToken token) throws FirebaseAuthException { Payload payload = token.getPayload(); Header header = token.getHeader(); String errorMessage = null; boolean isCustomToken = payload.getAudience() != null && payload.getAudience().equals(FIREBASE_AUDIENCE); boolean isLegacyCustomToken = header.getAlgorithm() != null && header.getAlgorithm().equals("HS256") && payload.get("v") != null && payload.get("v").equals(new BigDecimal(0)) && payload.get("d") != null && payload.get("d") instanceof ArrayMap && ((ArrayMap) payload.get("d")).get("uid") != null; if (header.getKeyId() == null) { if (isCustomToken) { errorMessage = "verifyIdToken() expects an ID token, but was given a custom token."; } else if (isLegacyCustomToken) { errorMessage = "verifyIdToken() expects an ID token, but was given a legacy custom token."; } else { errorMessage = "Firebase ID token has no \"kid\" claim."; } } else if (header.getAlgorithm() == null || !header.getAlgorithm().equals(ALGORITHM)) { errorMessage = String.format( "Firebase ID token has incorrect algorithm. Expected \"%s\" but got \"%s\".", ALGORITHM, header.getAlgorithm()); } else if (!token.verifyAudience(getAudience())) { errorMessage = String.format( "Firebase ID token has incorrect \"aud\" (audience) claim. Expected \"%s\" but got " + "\"%s\".", concat(getAudience()), concat(token.getPayload().getAudienceAsList())); errorMessage += PROJECT_ID_MATCH_MESSAGE; } else if (!token.verifyIssuer(getIssuers())) { errorMessage = String.format( "Firebase ID token has incorrect \"iss\" (issuer) claim. " + "Expected \"%s\" but got \"%s\".", concat(getIssuers()), token.getPayload().getIssuer()); errorMessage += PROJECT_ID_MATCH_MESSAGE; } else if (payload.getSubject() == null) { errorMessage = "Firebase ID token has no \"sub\" (subject) claim."; } else if (payload.getSubject().isEmpty()) { errorMessage = "Firebase ID token has an empty string \"sub\" (subject) claim."; } else if (payload.getSubject().length() > 128) { errorMessage = "Firebase ID token has \"sub\" (subject) claim longer than 128 characters."; } else if (!token.verifyTime(getClock().currentTimeMillis(), getAcceptableTimeSkewSeconds())) { errorMessage = "Firebase ID token has expired or is not yet valid. Get a fresh token from your client " + "app and try again."; } if (errorMessage != null) { errorMessage += VERIFY_ID_TOKEN_DOCS_MESSAGE; throw new FirebaseAuthException(ERROR_INVALID_CREDENTIAL, errorMessage); } try { if (!verifySignature(token)) { throw new FirebaseAuthException( ERROR_INVALID_CREDENTIAL, "Firebase ID token isn't signed by a valid public key." + VERIFY_ID_TOKEN_DOCS_MESSAGE); } } catch (IOException | GeneralSecurityException e) { throw new FirebaseAuthException( ERROR_RUNTIME_EXCEPTION, "Error while verifying token signature.", e); } return true; }
private void registerUser(){ String email = editText.getText().toString().trim(); String password = editText2.getText().toString().trim(); String password_confirm = editText3.getText().toString().trim(); Log.i("Test", password_confirm); Log.i("Test", password); if(TextUtils.isEmpty(email)){ //email is empty Toast.makeText(this,"Please enter email", Toast.LENGTH_SHORT).show(); return; } if(TextUtils.isEmpty(password)){ //password is empty Toast.makeText(this,"Please enter password", Toast.LENGTH_SHORT).show(); return; } if(!password.equalsIgnoreCase(password_confirm)){ Toast.makeText(this,"Password not correct", Toast.LENGTH_SHORT).show(); return; } progressDialog.setMessage("Registering User..."); progressDialog.show(); firebaseAuth.createUserWithEmailAndPassword(email, password) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { if(task.isSuccessful()){ //Toast.makeText(MainActivity.this,"Registered Successfully",Toast.LENGTH_SHORT).show(); startActivity(new Intent(getApplicationContext(),MainActivity.class)); finish(); } else{ FirebaseAuthException e = (FirebaseAuthException)task.getException(); Toast.makeText(RegisterActivity.this, "Failed Registration: "+e.getMessage(), Toast.LENGTH_SHORT).show(); //Toast.makeText(MainActivity.this,"Could not register. Please try again.",Toast.LENGTH_SHORT).show(); } progressDialog.hide(); } }); }