1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CodeCloud\Bundle\ShopifyBundle\Controller; |
4
|
|
|
|
5
|
|
|
use CodeCloud\Bundle\ShopifyBundle\Event\PreAuthEvent; |
6
|
|
|
use CodeCloud\Bundle\ShopifyBundle\Model\ShopifyStoreManagerInterface; |
7
|
|
|
use GuzzleHttp\ClientInterface; |
|
|
|
|
8
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
9
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
|
|
|
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
|
|
|
11
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
|
|
|
|
12
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
13
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
|
|
|
14
|
|
|
|
15
|
|
|
class OAuthController |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var UrlGeneratorInterface |
19
|
|
|
*/ |
20
|
|
|
private $router; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private $config; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var ClientInterface |
29
|
|
|
*/ |
30
|
|
|
private $client; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var ShopifyStoreManagerInterface |
34
|
|
|
*/ |
35
|
|
|
private $stores; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var EventDispatcherInterface |
39
|
|
|
*/ |
40
|
|
|
private $dispatcher; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param UrlGeneratorInterface $router |
44
|
|
|
* @param array $config |
45
|
|
|
* @param ClientInterface $client |
46
|
|
|
* @param ShopifyStoreManagerInterface $stores |
47
|
|
|
* @param EventDispatcherInterface $dispatcher |
48
|
|
|
*/ |
49
|
|
|
public function __construct( |
50
|
|
|
UrlGeneratorInterface $router, |
51
|
|
|
array $config, |
52
|
|
|
ClientInterface $client, |
53
|
|
|
ShopifyStoreManagerInterface $stores, |
54
|
|
|
EventDispatcherInterface $dispatcher |
55
|
|
|
) { |
56
|
|
|
$this->router = $router; |
57
|
|
|
$this->client = $client; |
58
|
|
|
$this->stores = $stores; |
59
|
|
|
$this->config = (new OptionsResolver()) |
60
|
|
|
->setRequired(['api_key', 'shared_secret', 'scope', 'redirect_route']) |
61
|
|
|
->resolve($config) |
62
|
|
|
; |
63
|
|
|
$this->dispatcher = $dispatcher; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Handles initial auth request from Shopify. |
68
|
|
|
* |
69
|
|
|
* @param Request $request |
70
|
|
|
* @return RedirectResponse |
71
|
|
|
*/ |
72
|
|
|
public function auth(Request $request) |
73
|
|
|
{ |
74
|
|
|
if (!$storeName = $request->get('shop')) { |
75
|
|
|
throw new BadRequestHttpException('Request is missing required parameter "shop".'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if ($response = $this->dispatcher->dispatch( |
79
|
|
|
PreAuthEvent::NAME, |
80
|
|
|
new PreAuthEvent($storeName))->getResponse() |
|
|
|
|
81
|
|
|
) { |
82
|
|
|
return $response; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$verifyUrl = $this->router->generate('codecloud_shopify_verify', [], UrlGeneratorInterface::ABSOLUTE_URL); |
86
|
|
|
$verifyUrl = str_replace("http://", "https://", $verifyUrl); |
87
|
|
|
|
88
|
|
|
$params = [ |
89
|
|
|
'client_id' => $this->config['api_key'], |
90
|
|
|
'scope' => $this->config['scope'], |
91
|
|
|
'redirect_uri' => $verifyUrl, |
92
|
|
|
]; |
93
|
|
|
|
94
|
|
|
$shopifyEndpoint = 'https://%s/admin/oauth/authorize?%s'; |
95
|
|
|
$url = sprintf($shopifyEndpoint, $storeName, http_build_query($params)); |
96
|
|
|
|
97
|
|
|
return new RedirectResponse($url); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Handles auth verification callback from Shopify. |
102
|
|
|
* |
103
|
|
|
* @param Request $request |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
public function verify(Request $request) |
107
|
|
|
{ |
108
|
|
|
$authCode = $request->get('code'); |
109
|
|
|
$storeName = $request->get('shop'); |
110
|
|
|
|
111
|
|
|
if (!$authCode || !$storeName) { |
112
|
|
|
throw new BadRequestHttpException('Request is missing required parameters: "code", "shop".'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$params = [ |
116
|
|
|
'body' => \GuzzleHttp\json_encode([ |
117
|
|
|
'client_id' => $this->config['api_key'], |
118
|
|
|
'client_secret' => $this->config['shared_secret'], |
119
|
|
|
'code' => $authCode |
120
|
|
|
]), |
121
|
|
|
'headers' => [ |
122
|
|
|
'Content-Type' => 'application/json', |
123
|
|
|
], |
124
|
|
|
]; |
125
|
|
|
|
126
|
|
|
$response = $this->client->request('POST', 'https://' . $storeName . '/admin/oauth/access_token', $params); |
127
|
|
|
$responseJson = \GuzzleHttp\json_decode($response->getBody(), true); |
128
|
|
|
|
129
|
|
|
$accessToken = $responseJson['access_token']; |
130
|
|
|
$this->stores->authenticateStore($storeName, $accessToken); |
131
|
|
|
|
132
|
|
|
return new RedirectResponse( |
133
|
|
|
$this->router->generate($this->config['redirect_route'], $request->query->all()) |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths