WordPress:自定义WP REST API (WP API)授权

渐渐地,那原本被夜幕笼罩的天空出现了微明。一颗颗启明星逐渐变得苍白无力,在浅浅的日光的照射下,胆怯的它们终于退却了。随着启明星的消失,害羞的太阳射出了几道耀眼的金光。

WP REST API (WP API) 是一个WordPress插件,其用途是为 WordPress 核心添加一个 JSON REST API , 以便于像移动应用之类的应用与 WordPress 进行交互。

WP-API 是可扩展的,并且具有齐备的文档,如果你使用 WP REST API (WP API) ,你很可能会被授权问题所困扰。

WP REST API (WP API) 能让你创建、编辑获取文章(各种WordPress内置的文章类型的文章以及自定义类型的文章)、创建、编辑和获取用户等,因此,它在某些情形下需要认证(授权),比如创建和编辑操作,就绝对需要授权才行,否则,处理申请会被 WordPress 拒绝。

据 WP REST API (WP API) 的认证(授权)文档来看,认证方式有三种:cookie、oauth和简单认证。本文记录如何实现自定义认证。

据WordPress 官方开发记录显示:这个插件很可能会在2015年4月22日发布的 WordPress 4.2 版本中加入到WordPress核心,那样的话,授权方式可能会有所改变,但不会大变。

举个简单的例子: 某个用户通过手机拍了一张照片,想上传到某个启用了WP REST API (WP API) 的 WordPress 网站,那么,就需要认证了吧,那么,怎么做呢?

下面将说一种用于此种情形的认证方式。

用自定义 filter hook

在该插件目录 lib 下有个类文件 class-wp-json-server.php ,其中有这段儿:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/** 
 * Check the authentication headers if supplied 
 * 
 * @return WP_Error|null WP_Error indicates unsuccessful login, null indicates successful or no authentication provided 
 */ 
public function check_authentication() { 
    /** 
     * Pass an authentication error to the API 
     * 
     * This is used to pass a {@see WP_Error} from an authentication method 
     * back to the API. 
     * 
     * Authentication methods should check first if they're being used, as 
     * multiple authentication methods can be enabled on a site (cookies, 
     * HTTP basic auth, OAuth). If the authentication method hooked in is 
     * not actually being attempted, null should be returned to indicate 
     * another authentication method should check instead. Similarly, 
     * callbacks should ensure the value is `null` before checking for 
     * errors. 
     * 
     * A {@see WP_Error} instance can be returned if an error occurs, and 
     * this should match the format used by API methods internally (that is, 
     * the `status` data should be used). A callback can return `true` to 
     * indicate that the authentication method was used, and it succeeded. 
     * 
     * @param WP_Error|null|boolean WP_Error if authentication error, null if authentication method wasn't used, true if authentication succeeded 
     */ 
    return apply_filters( 'json_authentication_errors', null ); 
}

/** * Check the authentication headers if supplied * * @return WP_Error|null WP_Error indicates unsuccessful login, null indicates successful or no authentication provided */ public function check_authentication() { /** * Pass an authentication error to the API * * This is used to pass a {@see WP_Error} from an authentication method * back to the API. * * Authentication methods should check first if they're being used, as * multiple authentication methods can be enabled on a site (cookies, * HTTP basic auth, OAuth). If the authentication method hooked in is * not actually being attempted, null should be returned to indicate * another authentication method should check instead. Similarly, * callbacks should ensure the value is `null` before checking for * errors. * * A {@see WP_Error} instance can be returned if an error occurs, and * this should match the format used by API methods internally (that is, * the `status` data should be used). A callback can return `true` to * indicate that the authentication method was used, and it succeeded. * * @param WP_Error|null|boolean WP_Error if authentication error, null if authentication method wasn't used, true if authentication succeeded */ return apply_filters( 'json_authentication_errors', null ); }

基于上面的这个函数以及其被调用位置,我们可以加进去一个hook,以确认认证是否成功:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/** 
* WP JSON API 认证检查 
* @param  null 
* @return boolean     是否认证成功 
* @author suifengtec  coolwp.com 
*/ 
function coolwp_rest_api_auth_check( $result ){ 

   if( 
        !isset($_GET['id']) 
       // ||!isset($_GET['app_key']) 
        ||!isset($_GET['app_token']) 
        ||empty($_GET['id']) 
       // ||empty($_GET['app_key']) 
        ||empty($_GET['app_token']) 


    ){ 
         return false; 
    } 

    //获取从应用GET过来的用户id、app_key和app_token,当然了,你也可以只用一个去app_key和app_token中的任何一个去检查 
    $user_id =  (int)$_GET['id']; 
   // $app_key = sanitize_text_field($_GET['app_key']); 
    $app_token = sanitize_text_field($_GET['app_token']); 

    //查询app_key和app_token,当然了,你也可以自定义一种算法, 
    //$wp_key = get_user_meta( $user_id, 'app_key', true); 
    $wp_token = get_user_meta( $user_id, 'app_token', true); 


    //将从应用客户端获取到的值与数据库存储的值进行对比 
    if( 
        ( $wp_token == $app_token ) 
       // &&( $wp_key == $app_key ) 

    ){ 

       return true; 
    } 

    return false; 

} 
add_filter('json_authentication_errors', 'coolwp_rest_api_auth_check');

/** * WP JSON API 认证检查 * @param null * @return boolean 是否认证成功 * @author suifengtec coolwp.com */ function coolwp_rest_api_auth_check( $result ){ if( !isset($_GET['id']) // ||!isset($_GET['app_key']) ||!isset($_GET['app_token']) ||empty($_GET['id']) // ||empty($_GET['app_key']) ||empty($_GET['app_token']) ){ return false; } //获取从应用GET过来的用户id、app_key和app_token,当然了,你也可以只用一个去app_key和app_token中的任何一个去检查 $user_id = (int)$_GET['id']; // $app_key = sanitize_text_field($_GET['app_key']); $app_token = sanitize_text_field($_GET['app_token']); //查询app_key和app_token,当然了,你也可以自定义一种算法, //$wp_key = get_user_meta( $user_id, 'app_key', true); $wp_token = get_user_meta( $user_id, 'app_token', true); //将从应用客户端获取到的值与数据库存储的值进行对比 if( ( $wp_token == $app_token ) // &&( $wp_key == $app_key ) ){ return true; } return false; } add_filter('json_authentication_errors', 'coolwp_rest_api_auth_check');

结论

加入 rest api 的 WordPress 甚至可以让你做一个在线支付网站,有了这组 api ,基于 WordPress 的原生安卓应用和IOS应用可以更好的与 WordPress 站点进行交互。

到此这篇关于WordPress:自定义WP REST API (WP API)授权就介绍到这了。你不能决定生命的长短,但你可以控制它的质量;你不能左右天气,但你可以改变心情;你不能改变容貌,但你可以展现笑容;你不能控制他人,但你可以掌握自己;你不能预知明天,但你可以掌握今天;你不能样样胜利,但你可以事事尽心。更多相关WordPress:自定义WP REST API (WP API)授权内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!

您可能有感兴趣的文章
WordPress站点Gravatar头像前后台不显示的如何解决办法

WordPress做公司官网好吗?会不会显得档次很低?

WordPress主题需要支持https吗?WordPress站点如何如何实现https?

WordPress站点的页面/标签/分类URL地址如何添加.html?

WordPress站点更换了域名后数据库应该如何操作替换新旧域名?