[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:
- login :
http://localhost/wordpress/wp-json/jwt-auth/v1/token
- get user blogs:
http://localhost/wordpress/wp-json/myplugin/v1/blogs
- get get first blog token :
http://localhost/wordpress/wp-json/token/regain/2
,param2
is the user_blogid,get the return token,change the Angularjs saved user Token - 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.
[wordpress]wp-api-jwt-auth 尝试添加运行在多站点中 need change的更多相关文章
- wp api jwt 403 (Forbidden) -- JWT is not configurated properly, please contact the admin
需要在 wp-config.php 文件中配置 define('JWT_AUTH_SECRET_KEY', 'your-top-secrect-key'); 參考 403 (Forbidden) -- ...
- sharepoint 2013 文档库 资源管理器打开报错 在文件资源管理器中打开此位置时遇到问题,将此网站添加到受信任站点列表,然后重试。
我们在使用sharepoint 2013的文档库或者资源库的时候,经常会需要用到使用“资源管理器”来管理文档,但是有时候,点击“使用资源管理器打开”,会提示如下错误: 在文件资源管理器中打开此位置时遇 ...
- 利用WordPress REST API 开发微信小程序从入门到放弃
自从我发布并开源WordPress版微信小程序以来,很多WordPress网站的站长问有关程序开发的问题,其实在文章:<用微信小程序连接WordPress网站>讲述过一些基本的要点,不过仍 ...
- WordPress REST API 内容注入漏洞
1 WordPress REST API 内容注入漏洞 1.1 摘要 1.1.1 漏洞介绍 WordPress是一个以PHP和MySQL为平台的自由开源的博客软件和内容管理系统.在4.7.0版本后,R ...
- WordPress Option API(数据库储存 API)
WordPress Option API 是提供给开发者的数据库存储机制,通过调用函数,可以快速.安全的把数据存储到数据库里(都在 wp_options 表). 每个设置的模式是 key – valu ...
- WordPress版微信小程序开发系列(一):WordPress REST API
自动我发布开源程序WordPress版微信小程序以来,很多WordPress站长在搭建微信小程序的过程中会碰到各种问题来咨询我,有些问题其实很简单,只要仔细看看我写的文章,就可以自己解决.不过这些文章 ...
- .net core 3 web api jwt 一直 401
最近在给客户开发 Azure DevOps Exension, 该扩展中某个功能需要调用使用 .NET Core 3 写的 Web Api. 在拜读了 Authenticating requests ...
- 30.怎样在Swift中添加运行时属性?
和OC一样,Swift中也可以添加运行时属性.下面将提供一个完整的例子,演示如何给按钮点击事件添加运行时属性. 1.示例 import UIKit var s_GofButtonTouchDownKe ...
- Visual Studio 2008中添加运行按钮 转载
在Visual Studio 2008中添加运行按钮 默认情况下,VS2008中的工具栏上没有运行按钮,只有调试(Debug)按钮,可按照以下方法添加 1.点击菜单Tools(工具)->Cust ...
随机推荐
- Javascript模块规范(CommonJS规范&&AMD规范)
Javascript模块化编程(AMD&CommonJS) 前端模块化开发的价值:https://github.com/seajs/seajs/issues/547 模块的写法 查看 AMD规 ...
- Oracle用户、权限、角色管理
Oracle 权限设置一.权限分类:系统权限:系统规定用户使用数据库的权限.(系统权限是对用户而言). 实体权限:某种权限用户对其它用户的表或视图的存取权限.(是针对表或视图而言的). 二.系统权 ...
- JS瀑布流布局模式(2)
这个例子与上一篇类似,唯一的区别是排序的方式有差别.上一篇是在高度最小的列里插入内容,这个案例是按顺序放置内容. 两种方法各有优缺点.第一种需要在图片内容加载完成的情况下有效,各个列的图高度差异不大. ...
- STM32 使用 printf 发送数据配置方法 -- 串口 UART, JTAG SWO, JLINK RTT
STM32串口通信中使用printf发送数据配置方法(开发环境 Keil RVMDK) http://home.eeworld.com.cn/my/space-uid-338727-blogid-47 ...
- ARM architectures
https://gitorious.org/freebsd/freebsd/raw/56c5165837bf08f50ca4a08c6b2da91f73852960:sys/arm/include/a ...
- Android Studio系列教程一--下载与安装
背景 相信大家对Android Studio已经不陌生了,Android Studio是Google于2013 I/O大会针对Android开发推出的新的开发工具,目前很多开源项目都已经在采用,Goo ...
- CodeForces 173C Spiral Maximum 记忆化搜索 滚动数组优化
Spiral Maximum 题目连接: http://codeforces.com/problemset/problem/173/C Description Let's consider a k × ...
- Codeforces Round #335 (Div. 2) A. Magic Spheres 水题
A. Magic Spheres Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.codeforces.com/contest/606/ ...
- Codeforces Round #308 (Div. 2) D. Vanya and Triangles 水题
D. Vanya and Triangles Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55 ...
- uoj #9. 【UTR #1】vfk的数据 水题
#9. [UTR #1]vfk的数据 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://uoj.ac/problem/9 Description ...