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


PHP JWT::split方法代码示例

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


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

示例2: testVerify

 public function testVerify()
 {
     $msg = 'eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.Iio6aHR0cDovL2FwcGxpY2F0aW9uL2NsaWNreT9ibGFoPTEuMjMmZi5vbz00NTYgQUMwMDAgMTIzIg.E_U8X2YpMT5K1cEiT_3-IvBYfrdIFIeVYeOqre_Z5Cg';
     $tks = JWT::split($msg);
     list(, , $signature) = JWT::decode($tks);
     $this->assertTrue(JWT::verify($tks['header'], $tks['body'], $signature, 'my_key'));
 }
开发者ID:50onred,项目名称:php-jwt,代码行数:7,代码来源:JWTTest.php


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