Skip to content

Commit

Permalink
更新jwt 可保存 object类型
Browse files Browse the repository at this point in the history
  • Loading branch information
webguosai committed Oct 28, 2022
1 parent 9a88012 commit 0f991dd
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 67 deletions.
20 changes: 0 additions & 20 deletions src/Authentication/Authentication.php

This file was deleted.

23 changes: 0 additions & 23 deletions src/Authentication/Driver/Session.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
<?php

namespace Webguosai\Authentication\Driver;
namespace Webguosai\Authentication;

use Webguosai\Authentication\Authentication;
use Webguosai\Authentication\Exception\TokenExpiredException;
use Webguosai\Authentication\Exception\TokenInvalidException;
use Webguosai\Http\Request;
use Webguosai\Util\Jwt as JWTUtil;

class Jwt implements Authentication
class JwtToken
{
protected $queryKey = 'token';
public $pre = 'Bearer ';

public function __construct($jwtSecret)
{
JWTUtil::setConfig($jwtSecret);
}

/**
* 解析
* @param null $jwt
* @return mixed
* @throws TokenExpiredException
* @throws TokenInvalidException
*/
public function parse()
public function parse($jwt = null)
{
$jwt = $this->fromQuery();

if (is_null($jwt)) {
$jwt = $this->fromHeader();
$jwt = $this->fromQuery();

if (is_null($jwt)) {
$jwt = $this->fromHeader();
}
}

return $this->parseJwt($jwt);
Expand Down
19 changes: 5 additions & 14 deletions src/Util/Jwt.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,12 @@

namespace Webguosai\Util;

/**
* jwt类
* 演示:
*
Jwt::setConfig('key123456', 'domain.com');
$code = Jwt::encode(1111, 5);
dump($code);
dd(JWT::decode($code));
*
*/
class Jwt
{
const ALG = 'HS256';
private static $key = 'GkhFiMmJQYFeiUJ8NxXjr22tbgFXFg6IHSaQR2HK8qU3tMHFlYWBTs6gn2kN7QEq';
private static $domain = '';
public static function setConfig($key, $domain)
private static $domain;
public static function setConfig($key, $domain = '')
{
self::$key = $key;
self::$domain = $domain;
Expand All @@ -43,7 +33,7 @@ public static function encode($data, int $exp = 3600, string $iss = null, string
"iat" => $time,
// 定义在什么时间之前,该jwt都是不可用的
"nbf" => $time,
'data' => json_encode($data)
'data' => serialize($data)
];
if ($exp) {
$token['exp'] = $time + $exp;
Expand All @@ -60,7 +50,8 @@ public static function decode(string $jwt)
{
try {
$params = \Firebase\JWT\JWT::decode($jwt, new \Firebase\JWT\Key(self::$key, self::ALG));
return json_decode($params->data, true);
// return json_decode($params->data, true);
return unserialize($params->data);
} catch (\Exception $e) {
return null;
}
Expand Down
5 changes: 2 additions & 3 deletions tests/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@


/** 鉴权 **/
//$jwt = new \Webguosai\Authentication\Driver\Jwt();
//$jwt = new \Webguosai\Authentication\JwtToken('123');
//dump($jwt->authenticate([
// 'name' => 'guosai',
// 'password' => 'guosai',
Expand Down Expand Up @@ -381,11 +381,10 @@

/** JWT **/
//Jwt::setConfig('key123456', 'domain.com');
//$code = Jwt::encode(1111, 5);
//$code = Jwt::encode([1,2,3,4], 5);
//dump($code);
//dd(JWT::decode($code));


/** 将整数秒转换成xx天xx时xx分xx秒格式 **/
//dump(CountdownFormat::getFormat(60*60*24*30+1));//最简单的调用
//dump(CountdownFormat::getFormat(60*60*24*30+1,'dhm'));//自定义返回格式,默认xx天xx时xx分xx秒
Expand Down

0 comments on commit 0f991dd

Please sign in to comment.