This commit is contained in:
2021-08-11 14:06:42 +08:00
parent d6f0abf28d
commit bd17e8a713
3 changed files with 84 additions and 10 deletions
+31
View File
@@ -0,0 +1,31 @@
<?php
namespace Kiri\Jwt;
class ASE extends \SplEnum
{
const AES_128_ECB = 'aes-128-ecb';
const AES_192_ECB = 'aes-192-ecb';
const AES_256_ECB = 'aes-256-ecb';
const ARIA_128_ECB = 'aria-128-ecb';
const ARIA_192_ECB = 'aria-192-ecb';
const ARIA_256_ECB = 'aria-256-ecb';
const BF_ECB = 'bf-ecb';
const CAMELLIA_128_ECB = 'camellia-128-ecb';
const CAMELLIA_192_ECB = 'camellia-192-ecb';
const CAMELLIA_256_ECB = 'camellia-256-ecb';
const CAST5_ECB = 'cast5-ecb';
const DES_ECB = 'des-ecb';
const DES_EDE = 'des-ede';
const DES_EDE3 = 'des-ede3';
const ID_SMIME_ALG_CMS3DESWRAP = 'id-smime-alg-CMS3DESwrap';
const IDEA_ECB = 'idea-ecb';
const RC2_ECB = 'rc2-ecb';
const RC4 = 'rc4';
const RC4_40 = 'rc4-40';
const RC4_HMAC_MD5 = 'rc4-hmac-md5';
const SEED_ECB = 'seed-ecb';
const SM4_ECB = 'sm4-ecb';
}
+27 -7
View File
@@ -27,16 +27,31 @@ class Jwt extends Component
/**
* @throws ConfigException
* @throws Exception
* 'jwt' => [
* 'scene' => 'application',
* 'timeout' => 7200,
* 'encrypt' => '',
* 'iv' => '',
* 'key' => '',
* ]
*/
public function init()
{
$this->request = di(Request::class);
if (!Config::has('ssl.public') || !Config::has('ssl.private')) {
return;
}
$this->public = Config::get('ssl.public', $this->public);
$this->private = Config::get('ssl.private', $this->private);
$this->timeout = Config::get('ssl.timeout', 7200);
$jwt = Config::get('jwt', []);
if ($jwt) {
$this->setScene($jwt['scene'] ?? 'application');
$this->setEncrypt($jwt['encrypt'] ?? ASE::AES_128_ECB);
$this->setKey($jwt['key'] ?? get_called_class());
$defaultIv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($this->encrypt));
$this->setIv($jwt['iv'] ?? $defaultIv);
}
}
/**
@@ -62,9 +77,14 @@ class Jwt extends Component
*/
private function jwtHeader(): string
{
$string = openssl_encrypt(json_encode(['type' => 'openssl', 'encrypt' => $this->encrypt]),
"seed-ecb", $this->key);
return urlencode(str_replace('=', '', $string));
$string = openssl_encrypt(
json_encode(['type' => 'openssl', 'encrypt' => $this->encrypt]),
$this->encrypt,
$this->key,
0,
$this->iv
);
return str_replace('=', '', $string);
}
@@ -91,7 +111,7 @@ class Jwt extends Component
$params[] = $this->jwtHeader();
$params[] = $this->jwtBody($unionId);
$params[] = hash($this->encrypt, $params[0] . $params[1]);
$params[] = hash('ripemd128', $params[0] . $params[1]);
return implode('.', $params);
}
+26 -3
View File
@@ -29,6 +29,9 @@ trait JwtHelper
private string $encrypt = 'sha256';
private string $iv = '';
private ?string $public = '-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA6BuML3gtLGde7QKNuNST
UCB9gdHC7XIpOc7Wx2I64Esj3UxWHTgp3URj0ge8zpy7A3FfBdppR7d1nwoD6Xad
@@ -68,6 +71,8 @@ mlAZUEjsoaT9vjvjGTxl3uCm0TX5KTgtSJIt2kA1tYVjQef+/iZTHxY=
-----END RSA PRIVATE KEY-----';
/**
* @param string $publicKey
*/
@@ -86,9 +91,9 @@ mlAZUEjsoaT9vjvjGTxl3uCm0TX5KTgtSJIt2kA1tYVjQef+/iZTHxY=
public function setScene(string $scene)
{
$this->scene = $scene;
}
{
$this->scene = $scene;
}
/**
@@ -108,4 +113,22 @@ mlAZUEjsoaT9vjvjGTxl3uCm0TX5KTgtSJIt2kA1tYVjQef+/iZTHxY=
}
/**
* @param string $privateKey
*/
public function setIv(string $privateKey)
{
$this->iv = $privateKey;
}
/**
* @param string $privateKey
*/
public function setEncrypt(string $privateKey)
{
$this->encrypt = $privateKey;
}
}