作者 lackoxygen

add:app change

@@ -74,7 +74,7 @@ class Signature extends Config @@ -74,7 +74,7 @@ class Signature extends Config
74 * @param string $body 74 * @param string $body
75 * @return false|string 75 * @return false|string
76 */ 76 */
77 - protected function sha256(string $body): string 77 + public function sha256(string $body): string
78 { 78 {
79 return (string)\hash_hmac("sha256", $body, $this->config->getAppSecret()); 79 return (string)\hash_hmac("sha256", $body, $this->config->getAppSecret());
80 } 80 }
@@ -83,7 +83,7 @@ class Signature extends Config @@ -83,7 +83,7 @@ class Signature extends Config
83 * @param string $body 83 * @param string $body
84 * @return string 84 * @return string
85 */ 85 */
86 - protected function md5(string $body): string 86 + public function md5(string $body): string
87 { 87 {
88 return \md5($body); 88 return \md5($body);
89 } 89 }
@@ -2,22 +2,95 @@ @@ -2,22 +2,95 @@
2 2
3 namespace Lackoxygen\TiktokShop\Support; 3 namespace Lackoxygen\TiktokShop\Support;
4 4
  5 +use Illuminate\Support\Arr;
  6 +use Lackoxygen\TiktokShop\Application;
5 use Lackoxygen\TiktokShop\Attribute\Config\Config; 7 use Lackoxygen\TiktokShop\Attribute\Config\Config;
  8 +use Lackoxygen\TiktokShop\Attribute\Config\Option;
  9 +use Lackoxygen\TiktokShop\ServiceProvider;
  10 +use Psr\Http\Message\RequestInterface;
6 11
7 class Verify extends Config 12 class Verify extends Config
8 { 13 {
9 /** 14 /**
10 - * @param $sign 15 + * @var Signature
  16 + */
  17 + protected Signature $signature;
  18 +
  19 + /**
  20 + * @param Option $option
  21 + */
  22 + public function __construct(Option $option)
  23 + {
  24 + parent::__construct($option);
  25 +
  26 + $this->signature = new Signature($option);
  27 + }
  28 +
  29 + /**
  30 + * @param string $sign
11 * @param string $body 31 * @param string $body
12 * @return bool 32 * @return bool
13 */ 33 */
14 - public function md5($sign, string $body): bool 34 + public function md5(string $sign, string $body): bool
15 { 35 {
16 - $md5 = md5( 36 + $md5 = $this->signature->md5(
17 $this->config->getAppKey() . 37 $this->config->getAppKey() .
18 $body . 38 $body .
19 $this->config->getAppSecret() 39 $this->config->getAppSecret()
20 ); 40 );
21 return $md5 === $sign; 41 return $md5 === $sign;
22 } 42 }
  43 +
  44 + /**
  45 + * @param string $sign
  46 + * @param string $body
  47 + * @return bool
  48 + */
  49 + public function sha256(string $sign, string $body): bool
  50 + {
  51 + $sha256 = $this->signature->sha256(
  52 + $body
  53 + );
  54 +
  55 + return $sha256 === $sign;
  56 + }
  57 +
  58 + /**
  59 + * @param RequestInterface $request
  60 + * @return bool|void
  61 + */
  62 + public function through(RequestInterface $request)
  63 + {
  64 + $signMethod = $request->getHeaderLine('sign-method');
  65 +
  66 + $eventSign = $request->getHeaderLine('event-sign');
  67 +
  68 + $appId = $request->getHeaderLine('app-id');
  69 +
  70 + $configs = \config(ServiceProvider::$name);
  71 +
  72 + $appName = null;
  73 +
  74 + foreach ($configs as $name => $config) {
  75 + if (Arr::get($config, 'app_key') === $appId) {
  76 + $appName = $name;
  77 + break;
  78 + }
  79 + }
  80 +
  81 + if (!$appName) {
  82 + throw new \InvalidArgumentException('Unknown application information');
  83 + }
  84 +
  85 + $config = Arr::get($configs, $appName);
  86 +
  87 + $this->config = Application::newConfigFormArray($config);
  88 +
  89 + switch ($signMethod) {
  90 + case 'hmac-sha256':
  91 + return $this->sha256($eventSign, $request->getBody());
  92 + case 'md5':
  93 + return $this->md5($eventSign, $request->getBody());
  94 + }
  95 + }
23 } 96 }