当前位置: 首页>>代码示例>>PHP>>正文


PHP JWT::jsonDecode方法代码示例

本文整理汇总了PHP中JWT::jsonDecode方法的典型用法代码示例。如果您正苦于以下问题:PHP JWT::jsonDecode方法的具体用法?PHP JWT::jsonDecode怎么用?PHP JWT::jsonDecode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在JWT的用法示例。


在下文中一共展示了JWT::jsonDecode方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: decode

 /**
  * Decodes a JWT string into a PHP object.
  *
  * @param string      $jwt       The JWT
  * @param string|Array|null $key The secret key, or map of keys
  * @param bool        $verify    Don't skip verification process
  *
  * @return object      The JWT's payload as a PHP object
  * @throws UnexpectedValueException Provided JWT was invalid
  * @throws DomainException          Algorithm was not provided
  * 
  * @uses jsonDecode
  * @uses urlsafeB64Decode
  */
 public static function decode($jwt, $key = null, $verify = true)
 {
     $tks = explode('.', $jwt);
     if (count($tks) != 3) {
         throw new UnexpectedValueException('Wrong number of segments');
     }
     list($headb64, $bodyb64, $cryptob64) = $tks;
     if (null === ($header = JWT::jsonDecode(JWT::urlsafeB64Decode($headb64)))) {
         throw new UnexpectedValueException('Invalid segment encoding');
     }
     if (null === ($payload = JWT::jsonDecode(JWT::urlsafeB64Decode($bodyb64)))) {
         throw new UnexpectedValueException('Invalid segment encoding');
     }
     $sig = JWT::urlsafeB64Decode($cryptob64);
     if ($verify) {
         if (empty($header->alg)) {
             throw new DomainException('Empty algorithm');
         }
         if (is_array($key)) {
             if (isset($header->kid)) {
                 $key = $key[$header->kid];
             } else {
                 throw new DomainException('"kid" empty, unable to lookup correct key');
             }
         }
         if (!JWT::verify("{$headb64}.{$bodyb64}", $sig, $key, $header->alg)) {
             throw new UnexpectedValueException('Signature verification failed');
         }
         // Check token expiry time if defined.
         if (isset($payload->exp) && time() >= $payload->exp) {
             throw new UnexpectedValueException('Expired Token');
         }
     }
     return $payload;
 }
开发者ID:vcorobceanu,项目名称:WebAPL,代码行数:49,代码来源:FireBaseAuth.php

示例2: decode

 /**
  * @param string      $jwt    The JWT
  * @param string|null $key    The secret key
  * @param bool        $verify Don't skip verification process 
  *
  * @return object The JWT's payload as a PHP object
  *
  * return false if any execption occured
  */
 public static function decode($jwt, $key = null, $verify = true)
 {
     $tks = explode('.', $jwt);
     if (count($tks) != 3) {
         //throw new UnexpectedValueException('Wrong number of segments');
         return false;
     }
     list($headb64, $payloadb64, $cryptob64) = $tks;
     if (null === ($header = JWT::jsonDecode(JWT::urlsafeB64Decode($headb64)))) {
         //throw new UnexpectedValueException('Invalid segment encoding');
         return false;
     }
     if (null === ($payload = JWT::jsonDecode(JWT::urlsafeB64Decode($payloadb64)))) {
         //throw new UnexpectedValueException('Invalid segment encoding');
         return false;
     }
     $sig = JWT::urlsafeB64Decode($cryptob64);
     if ($verify) {
         if (empty($header->alg)) {
             //throw new DomainException('Empty algorithm');
             return false;
         }
         if ($sig != JWT::sign("{$headb64}.{$payloadb64}", $key, $header->alg)) {
             //throw new UnexpectedValueException('Signature verification failed');
             return false;
         }
     }
     return $payload;
 }
开发者ID:laiello,项目名称:xyz-php-sample,代码行数:38,代码来源:jwt.php

示例3: responseShouldContainJwtToken

 /**
  * Validate Jwt token
  *
  * @param string $token_field_name
  *
  * @Then /^(?:the )?response should contain jwt token in field "([^"]*)"$/
  */
 public function responseShouldContainJwtToken($token_field_name)
 {
     $response = $this->response->json();
     Assertions::assertArrayHasKey($token_field_name, $response);
     $tks = explode('.', $response[$token_field_name]);
     Assertions::assertEquals(3, count($tks));
     list($headb64, $bodyb64, $cryptob64) = $tks;
     $sig = \JWT::urlsafeB64Decode($cryptob64);
     $header = \JWT::jsonDecode(\JWT::urlsafeB64Decode($headb64));
     Assertions::assertTrue(\JWT::verify("{$headb64}.{$bodyb64}", $sig, $this->config['secret_key'], $header->alg));
 }
开发者ID:eriwin,项目名称:jwt-api-extension,代码行数:18,代码来源:JwtApiContext.php

示例4: decode

 /**
  * 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');
     }
     $tks = explode('.', $jwt);
     if (count($tks) != 3) {
         throw new UnexpectedValueException('Wrong number of segments');
     }
     list($headb64, $bodyb64, $cryptob64) = $tks;
     if (null === ($header = JWT::jsonDecode(JWT::urlsafeB64Decode($headb64)))) {
         throw new UnexpectedValueException('Invalid header encoding');
     }
     if (null === ($payload = JWT::jsonDecode(JWT::urlsafeB64Decode($bodyb64)))) {
         throw new UnexpectedValueException('Invalid claims encoding');
     }
     $sig = JWT::urlsafeB64Decode($cryptob64);
     if (empty($header->alg)) {
         throw new DomainException('Empty algorithm');
     }
     if (empty(self::$supported_algs[$header->alg])) {
         throw new DomainException('Algorithm not supported');
     }
     if (!is_array($allowed_algs) || !in_array($header->alg, $allowed_algs)) {
         throw new DomainException('Algorithm not allowed');
     }
     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');
         }
     }
     // Check the signature
     if (!JWT::verify("{$headb64}.{$bodyb64}", $sig, $key, $header->alg)) {
         throw new Exception('Signature verification failed');
     }
     // 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.
     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).
     if (isset($payload->iat) && $payload->iat > time() + self::$leeway) {
         throw new BeforeValidException('Cannot handle token prior to ' . date(DateTime::ISO8601, $payload->iat));
     }
     // Check if this token has expired.
     if (isset($payload->exp) && time() - self::$leeway >= $payload->exp) {
         throw new ExpiredException('Expired token');
     }
     return $payload;
 }
开发者ID:bhobbs92,项目名称:490-project,代码行数:75,代码来源:jwt.php

示例5: decode

 public static function decode($jwt, $key, $allowed_algs = array())
 {
     if (empty($key)) {
         throw new Exception('密钥不能为空');
     }
     $tks = explode('.', $jwt);
     if (count($tks) != 3) {
         throw new Exception('分段有错误');
     }
     list($headb64, $bodyb64, $cryptob64) = $tks;
     if (null === ($header = JWT::jsonDecode(JWT::urlsafeB64Decode($headb64)))) {
         throw new Exception('head编码错误');
     }
     if (null === ($payload = JWT::jsonDecode(JWT::urlsafeB64Decode($bodyb64)))) {
         throw new Exception('声明编码错误');
     }
     $sig = JWT::urlsafeB64Decode($cryptob64);
     if (empty($header->alg)) {
         throw new Exception('加密算法为空');
     }
     if (empty(self::$supported_algs[$header->alg])) {
         throw new Exception('加密算法不支持');
     }
     if (!is_array($allowed_algs) || !in_array($header->alg, $allowed_algs)) {
         throw new Exception('加密算法不允许使用');
     }
     if (is_array($key) || $key instanceof \ArrayAccess) {
         if (isset($header->kid)) {
             $key = $key[$header->kid];
         } else {
             throw new Exception('"kid"为空');
         }
     }
     // Check the signature
     if (!JWT::verify("{$headb64}.{$bodyb64}", $sig, $key, $header->alg)) {
         throw new Exception('签名认证失败');
     }
     if (isset($payload->nbf) && $payload->nbf > time() + self::$leeway) {
         throw new Exception('此日期前不能使用 ' . date(DateTime::ISO8601, $payload->nbf));
     }
     if (isset($payload->iat) && $payload->iat > time() + self::$leeway) {
         throw new Exception('令牌此日期前不能使用 ' . date(DateTime::ISO8601, $payload->iat));
     }
     // Check if this token has expired.
     if (isset($payload->exp) && time() - self::$leeway >= $payload->exp) {
         throw new Exception('令牌已过期');
     }
     return $payload;
 }
开发者ID:inspurhua,项目名称:myjwt,代码行数:49,代码来源:JWT.php

示例6: decode

 /**
  * Decodes a JWT string into a PHP object.
  *
  * @param string      $jwt       The JWT
  * @param string|Array|null $key The secret key, or map of keys
  * @param bool        $verify    Don't skip verification process
  *
  * @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 = null, $verify = true)
 {
     $tks = explode('.', $jwt);
     if (count($tks) != 3) {
         return null;
     }
     list($headb64, $bodyb64, $cryptob64) = $tks;
     if (null === ($header = JWT::jsonDecode(JWT::urlsafeB64Decode($headb64)))) {
         return null;
     }
     if (null === ($payload = JWT::jsonDecode(JWT::urlsafeB64Decode($bodyb64)))) {
         return null;
     }
     $sig = JWT::urlsafeB64Decode($cryptob64);
     if ($verify) {
         if (empty($header->alg)) {
             return null;
         }
         if (is_array($key)) {
             if (isset($header->kid)) {
                 $key = $key[$header->kid];
             } else {
                 return null;
             }
         }
         // Check the signature
         if (!JWT::verify("{$headb64}.{$bodyb64}", $sig, $key, $header->alg)) {
             return null;
         }
         // 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.
         if (isset($payload->nbf) && $payload->nbf > time()) {
             return null;
         }
         // 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 > time()) {
             return null;
         }
         // Check if this token has expired.
         if (isset($payload->exp) && time() >= $payload->exp) {
             return null;
         }
     }
     return $payload;
 }
开发者ID:CodyJae,项目名称:Whiteboard-Site,代码行数:66,代码来源:JWT.php

示例7: decode

 /**
  * Decodes a JWT string into a PHP object.
  *
  * @param string      $jwt       The JWT
  * @param string|Array|null $key The secret key, or map of keys
  * @param bool        $verify    Don't skip verification process
  *
  * @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 = null, $verify = true)
 {
     $tks = JWT::split($jwt);
     if (null === ($header = JWT::jsonDecode(JWT::urlsafeB64Decode($tks['header'])))) {
         throw new UnexpectedValueException('Invalid header encoding');
     }
     if (null === ($payload = JWT::jsonDecode(JWT::urlsafeB64Decode($tks['body'])))) {
         throw new UnexpectedValueException('Invalid claims encoding');
     }
     $signature = JWT::urlsafeB64Decode($tks['sig']);
     if ($verify) {
         if (empty($header->alg)) {
             throw new DomainException('Empty algorithm');
         }
         if (is_array($key)) {
             if (isset($header->kid)) {
                 $key = $key[$header->kid];
             } else {
                 throw new DomainException('"kid" empty, unable to lookup correct key');
             }
         }
         // Check the signature
         if ($key && !JWT::verify($tks['header'], $tks['body'], $signature, $key, $header->alg)) {
             throw new SignatureInvalidException('Signature verification failed');
         }
         // 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.
         if (isset($payload->nbf) && $payload->nbf > time()) {
             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 > time()) {
             throw new BeforeValidException('Cannot handle token prior to ' . date(DateTime::ISO8601, $payload->iat));
         }
         // Check if this token has expired.
         if (isset($payload->exp) && time() >= $payload->exp) {
             throw new ExpiredException('Expired token');
         }
     }
     return array($header, $payload, $signature);
 }
开发者ID:50onred,项目名称:php-jwt,代码行数:62,代码来源:JWT.php

示例8: testMalformedJsonThrowsException

 function testMalformedJsonThrowsException()
 {
     $this->setExpectedException('DomainException');
     JWT::jsonDecode('this is not valid JSON string');
 }
开发者ID:saarmstrong,项目名称:jwt,代码行数:5,代码来源:JWTTest.php

示例9: decode

 /**
  * Decodes a JWT string into a PHP object.
  *
  * @param string      $jwt           The JWT
  * @param string|Array|null $key     The secret key, or map of keys
  * @param Array       $allowed_algs  List of supported verification algorithms
  * @param Array       $options       Extra options (audience, issuer, jwtid, subject)
  *
  * @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
  * @throws InvalidAudienceException     Provided JWT is having a 'aud' value other than audience option
  * @throws InvalidIssuerException       Provided JWT is having a 'iss' value other than issuer option
  * @throws InvalidJWTIdException        Provided JWT is having a 'jit' value other than jwtid option
  * @throws InvalidSubjectException      Provided JWT is having a 'sub' value other than subject option
  *
  * @uses jsonDecode
  * @uses urlsafeB64Decode
  */
 public static function decode($jwt, $key = null, $allowed_algs = array(), $options = array())
 {
     $tks = explode('.', $jwt);
     if (count($tks) != 3) {
         throw new UnexpectedValueException('Wrong number of segments');
     }
     list($headb64, $bodyb64, $cryptob64) = $tks;
     if (null === ($header = JWT::jsonDecode(JWT::urlsafeB64Decode($headb64)))) {
         throw new UnexpectedValueException('Invalid header encoding');
     }
     if (null === ($payload = JWT::jsonDecode(JWT::urlsafeB64Decode($bodyb64)))) {
         throw new UnexpectedValueException('Invalid claims encoding');
     }
     $sig = JWT::urlsafeB64Decode($cryptob64);
     if (isset($key)) {
         if (empty($header->alg)) {
             throw new DomainException('Empty algorithm');
         }
         if (empty(self::$supported_algs[$header->alg])) {
             throw new DomainException('Algorithm not supported');
         }
         if (!is_array($allowed_algs) || !in_array($header->alg, $allowed_algs)) {
             throw new DomainException('Algorithm not allowed');
         }
         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');
             }
         }
         // Check the signature
         if (!JWT::verify("{$headb64}.{$bodyb64}", $sig, $key, $header->alg)) {
             throw new SignatureInvalidException('Signature verification failed');
         }
         // 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.
         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).
         if (isset($payload->iat) && $payload->iat > time() + self::$leeway) {
             throw new BeforeValidException('Cannot handle token prior to ' . date(DateTime::ISO8601, $payload->iat));
         }
         // Check if this token has expired.
         if (isset($payload->exp) && time() - self::$leeway >= $payload->exp) {
             throw new ExpiredException('Expired token');
         }
         if (isset($options['issuer']) && is_string($options['issuer'])) {
             if (!isset($payload->iss) || !is_string($payload->iss) || $payload->iss !== $options['issuer']) {
                 throw new InvalidIssuerException('Invalid issuer');
             }
         }
         if (isset($options['subject']) && is_string($options['subject'])) {
             if (!isset($payload->sub) || !is_string($payload->sub) || $payload->sub !== $options['subject']) {
                 throw new InvalidSubjectException('Invalid subject');
             }
         }
         if (isset($options['jwtid']) && is_string($options['jwtid'])) {
             if (!isset($payload->jti) || !is_string($payload->jti) || $payload->jti !== $options['jwtid']) {
                 throw new InvalidJWTIdException('Invalid JWT ID');
             }
         }
         if (isset($options['audience']) && (is_string($options['audience']) || is_array($options['audience']))) {
             if (!isset($payload->aud)) {
                 throw new InvalidAudienceException('Invalid audience');
             }
             $target = is_array($payload->aud) ? $payload->aud : array($payload->aud);
             $audiences = is_array($options['audience']) ? $options['audience'] : array($options['audience']);
             $audienceFound = false;
             foreach ($audiences as $audience) {
                 if (is_string($audience) && array_search($audience, $payload->aud) !== false) {
                     $audienceFound = true;
                     break;
//.........这里部分代码省略.........
开发者ID:TATDK,项目名称:php-jwt,代码行数:101,代码来源:JWT.php

示例10: testJsonDecode

 public function testJsonDecode()
 {
     $data = JWT::jsonDecode('{"foo": "bar"}');
     $this->assertEquals('bar', $data->foo);
     $this->assertEquals('bar', $data['foo']);
     $this->assertEquals('abc', JWT::jsonDecode(json_encode('abc')));
 }
开发者ID:ADmad,项目名称:php-jwt,代码行数:7,代码来源:JWTTest.php


注:本文中的JWT::jsonDecode方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。