From 8955713546a086851c3095de9fa8228617f026f4 Mon Sep 17 00:00:00 2001 From: overtrue Date: Fri, 13 Dec 2024 09:19:12 +0800 Subject: [PATCH] feat: encrypt as array --- src/Kernel/Encryptor.php | 37 +++++++++++++++---- .../Exceptions/BadMethodCallException.php | 4 +- src/Kernel/Exceptions/BadRequestException.php | 4 +- .../Exceptions/BadResponseException.php | 4 +- src/Kernel/Exceptions/DecryptException.php | 4 +- src/Kernel/Exceptions/Exception.php | 4 +- .../Exceptions/InvalidArgumentException.php | 4 +- .../Exceptions/InvalidConfigException.php | 4 +- src/Kernel/Exceptions/RuntimeException.php | 4 +- .../Exceptions/ServiceNotFoundException.php | 4 +- src/Kernel/Form/Form.php | 4 +- src/Kernel/HttpClient/Response.php | 3 +- src/MiniApp/Server.php | 4 +- src/MiniApp/Utils.php | 4 +- src/OfficialAccount/Account.php | 3 +- src/OfficialAccount/Utils.php | 4 +- src/OpenPlatform/Account.php | 3 +- src/OpenPlatform/AuthorizerAccessToken.php | 4 +- src/OpenWork/Account.php | 3 +- .../Exceptions/InvalidSignatureException.php | 4 +- src/Pay/LegacySignature.php | 6 ++- src/Pay/ResponseValidator.php | 4 +- src/Pay/Signature.php | 4 +- src/Pay/URLSchemeBuilder.php | 4 +- src/Pay/Utils.php | 4 +- src/Pay/Validator.php | 4 +- src/Work/Account.php | 3 +- src/Work/Utils.php | 4 +- tests/Kernel/Traits/DecryptXmlMessageTest.php | 4 +- 29 files changed, 110 insertions(+), 36 deletions(-) diff --git a/src/Kernel/Encryptor.php b/src/Kernel/Encryptor.php index cbdf54cda..40258ceb1 100644 --- a/src/Kernel/Encryptor.php +++ b/src/Kernel/Encryptor.php @@ -83,6 +83,31 @@ public function getToken(): string * @throws Exception */ public function encrypt(string $plaintext, ?string $nonce = null, int|string|null $timestamp = null): string + { + return $this->encryptAsXml($plaintext, $nonce, $timestamp); + } + + /** + * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException + */ + public function encryptAsXml(string $plaintext, ?string $nonce = null, int|string|null $timestamp = null): string + { + $encrypted = $this->encryptAsArray($plaintext, $nonce, $timestamp); + + $response = [ + 'Encrypt' => $encrypted['ciphertext'], + 'MsgSignature' => $encrypted['signature'], + 'TimeStamp' => $encrypted['timestamp'], + 'Nonce' => $encrypted['nonce'], + ]; + + return Xml::build($response); + } + + /** + * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException + */ + public function encryptAsArray(string $plaintext, ?string $nonce = null, int|string|null $timestamp = null): array { try { $plaintext = Pkcs7::padding(random_bytes(16).pack('N', strlen($plaintext)).$plaintext.$this->appId, 32); @@ -102,14 +127,12 @@ public function encrypt(string $plaintext, ?string $nonce = null, int|string|nul $nonce ??= Str::random(); $timestamp ??= time(); - $response = [ - 'Encrypt' => $ciphertext, - 'MsgSignature' => $this->createSignature($this->token, $timestamp, $nonce, $ciphertext), - 'TimeStamp' => $timestamp, - 'Nonce' => $nonce, + return [ + 'ciphertext' => $ciphertext, + 'signature' => $this->createSignature($this->token, $timestamp, $nonce, $ciphertext), + 'timestamp' => $timestamp, + 'nonce' => $nonce, ]; - - return Xml::build($response); } public function createSignature(mixed ...$attributes): string diff --git a/src/Kernel/Exceptions/BadMethodCallException.php b/src/Kernel/Exceptions/BadMethodCallException.php index 7551b9f1c..6d36f2d94 100644 --- a/src/Kernel/Exceptions/BadMethodCallException.php +++ b/src/Kernel/Exceptions/BadMethodCallException.php @@ -2,4 +2,6 @@ namespace EasyWeChat\Kernel\Exceptions; -class BadMethodCallException extends Exception {} +class BadMethodCallException extends Exception +{ +} diff --git a/src/Kernel/Exceptions/BadRequestException.php b/src/Kernel/Exceptions/BadRequestException.php index 86707c377..f8e856e3d 100644 --- a/src/Kernel/Exceptions/BadRequestException.php +++ b/src/Kernel/Exceptions/BadRequestException.php @@ -4,4 +4,6 @@ namespace EasyWeChat\Kernel\Exceptions; -class BadRequestException extends Exception {} +class BadRequestException extends Exception +{ +} diff --git a/src/Kernel/Exceptions/BadResponseException.php b/src/Kernel/Exceptions/BadResponseException.php index c9b3bc498..7ecb54251 100644 --- a/src/Kernel/Exceptions/BadResponseException.php +++ b/src/Kernel/Exceptions/BadResponseException.php @@ -4,4 +4,6 @@ namespace EasyWeChat\Kernel\Exceptions; -class BadResponseException extends Exception {} +class BadResponseException extends Exception +{ +} diff --git a/src/Kernel/Exceptions/DecryptException.php b/src/Kernel/Exceptions/DecryptException.php index 49ca62293..845e4e4c0 100644 --- a/src/Kernel/Exceptions/DecryptException.php +++ b/src/Kernel/Exceptions/DecryptException.php @@ -4,4 +4,6 @@ namespace EasyWeChat\Kernel\Exceptions; -class DecryptException extends Exception {} +class DecryptException extends Exception +{ +} diff --git a/src/Kernel/Exceptions/Exception.php b/src/Kernel/Exceptions/Exception.php index 11ff83d2f..e34ca12b7 100644 --- a/src/Kernel/Exceptions/Exception.php +++ b/src/Kernel/Exceptions/Exception.php @@ -6,4 +6,6 @@ use Exception as BaseException; -class Exception extends BaseException {} +class Exception extends BaseException +{ +} diff --git a/src/Kernel/Exceptions/InvalidArgumentException.php b/src/Kernel/Exceptions/InvalidArgumentException.php index 3b4a691bf..f2174e8ae 100644 --- a/src/Kernel/Exceptions/InvalidArgumentException.php +++ b/src/Kernel/Exceptions/InvalidArgumentException.php @@ -4,4 +4,6 @@ namespace EasyWeChat\Kernel\Exceptions; -class InvalidArgumentException extends Exception {} +class InvalidArgumentException extends Exception +{ +} diff --git a/src/Kernel/Exceptions/InvalidConfigException.php b/src/Kernel/Exceptions/InvalidConfigException.php index a1c7a475b..b808fe7d0 100644 --- a/src/Kernel/Exceptions/InvalidConfigException.php +++ b/src/Kernel/Exceptions/InvalidConfigException.php @@ -4,4 +4,6 @@ namespace EasyWeChat\Kernel\Exceptions; -class InvalidConfigException extends Exception {} +class InvalidConfigException extends Exception +{ +} diff --git a/src/Kernel/Exceptions/RuntimeException.php b/src/Kernel/Exceptions/RuntimeException.php index 9bb8cb3dc..dda4f517f 100644 --- a/src/Kernel/Exceptions/RuntimeException.php +++ b/src/Kernel/Exceptions/RuntimeException.php @@ -4,4 +4,6 @@ namespace EasyWeChat\Kernel\Exceptions; -class RuntimeException extends Exception {} +class RuntimeException extends Exception +{ +} diff --git a/src/Kernel/Exceptions/ServiceNotFoundException.php b/src/Kernel/Exceptions/ServiceNotFoundException.php index eb1b256d1..3da7e1182 100644 --- a/src/Kernel/Exceptions/ServiceNotFoundException.php +++ b/src/Kernel/Exceptions/ServiceNotFoundException.php @@ -4,4 +4,6 @@ namespace EasyWeChat\Kernel\Exceptions; -class ServiceNotFoundException extends Exception {} +class ServiceNotFoundException extends Exception +{ +} diff --git a/src/Kernel/Form/Form.php b/src/Kernel/Form/Form.php index f940627f1..1a3bc582f 100644 --- a/src/Kernel/Form/Form.php +++ b/src/Kernel/Form/Form.php @@ -11,7 +11,9 @@ class Form /** * @param array $fields */ - public function __construct(protected array $fields) {} + public function __construct(protected array $fields) + { + } /** * @param array $fields diff --git a/src/Kernel/HttpClient/Response.php b/src/Kernel/HttpClient/Response.php index e8b3ec6c2..5154f2c5c 100644 --- a/src/Kernel/HttpClient/Response.php +++ b/src/Kernel/HttpClient/Response.php @@ -47,7 +47,8 @@ public function __construct( protected ResponseInterface $response, protected ?Closure $failureJudge = null, protected bool $throw = true - ) {} + ) { + } public function throw(bool $throw = true): static { diff --git a/src/MiniApp/Server.php b/src/MiniApp/Server.php index 370be0535..4e40e0d63 100644 --- a/src/MiniApp/Server.php +++ b/src/MiniApp/Server.php @@ -4,4 +4,6 @@ namespace EasyWeChat\MiniApp; -class Server extends \EasyWeChat\OfficialAccount\Server {} +class Server extends \EasyWeChat\OfficialAccount\Server +{ +} diff --git a/src/MiniApp/Utils.php b/src/MiniApp/Utils.php index 838801427..43eefde03 100644 --- a/src/MiniApp/Utils.php +++ b/src/MiniApp/Utils.php @@ -12,7 +12,9 @@ class Utils { - public function __construct(protected Application $app) {} + public function __construct(protected Application $app) + { + } /** * @throws HttpException diff --git a/src/OfficialAccount/Account.php b/src/OfficialAccount/Account.php index e0ba514ee..9793fac77 100644 --- a/src/OfficialAccount/Account.php +++ b/src/OfficialAccount/Account.php @@ -14,7 +14,8 @@ public function __construct( protected ?string $secret, protected ?string $token = null, protected ?string $aesKey = null - ) {} + ) { + } public function getAppId(): string { diff --git a/src/OfficialAccount/Utils.php b/src/OfficialAccount/Utils.php index f66fbc2d7..7b31134fd 100644 --- a/src/OfficialAccount/Utils.php +++ b/src/OfficialAccount/Utils.php @@ -15,7 +15,9 @@ class Utils { - public function __construct(protected Application $app) {} + public function __construct(protected Application $app) + { + } /** * @param array $jsApiList diff --git a/src/OpenPlatform/Account.php b/src/OpenPlatform/Account.php index b5a61e8e1..6bd223cb0 100644 --- a/src/OpenPlatform/Account.php +++ b/src/OpenPlatform/Account.php @@ -13,7 +13,8 @@ public function __construct( protected string $secret, protected string $token, protected string $aesKey - ) {} + ) { + } public function getAppId(): string { diff --git a/src/OpenPlatform/AuthorizerAccessToken.php b/src/OpenPlatform/AuthorizerAccessToken.php index ecdc2439f..c58588ab9 100644 --- a/src/OpenPlatform/AuthorizerAccessToken.php +++ b/src/OpenPlatform/AuthorizerAccessToken.php @@ -11,7 +11,9 @@ class AuthorizerAccessToken implements AccessToken, Stringable { - public function __construct(protected string $appId, protected string $accessToken) {} + public function __construct(protected string $appId, protected string $accessToken) + { + } public function getAppId(): string { diff --git a/src/OpenWork/Account.php b/src/OpenWork/Account.php index af97c888c..13ff23622 100644 --- a/src/OpenWork/Account.php +++ b/src/OpenWork/Account.php @@ -15,7 +15,8 @@ public function __construct( protected string $suiteSecret, protected string $token, protected string $aesKey - ) {} + ) { + } public function getCorpId(): string { diff --git a/src/Pay/Exceptions/InvalidSignatureException.php b/src/Pay/Exceptions/InvalidSignatureException.php index 18b9446d3..9d89b344b 100644 --- a/src/Pay/Exceptions/InvalidSignatureException.php +++ b/src/Pay/Exceptions/InvalidSignatureException.php @@ -4,4 +4,6 @@ use EasyWeChat\Kernel\Exceptions\RuntimeException; -class InvalidSignatureException extends RuntimeException {} +class InvalidSignatureException extends RuntimeException +{ +} diff --git a/src/Pay/LegacySignature.php b/src/Pay/LegacySignature.php index 4ae63b169..07182fac2 100644 --- a/src/Pay/LegacySignature.php +++ b/src/Pay/LegacySignature.php @@ -18,7 +18,9 @@ class LegacySignature { - public function __construct(protected MerchantInterface $merchant) {} + public function __construct(protected MerchantInterface $merchant) + { + } /** * @param array $params @@ -39,7 +41,7 @@ public function sign(array $params): array ], $params ), - static fn ($value, $key) => !($key === 'sign' || $value === '' || is_null($value)), + static fn ($value, $key) => ! ($key === 'sign' || $value === '' || is_null($value)), ARRAY_FILTER_USE_BOTH ); diff --git a/src/Pay/ResponseValidator.php b/src/Pay/ResponseValidator.php index 085606ab5..7ceab6b3f 100644 --- a/src/Pay/ResponseValidator.php +++ b/src/Pay/ResponseValidator.php @@ -11,7 +11,9 @@ class ResponseValidator implements \EasyWeChat\Pay\Contracts\ResponseValidator { - public function __construct(protected MerchantInterface $merchant) {} + public function __construct(protected MerchantInterface $merchant) + { + } /** * @throws \EasyWeChat\Kernel\Exceptions\BadResponseException diff --git a/src/Pay/Signature.php b/src/Pay/Signature.php index bc019ac14..c44cec5b5 100644 --- a/src/Pay/Signature.php +++ b/src/Pay/Signature.php @@ -20,7 +20,9 @@ class Signature { - public function __construct(protected MerchantInterface $merchant) {} + public function __construct(protected MerchantInterface $merchant) + { + } /** * @param array $options diff --git a/src/Pay/URLSchemeBuilder.php b/src/Pay/URLSchemeBuilder.php index cc002a9fb..23b85af73 100644 --- a/src/Pay/URLSchemeBuilder.php +++ b/src/Pay/URLSchemeBuilder.php @@ -12,7 +12,9 @@ class URLSchemeBuilder { - public function __construct(protected MerchantInterface $merchant) {} + public function __construct(protected MerchantInterface $merchant) + { + } /** * @throws Exception diff --git a/src/Pay/Utils.php b/src/Pay/Utils.php index 8f12a2250..ef8c1a20f 100644 --- a/src/Pay/Utils.php +++ b/src/Pay/Utils.php @@ -18,7 +18,9 @@ class Utils { - public function __construct(protected MerchantInterface $merchant) {} + public function __construct(protected MerchantInterface $merchant) + { + } /** * @see https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter4_1_4.shtml diff --git a/src/Pay/Validator.php b/src/Pay/Validator.php index 5beb30735..7f976f0bf 100644 --- a/src/Pay/Validator.php +++ b/src/Pay/Validator.php @@ -21,7 +21,9 @@ class Validator implements \EasyWeChat\Pay\Contracts\Validator public const HEADER_SIGNATURE = 'Wechatpay-Signature'; - public function __construct(protected MerchantInterface $merchant) {} + public function __construct(protected MerchantInterface $merchant) + { + } /** * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException diff --git a/src/Work/Account.php b/src/Work/Account.php index aac1a790e..430e4ab56 100644 --- a/src/Work/Account.php +++ b/src/Work/Account.php @@ -13,7 +13,8 @@ public function __construct( protected string $secret, protected string $token, protected string $aesKey, - ) {} + ) { + } public function getCorpId(): string { diff --git a/src/Work/Utils.php b/src/Work/Utils.php index 8f2809525..8de79e18d 100644 --- a/src/Work/Utils.php +++ b/src/Work/Utils.php @@ -16,7 +16,9 @@ class Utils { - public function __construct(protected Application $app) {} + public function __construct(protected Application $app) + { + } /** * @param array $jsApiList diff --git a/tests/Kernel/Traits/DecryptXmlMessageTest.php b/tests/Kernel/Traits/DecryptXmlMessageTest.php index ed0fda12a..82d12db68 100644 --- a/tests/Kernel/Traits/DecryptXmlMessageTest.php +++ b/tests/Kernel/Traits/DecryptXmlMessageTest.php @@ -37,4 +37,6 @@ public function test_it_can_decrypt_message() } } -class DummyMessage extends Message {} +class DummyMessage extends Message +{ +}