我有一个 web 应用程序可以对用户的 flickr api 进行身份验证。它使用 OAuth 1.0,大部分时间都可以进行身份验证。但是在随机的时间,在完全随机的时间,flickr 无法识别我的签名。它在请求请求 token 时返回无效签名错误。但是下一次尝试中的相同代码将正确地返回请求 token 。
我怀疑这与我生成随机数或时间戳的方式有关。否则它不应该在连续的尝试中起作用,对吧?
这就是我生成随机数和时间戳值的方式:
$nonce = md5(microtime(true).rand());
$timestamp = mktime();
这有什么问题吗?有没有更好的方法来生成 nonce 值?这种随机故障非常令人困惑。我想不出任何其他原因导致我收到无效错误,随机!
跟进
根据 Jan Gerlinger 的建议,我将 mktime() 更改为 time()。它肯定减少了发生的频率。但它仍然会在随机时间给出无效签名错误,在更改为 time() 后很少见,我可能会补充。
因此,我假设 timestamp(mktime) 是导致这些随机错误的问题之一。但是那里还有其他问题。也许在 nonce 生成中?
最佳答案
Depending on your PHP version , 你应该使用 time()
而不是 mktime()
。
对于随机数,documentation说:
A nonce is a random string, uniquely generated by the client to allow the server to verify that a request has never been made before and helps prevent replay attacks when requests are made over a non-secure channel. The nonce value MUST be unique across all requests with the same timestamp, client credentials, and token combinations.
如果rand()
将在一微秒内两次返回相同的值,您将两次获得相同的随机数。由于 MD5 冲突,生成 MD5 哈希也不能保证您获得唯一值。这里更好的方法是使用 global counter for the nonce .
但是,如果您经常遇到此错误,则 nonce 可能不是您的问题,因为这些冲突不应该经常发生。
有时 invalid signature 错误的问题是客户端的服务器时间与提供者的服务器时间不同,因此您可以通过时间同步检查您的服务器上是否发生了奇怪的事情。
关于php - flickr oauth api 随机返回签名无效错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12598864/