Esempio n. 1
0
 public static function create($claimsKey, $claimsValue = null)
 {
     $claims = Claims::set($claimsKey, $claimsValue);
     // 权利声明
     $key = config('jwt.key');
     // 密钥
     $alg = config('jwt.alg') ? config('jwt.alg') : 'HS256';
     // 算法
     $token = JWT::encode($claims, $key, $alg);
     // 编码
     return $token;
 }
Esempio n. 2
0
 public static function get($token = null)
 {
     $token = is_null($token) ? Token::find() : $token;
     $key = config('jwt.key');
     // 密钥
     $alg = config('jwt.alg') ? config('jwt.alg') : 'HS256';
     // 算法
     if ($token) {
         try {
             if ($claims = JWT::decode($token, $key, array($alg))) {
                 return $claims;
             }
         } catch (\InvalidArgumentException $e) {
             data()->setErr('token', '服务器设置错误: ' . $e->getMessage())->status(500);
         } catch (\Exception $e) {
             data()->setErr('token', '无效的TOKEN: ' . $e->getMessage())->status(403);
         }
     }
     return null;
 }
Esempio n. 3
0
File: JWT.php Progetto: alpfish/alp
 /**
  * Encode a PHP object into a JSON string.
  *
  * @param object|array $input A PHP object or array
  *
  * @return string JSON representation of the PHP object or array
  *
  * @throws DomainException Provided object could not be encoded to valid JSON
  */
 public static function jsonEncode($input)
 {
     $json = json_encode($input);
     if (function_exists('json_last_error') && ($errno = json_last_error())) {
         JWT::handleJsonError($errno);
     } elseif ($json === 'null' && $input !== null) {
         throw new DomainException('Null result with non-null input');
     }
     return $json;
 }