[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 ...
随机推荐
- PPAS上运行pg_dump经过
目前我有两台机器, 分别已经安装了PPAS9.1,安装后建立了OS系统用户enterprisedb和数据库用户enterprisedb. 机器1:master 192.168.10.88 机器2: ...
- Windows Server Backup 备份Hypver-V VM
在Windows Server 2012中,可以通过Windows Server Backup备份Hypver-V VM.在还原时,将会还原到Hypver-V管理器中. 设置只保存2个备份副本,命令如 ...
- python批量GBK转UTF-8
有时候编码问题在导入工程的时候很烦,所以还是让世界都是"UTF-8"吧. 抄来一段代码: #!/usr/env python # -*- coding: utf8 -*- impo ...
- linux scp 服务器远程拷贝
一.将本机文件复制到远程服务器上 #scp /home/administrator/news.txt root@192.168.1.1:/etc/squid /home/administrator/ ...
- hdu 5258 数长方形 离散化
数长方形 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5258 Des ...
- tomcat解压war包的一点例外
我在项目的开发过程中,发现Tomcat解压war 的一点例外. 现象如下: 使用ANT工具把web应用程序打包为war文件.然后把war文件放到tomcat的webapps,让tomca ...
- c语言全局变量和局部变量问题汇总
.局部变量是否能和全局变量重名? 答:能,局部会屏蔽全局.要用全局变量,须要使用"::" 局部变量能够与全局变量同名,在函数内引用这个变量时,会用到同名的局部变量,而不会用到全局变 ...
- Ubuntu15.04 安装Android开发环境
一.安装Java SE JKD A.下载 http://www.oracle.com/technetwork/java/javase/downloads/index.html?ssSourceSite ...
- Linux scp 使用详解
一般情况,本地网络跟远程网络进行数据交抱,或者数据迁移,常用的有三种方法,一是ftp,二是wget /fetch 三是,rsync 大型数据迁移用rysync,其次用fetch/wget ,最次是ft ...
- UIView的生命周期
一. 大体流程: (loadView/nib)文件来加载view到内存-->viewDidLoad函数进一步初始化这些view-->内存不足时, 调用viewDidUnload函数释放vi ...