Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The supplied auth credential is malformed or has expired

In my Android app I'm signing in via Firebase AuthUI using Google sign in. That part works. Now I tried to sign in using the IdToken but I always get this error message:

com.google.android.gms.tasks.RuntimeExecutionException: com.google.firebase.auth.FirebaseAuthInvalidCredentialsException: The supplied auth credential is malformed or has expired. [ Invalid id_token in IdP response: XXXXXXX... ]

This works and logs me in successfully in my Firebase project:

@OnClick(R.id.sign_in)
public void signIn() {
    startActivityForResult(
            AuthUI.getInstance().createSignInIntentBuilder()
                    .setTheme(getSelectedTheme())
                    .setLogo(getSelectedLogo())
                    .setAvailableProviders(getSelectedProviders())
                    .setTosAndPrivacyPolicyUrls(getSelectedTosUrl(),getSelectedPrivacyPolicyUrl())
                    .setIsSmartLockEnabled(true,
                            true)
                    .build(),
            RC_SIGN_IN);
}

But trying to use signInWithCredential using credentials made from the IdToken gives me the error above:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RC_SIGN_IN) {
        handleSignInResponse(resultCode, data);
        return;
    }

}

@MainThread
private void handleSignInResponse(int resultCode, Intent data) {
    IdpResponse response = IdpResponse.fromResultIntent(data);

    if (resultCode == RESULT_OK) {
        FirebaseAuth auth = FirebaseAuth.getInstance();
        FirebaseUser firebaseUser = auth.getCurrentUser();

        if(firebaseUser != null){

            auth.getCurrentUser().getIdToken(true).addOnCompleteListener(task -> {

                String idToken = task.getResult().getToken();
                AuthCredential cred = GoogleAuthProvider.getCredential(idToken ,null);

                auth.signInWithCredential(cred).addOnCompleteListener(task1 -> 
                        Log.v(TAG,"results: " + task1.getResult()));

            });
        }
    }
}

I'm following this tutorial and my goal is to sign in to multiple Firebase projects.

Trying to sign in to a secondary Firebase project using the credentials from the IdToken above returns the same error.

FirebaseApp app = FirebaseApp.getInstance("secondary");
FirebaseAuth.getInstance(app).signInWithCredential(credential);
like image 914
Hugo Avatar asked Jan 21 '20 19:01

Hugo


People also ask

What is the response to the identity platform authentication request?

The response contains the Identity Platform ID token and refresh token associated with the authenticated user. CREDENTIAL_TOO_OLD_LOGIN_AGAIN: The user's credential is no longer valid. The user must sign in again. TOKEN_EXPIRED: The user's credential is no longer valid.

How to sign in a user with an OAuth credential?

You can sign in a user with an OAuth credential by issuing an HTTP POST request to the Auth verifyAssertion endpoint. Content-Type: application/json The URI to which the IDP redirects the user back. Contains the OAuth credential (an ID token or access token) and provider ID which issues the credential.

What is the difference between @credential_too_old_login_again and token_expired?

CREDENTIAL_TOO_OLD_LOGIN_AGAIN: The user's credential is no longer valid. The user must sign in again. TOKEN_EXPIRED: The user's credential is no longer valid.

What is a credential_too_old_login_again?

The response contains the Identity Platform ID token and refresh token associated with the authenticated user. CREDENTIAL_TOO_OLD_LOGIN_AGAIN: The user's credential is no longer valid. The user must sign in again. TOKEN_EXPIRED: The user's credential is no longer valid. The user must sign in again.


1 Answers

I got this error when the time or time zone on the device was wrong. Setting the correct time solved it.

like image 92
Daniel Avatar answered Sep 25 '22 08:09

Daniel