为什么不发送授权承载?

问题描述 投票:0回答:1

为了发送和接收授权载体,我确实读过此Correct way to set Bearer token with CURL和这个How to properly use Bearer tokens?这是我的代码:

$url = "http://www.example.com/phpinfo.php";
$data = array('long_url' => 'http://www.google.com');

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$header = array('Authorization: Bearer ffaaf96dd9');
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$response = curl_exec($ch);
curl_close($ch);
print($response);

[您看到我正在将其发送到我的phpinfo页面,但是在我的phpinfo中我没有看到$_SERVER['Authorization'],我在任何地方都没有看到我的令牌,我的代码有问题或者我应该检查什么? ?

编辑:example.com URL实际上是设置我的站点phpinfo页面。

php curl bearer-token
1个回答
0
投票

带有Bearer令牌的请求可以与oAuth跨服务器API功能一起使用。

oAuth是一个开放源代码框架,它允许在服务器之间创建安全的通信,而不会持续使用密码。

Bearer客户程序允许对信息数组进行编码,以进行用户身份验证和敏感数据传输。

取决于您需要使用它的什么,根据您的需要,GitHub上很可能已经存在某种插件或扩展。

例如,您可以为Wordpress安装oAuth&Rest_API和jwt-authentication-for-wp-rest-api插件,然后使用您自己的插件进行扩展。您将需要创建自定义令牌生成功能,接收URL点等。

然后,您将能够安全地发送/接收信息,例如,在Chrome / Safari浏览器扩展程序和您的Wordpress网站之间。

  1. WordPress网站上的示例接收网址:

               add_action( 'rest_api_init', function () {
                    //apply_filters( 'determine_current_user', true );
                    register_rest_route( 'humanai/v1', 'data', array(
    
                        'methods'  => 'POST',
                        'callback' => function($request){ 
                                global $wpdb;
                                $data = $request->get_params();
                                $query = array( 'meta_key' => 'hai-token', 'meta_value' => $data[0]['token'] );
                                $user_id = $wpdb->query('SELECT * FROM '.$wpdb->prefix.'usermeta WHERE meta_key = \'hai-token\' AND meta_value=\''. $data[0]['token'].'\'');
    

/ *请注意processing_function,您将使用它来处理请求并根据需要返回任何数据。* /

                                return processing_function($user_id, $request);
                        }

                    ) );
                ),12);
  1. 处理功能

         function processing_function($user_id, $request){
              $res = update_user_meta($user_id,'new_creadit_card_number',$request['new_creadit_card_number']);
         }
    
  2. 当然,您需要一个函数来控制Bearer令牌 ...承载令牌有一个名为Bearer的原因...因为它承载了信息,请在下面的示例中查看:

    function jwt_token($attr=null){
    
        $secret_key = defined('JWT_AUTH_SECRET_KEY') ? JWT_AUTH_SECRET_KEY : false;
    
        /** First thing, check the secret key if not exist return a error*/
        if (!$secret_key) {
            return new WP_Error(
                'jwt_auth_bad_config',
                __('JWT is not configured properly, please contact the admin', 'wp-api-jwt-auth'),
                array(
                    'status' => 403,
                )
            );
        }
        /** Try to authenticate the user with the passed credentials*/
        $user = wp_get_current_user();
    
        /** If the authentication fails return a error*/
        if (is_wp_error($user)) {
            $error_code = $user->get_error_code();
            return new WP_Error(
                '[jwt_auth] '.$error_code,
                $user->get_error_message($error_code),
                array(
                    'status' => 403,
                )
            );
        }
    
        /** Valid credentials, the user exists create the according Token */
        $issuedAt = time();
        $notBefore = apply_filters('jwt_auth_not_before', $issuedAt, $issuedAt);
        $expire = apply_filters('jwt_auth_expire', $issuedAt + (DAY_IN_SECONDS * 30), $issuedAt);
    
        $token = array(
            'iss' => get_bloginfo('url'),
            'iat' => $issuedAt,
            'nbf' => $notBefore,
            'exp' => $expire,
            'data' => array(
                'user' => array(
                    'id' => $user->data->ID,
                ),
            ),
        );
    
        require dirname(dirname(dirname(__FILE__))) . '/jwt-authentication-for-wp-rest-api/includes/vendor/autoload.php';
    
            /** Let the user modify the token data before the sign. */
            $token = JWT::encode(apply_filters('jwt_auth_token_before_sign', $token, $user), $secret_key);
    

/ *注意事项令牌已签名,现在将带有用户数据的对象创建到客户端。* /

            $data = array(
                'token' => $token,
                'user_email' => $user->data->user_email,
                'user_nicename' => $user->data->user_nicename,
                'user_display_name' => $user->data->display_name,
                'user_new_credit_card' => 'XXXX XXXX XXXX XXXX'  
            );

            /** Let the user modify the data before send it back */
            return apply_filters('jwt_auth_token_before_dispatch', $data, $user);

    }

请注意:] >>

这不是完整的功能,软件,也不是原始问题的完整解决方案。

所有信息均严格出于教育目的提供。

我强烈建议使用其他加密方法来保护敏感信息。

构建完整的功能/软件并面临新问题时,为什么不在下面的评论中将它们链接到新问题中? -我将在新的答案中为您提供尽可能多的帮助。

© www.soinside.com 2019 - 2024. All rights reserved.