Firebase JWT:签名验证失败

Web*_*man 3 php jwt firebase-authentication

我正在尝试对 Firebase 使用 JWT 身份验证,但我总是收到此错误:“致命错误:未捕获的 Firebase\JWT\SignatureInvalidException:签名验证失败”。

代码是这样的:

$key = "test";

$tokenId    = base64_encode(mcrypt_create_iv(32));
$issuedAt   = time();
$notBefore  = $issuedAt + 10;
$expire     = $notBefore + 60;
$serverName = $_SERVER["SERVER_NAME"];


$data = [
    'iat'  => $issuedAt,
    'jti'  => $tokenId,
    'iss'  => $serverName,
    'nbf'  => $notBefore,
    'exp'  => $expire,
    "userId"  => 1
];


$secretKey = base64_decode($key);


$jwt = \Firebase\JWT\JWT::encode($data, $secretKey, 'HS256');

// and when I decode the tokens, I got that exception
$decoded = \Firebase\JWT\JWT::decode($jwt, $key, array('HS256'));
Run Code Online (Sandbox Code Playgroud)

我错了什么?

Han*_* Z. 5

您不需要$secretKey或 base64_decode 密钥,只需执行以下操作:

$jwt = \Firebase\JWT\JWT::encode($data, $key, 'HS256');
$decoded = \Firebase\JWT\JWT::decode($jwt, $key, array('HS256'));
Run Code Online (Sandbox Code Playgroud)