作者 lackoxygen

test

@@ -2,10 +2,12 @@ @@ -2,10 +2,12 @@
2 2
3 return [ 3 return [
4 'default' => [ 4 'default' => [
5 - 'app_key' => env('TIKTOK_APP_KEY', ''),  
6 - 'app_secret' => env('TIKTOK_APP_SECRET', ''),  
7 - 'enable_mock' => env('TIKTOK_ENABLE_MOCK', false),  
8 - 'base_uri' => env('TIKTOK_BASE_URI', 'https://openapi-sandbox.jinritemai.com'),  
9 - 'timeout' => env('TIKTOK_TIMEOUT', 30), 5 + 'app_key' => '',
  6 + 'app_secret' => '',
  7 + 'enable_mock' => false,
  8 + 'base_uri' => 'https://openapi-sandbox.jinritemai.com',
  9 + 'timeout' => 5.0,
  10 + 'sign_method' => 'hmac-sha256',
  11 + 'version' => 2
10 ] 12 ]
11 ]; 13 ];
@@ -102,13 +102,14 @@ class Application @@ -102,13 +102,14 @@ class Application
102 */ 102 */
103 public static function newConfigFormArray(array $options): Option 103 public static function newConfigFormArray(array $options): Option
104 { 104 {
105 - return new Option(  
106 - Arr::get($options, 'app_key'),  
107 - Arr::get($options, 'app_secret'),  
108 - Arr::get($options, 'base_uri'),  
109 - (float)Arr::get($options, 'timeout'),  
110 - Arr::get($options, 'enable_mock')  
111 - ); 105 + $option = new Option();
  106 + $option->setAppKey((string)Arr::get($options, 'app_key', ''));
  107 + $option->setAppSecret((string)Arr::get($options, 'app_secret', ''));
  108 + $option->setBaseUri((string)Arr::get($options, 'base_uri', 'https://openapi-sandbox.jinritemai.com'));
  109 + $option->setTimeout((float)Arr::get($options, 'timeout', 5));
  110 + $option->setEnableMock((bool)Arr::get($options, 'enable_mock'));
  111 + $option->setSignMethod((string)Arr::get($options, 'sign_method', 'sha256'));
  112 + return $option;
112 } 113 }
113 114
114 /** 115 /**
@@ -32,28 +32,17 @@ class Option @@ -32,28 +32,17 @@ class Option
32 /** 32 /**
33 * @var float 33 * @var float
34 */ 34 */
35 - private float $timeout; 35 + private float $timeout = 5.0;
36 36
37 /** 37 /**
38 - * @param string $appKey  
39 - * @param string $appSecret  
40 - * @param string $baseUri  
41 - * @param float $timeout  
42 - * @param bool $enableMock 38 + * @var string
43 */ 39 */
44 - public function __construct(  
45 - string $appKey = '',  
46 - string $appSecret = '',  
47 - string $baseUri = '',  
48 - float $timeout = 30.0,  
49 - bool $enableMock = false  
50 - ) {  
51 - $this->appKey = $appKey;  
52 - $this->appSecret = $appSecret;  
53 - $this->baseUri = $baseUri;  
54 - $this->timeout = $timeout;  
55 - $this->enableMock = $enableMock;  
56 - } 40 + private string $signMethod = 'hmac-sha256';
  41 +
  42 + /**
  43 + * @var string
  44 + */
  45 + private string $version;
57 46
58 /** 47 /**
59 * @param mixed $appKey 48 * @param mixed $appKey
@@ -151,4 +140,36 @@ class Option @@ -151,4 +140,36 @@ class Option
151 { 140 {
152 return $this->timeout; 141 return $this->timeout;
153 } 142 }
  143 +
  144 + /**
  145 + * @param string $signMethod
  146 + */
  147 + public function setSignMethod(string $signMethod): void
  148 + {
  149 + $this->signMethod = $signMethod;
  150 + }
  151 +
  152 + /**
  153 + * @return string
  154 + */
  155 + public function getSignMethod(): string
  156 + {
  157 + return $this->signMethod;
  158 + }
  159 +
  160 + /**
  161 + * @param string $version
  162 + */
  163 + public function setVersion(string $version): void
  164 + {
  165 + $this->version = $version;
  166 + }
  167 +
  168 + /**
  169 + * @return string
  170 + */
  171 + public function getVersion(): string
  172 + {
  173 + return $this->version;
  174 + }
154 } 175 }
@@ -24,11 +24,6 @@ class Request @@ -24,11 +24,6 @@ class Request
24 /** 24 /**
25 * @var string 25 * @var string
26 */ 26 */
27 - private string $v = '2';  
28 -  
29 - /**  
30 - * @var string  
31 - */  
32 private string $method = 'GET'; 27 private string $method = 'GET';
33 28
34 /** 29 /**
@@ -47,6 +42,11 @@ class Request @@ -47,6 +42,11 @@ class Request
47 private bool $signature = true; 42 private bool $signature = true;
48 43
49 /** 44 /**
  45 + * @var string
  46 + */
  47 + private string $signMethod = 'sha256';
  48 +
  49 + /**
50 * @return string 50 * @return string
51 */ 51 */
52 public function getService(): string 52 public function getService(): string
@@ -81,22 +81,6 @@ class Request @@ -81,22 +81,6 @@ class Request
81 /** 81 /**
82 * @return string 82 * @return string
83 */ 83 */
84 - public function getV(): string  
85 - {  
86 - return $this->v;  
87 - }  
88 -  
89 - /**  
90 - * @param mixed $v  
91 - */  
92 - public function setV($v): void  
93 - {  
94 - $this->v = $v;  
95 - }  
96 -  
97 - /**  
98 - * @return string  
99 - */  
100 public function getMethod(): string 84 public function getMethod(): string
101 { 85 {
102 return $this->method; 86 return $this->method;
@@ -158,4 +142,20 @@ class Request @@ -158,4 +142,20 @@ class Request
158 { 142 {
159 $this->path = $path; 143 $this->path = $path;
160 } 144 }
  145 +
  146 + /**
  147 + * @param string $signMethod
  148 + */
  149 + public function setSignMethod(string $signMethod): void
  150 + {
  151 + $this->signMethod = $signMethod;
  152 + }
  153 +
  154 + /**
  155 + * @return string
  156 + */
  157 + public function getSignMethod(): string
  158 + {
  159 + return $this->signMethod;
  160 + }
161 } 161 }
@@ -72,15 +72,6 @@ class Builder extends Config @@ -72,15 +72,6 @@ class Builder extends Config
72 return $this; 72 return $this;
73 } 73 }
74 74
75 - /**  
76 - * @param string $v  
77 - * @return $this  
78 - */  
79 - public function v(string $v): Builder  
80 - {  
81 - $this->requestHandler->setV($v);  
82 - return $this;  
83 - }  
84 75
85 /** 76 /**
86 * @param $timestamp 77 * @param $timestamp
@@ -141,10 +132,7 @@ class Builder extends Config @@ -141,10 +132,7 @@ class Builder extends Config
141 $mock = new Mock($this->request, $this->method); 132 $mock = new Mock($this->request, $this->method);
142 return $mock->response(); 133 return $mock->response();
143 } 134 }
144 - try {  
145 - return $client->request($this->requestHandler);  
146 - } catch (GuzzleException $e) {  
147 - throw new ClientException($e);  
148 - } 135 +
  136 + return $client->request($this->requestHandler);
149 } 137 }
150 } 138 }
@@ -47,7 +47,7 @@ class Client @@ -47,7 +47,7 @@ class Client
47 $sig->setMethod($request->getService()); 47 $sig->setMethod($request->getService());
48 $sig->setAppKey($request->config->getAppKey()); 48 $sig->setAppKey($request->config->getAppKey());
49 $sig->setAppSecret($request->config->getAppSecret()); 49 $sig->setAppSecret($request->config->getAppSecret());
50 - $sig->setVersion($request->getV()); 50 + $sig->setVersion($request->config->getVersion());
51 $params = $request->getParams(); 51 $params = $request->getParams();
52 Sort::ksort($params); 52 Sort::ksort($params);
53 $sig->setParamJson(Json::marshal($params)); 53 $sig->setParamJson(Json::marshal($params));
@@ -63,6 +63,7 @@ class Client @@ -63,6 +63,7 @@ class Client
63 protected function withSignatureQuery(Request $request): array 63 protected function withSignatureQuery(Request $request): array
64 { 64 {
65 $params = $request->getParams(); 65 $params = $request->getParams();
  66 +
66 Sort::ksort($params); 67 Sort::ksort($params);
67 68
68 return [ 69 return [
@@ -73,7 +74,7 @@ class Client @@ -73,7 +74,7 @@ class Client
73 'timestamp' => $request->getTimestamp(), 74 'timestamp' => $request->getTimestamp(),
74 'v' => $request->getV(), 75 'v' => $request->getV(),
75 'sign' => $this->signature($request), 76 'sign' => $this->signature($request),
76 - 'sign_method' => 'hmac-sha256', 77 + 'sign_method' => $request->config->getSignMethod(),
77 ]; 78 ];
78 } 79 }
79 80
@@ -2,28 +2,24 @@ @@ -2,28 +2,24 @@
2 2
3 namespace Lackoxygen\TiktokShop\Util; 3 namespace Lackoxygen\TiktokShop\Util;
4 4
5 -class Signature  
6 -{  
7 - private string $appKey;  
8 -  
9 - private string $appSecret; 5 +use Lackoxygen\TiktokShop\Attribute\Config\Config;
10 6
  7 +class Signature extends Config
  8 +{
  9 + /**
  10 + * @var string
  11 + */
11 private string $method; 12 private string $method;
12 13
  14 + /**
  15 + * @var string
  16 + */
13 private string $timestamp; 17 private string $timestamp;
14 18
15 - private string $paramJson;  
16 -  
17 - private string $version;  
18 -  
19 /** 19 /**
20 - * @param mixed $appKey 20 + * @var string
21 */ 21 */
22 - public function setAppKey(string $appKey): Signature  
23 - {  
24 - $this->appKey = $appKey;  
25 - return $this;  
26 - } 22 + private string $paramJson;
27 23
28 /** 24 /**
29 * @param mixed $method 25 * @param mixed $method
@@ -43,14 +39,6 @@ class Signature @@ -43,14 +39,6 @@ class Signature
43 return $this; 39 return $this;
44 } 40 }
45 41
46 - /**  
47 - * @param mixed $appSecret  
48 - */  
49 - public function setAppSecret($appSecret): Signature  
50 - {  
51 - $this->appSecret = $appSecret;  
52 - return $this;  
53 - }  
54 42
55 /** 43 /**
56 * @param mixed $paramJson 44 * @param mixed $paramJson
@@ -62,23 +50,42 @@ class Signature @@ -62,23 +50,42 @@ class Signature
62 } 50 }
63 51
64 /** 52 /**
65 - * @param mixed $version 53 + * @return string
66 */ 54 */
67 - public function setVersion($version): Signature 55 + public function generate()
68 { 56 {
69 - $this->version = $version;  
70 - return $this; 57 + $paramPattern = 'app_key' . $this->config->getAppKey(
  58 + ) . 'method' . $this->method . 'param_json' . $this->paramJson .
  59 + 'timestamp' . $this->timestamp . 'v' . $this->config->getVersion();
  60 +
  61 + $body = $this->config->getAppSecret() . $paramPattern . $this->config->getAppSecret();
  62 +
  63 + switch ($this->config->getSignMethod()) {
  64 + case 'hmac-sha256':
  65 + case 'sha256':
  66 + return $this->sha256($body);
  67 + case 'md5':
  68 + return $this->md5($body);
  69 + default:
  70 + return '';
  71 + }
71 } 72 }
72 73
73 /** 74 /**
  75 + * @param string $body
74 * @return false|string 76 * @return false|string
75 */ 77 */
76 - public function generate() 78 + protected function sha256(string $body): string
  79 + {
  80 + return (string)\hash_hmac("sha256", $body, $this->config->getAppSecret());
  81 + }
  82 +
  83 + /**
  84 + * @param string $body
  85 + * @return string
  86 + */
  87 + protected function md5(string $body): string
77 { 88 {
78 - $paramPattern = 'app_key' . $this->appKey . 'method' .  
79 - $this->method . 'param_json' . $this->paramJson .  
80 - 'timestamp' . $this->timestamp . 'v' . $this->version;  
81 - $signPattern = $this->appSecret . $paramPattern . $this->appSecret;  
82 - return hash_hmac("sha256", $signPattern, $this->appSecret); 89 + return \md5($body . $this->config->getAppSecret());
83 } 90 }
84 } 91 }