5
5
use Closure ;
6
6
use Illuminate \Http \Request ;
7
7
use Illuminate \Support \Facades \Auth ;
8
- use Vin \ ShopwareSdk \ Data \ Webhook \ ShopRequest ;
8
+ use Sas \ ShopwareLaravelSdk \ Models \ SwShop ;
9
9
use Sas \ShopwareLaravelSdk \Repositories \ShopRepository ;
10
+ use Vin \ShopwareSdk \Data \Webhook \Shop ;
11
+ use Vin \ShopwareSdk \Data \Webhook \ShopRequest ;
10
12
use Vin \ShopwareSdk \Exception \AuthorizationFailedException ;
11
13
use Vin \ShopwareSdk \Service \WebhookAuthenticator ;
12
14
13
15
class SwAppMiddleware
14
16
{
15
- private ShopRepository $ shopRepository ;
17
+ public const REQUIRED_KEYS = [
18
+ ShopRequest::SHOP_ID_REQUEST_PARAMETER ,
19
+ ShopRequest::SHOP_URL_REQUEST_PARAMETER ,
20
+ ShopRequest::SHOPWARE_VERSION_REQUEST_PARAMETER ,
21
+ ShopRequest::SHOP_SIGNATURE_REQUEST_PARAMETER ,
22
+ ShopRequest::TIME_STAMP_REQUEST_PARAMETER ,
23
+ ];
24
+
25
+ protected ShopRepository $ shopRepository ;
16
26
17
27
public function __construct (ShopRepository $ shopRepository )
18
28
{
@@ -22,37 +32,21 @@ public function __construct(ShopRepository $shopRepository)
22
32
/**
23
33
* Handle an incoming request.
24
34
*
25
- * @param \Illuminate\Http\ Request $request
26
- * @param \ Closure $next
27
- * @param string|null ...$guards
35
+ * @param Request $request
36
+ * @param Closure $next
37
+ * @param string|null ...$guards
28
38
* @return mixed
29
39
*/
30
40
public function handle (Request $ request , Closure $ next , ...$ guards )
31
41
{
32
- $ authenticated = false ;
33
42
$ shop = null ;
34
43
35
44
if ($ request ->getMethod () === 'POST ' && $ this ->supportsPostRequest ($ request )) {
36
- $ requestContent = json_decode ($ request ->getContent (), true );
37
- $ shopId = $ requestContent ['source ' ][ShopRequest::SHOP_ID_REQUEST_PARAMETER ];
38
-
39
- $ shop = $ this ->shopRepository ->getShopById ($ shopId );
40
-
41
- $ authenticated = $ shop && WebhookAuthenticator::authenticatePostRequest ($ shop ->shop_secret );
45
+ $ shop = $ this ->authenticatePostRequest ($ request );
42
46
} elseif ($ request ->getMethod () === 'GET ' && $ this ->supportsGetRequest ($ request )) {
43
- $ shopId = $ request ->query ->get (ShopRequest::SHOP_ID_REQUEST_PARAMETER );
44
- $ shop = $ this ->shopRepository ->getShopById ($ shopId );
45
-
46
- $ authenticated = $ shop && WebhookAuthenticator::authenticateGetRequest ($ shop ->shop_secret );
47
- }elseif ($ request ->getMethod () === 'DELETE ' && $ this ->supportsGetRequest ($ request )) {
48
- $ shopId = $ request ->query ->get (ShopRequest::SHOP_ID_REQUEST_PARAMETER );
49
- $ shop = $ this ->shopRepository ->getShopById ($ shopId );
50
-
51
- $ authenticated = $ shop && WebhookAuthenticator::authenticateGetRequest ($ shop ->shop_secret );
52
- }
53
-
54
- if (!$ authenticated ) {
55
- throw new AuthorizationFailedException ($ request ->getMethod () . ' is not supported or the data is invalid ' );
47
+ $ shop = $ this ->authenticateGetRequest ($ request );
48
+ } elseif ($ request ->getMethod () === 'DELETE ' && $ this ->supportsGetRequest ($ request )) {
49
+ $ shop = $ this ->authenticateDeleteRequest ($ request );
56
50
}
57
51
58
52
// TODO: set custom guard for app
@@ -63,7 +57,18 @@ public function handle(Request $request, Closure $next, ...$guards)
63
57
return $ next ($ request );
64
58
}
65
59
66
- private function supportsPostRequest (Request $ request ): bool
60
+ protected function checkRequiredKeys (array $ data ): bool
61
+ {
62
+ foreach (self ::REQUIRED_KEYS as $ key ) {
63
+ if (!array_key_exists ($ key , $ data )) {
64
+ return false ;
65
+ }
66
+ }
67
+
68
+ return true ;
69
+ }
70
+
71
+ protected function supportsPostRequest (Request $ request ): bool
67
72
{
68
73
$ requestContent = json_decode ($ request ->getContent (), true );
69
74
@@ -76,26 +81,53 @@ private function supportsPostRequest(Request $request): bool
76
81
return $ this ->checkRequiredKeys ($ requestContent ['source ' ]);
77
82
}
78
83
79
- private function supportsGetRequest (Request $ request ): bool
84
+ protected function supportsGetRequest (Request $ request ): bool
80
85
{
81
86
return $ this ->checkRequiredKeys ($ request ->query ->all ());
82
87
}
83
88
84
- private function checkRequiredKeys (array $ data ): bool {
85
- $ requiredKeys = [
86
- ShopRequest::SHOP_ID_REQUEST_PARAMETER ,
87
- ShopRequest::SHOP_URL_REQUEST_PARAMETER ,
88
- ShopRequest::SHOPWARE_VERSION_REQUEST_PARAMETER ,
89
- ShopRequest::SHOP_SIGNATURE_REQUEST_PARAMETER ,
90
- ShopRequest::TIME_STAMP_REQUEST_PARAMETER ,
91
- ];
89
+ protected function authenticatePostRequest (Request $ request ): SwShop
90
+ {
91
+ $ requestContent = json_decode ($ request ->getContent (), true );
92
+ $ sourceRequest = $ requestContent ['source ' ];
93
+ $ shopId = $ sourceRequest [ShopRequest::SHOP_ID_REQUEST_PARAMETER ];
92
94
93
- foreach ($ requiredKeys as $ key ) {
94
- if (!array_key_exists ($ key , $ data )) {
95
- return false ;
96
- }
95
+ $ shop = $ this ->shopRepository ->getShopById ($ shopId );
96
+
97
+ $ authenticated = $ shop && WebhookAuthenticator::authenticatePostRequest ($ shop ->shop_secret );
98
+
99
+ if (!$ authenticated ) {
100
+ throw new AuthorizationFailedException ($ request ->getMethod () . ' is not supported or the data is invalid ' );
97
101
}
98
102
99
- return true ;
103
+ return $ shop ;
104
+ }
105
+
106
+ protected function authenticateGetRequest (Request $ request ): SwShop
107
+ {
108
+ $ shopId = $ request ->query ->get (ShopRequest::SHOP_ID_REQUEST_PARAMETER );
109
+ $ shop = $ this ->shopRepository ->getShopById ($ shopId );
110
+
111
+ $ authenticated = $ shop && WebhookAuthenticator::authenticateGetRequest ($ shop ->shop_secret );
112
+
113
+ if (!$ authenticated ) {
114
+ throw new AuthorizationFailedException ($ request ->getMethod () . ' is not supported or the data is invalid ' );
115
+ }
116
+
117
+ return $ shop ;
118
+ }
119
+
120
+ protected function authenticateDeleteRequest (Request $ request ): SwShop
121
+ {
122
+ $ shopId = $ request ->query ->get (ShopRequest::SHOP_ID_REQUEST_PARAMETER );
123
+ $ shop = $ this ->shopRepository ->getShopById ($ shopId );
124
+
125
+ $ authenticated = $ shop && WebhookAuthenticator::authenticateGetRequest ($ shop ->shop_secret );
126
+
127
+ if (!$ authenticated ) {
128
+ throw new AuthorizationFailedException ($ request ->getMethod () . ' is not supported or the data is invalid ' );
129
+ }
130
+
131
+ return $ shop ;
100
132
}
101
133
}
0 commit comments