duanchi6377 2016-10-28 08:48
浏览 57
已采纳

使用Slim 3在app运行之前验证usertoken

I am using Slim Framework 3 to make a small internal API to get fetch facebook data. There is about 30 specific users which have access to the API.

I want to authenticate a user by a user token send from the website, and that token is to be checked before the app is run.

The token on the user is set in the DB and when the user is requesting the API a token is send with a GET and if there is a match on the DB and the GET token, the user should be granted access to the API, otherwise the user should be forbidden to access.

I am using this to get facebook data:

$app->get('/fbdata/campaign/{campaign}/bankarea/{bankarea}/from/{from}/to/{to}/utoken/{utoken}', function(Request $request, Response $response) {

    $bd = new BankAppData();
    $getFb = new GetFacebookData();

    $bankarea = $request->getAttribute('bankarea');
    $campaign = $request->getAttribute('campaign');

    $appid = $bd->BankData($bankarea)->appid;
    $appsecret = $bd->BankData($bankarea)->appsecret;
    $fbtoken = $bd->BankData($bankarea)->fbtoken;

    $dateFrom = $request->getAttribute('from');
    $dateTo = $request->getAttribute('to');

    $getFb->FetchData($appid, $appsecret, $fbtoken, $campaign, $bankarea, "act_XXXX", $dateFrom, $dateTo);


});

This works just fine, but I want to use a AuthenticationHandler class for checking the utoken before the above is run.

I am adding it by using $app->add(new SNDB\AuthenticationHandler()); but I am unsure on how I can get the utoken from the URL in my AuthenticationHandler class.

Basically I want to do something like

function Authenticate() {

   if($dbToken != $utoken) {
      //No access - app will just stop doing anything else
   } else {
     //You have access - just continue what you was trying to do
   }

}
  • 写回答

1条回答 默认 最新

  • dongluo6343 2016-10-28 11:29
    关注

    You should take a look at the middleware concept from slim3.

    Basically there are 2 options how to add middleware:

    1. per anonymous function

      $app->add(function ($request, $response, $next) {
          $response->getBody()->write('BEFORE');
          $response = $next($request, $response);
          $response->getBody()->write('AFTER');
      
          return $response;
      });
      
    2. per invokable class

      class ExampleMiddleware
      {
          public function __invoke($request, $response, $next)
          {
              $response->getBody()->write('BEFORE');
              $response = $next($request, $response);
              $response->getBody()->write('AFTER');
      
              return $response;
          }
      }
      $app->add(new ExampleMiddleware);
      

    There you have the PSR-7 request and can get your utoken from the url.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥15 pyqt信号槽连接写法
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。