【发布时间】:2014-06-05 12:58:18
【问题描述】:
我尝试从 PHP 中的 OAuth 1.0 开始,但遇到了奇怪的问题。我创建了伪消费者,它根据规范生成签名,并通过 POST 将使用的参数发送给提供者。消费者用途:
$oauth_consumer_key = '123';
$oauth_consumer_secret = '456';
$oauth_signature_method = 'HMAC-SHA1';
$oauth_timestamp = time();
$oauth_nonce = uniqid();
$oauth_version = '1.0';
$oauth_callback = 'http://localhost/oauth/callback';
$oauth = new OAuth($oauth_consumer_key, $oauth_consumer_secret);
$oauth->enableDebug();
$oauth_signature = $oauth->generateSignature('POST', $oauth_callback, array($oauth_consumer_key, $oauth_signature_method, $oauth_timestamp, $oauth_nonce, $oauth_version));
在提供者方面,一切似乎都按预期工作。收到所有值:
object(OAuthProvider)[1]
public 'consumer_key' => string '123' (length=3)
public 'consumer_secret' => string '456' (length=3)
public 'nonce' => string '5390610001c90' (length=13)
public 'token' => null
public 'token_secret' => null
public 'timestamp' => string '1401970944' (length=10)
public 'version' => string '1.0' (length=3)
public 'signature_method' => string 'HMAC-SHA1' (length=9)
public 'callback' => string 'http://localhost/oauth/callback' (length=31)
public 'request_token_endpoint' => boolean true
public 'signature' => string '8lNbnGTOen4TEOHS9KcpgCiBl+M=' (length=28)
但这是蜜月期的结束 - 尝试验证签名会导致错误:signature_invalid。这是我在 Providers 端使用的:
$provider = new OAuthProvider();
$provider->isRequestTokenEndpoint(true);
$provider->consumerHandler('lookupConsumer');
$provider->timestampNonceHandler('timestampNonceChecker');
try
{
$request_verified = $provider->checkOAuthRequest();
}
catch(OAuthException $e)
{
echo $provider->reportProblem($e);
}
以及我收到的问题报告:
oauth_problem=signature_invalid&debug_sbs=POST&http%3A%2F%2Flocalhost%2Foauth%2Fcustom_auth%2Frequest_token.php&oauth_callback%3Dhttp%253A%252F%252Flocalhost%252Foauth%252Fcallback%26oauth_consumer_key%3D123%26oauth_nonce%3D5390610001c90%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1401970944%26oauth_version%3D1.0
另外让我感到困惑的是,当我将 generateSignature 用于相同的常量参数(为了调试我将时间戳和随机数设置为常量值)时,它每次都会给我不同的值,比如如果还有是一些我不知道的随机元素。作为验证样本 - hash_hmac 没有这样的问题。
是我遗漏了什么还是官方 PHP OAuth 实现 (http://pecl.php.net/package/oauth) 有问题?
【问题讨论】: