Sign in With Apple - Android Implementation with Firebase

Viewed 36

setting up the Android Firebase on Android.

I need to get the unique identifier of the user, so that I can have consistency accross all platforms no matter if the email is hided or not.

Based upon https://firebase.google.com/docs/auth/android/apple , using .startActivityForSignInWithProvider(this, provider.build()) we can see:

    public void onSuccess(AuthResult authResult) {
        Log.d(TAG, "checkPending:onSuccess:" + authResult);
        // Get the user profile with authResult.getUser() and
        // authResult.getAdditionalUserInfo(), and the ID
        // token from Apple with authResult.getCredential().

And from Apple https://developer.apple.com/documentation/sign_in_with_apple/sign_in_with_apple_rest_api/authenticating_users_with_sign_in_with_apple#3383773 we can also see

The identity token is a JSON Web Token (JWT) and contains

and

sub
The subject registered claim identifies the principal that’s the subject of the identity token. Because this token is for your app, the value is the unique identifier for the user.

Question:

What I got from firebase is AuthCredential , but I am expecting JWT with "sub" in it.

How can I get it?

1 Answers

thanks to Ricardo from Firebase support I got this working.

in Android the sub value from Apple is in

"firebase":{"identities":{"apple.com":["001814.24f212edfsdfdfsfd69b7b5fee972e.1722"],"email":["a@b.com"]},"sign_in_provider":"apple.com"}

You get it using

auth.startActivityForSignInWithProvider(this, provider.build()).addOnSuccessListener( new OnSuccessListener<AuthResult>() {
                        @Override
                        public void onSuccess(AuthResult authResult) {
                            // Sign-in successful!
                          
                            FirebaseUser user = authResult.getUser();

                            AuthCredential identityToken = authResult.getCredential();
                     


                            authResult.getUser().getIdToken(false).addOnSuccessListener(new OnSuccessListener<GetTokenResult>() {
                                @Override
                                public void onSuccess(GetTokenResult result) {
                                    String idToken = result.getToken();
                                    //Do whatever
                                    Log.d(TAG, "GetTokenResult result = " + idToken);


                                    try {
                                        decodeJWT(idToken);
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }


                                }
                            });



                        }
                    })

Where the JWT is decoded with

private static void decodeJWT(String JWTEncoded) throws Exception {
    try {
        String[] split = JWTEncoded.split("\\.");
        Log.d("JWT_DECODED", "Header: " + getJson(split[0]));
        Log.d("JWT_DECODED", "Body: " + getJson(split[1]));
    } catch (UnsupportedEncodingException e) {
        //Error
    }
}

private static String getJson(String strEncoded) throws UnsupportedEncodingException{
    byte[] decodedBytes = Base64.decode(strEncoded, Base64.URL_SAFE);
    return new String(decodedBytes, "UTF-8");
}
Related