Oauth 2.0 重新身份验证不需要JWT刷新令牌

Oauth 2.0 重新身份验证不需要JWT刷新令牌,oauth-2.0,jwt,refresh-token,jwt-auth,Oauth 2.0,Jwt,Refresh Token,Jwt Auth,到目前为止,我正在为API身份验证使用短期令牌和刷新令牌。我使用刷新令牌仅用于获取用户id以查询数据库,以检查用户的最新权限和活动/阻止状态。现在我在想,为什么不从短liven令牌本身提取这个用户id呢。下面的函数用于解码JWT,在签名验证后验证此过期。所以,若我得到“expiration”错误,这意味着令牌签名是好的,令牌是未回火的。现在我可以从过期的JWT中提取中间(xxx.yyy.zzz中的yyy)base64编码数据以获取用户id。所以我认为使用刷新令牌不值得。还可以在令牌本身中定义更长

到目前为止,我正在为API身份验证使用短期令牌和刷新令牌。我使用刷新令牌仅用于获取用户id以查询数据库,以检查用户的最新权限和活动/阻止状态。现在我在想,为什么不从短liven令牌本身提取这个用户id呢。下面的函数用于解码JWT,在签名验证后验证此过期。所以,若我得到“expiration”错误,这意味着令牌签名是好的,令牌是未回火的。现在我可以从过期的JWT中提取中间(xxx.yyy.zzz中的yyy)base64编码数据以获取用户id。所以我认为使用刷新令牌不值得。还可以在令牌本身中定义更长的访问时间,只需自定义时间戳,这样我就可以在一个令牌中定义两个时间限制,例如5分钟和90天。你的想法是什么

public static function decode($jwt, $key, array $allowed_algs = array())
    {
        $timestamp = is_null(static::$timestamp) ? time() : static::$timestamp;

        if (empty($key)) {
            throw new InvalidArgumentException('Key may not be empty');
        }
        $tks = explode('.', $jwt);
        if (count($tks) != 3) {
            throw new UnexpectedValueException('Wrong number of segments');
        }
        list($headb64, $bodyb64, $cryptob64) = $tks;
        if (null === ($header = static::jsonDecode(static::urlsafeB64Decode($headb64)))) {
            throw new UnexpectedValueException('Invalid header encoding');
        }
        if (null === $payload = static::jsonDecode(static::urlsafeB64Decode($bodyb64))) {
            throw new UnexpectedValueException('Invalid claims encoding');
        }
        if (false === ($sig = static::urlsafeB64Decode($cryptob64))) {
            throw new UnexpectedValueException('Invalid signature encoding');
        }
        if (empty($header->alg)) {
            throw new UnexpectedValueException('Empty algorithm');
        }
        if (empty(static::$supported_algs[$header->alg])) {
            throw new UnexpectedValueException('Algorithm not supported');
        }
        if (!in_array($header->alg, $allowed_algs)) {
            throw new UnexpectedValueException('Algorithm not allowed');
        }
        if (is_array($key) || $key instanceof \ArrayAccess) {
            if (isset($header->kid)) {
                if (!isset($key[$header->kid])) {
                    throw new UnexpectedValueException('"kid" invalid, unable to lookup correct key');
                }
                $key = $key[$header->kid];
            } else {
                throw new UnexpectedValueException('"kid" empty, unable to lookup correct key');
            }
        }

        // Check the signature
        if (!static::verify("$headb64.$bodyb64", $sig, $key, $header->alg)) {
            throw new SignatureInvalidException('Signature verification failed');
        }

        // Check the nbf if it is defined. This is the time that the
        // token can actually be used. If it's not yet that time, abort.
        if (isset($payload->nbf) && $payload->nbf > ($timestamp + static::$leeway)) {
            throw new BeforeValidException(
                'Cannot handle token prior to ' . date(DateTime::ISO8601, $payload->nbf)
            );
        }

        // Check that this token has been created before 'now'. This prevents
        // using tokens that have been created for later use (and haven't
        // correctly used the nbf claim).
        if (isset($payload->iat) && $payload->iat > ($timestamp + static::$leeway)) {
            throw new BeforeValidException(
                'Cannot handle token prior to ' . date(DateTime::ISO8601, $payload->iat)
            );
        }

        // Check if this token has expired.
        if (isset($payload->exp) && ($timestamp - static::$leeway) >= $payload->exp) {
            throw new ExpiredException('Expired token');
        }

        return $payload;
    }

不要这样做。你的计划违反了惯例。这也违反了OAuth标准。新开发人员会认为这是一个bug。您的公司可能会雇佣安全审计员,该人员将使用过期的令牌测试访问权限。这将被报告为您必须修复的问题。

好的,但您是否看到任何漏洞?我想删除刷新令牌以节省带宽。访问令牌总是有被破坏的风险。随着到期时间的缩短,风险降低(例如,如果某人从某个日志中获取昨天的令牌)。现在您可能会说,您可以拥有到期时间为5小时的访问令牌,或者另一个在5分钟后到期的访问令牌,但您可以接受它5小时。诚然,漏洞并没有区别,但您并没有遵循标准。