作者 lackoxygen

test

... ... @@ -2,10 +2,12 @@
return [
'default' => [
'app_key' => env('TIKTOK_APP_KEY', ''),
'app_secret' => env('TIKTOK_APP_SECRET', ''),
'enable_mock' => env('TIKTOK_ENABLE_MOCK', false),
'base_uri' => env('TIKTOK_BASE_URI', 'https://openapi-sandbox.jinritemai.com'),
'timeout' => env('TIKTOK_TIMEOUT', 30),
'app_key' => '',
'app_secret' => '',
'enable_mock' => false,
'base_uri' => 'https://openapi-sandbox.jinritemai.com',
'timeout' => 5.0,
'sign_method' => 'hmac-sha256',
'version' => 2
]
];
... ...
... ... @@ -102,13 +102,14 @@ class Application
*/
public static function newConfigFormArray(array $options): Option
{
return new Option(
Arr::get($options, 'app_key'),
Arr::get($options, 'app_secret'),
Arr::get($options, 'base_uri'),
(float)Arr::get($options, 'timeout'),
Arr::get($options, 'enable_mock')
);
$option = new Option();
$option->setAppKey((string)Arr::get($options, 'app_key', ''));
$option->setAppSecret((string)Arr::get($options, 'app_secret', ''));
$option->setBaseUri((string)Arr::get($options, 'base_uri', 'https://openapi-sandbox.jinritemai.com'));
$option->setTimeout((float)Arr::get($options, 'timeout', 5));
$option->setEnableMock((bool)Arr::get($options, 'enable_mock'));
$option->setSignMethod((string)Arr::get($options, 'sign_method', 'sha256'));
return $option;
}
/**
... ...
... ... @@ -32,28 +32,17 @@ class Option
/**
* @var float
*/
private float $timeout;
private float $timeout = 5.0;
/**
* @param string $appKey
* @param string $appSecret
* @param string $baseUri
* @param float $timeout
* @param bool $enableMock
* @var string
*/
public function __construct(
string $appKey = '',
string $appSecret = '',
string $baseUri = '',
float $timeout = 30.0,
bool $enableMock = false
) {
$this->appKey = $appKey;
$this->appSecret = $appSecret;
$this->baseUri = $baseUri;
$this->timeout = $timeout;
$this->enableMock = $enableMock;
}
private string $signMethod = 'hmac-sha256';
/**
* @var string
*/
private string $version;
/**
* @param mixed $appKey
... ... @@ -151,4 +140,36 @@ class Option
{
return $this->timeout;
}
/**
* @param string $signMethod
*/
public function setSignMethod(string $signMethod): void
{
$this->signMethod = $signMethod;
}
/**
* @return string
*/
public function getSignMethod(): string
{
return $this->signMethod;
}
/**
* @param string $version
*/
public function setVersion(string $version): void
{
$this->version = $version;
}
/**
* @return string
*/
public function getVersion(): string
{
return $this->version;
}
}
... ...
... ... @@ -24,11 +24,6 @@ class Request
/**
* @var string
*/
private string $v = '2';
/**
* @var string
*/
private string $method = 'GET';
/**
... ... @@ -47,6 +42,11 @@ class Request
private bool $signature = true;
/**
* @var string
*/
private string $signMethod = 'sha256';
/**
* @return string
*/
public function getService(): string
... ... @@ -81,22 +81,6 @@ class Request
/**
* @return string
*/
public function getV(): string
{
return $this->v;
}
/**
* @param mixed $v
*/
public function setV($v): void
{
$this->v = $v;
}
/**
* @return string
*/
public function getMethod(): string
{
return $this->method;
... ... @@ -158,4 +142,20 @@ class Request
{
$this->path = $path;
}
/**
* @param string $signMethod
*/
public function setSignMethod(string $signMethod): void
{
$this->signMethod = $signMethod;
}
/**
* @return string
*/
public function getSignMethod(): string
{
return $this->signMethod;
}
}
... ...
... ... @@ -72,15 +72,6 @@ class Builder extends Config
return $this;
}
/**
* @param string $v
* @return $this
*/
public function v(string $v): Builder
{
$this->requestHandler->setV($v);
return $this;
}
/**
* @param $timestamp
... ... @@ -141,10 +132,7 @@ class Builder extends Config
$mock = new Mock($this->request, $this->method);
return $mock->response();
}
try {
return $client->request($this->requestHandler);
} catch (GuzzleException $e) {
throw new ClientException($e);
}
}
}
... ...
... ... @@ -47,7 +47,7 @@ class Client
$sig->setMethod($request->getService());
$sig->setAppKey($request->config->getAppKey());
$sig->setAppSecret($request->config->getAppSecret());
$sig->setVersion($request->getV());
$sig->setVersion($request->config->getVersion());
$params = $request->getParams();
Sort::ksort($params);
$sig->setParamJson(Json::marshal($params));
... ... @@ -63,6 +63,7 @@ class Client
protected function withSignatureQuery(Request $request): array
{
$params = $request->getParams();
Sort::ksort($params);
return [
... ... @@ -73,7 +74,7 @@ class Client
'timestamp' => $request->getTimestamp(),
'v' => $request->getV(),
'sign' => $this->signature($request),
'sign_method' => 'hmac-sha256',
'sign_method' => $request->config->getSignMethod(),
];
}
... ...
... ... @@ -2,28 +2,24 @@
namespace Lackoxygen\TiktokShop\Util;
class Signature
{
private string $appKey;
private string $appSecret;
use Lackoxygen\TiktokShop\Attribute\Config\Config;
class Signature extends Config
{
/**
* @var string
*/
private string $method;
/**
* @var string
*/
private string $timestamp;
private string $paramJson;
private string $version;
/**
* @param mixed $appKey
* @var string
*/
public function setAppKey(string $appKey): Signature
{
$this->appKey = $appKey;
return $this;
}
private string $paramJson;
/**
* @param mixed $method
... ... @@ -43,14 +39,6 @@ class Signature
return $this;
}
/**
* @param mixed $appSecret
*/
public function setAppSecret($appSecret): Signature
{
$this->appSecret = $appSecret;
return $this;
}
/**
* @param mixed $paramJson
... ... @@ -62,23 +50,42 @@ class Signature
}
/**
* @param mixed $version
* @return string
*/
public function setVersion($version): Signature
public function generate()
{
$this->version = $version;
return $this;
$paramPattern = 'app_key' . $this->config->getAppKey(
) . 'method' . $this->method . 'param_json' . $this->paramJson .
'timestamp' . $this->timestamp . 'v' . $this->config->getVersion();
$body = $this->config->getAppSecret() . $paramPattern . $this->config->getAppSecret();
switch ($this->config->getSignMethod()) {
case 'hmac-sha256':
case 'sha256':
return $this->sha256($body);
case 'md5':
return $this->md5($body);
default:
return '';
}
}
/**
* @param string $body
* @return false|string
*/
public function generate()
protected function sha256(string $body): string
{
return (string)\hash_hmac("sha256", $body, $this->config->getAppSecret());
}
/**
* @param string $body
* @return string
*/
protected function md5(string $body): string
{
$paramPattern = 'app_key' . $this->appKey . 'method' .
$this->method . 'param_json' . $this->paramJson .
'timestamp' . $this->timestamp . 'v' . $this->version;
$signPattern = $this->appSecret . $paramPattern . $this->appSecret;
return hash_hmac("sha256", $signPattern, $this->appSecret);
return \md5($body . $this->config->getAppSecret());
}
}
... ...