【问题标题】:Authenticate usertoken before app run with Slim 3在使用 Slim 3 运行应用程序之前验证用户令牌
【发布时间】:2016-10-28 23:42:32
【问题描述】:

我正在使用 Slim Framework 3 制作一个小的内部 API 来获取 facebook 数据。大约有 30 个特定用户可以访问 API。

我想通过从网站发送的用户令牌对用户进行身份验证,并且要在应用程序运行之前检查该令牌。

用户的令牌是在数据库中设置的,当用户请求 API 时,会使用 GET 发送令牌,如果数据库和 GET 令牌匹配,则应授予用户访问权限API,否则应禁止用户访问。

我正在使用它来获取 Facebook 数据:

$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);


});

这很好用,但我想在上面运行之前使用AuthenticationHandler 类来检查utoken

我正在使用 $app->add(new SNDB\AuthenticationHandler()); 添加它,但我不确定如何从 AuthenticationHandler 类中的 URL 获取 utoken。

基本上我想做类似的事情

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
   }

}

【问题讨论】:

    标签: php rest authentication token slim


    【解决方案1】:

    你应该看看middleware concept from slim3

    添加中间件基本上有两种选择:

    1. anonymous function

      $app->add(function ($request, $response, $next) {
          $response->getBody()->write('BEFORE');
          $response = $next($request, $response);
          $response->getBody()->write('AFTER');
      
          return $response;
      });
      
    2. 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);
      

    你有PSR-7 request 并且可以从网址获取你的utoken

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-29
      • 1970-01-01
      • 2017-04-29
      • 2015-11-03
      • 2022-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多