普通网友 2016-02-19 12:26
浏览 298

使用JWT Auth和现有的身份验证系统

So, this is the first time that I am trying to implement auth in my API. I already had an existing authentication system - Cartalyst Sentinel 2.0 and now to add auth I am using JWT. What I have done is :

  • Send a token to the client end at the time of login.

  • Replaced the existing authentication sentinel middleware with a new middleware that does both authentication and authorization.

Original Middleware :

$authenticateForLogin = function ($app, $user) {
    return function () use ($app, $user) {          
      if ( Sentinel::check() === false ) {              
          $app->response()->status(401);
                    $app->stop();
      }
    };
};

New Middleware :

$checkForAuthorization = function ($app, $user) {
    return function () use ($app, $user) {      
      if ( Sentinel::check() === false ) {          
          $app->response()->status(401);
          $app->stop();
      } else {

        $authHeader = apache_request_headers()["Authorization"];
        if ($authHeader) {
          $jwt = str_replace("Authorization: Bearer ", "", $authHeader);
          if ($jwt) {
              try {
                  $secretKey = base64_decode(getDbConfig()["AUTH_SECRET"]);                      
                  $token = JWT::decode($jwt, $secretKey, array('HS512'));                  
                  header('Content-type: application/json');
                  echo json_encode([
                      'message'    => "Auth Test Successful"
                  ]);    
              } catch (Exception $e) {
                 //some action
              }
          } else {
             //some action
          }
        } else {
          //some action
        }
      }      
    };
};

So my questions are -

  • Is this the right approach ? I am under the impression that authentication and authorization are two separate processes. Are there any security flaws to this ?

  • Sentinel authentication is cookies, session based system. So is it good to use something like JWT on the top of it ? Or should I do authentication also using JWT (don't know how yet) ?

I have lot of doubts regarding Auth/JWT. But these come first.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 DIFY API Endpoint 问题。
    • ¥20 sub地址DHCP问题
    • ¥15 delta降尺度计算的一些细节,有偿
    • ¥15 Arduino红外遥控代码有问题
    • ¥15 数值计算离散正交多项式
    • ¥30 数值计算均差系数编程
    • ¥15 redis-full-check比较 两个集群的数据出错
    • ¥15 Matlab编程问题
    • ¥15 训练的多模态特征融合模型准确度很低怎么办
    • ¥15 kylin启动报错log4j类冲突