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.

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

报告相同问题?

悬赏问题

  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度