【发布时间】:2015-03-24 12:52:38
【问题描述】:
晚上好:
我正在实施两条腿的 OAuth 2.0,我想知道如何生成“随机”且唯一的 refresh_token。
用户会发送一个refresh_token,这个token会在数据库中查找到这个token相关的用户。如何生成令牌以防止数据库中的冲突?
提前致谢
【问题讨论】:
标签: oauth-2.0
晚上好:
我正在实施两条腿的 OAuth 2.0,我想知道如何生成“随机”且唯一的 refresh_token。
用户会发送一个refresh_token,这个token会在数据库中查找到这个token相关的用户。如何生成令牌以防止数据库中的冲突?
提前致谢
【问题讨论】:
标签: oauth-2.0
PHPLeague OAuth2 库使用辅助类生成随机密钥。
如果您使用的是 PHP,请查看:openssl_random_pseudo_bytes()
https://github.com/thephpleague/oauth2-server/blob/master/src/Util/SecureKey.php
具体来说:
class DefaultAlgorithm implements KeyAlgorithmInterface
{
public function generate($len = 40)
{
$stripped = '';
do {
$bytes = openssl_random_pseudo_bytes($len, $strong);
// We want to stop execution if the key fails because, well, that is bad.
if ($bytes === false || $strong === false) {
throw new \Exception('Error Generating Key');
}
$stripped .= str_replace(['/', '+', '='], '', base64_encode($bytes));
} while (strlen($stripped) < $len);
return substr($stripped, 0, $len);
}
}
【讨论】: