【发布时间】:2019-01-28 17:06:19
【问题描述】:
我有一个使用 Bramus PHP 路由器并验证来自 Auth0 的 JWT 的 API 后端。这一切都很好,但我希望扩展功能并从 JWT 中获取其他信息,然后我可以将这些信息传递给 API 调用。
也就是说,当用户进行 API 调用时,他们会将用户 ID 作为 URL 中的变量发送。但是,这个值已经在 JWT 中,所以为了安全起见,我想将用户 ID 从 JWT 中提取出来,而不是通过 URL 传递给它。
这是我尝试使用的路由代码的 sn-p:
....
$router->before('GET|POST', '/api.*', function() use ($app) {
$userid = '12345';
});
// Check for read:messages scope
$router->before('GET', '/api/private-scoped', function() use ($app) {
if (!$app->checkScope('read:messages')){
header('HTTP/1.0 403 forbidden');
header('Content-Type: application/json; charset=utf-8');
echo json_encode(array("message" => "Insufficient scope."));
exit();
}
});
$router->get('/api/users/get-info/(\w+)', function($userid) use ($app) {
header('Content-Type: application/json; charset=utf-8');
echo json_encode($app->getUserInfo($userid));
});
$router->get('/api/users/test', function() {
header('Content-Type: application/json; charset=utf-8');
echo json_encode(array("user" => $userid));
});
....
当我访问/api/users/test 时,我得到以下回复:
{
"user": null
}
如何将变量$userid 传递到路由器中,以便在其他功能中使用它?
【问题讨论】: