Firebase JWT-php 'Signature verification failed' on JWT::d ecode



下面是我生成令牌的代码:

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
);
}

这是我试图解码令牌的代码:

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;
}

它抛出";签名验证失败";JWT代码中的错误。

据我所知,我正在按照回购主屏幕上给出的例子进行合理的近似。

我正在使用HS512,但也尝试过HS256,没有任何区别。

我已经确认,我试图解码的令牌正是第一种方法中生成的令牌。

由于$hash$signature不匹配,因此此处的比较检查失败。

所以我并没有发送与接收到的令牌完全相同的令牌。JWT对令牌数据进行编码时,会修剪掉base64编码字符串末尾的=。我存储的内容在末尾(通常(包含那些=。正因为如此,当它运行比较时,它失败了。

总之,dur可以更好地检查值。

最新更新