1

I am creating JWT using documentation mentioned here.

Everything is done as mentioned in the documentation.

Here is the code snippet.

When I am returning this token to android client, Android client throws following error.

com.google.android.gms.tasks.RuntimeExecutionException: com.google.firebase.auth.FirebaseAuthInvalidCredentialsException: The custom token format is incorrect. Please check the documentation.

I am not sure, what am I missing while creating token.

$service_account_email = "[email protected]";
            $private_key = "-----BEGIN PRIVATE KEY-----VERY LONG KEY-----END PRIVATE KEY-----\n";//See github link for key if needed

            $now_seconds = time();
            $payload = array(
              "iss" => $service_account_email,
              "sub" => $service_account_email,
              "aud" => "https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit",
              "iat" => $now_seconds,
              "exp" => $now_seconds+(60*60),  // Maximum expiration time is one hour
              "uid" => $mobile
            );
            $token = JWT::encode($payload, $private_key, "HS256");

Here is the screenshot of extracted token using jwt.io enter image description here

4
  • 1
    Please do not post code just as an external link, it's always best to post code directly into the question and then if you want link to an external fiddle Commented May 25, 2016 at 6:27
  • @Draken: Thanks for the edit. Will take care of this for future posts. Commented May 25, 2016 at 6:54
  • Have you found any solution?:) Commented Jun 8, 2016 at 14:00
  • @FlorinT. Not yet. We have decided to wait till new Firebase upgrade becomes stable and continue with the old Firebase till that time. Commented Jun 9, 2016 at 5:00

1 Answer 1

0

I was also facing a similar issue when I was simply trying to run the Example with RS256 (openssl) from firebase/php-jwt Readme File.

I posted a question about the same here.

and based on the Answer and comments, I learned that the Private and Public keys in the example are incorrect.

I generated new key pair using the following commands (copied from this Gist, the other options did not work when I tried.)

Private Key

openssl genrsa -out private.pem 2048

Public Key

openssl rsa -in private.pem -pubout -out public.pem

and I used those files in my code and now it's working fine and even getting verified on jwt.io

Here is the complete code:

<?php

include '../vendor/autoload.php';

use Firebase\JWT\JWT;
use Firebase\JWT\Key;

$privateKey = file_get_contents(__DIR__ . '/private.pem');

$publicKey = file_get_contents(__DIR__ . '/public.pem');

$payload = [
    'iss' => 'example.org',
    'aud' => 'example.com',
    'iat' => 1356999524,
    'nbf' => 1357000000,
];

$jwt = JWT::encode($payload, $privateKey, 'RS256');
echo "Encode:\n" . print_r($jwt, true) . "<br/><br/>";

$decoded = JWT::decode($jwt, new Key($publicKey, 'RS256'));

/*
NOTE: This will now be an object instead of an associative array. To get
an associative array, you will need to cast it as such:
 */

$decoded_array = (array) $decoded;
echo "Decode:\n" . print_r($decoded_array, true) . "\n";

Screenshot from jwt.io: enter image description here

Hope this helps.

Best.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.