[wordpress]wp-api-jwt-auth 尝试添加运行在多站点中 need change

Hi,Thank you this plugin,because i use this plugin on Wordpress one Network,so the request other api's url will be change.
my step is:

  1. login : http://localhost/wordpress/wp-json/jwt-auth/v1/token
  2. get user blogs: http://localhost/wordpress/wp-json/myplugin/v1/blogs
  3. get get first blog token : http://localhost/wordpress/wp-json/token/regain/2,param 2 is the user_blogid,get the return token,change the Angularjs saved user Token
  4. get test blog posts : http://localhost/wordpress/test/wp-json/wp/v2/posts

step 2 api like:

function list_blogs($request ){
    $current_user = wp_get_current_user();
    $user_blogs = get_blogs_of_user( $current_user->ID );
    if(count($user_blogs)==0){
        return null;
    }
    else{
        return $user_blogs;
    }
}

add_action( 'rest_api_init', function () {
    register_rest_route( 'myplugin/v1', '/blogs', array(
        'methods' => 'GET',
        'callback' => 'list_blogs',
    ) );
} );

file public/class-jwt-auth-public.php,methodadd_api_routes(),i add

register_rest_route($this->namespace, 'token/regain/(?P<blog_id>[0-9]+)', [
    'methods' => 'POST',
    'callback' => array($this, 'regain_token'),
]);

i add method,code is:

/**
* regain the jwt auth for multiSite
* @param WP_REST_REQUEST $request
* 
* @return string token
*/
public function regain_token($request){
    $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 configurated properly, please contact the admin', 'wp-api-jwt-auth'),
            array(
                'status' => 403,
            )
        );
    }
    /** Second thing, check the user is logined if not exist return a error*/
    $current_user = wp_get_current_user();
    if (  0 == $current_user->ID ) {
        return new WP_Error( 'rest_not_logged_in', __( 'You are not currently logged in.' ), array( 'status' => 401 ) );
    }
    
    $url_params = $request->get_url_params();
    $param_blog_id = $url_params['blog_id'];
    /** Three thing, check the $param_blog_id belong to the logined user blogs list if not exist return a error*/
    $user_blogs = get_blogs_of_user($current_user->ID);
    $blog_details = null;
    $blog_ids = array();
    $blog_is_exist = false;
    foreach ($user_blogs AS $user_blog) {
       if($param_blog_id == $user_blog->userblog_id){
        $blog_details = $user_blog;
        $blog_is_exist = true;
       }
    }
    if(!$blog_is_exist){
        return new WP_Error( 'jwt_auth_user_not_have_current_blog', __( 'current user not have this blog.' ), array( 'status' => 400 ) );   
    }
    
     /** 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 * 7), $issuedAt);
    
    $token = array(
        'iss' => $blog_details->siteurl,
        'iat' => $issuedAt,
        'nbf' => $notBefore,
        'exp' => $expire,
        'data' => array(
            'user' => array(
                'id' =>$current_user->ID,
            ),
        ),
    );

    /** Let the user modify the token data before the restore. */
    $token = JWT::encode(apply_filters('jwt_auth_token_before_restore', $token), $secret_key);

    /** The token is signed,only return token */
    $data = array(
        'token' => $token
    );

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

the code many is use the generate_token() method code,I only want the logined usre not login again,so i try add this code.

转载于:https://www.cnblogs.com/fsong/p/5997171.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WP-REST-APIWordPress 的一种接口,它通过提供标准化的RESTful API,允许开发人员使用HTTP请求来访问和操作WordPress站点的内容和数据。通过这个接口,开发人员可以使用不同的编程语言和技术来与WordPress进行交互,从而使得开发更加灵活和自由。 JWT(JSON Web Token)是一种用于认证和授权的开放标准。它通过将用户信息和权限信息编码成一种加密的令牌,以实现跨服务器和跨域的身份验证。JWT 是由三部分组成的:头部、负载和签名。头部包含令牌的加密算法和类型信息,负载包含用户的相关信息,签名用于验证令牌的真实性和完整性。 WP-REST-API JWT整合了WordPress的REST APIJWT的认证机制,使得在使用WP-REST-API进行开发的过程,可以增加身份验证和授权的功能。它允许开发人员在请求WordPress REST API时,通过在请求头或参数提供有效的JWT令牌来验证用户的身份和权限,并根据令牌的负载信息来进行授权。 WP-REST-API JWT的使用具有很多优势。首先,它提供了一种轻量级的身份验证方式,减少了开发的复杂性。其次,通过JWT令牌的机制,可以实现无状态的认证和授权,提高了性能和可扩展性。此外,JWT还提供了一种可靠的机制来防止伪造和篡改请求数据,增强了系统的安全性。 总而言之,WP-REST-API JWT为开发人员提供了一种方便、灵活和安全的方式来使用WordPress的REST API。它简化了身份验证和授权的过程,并通过使用JWT令牌提高了系统的性能和安全性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值