Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
edenleung committed Aug 14, 2021
1 parent 55e1028 commit d6f64ad
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 45 deletions.
1 change: 1 addition & 0 deletions .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":1,"defects":[],"times":{"JwtAuth\\Tests\\JwtAuthTest::testGetConfig":0.005}}
12 changes: 12 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "xiaodi/jwt-auth",
"license": "MIT",
"authors": [
{
"name": "Eden Leung",
Expand All @@ -15,5 +16,16 @@
"psr-4": {
"JwtAuth\\": "src"
}
},
"require-dev": {
"phpunit/phpunit": "9.5.x-dev"
},
"autoload-dev": {
"psr-4": {
"JwtAuth\\Tests\\": "tests/"
}
},
"scripts": {
"test": "vendor/bin/phpunit"
}
}
22 changes: 22 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
beStrictAboutTestsThatDoNotTestAnything="false"
bootstrap="tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
verbose="true"
>
<testsuites>
<testsuite name="Jw Auth Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<filter>
</filter>
</phpunit>
36 changes: 18 additions & 18 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function __construct(array $options)

/**
* 获取 加密密钥
*
*
* @return \Lcobucci\JWT\Signer\Key\InMemory
*/
public function getHmacKey(): Key
Expand All @@ -113,8 +113,8 @@ public function getHmacKey(): Key
}

/**
* 获取 是否使用 RSA 加密
*
* 获取 是否使用 RSA 加密
*
* @return bool
*/
public function RSASigner()
Expand All @@ -126,7 +126,7 @@ public function RSASigner()

/**
* 获取 加密key
*
*
* @return Key
*/
public function getSignerKey(): Key
Expand All @@ -144,7 +144,7 @@ public function getSignerKey(): Key

/**
* 获取 RSA 公钥
*
*
* @return Key
*/
public function getPublicKey(): Key
Expand All @@ -154,7 +154,7 @@ public function getPublicKey(): Key

/**
* 获取 RSA 私钥
*
*
* @return Key
*/
public function getPrivateKey(): Key
Expand All @@ -164,7 +164,7 @@ public function getPrivateKey(): Key

/**
* 获取有效果期
*
*
* @return number
*/
public function getExpires()
Expand All @@ -174,7 +174,7 @@ public function getExpires()

/**
* 获取刷新ttl
*
*
* @return string
*/
public function getRefreshTTL()
Expand All @@ -184,7 +184,7 @@ public function getRefreshTTL()

/**
* 获取iss
*
*
* @return string
*/
public function getIss(): string
Expand All @@ -194,7 +194,7 @@ public function getIss(): string

/**
* 获取auid
*
*
* @return string
*/
public function getAud(): string
Expand All @@ -204,7 +204,7 @@ public function getAud(): string

/**
* 获取not_before
*
*
* @return number
*/
public function getNotBefore()
Expand All @@ -214,7 +214,7 @@ public function getNotBefore()

/**
* 获取加密对象
*
*
* @return Signer
*/
public function getSigner(): Signer
Expand All @@ -225,7 +225,7 @@ public function getSigner(): Signer

/**
* 获取重新登录code
*
*
* @return number
*/
public function getReloginCode()
Expand All @@ -235,7 +235,7 @@ public function getReloginCode()

/**
* 获取刷新code
*
*
* @return number
*/
public function getRefreshCode()
Expand All @@ -245,7 +245,7 @@ public function getRefreshCode()

/**
* 获取是否自动续签
*
*
* @return bool
*/
public function getAutomaticRenewal()
Expand All @@ -255,7 +255,7 @@ public function getAutomaticRenewal()

/**
* 获取token途径
*
*
* @return string|null
*/
public function getType()
Expand All @@ -265,7 +265,7 @@ public function getType()

/**
* 获取事件
*
*
* @return string|null
*/
public function getEventHandler()
Expand All @@ -275,7 +275,7 @@ public function getEventHandler()

/**
* 获取用户模型
*
*
* @return string|null
*/
public function getUserModel()
Expand Down
12 changes: 2 additions & 10 deletions src/EventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,9 @@

interface EventHandler
{
/**
* 登录事件
* @param Token $token
* @return void
*/
public function login(Token $token);

/**
* 登出事件
* @param Token $token
* @return void
*/
public function logout(Token $token);

public function verify(Token $token);
}
8 changes: 4 additions & 4 deletions src/Jwt.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected function init()

/**
* 加密配置
*
*
* @return void
*/
protected function initJwtConfiguration()
Expand Down Expand Up @@ -98,7 +98,7 @@ public function parseToken(string $token): Token

/**
* 效验 Token
*
*
* @param string $token
* @return bool
*/
Expand All @@ -125,13 +125,13 @@ protected function getValidateConfig()
{
return Configuration::forSymmetricSigner(
$this->config->getSigner(),
$this->config->RSASigner() ? $this->config->getPublicKey() : $this->config->getHamcKey()
$this->config->RSASigner() ? $this->config->getPublicKey() : $this->config->getHmacKey()
);
}

/**
* 验证成功后的Token
*
*
* @return Token|Exception
*/
public function getVerifyToken()
Expand Down
22 changes: 9 additions & 13 deletions src/JwtAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ class JwtAuth
*/
protected $user;

public function __construct(Config $config)
public function __construct(Config $config, EventHandler $event = null)
{
$this->config = $config;
$this->event = $event;
$this->init();
}

Expand All @@ -39,7 +40,6 @@ protected function init()
$this->jwt = new Jwt($this);

$this->initUser();
$this->initEvent();
}

protected function initUser()
Expand All @@ -49,11 +49,6 @@ protected function initUser()
}
}

protected function initEvent()
{
$this->event = new Event($this->config->getEventHandler());
}

/**
* 获取 Token 配置
*/
Expand All @@ -66,31 +61,32 @@ public function getConfig(): Config
* 生成 Token
* @param $id 唯一标识
* @param $cliams 附带参数
*
*
* @return Token
*/
public function token($id, $cliams = [])
{
$token = $this->jwt->make($id, $cliams);

$this->event->login($token);
$this->event->login(($this->parseToken($token));

return $token;
}

/**
* 检测合法性
* @param string $token
* @return bool
*/
public function verify($token)
{
return $this->jwt->validate($token);
$flag = $this->jwt->validate($token);
$this->event->verify($this->parseToken($token));
return $flag;
}

/**
* 解析 Token
* @param string $token
* @param $token jwt token
* @return Token
*/
public function parseToken($token)
Expand All @@ -109,7 +105,7 @@ public function getVerifyToken()

/**
* 获取登录用户对象
*
*
* @return AuthorizationUserInterface|null
*/
public function getUser()
Expand Down
14 changes: 14 additions & 0 deletions tests/JwtAuthTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace JwtAuth\Tests;

use JwtAuth\Tests\TestCase;

class JwtAuthTest extends TestCase
{

public function testGetConfig()
{
$this->assertEquals(true, true);
}
}
10 changes: 10 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace JwtAuth\Tests;

class TestCase extends \PHPUnit\Framework\TestCase
{
protected function setUp():void
{
}
}
4 changes: 4 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

<?php

include __DIR__.'/../vendor/autoload.php';

0 comments on commit d6f64ad

Please sign in to comment.