Ejemplo n.º 1
0
Archivo: JWT.php Proyecto: alpfish/alp
 /**
  * Decodes a JWT string into a PHP object.
  *
  * @param string            $jwt            The JWT
  * @param string|array|null $key            The key, or map of keys.
  *                                          If the algorithm used is asymmetric, this is the public key
  * @param array             $allowed_algs   List of supported verification algorithms
  *                                          Supported algorithms are 'HS256', 'HS384', 'HS512' and 'RS256'
  *
  * @return object The JWT's payload as a PHP object
  *
  * @throws DomainException              Algorithm was not provided
  * @throws UnexpectedValueException     Provided JWT was invalid
  * @throws SignatureInvalidException    Provided JWT was invalid because the signature verification failed
  * @throws BeforeValidException         Provided JWT is trying to be used before it's eligible as defined by 'nbf'
  * @throws BeforeValidException         Provided JWT is trying to be used before it's been created as defined by 'iat'
  * @throws ExpiredException             Provided JWT has since expired, as defined by the 'exp' claim
  *
  * @uses jsonDecode
  * @uses urlsafeB64Decode
  */
 public static function decode($jwt, $key, $allowed_algs = array())
 {
     // 检查密钥
     if (empty($key)) {
         throw new InvalidArgumentException('Key may not be empty');
         // 500 设置错误
     }
     // 检查服务器算法
     if (!is_array($allowed_algs)) {
         throw new InvalidArgumentException('alg算法设置错误');
         // 500 设置错误
     }
     // 分解成数组
     $tks = explode('.', $jwt);
     if (count($tks) != 3) {
         throw new UnexpectedValueException('Wrong number of segments');
         // 4XX JWT无效
     }
     // 头解码
     list($headb64, $bodyb64, $cryptob64) = $tks;
     if (null === ($header = JWT::jsonDecode(JWT::urlsafeB64Decode($headb64)))) {
         throw new UnexpectedValueException('Invalid header encoding');
         // 4XX JWT无效
     }
     // 体解码
     if (null === ($payload = JWT::jsonDecode(JWT::urlsafeB64Decode($bodyb64)))) {
         throw new UnexpectedValueException('Invalid claims encoding');
         // 4XX JWT无效
     }
     $sig = JWT::urlsafeB64Decode($cryptob64);
     // 检查算法
     if (empty($header->alg)) {
         throw new DomainException('Empty algorithm');
         // 4XX JWT无效
     }
     if (empty(self::$supported_algs[$header->alg])) {
         throw new DomainException('Algorithm not supported');
         // 4XX JWT无效
     }
     if (!is_array($allowed_algs) || !in_array($header->alg, $allowed_algs)) {
         throw new DomainException('Algorithm not allowed');
         // 500 或 4xx
     }
     // 检查密钥数组
     if (is_array($key) || $key instanceof \ArrayAccess) {
         if (isset($header->kid)) {
             $key = $key[$header->kid];
         } else {
             throw new DomainException('"kid" empty, unable to lookup correct key');
             // 500 或 4XX
         }
     }
     // Check the signature
     // 验证密钥
     if (!JWT::verify("{$headb64}.{$bodyb64}", $sig, $key, $header->alg)) {
         throw new SignatureInvalidException('Signature verification failed');
         // 4XX JWT无效
     }
     // Check if 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.
     // 检查nbf(不能在这之前使用)
     if (isset($payload->nbf) && $payload->nbf > time() + self::$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).
     // 检查时钟偏移(创建后的JWT在$leeway秒内不能使用)
     if (isset($payload->iat) && $payload->iat > time() + self::$leeway) {
         throw new BeforeValidException('Cannot handle token prior to ' . date(DateTime::ISO8601, $payload->iat));
     }
     // 检查JWT是否过期, 以服务器设置为先
     $exp = isset($payload->exp) ? $payload->exp : 7 * 24 * 60 * 60;
     $exp = config('jwt.claims.exp') ? config('jwt.claims.exp') : $exp;
     if (isset($payload->iat) && $payload->iat + $exp < time()) {
         throw new ExpiredException('Expired token');
     }
     return $payload;
 }