/
OAuthController.php
177 lines (151 loc) · 5.39 KB
/
OAuthController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
namespace CodeCloud\Bundle\ShopifyBundle\Controller;
use CodeCloud\Bundle\ShopifyBundle\Event\PostAuthEvent;
use CodeCloud\Bundle\ShopifyBundle\Event\PreAuthEvent;
use CodeCloud\Bundle\ShopifyBundle\Exception\InsufficientScopeException;
use CodeCloud\Bundle\ShopifyBundle\Model\ShopifyStoreManagerInterface;
use CodeCloud\Bundle\ShopifyBundle\Security\HmacSignature;
use GuzzleHttp\ClientInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
/**
* Handles the OAuth handshake with Shopify.
*
* @see https://help.shopify.com/api/getting-started/authentication/oauth
*/
class OAuthController
{
/**
* @var UrlGeneratorInterface
*/
private $router;
/**
* @var array
*/
private $config;
/**
* @var ClientInterface
*/
private $client;
/**
* @var ShopifyStoreManagerInterface
*/
private $stores;
/**
* @var EventDispatcherInterface
*/
private $dispatcher;
/**
* @var HmacSignature
*/
private $hmacSignature;
/**
* @param UrlGeneratorInterface $router
* @param array $config
* @param ClientInterface $client
* @param ShopifyStoreManagerInterface $stores
* @param EventDispatcherInterface $dispatcher
* @param HmacSignature $hmacSignature
*/
public function __construct(
UrlGeneratorInterface $router,
array $config,
ClientInterface $client,
ShopifyStoreManagerInterface $stores,
EventDispatcherInterface $dispatcher,
HmacSignature $hmacSignature
) {
$this->router = $router;
$this->client = $client;
$this->stores = $stores;
$this->config = (new OptionsResolver())
->setRequired(['api_key', 'shared_secret', 'scope', 'redirect_route'])
->resolve($config)
;
$this->dispatcher = $dispatcher;
$this->hmacSignature = $hmacSignature;
}
/**
* Handles initial auth request from Shopify.
*
* @param Request $request
* @return RedirectResponse
*/
public function auth(Request $request)
{
if (!$storeName = $request->get('shop')) {
throw new BadRequestHttpException('Request is missing required parameter "shop".');
}
if ($response = $this->dispatcher->dispatch(
PreAuthEvent::NAME,
new PreAuthEvent($storeName))->getResponse()
) {
return $response;
}
$verifyUrl = $this->router->generate('codecloud_shopify_verify', [], UrlGeneratorInterface::ABSOLUTE_URL);
$verifyUrl = str_replace("http://", "https://", $verifyUrl);
$nonce = uniqid();
$this->stores->preAuthenticateStore($storeName, $nonce);
$params = [
'client_id' => $this->config['api_key'],
'scope' => $this->config['scope'],
'redirect_uri' => $verifyUrl,
'state' => $nonce,
];
$shopifyEndpoint = 'https://%s/admin/oauth/authorize?%s';
$url = sprintf($shopifyEndpoint, $storeName, http_build_query($params));
return new RedirectResponse($url);
}
/**
* Handles auth verification callback from Shopify.
*
* @param Request $request
* @return string
*/
public function verify(Request $request)
{
$authCode = $request->get('code');
$storeName = $request->get('shop');
$nonce = $request->get('state');
$hmac = $request->get('hmac');
// todo validate store name
// todo leverage options resolver?
if (!$authCode || !$storeName || !$nonce || !$hmac) {
throw new BadRequestHttpException('Request is missing one or more of required parameters: "code", "shop", "state", "hmac".');
}
if (!$this->hmacSignature->isValid($hmac, $request->query->all())) {
throw new BadRequestHttpException('Invalid HMAC Signature');
}
$params = [
'body' => \GuzzleHttp\json_encode([
'client_id' => $this->config['api_key'],
'client_secret' => $this->config['shared_secret'],
'code' => $authCode
]),
'headers' => [
'Content-Type' => 'application/json',
],
];
// todo this can fail - 400
$response = $this->client->request('POST', 'https://' . $storeName . '/admin/oauth/access_token', $params);
$responseJson = \GuzzleHttp\json_decode($response->getBody(), true);
if ($responseJson['scope'] != $this->config['scope']) {
throw new InsufficientScopeException($this->config['scope'], $responseJson['scope']);
}
$accessToken = $responseJson['access_token'];
$this->stores->authenticateStore($storeName, $accessToken, $nonce);
if ($response = $this->dispatcher->dispatch(
PostAuthEvent::NAME,
new PostAuthEvent($storeName, $accessToken))->getResponse()
) {
return $response;
}
return new RedirectResponse(
$this->router->generate($this->config['redirect_route'])
);
}
}