'Firebase JWT-php 'Signature verification failed' on JWT::decode

Here's my code generating the token:

public static function GenerateNewAuthTokens(User $user): string {
    $issuedAt   = new DateTimeImmutable();
    $expire     = $issuedAt->modify('+' . AuthenticationHelper::AUTH_EXPIRE_MINUTES . ' minutes');
    $username   = $user->Username;
    $issuedAtTimestamp = $issuedAt->getTimestamp();

    $auth_data = [
        'iat'  => $issuedAtTimestamp,                // Issued at: time when the token was generated
        'iss'  => AuthenticationHelper::SERVER_NAME, // Issuer
        'nbf'  => $issuedAtTimestamp,                // Not before
        'exp'  => $expire->getTimestamp(),                           // Expire
        'userName' => $username,                     // User name
    ];

    return JWT::encode(
        $auth_data,
        AuthenticationHelper::SECRET_KEY,
        AuthenticationHelper::ALGORITHM
    );
}

Here is my code attempting to decode the token:

public static function GetAuthData(): ?object {
    $headers = getallheaders();
    if (isset($headers) && count($headers) && isset($headers['Authorization']) && strlen($headers['Authorization']) > 7) {
        try {
            $token = explode(" ", $headers['Authorization'])[1];
            $decodedToken = JWT::decode($token, new Key(AuthenticationHelper::SECRET_KEY, AuthenticationHelper::ALGORITHM));
            return $decodedToken;
        } catch (\Throwable $th) {
            //TODO
            $err = $th;
        }
    }
    return null;
}

It throws the "Signature verification failed" error in the JWT code here.

So far as I can tell - I'm following the example given on the repo home screen to a reasonable approximation.

I am using HS512 but have tried HS256 as well with no difference.

I have confirmed that the token I'm attempting to decode is exactly what was generated in the first method.

It's failing the compare check here, due to $hash and $signature not matching.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source