我使用PHP的’hash_hmac()’创建签名.
这是一个API控制器的例子
class API_Tasks_Controller extends API_Controller { public $restful = true; public function get_index($ID = null) { $this->verfiy_request(); if(!is_null($ID)) { return Response::Json(array("tasks"=>"just one"),200); } else { return Response::Json(array("tasks"=>"everthing"),200); } }}
这是API控制器类
class API_Controller extends Controller { public function verify_request() { //user ID $user_ID = (int) input::get('user_ID'); //signature $sig = input::get('sig'); //Lookup user $user = Sentry::user($user_ID); if($user) { //user email $email = $user->email; //user API key $API_key = $user->Metadata['API_key']; //recreate signature $_sig = hash_hmac("sha256",$email.$user_ID,$API_key); if($_sig === $sig) { return Response::Json(array("message"=>"Request Ok"),200); } else { return Response::Json(array("message"=>"Request Bad"),400); } } else { return Response::Json(array("message"=>"Request not authorized"),401); } }
发出获取请求http://API.xyz.com/v1/tasks/1?user_ID=1\u0026amp;sig=41295da38eadfa56189b041a022c6ae0fdcbcd5e65c83f0e9aa0e6fbae666cd8即使在我更改了user_ID参数的值时也会返回成功的消息,该参数应使签名无效请求无效.
似乎我的verfiy_request方法没有执行.
请帮帮我
class API_Tasks_Controller extends Base_Controller { public $restful = true; function __construct() { // Check if user is authorized $this->filter('before','API_checkauth'); } // rest of the class ....}
在您的routes.PHP文件中:
Route::filter('API_checkauth',function(){ //user ID $user_ID = (int) input::get('user_ID'); //signature $sig = input::get('sig'); try { //Lookup user $user = Sentry::user($user_ID); if($user) { //user email $email = $user->email; //user API key $API_key = $user->Metadata['API_key']; //recreate signature $_sig = hash_hmac("sha256",$API_key); if($_sig === $sig) { return Response::Json(array("message"=>"Request Ok"),200); } else { return Response::Json(array("message"=>"Request Bad"),400); } } else { return Response::Json(array("message"=>"Request not authorized"),401); } } catch (Sentry\SentryException $e) { $errors = $e->getMessage(); // catch errors such as user not existing or bad fIElds return Response::Json(array("message"=>$errors),404); }});
另外,谢谢你介绍我哨兵:-)
总结以上是内存溢出为你收集整理的php – 使用laravel进行REST api身份验证全部内容,希望文章能够帮你解决php – 使用laravel进行REST api身份验证所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)