Skip to content

Commit

Permalink
移除splbean依赖
Browse files Browse the repository at this point in the history
  • Loading branch information
kiss291323003 committed Apr 7, 2023
1 parent 77be68d commit d819bfe
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 27 deletions.
2 changes: 0 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
"php": ">=7.1.0",
"ext-json": "*",
"ext-openssl": ">=1.0.0",
"easyswoole/spl": "^1.2",
"easyswoole/component": "^2.0",
"easyswoole/utility": "^1.1"
},
"require-dev": {
Expand Down
4 changes: 2 additions & 2 deletions src/Encryption.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ class Encryption
{
use Singleton;

public function base64UrlEncode($content)
public static function base64UrlEncode($content)
{
return str_replace('=', '', strtr(base64_encode($content), '+/', '-_'));
}

public function base64UrlDecode($content)
public static function base64UrlDecode($content)
{
$remainder = strlen($content) % 4;
if ($remainder) {
Expand Down
6 changes: 3 additions & 3 deletions src/Jwt.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ public function decode(string $raw): ?JwtObject
}

// 验证header
$header = Encryption::getInstance()->base64UrlDecode($items[0]);
$header = Encryption::base64UrlDecode($items[0]);
$header = json_decode($header, true);
if (empty($header)) {
throw new Exception('Token header is empty!');
}

// 验证payload
$payload = Encryption::getInstance()->base64UrlDecode($items[1]);
$payload = Encryption::base64UrlDecode($items[1]);
$payload = json_decode($payload, true);
if (empty($payload)) {
throw new Exception('Token payload is empty!');
Expand All @@ -91,7 +91,7 @@ public function decode(string $raw): ?JwtObject
],
['prefix' => $this->prefix]
);
return new JwtObject($jwtObjConfig, true);
return new JwtObject($jwtObjConfig);
}

}
19 changes: 13 additions & 6 deletions src/JwtObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
namespace EasySwoole\Jwt;

use DomainException;
use EasySwoole\Spl\SplBean;
use EasySwoole\Utility\Random;
use UnexpectedValueException;

class JwtObject extends SplBean
class JwtObject extends \stdClass
{
public const STATUS_OK = 1;
public const STATUS_SIGNATURE_ERROR = -1;
Expand Down Expand Up @@ -38,6 +37,14 @@ class JwtObject extends SplBean
Jwt::ALG_METHOD_RS256 => 'SHA256'
];

function __construct(array $data)
{
foreach ($data as $key => $item){
$this->{$key} = $item;
}
$this->initialize();
}

protected function initialize(): void
{
if (empty($this->nbf)) {
Expand Down Expand Up @@ -77,13 +84,13 @@ protected function verify(): bool

if (in_array($this->getAlg(), [Jwt::ALG_METHOD_HMACSHA256, Jwt::ALG_METHOD_HS256])) {
$hash = hash_hmac('SHA256', $content, $this->getSecretKey(), true);
return hash_equals($this->getSignature(), Encryption::getInstance()->base64UrlEncode($hash));
return hash_equals($this->getSignature(), Encryption::base64UrlEncode($hash));
}

if (in_array($this->getAlg(), [Jwt::ALG_METHOD_AES, Jwt::ALG_METHOD_RS256])) {
$signatureAlg = $this->algMap[$this->getAlg()] ?? null;
if (!empty($signatureAlg)) {
$status = openssl_verify($content, Encryption::getInstance()->base64UrlDecode($this->getSignature()), $this->getSecretKey(), $signatureAlg);
$status = openssl_verify($content, Encryption::base64UrlDecode($this->getSignature()), $this->getSecretKey(), $signatureAlg);
if ($status < 0) {
throw new DomainException('OpenSSL error: ' . openssl_error_string());
}
Expand Down Expand Up @@ -344,7 +351,7 @@ public function __toString()
'alg' => $algMap[$this->getAlg()],
'typ' => 'JWT'
]);
$this->header = Encryption::getInstance()->base64UrlEncode($header);
$this->header = Encryption::base64UrlEncode($header);

$payload = json_encode([
'exp' => $this->getExp(),
Expand All @@ -357,7 +364,7 @@ public function __toString()
'status' => $this->getStatus(),
'data' => $this->getData()
], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
$this->payload = Encryption::getInstance()->base64UrlEncode($payload);
$this->payload = Encryption::base64UrlEncode($payload);

$this->signature = (new Signature([
'secretKey' => $this->getSecretKey(),
Expand Down
25 changes: 11 additions & 14 deletions src/Signature.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
<?php
/**
* @CreateTime: 2020/11/18 10:16 下午
* @Author: huizhang <[email protected]>
* @Copyright: copyright(2020) Easyswoole all rights reserved
* @Description: 签名生成
*/

namespace EasySwoole\Jwt;

use EasySwoole\Spl\SplBean;

class Signature extends SplBean
class Signature extends \stdClass
{

protected $secretKey;
protected $header;
protected $payload;
protected $alg;

/**
* php 7.4以下不支持在__toString()抛出异常
*/
public function __toString()
{
$content = $this->header . '.' . $this->payload;
Expand All @@ -29,12 +19,12 @@ public function __toString()
switch ($this->alg) {
case Jwt::ALG_METHOD_HMACSHA256:
case Jwt::ALG_METHOD_HS256:
$signature = Encryption::getInstance()->base64UrlEncode(
$signature = Encryption::base64UrlEncode(
hash_hmac('sha256', $content, $this->secretKey, true)
);
break;
case Jwt::ALG_METHOD_AES:
$signature = Encryption::getInstance()->base64UrlEncode(
$signature = Encryption::base64UrlEncode(
openssl_encrypt($content, 'AES-128-ECB', $this->secretKey)
);
break;
Expand All @@ -43,7 +33,7 @@ public function __toString()
if (!$success) {
$signature = "";
} else {
$signature = Encryption::getInstance()->base64UrlEncode($signature);
$signature = Encryption::base64UrlEncode($signature);
}
break;
default:
Expand All @@ -52,4 +42,11 @@ public function __toString()
return $signature;
}

function __construct(array $data)
{
foreach ($data as $key => $item){
$this->{$key} = $item;
}
}

}

0 comments on commit d819bfe

Please sign in to comment.