【发布时间】:2015-05-27 04:06:50
【问题描述】:
在编写用于我们内部系统的简单 API 时,我正在寻找一种体面的身份验证方法。 Stack Overflow 上的其他问题建议使用 HMAC 以及指向教程的链接,我继续并决定实施。
设置后,我意识到我不确定这对于实际身份验证有多大意义。使用的教程Here 列出了客户端上的一个公共哈希,该哈希从未在服务器端代码中使用。它只是将内容和 privateHash 值散列在一起,并在服务器上进行比较。由于这一切都通过标头传递,我想知道这实际上有多安全?什么是 publicHash 值,因为它甚至似乎没有被使用?
客户:
<?php
$publicHash = '3441df0babc2a2dda551d7cd39fb235bc4e09cd1e4556bf261bb49188f548348';
$privateHash = 'e249c439ed7697df2a4b045d97d4b9b7e1854c3ff8dd668c779013653913572e';
$content = json_encode( array( 'test' => 'content' ) );
$hash = hash_hmac('sha256', $content, $privateHash);
$headers = array(
'X-Public: '.$publicHash,
'X-Hash: '.$hash
);
$ch = curl_init('http://domain.com/api2/core/device/auth');
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$content);
$result = curl_exec($ch);
curl_close($ch);
echo "RESULT\n======\n".print_r($result, true)."\n\n";
?>
服务器
function auth()
{
$app = \Slim\Slim::getInstance();
$request = $app->request();
$publicHash = $request->headers('X-Public');
$contentHash = $request->headers('X-Hash');
$privateHash = 'e249c439ed7697df2a4b045d97d4b9b7e1854c3ff8dd668c779013653913572e';
$content = $request->getBody();
$hash = hash_hmac('sha256', $content, $privateHash);
if ($hash == $combinedHash)
{
$data = array('status' => "success");
response($data);
}
else
{
$data = array('status' => "failed");
response($data);
}
}
【问题讨论】:
标签: authentication hash oauth-2.0 slim hmac