【问题标题】:Refresh token unique刷新令牌唯一
【发布时间】:2015-03-24 12:52:38
【问题描述】:

晚上好:

我正在实施两条腿的 OAuth 2.0,我想知道如何生成“随机”且唯一的 refresh_token。

用户会发送一个refresh_token,这个token会在数据库中查找到这个token相关的用户。如何生成令牌以防止数据库中的冲突?

提前致谢

【问题讨论】:

    标签: oauth-2.0


    【解决方案1】:

    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);
        }
    }
    

    【讨论】:

    • 我之前看过该代码,但该代码不检查该令牌是否存在于与用户关联的数据库中。
    • 不,如果你想检查冲突,只需将它添加到 foreach 中以重新生成令牌......但是你为什么要检查冲突,refresh_tokens 仍然需要 client_id 和 client_secret。不用担心检查碰撞。
    • 我正在使用 client_credentials,所以 client_id 和 client_secret 对于所有涉及的应用程序都是相同的。不同的是用户名和密码,但遵循协议,刷新令牌将不再需要此数据
    • client_credentials 明确表示您不能使用刷新令牌。实际上我今天就这一点发布了一个问题......在规范中它说你不应该使用 refresh_token stackoverflow.com/questions/29233772/…
    • 是的。资源所有者和密码将是一个
    猜你喜欢
    • 2021-08-20
    • 2022-10-31
    • 2016-01-05
    • 2019-06-29
    • 1970-01-01
    • 2020-07-12
    • 2017-11-20
    • 1970-01-01
    • 2018-07-30
    相关资源
    最近更新 更多