首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误的类被调用

错误的类被调用
EN

Stack Overflow用户
提问于 2022-09-27 11:18:48
回答 1查看 150关注 0票数 3

我的代码出现了一个奇怪的问题。

我目前正在从事一个WordPress项目,其中涉及大量插件,但我关注的主要插件是WP的JWT认证,因为我正在使用它来实现REST身份验证。

当您将登录凭据发送到端点/jwt-auth/v1/token时,一切都很好,因为返回一个令牌和一些用户信息。

代码语言:javascript
复制
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来验证发送的令牌时,就会出现问题。

代码语言:javascript
复制
curl --request POST \
  --url http://localhost:12345/wp-json/jwt-auth/v1/token/validate \
  --header 'Authorization: Bearer {{ token }}'

我得到一个带有403状态代码的响应,下面的响应如下

代码语言:javascript
复制
{
    "code": "jwt_auth_invalid_token",
    "message": "Algorithm not allowed",
    "data": {
        "status": 403
    }
}

在做了一些调查之后,我意识到我有两个插件使用了类似的库,下面是

代码语言:javascript
复制
plugins
├── jwt-authentication-for-wp-rest-api
│   └── vendor # not exactly like this, but close enough
├── the-events-calendar
│   └── vendor

下面是负责验证令牌的类和方法

代码语言:javascript
复制
<?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,
                )
            );
        }
    }

    ...
}

您将注意到,我在方法中添加了几行代码,以尝试调试导致此问题的原因。

代码语言:javascript
复制
header('Content-Type: text/html; charset=UTF-8');
$jwt = new ReflectionClass(JWT::class);
var_dump($token, $jwt->getFileName()); exit;

输出是

代码语言:javascript
复制
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"

而不是像这样

代码语言:javascript
复制
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"

所以我想知道一个人能做些什么来解决这样的问题?

EN

回答 1

Stack Overflow用户

发布于 2022-10-04 14:34:19

我遇到了同样的错误,一个403错误“算法不允许”。

在插件文件夹中,您需要修改public > class-jwt-auth-public.php文件。

修改前第281行:

代码语言:javascript
复制
        /** Try to decode the token */
    try {
        $token = JWT::decode(
            $token,
            new Key($secret_key, apply_filters('jwt_auth_algorithm', 'HS256'))
        );

修改后的第281行:

代码语言:javascript
复制
        /** Try to decode the token */
    try {
        $token = JWT::decode(
            $token,
            $secret_key, 
            array_keys(JWT::$supported_algs)
        );

这不是最干净的解决方案,因为您基本上允许所有的JWT算法,但所有其他解决方案都不适合我!

我希望这就是你想要的:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73866715

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档