重新验证不需要JWT刷新令牌

时间:2020-02-04 11:56:28

标签: oauth-2.0 jwt refresh-token jwt-auth

到目前为止,我正在使用短期令牌并刷新API身份验证令牌。我仅使用刷新令牌来获取用户ID来查询数据库,以检查用户的最新权限和活动/已阻止状态。现在,我在想为什么我不应该从短期令牌本身中提取此用户ID。以下功能用于对JWT进行解码,在此有效期经过签名验证后进行验证。因此,如果我收到“过期”错误,则意味着令牌签名是好的,令牌没有受到限制。现在,我可以从过期的JWT中提取中间(yyy从xxx.yyy.zzz中)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;
    }

1 个答案:

答案 0 :(得分:0)

不要这样做。您的计划违反了常规做法。它也违反了OAuth标准。新开发人员会认为这是一个错误。您的公司可能会聘请安全审核员,该人员将使用过期的令牌测试访问权限。这将被报告为您必须解决的问题。