Invalid key problem with JWT token and PHP8

Asked

Viewed 48 times

0

Well I’m performing some tests of my project on PHP8 and I’m already refactoring it with some new features of PHP8.

But I’m racking my brain with a library I use to work with JWT token.

In this case the library and this: https://github.com/adhocore/php-jwt

Good use it by informing a key that was generated as follows:

ssh-keygen -t rsa -b 1024 -m PEM -f ws.key

I call the class that:

$jwt = new JWT("/var/www/ws.key", "RS256");

But I’m always making that mistake:

"Invalid key: Should be resource of private key"

Well I went to analyze the library code to verify where the error occurs:

    /**
 * Throw up if key is not resource or file path to private key.
 */
protected function validateKey()
{
    if (\is_string($key = $this->key)) {
        if (\substr($key, 0, 7) !== 'file://') {
            $key = 'file://' . $key;
        }

        $this->key = \openssl_get_privatekey($key, $this->passphrase ?: '');
    }

    if (!\is_resource($this->key)) {
        throw new JWTException('Invalid key: Should be resource of private key', static::ERROR_KEY_INVALID);
    }
}

Well the strange thing is that the same code and key run on a php7.4 server.

Well I’m not getting the bug, Obs I’m running Debian 10 and PHP8.

OBS: If I document the check that’s giving the error:

if (!\is_resource($this->key)) {
        throw new JWTException('Invalid key: Should be resource of private key', static::ERROR_KEY_INVALID);
    }

Everything works fine, which indicates that the key is created correctly. Something in is_resource switched from PHP7.4 to PHP8 and is causing this error. I am unable to identify the fault.

  • it seems that the key should be of type Resource, so it gives this error when returning false in is_resource

  • I thought about it too but I do not know how to create the key so, I’m researching here to see if I find something.

  • The strange thing is that it works in php 7.4

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.