我的代码出现了一个奇怪的问题。
我目前正在从事一个WordPress项目,其中涉及大量插件,但我关注的主要插件是WP的JWT认证,因为我正在使用它来实现REST身份验证。
当您将登录凭据发送到端点/jwt-auth/v1/token时,一切都很好,因为返回一个令牌和一些用户信息。
curl --request POST \
--url http://localhost:12345/wp-json/jwt-auth/v1/token \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--data '{
"username": "hello@example.com",
"password": "password"
}'当您试图通过调用端点/jwt-auth/v1/token/validate来验证发送的令牌时,就会出现问题。
curl --request POST \
--url http://localhost:12345/wp-json/jwt-auth/v1/token/validate \
--header 'Authorization: Bearer {{ token }}'我得到一个带有403状态代码的响应,下面的响应如下
{
"code": "jwt_auth_invalid_token",
"message": "Algorithm not allowed",
"data": {
"status": 403
}
}在做了一些调查之后,我意识到我有两个插件使用了类似的库,下面是
plugins
├── jwt-authentication-for-wp-rest-api
│ └── vendor # not exactly like this, but close enough
├── the-events-calendar
│ └── vendor下面是负责验证令牌的类和方法
<?php
/** Require the JWT library. */
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
class Jwt_Auth_Public
{
...
/**
* Main validation function, this function try to get the Authentication
* headers and decoded.
*
* @param bool $output
*
* @return WP_Error | Object | Array
*/
public function validate_token($output = true)
{
...
/** Try to decode the token */
try {
header('Content-Type: text/html; charset=UTF-8');
$jwt = new ReflectionClass(JWT::class);
var_dump($token, $jwt->getFileName()); exit;
$token = JWT::decode(
$token,
new Key($secret_key, apply_filters('jwt_auth_algorithm', 'HS256'))
);
...
/** If the output is true return an answer to the request to show it */
return array(
'code' => 'jwt_auth_valid_token',
'data' => array(
'status' => 200,
),
);
} catch (Exception $e) {
/** Something is wrong trying to decode the token, send back the error */
return new WP_Error(
'jwt_auth_invalid_token',
$e->getMessage(),
array(
'status' => 403,
)
);
}
}
...
}您将注意到,我在方法中添加了几行代码,以尝试调试导致此问题的原因。
header('Content-Type: text/html; charset=UTF-8');
$jwt = new ReflectionClass(JWT::class);
var_dump($token, $jwt->getFileName()); exit;输出是
string(260) "{{ token }}"
string(89) "/var/www/hello-world/public_html/wp-content/plugins/the-events-calendar/common/vendor/firebase/php-jwt/src/JWT.php"而不是像这样
string(260) "{{ token }}"
string(131) "/var/www/hello-world/public_html/wp-content/plugins/jwt-authentication-for-wp-rest-api/includes/vendor/firebase/php-jwt/src/JWT.php"所以我想知道一个人能做些什么来解决这样的问题?
发布于 2022-10-04 14:34:19
我遇到了同样的错误,一个403错误“算法不允许”。
在插件文件夹中,您需要修改public > class-jwt-auth-public.php文件。
修改前第281行:
/** Try to decode the token */
try {
$token = JWT::decode(
$token,
new Key($secret_key, apply_filters('jwt_auth_algorithm', 'HS256'))
);修改后的第281行:
/** Try to decode the token */
try {
$token = JWT::decode(
$token,
$secret_key,
array_keys(JWT::$supported_algs)
);这不是最干净的解决方案,因为您基本上允许所有的JWT算法,但所有其他解决方案都不适合我!
我希望这就是你想要的:)
https://stackoverflow.com/questions/73866715
复制相似问题