2016-04-21 239 views
1

我正在使用Slim Framework创建无状态REST API。在使用它之前,我在服务器端创建了一个SESSION,并在每个页面上进行会话检查。但现在,我不知道如何控制它。令牌身份验证Rest API会话

我在每个用户的数据库中都有一个api_key。在用户登录后,我用api_key回应并将用户重定向到index.php。但api_key不被保留。我怎样才能将api_key传递给每个使用Javascript的页面?原因是如果有人想要从我的REST API获取数据,他们必须向我发送一个api_key,并且如果用户登录之前我不想再次显示登录页面。

这里是我的REST API的一部分:

$app->post('/userlogin', function() use ($app) { 
    verifyRequiredParams(array('email', 'password')); 
    $email = $app->request->post('email'); 
    $password = $app->request->post('password'); 

    $objUserRegLog = new UserRegistrationLogin; 
    $result = $objUserRegLog->getUserByEmailAndPassword($email, $password); 
    if (!$result) { 
     $response["error"] = true; 
     $response["message"] = "Error! Invalid e-mail address or password."; 
    } else { 
     $response["error"] = false; 
     $response["id"] = $result["id"]; 
     $response["email"] = $result["email"]; 
     $response["api_key"] = $result["api_key"]; 
    } 
    echoResponse(200, $response); 
}); 

$app->get('/students', 'authenticateStudent', function() use ($app) { 
    $objStd = new Students; 
    $result = $objCases->getAllStudents(); 
    if (!$result) { 
     $response["error"] = true; 
     $response["error_msg"] = "An error occured."; 
     $status_code = 404; 
    } else { 
     $response["error"] = false; 
     $response["cases"] = $result; 
     $status_code = 200; 
    } 
    echoResponse($status_code, $response); 
}); 

function authenticateStudent(\Slim\Route $route) { 
    $headers = apache_request_headers(); 
    $response = array(); 
    $app = \Slim\Slim::getInstance(); 

    if (isset($headers['Authorization'])) { 
     $db = new DbOperation(); 
     $api_key = $headers['Authorization']; 
     if (!$db->isValidStudent($api_key)) { 
      $response["error"] = true; 
      $response["message"] = "Access Denied. Invalid Api key"; 
      echoResponse(401, $response); 
      $app->stop(); 
     } 
    } else { 
     $response["error"] = true; 
     $response["message"] = "Api key is misssing"; 
     echoResponse(400, $response); 
     $app->stop(); 
    } 
} 

而且随着AJAX调用:

$.signin = function() { 
    var inputVals = $("#form_signin").serialize(); 
    $.ajax({ 
     url : "api/v1/userlogin", 
     data : inputVals, 
     dataType : "json", 
     type : "post", 
     success : function(response) { 
      if (response.error) { 
       $(".popup").trigger("click"); 
       $(".modal-title").html(response.message_title); 
       $(".modal-body").html(response.message); 
      } else { 
       window.location.href = "index.php"; 
      } 
      console.log(response); 
     } 
    }); 
    return false; 
} 
+2

你为什么不使用cookies? –

+0

@HéctorValverde Pareja你能举个例子吗? Cookies是否安全? – user3142206

+0

@ user3142206如果您信任用户,则Cookie是安全的。对于令牌,它是完全没问题的。 – rottenoats

回答

0

那么,你需要了解每一个客户端发送到服务器的要求是独立的,所以您需要在客户端系统中放置一个变量(令牌),以便让他在每个请求中发送它,以便您知道谁始终与您的服务器通话。开始阅读:http://www.w3schools.com/php/php_cookies.asp

一旦您了解了cookie的工作原理和方式,请尝试详细了解身份验证和授权主题。

0

您有三种方式提供此类信息。

饼干

在大多数情况下,如果你有一个登录界面,想要由赫克托提到使用Cookie。一个cookie的潜在问题,有些人不希望他们从“随机”网站。然而,好处是你可以关闭你的标签,重新打开它,并且你仍然可以登录(这取决于cookie的类型,但是在大多数情况下你想使用这种类型)。

查询字符串

的另一种方式,乏味,是增加一个查询字符串参数与会话标识符。这也意味着您必须确保您网页上的每个单一链接都包含该会话标识符。此外,它通常被视为一个安全问题,因为看着你肩膀的人可以看到会话标识符(坦率地说,除非他们有摄影记忆......)对于阻止cookies的人来说,这是另一种方式。但是,由于它使用的是完全相同类型的会话标识符,因此您可能会侵犯用户在阻止Cookie时意味着什么。

HTML代码

最后的方式,这需要更多的工作,就是把值在HTML。例如,您可以使用<meta ...>标签。至少这样,它隐藏在看着你肩膀的人身上。但是,当某人点击你的链接时,你仍然需要传送它。这意味着您必须使用JavaScript在POST中加载任何其他页面。这是非常规的。就像之前的方法一样。您可能会侵犯用户“请勿追踪”的意愿。

安全性怎么样?

最安全的方法是使用标记为仅HTTP的cookie与HTTPS连接(即JavaScript无法访问它,因此无法使用它进行调节。)

所有其他方法都有其他安全问题。