1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CakePHPify : CakePHP Plugin for Shopify API Authentication |
4
|
|
|
* Copyright (c) Multidimension.al (http://multidimension.al) |
5
|
|
|
* Github : https://github.com/multidimension-al/cakephpify |
6
|
|
|
* |
7
|
|
|
* Licensed under The MIT License |
8
|
|
|
* For full copyright and license information, please see the LICENSE file |
9
|
|
|
* Redistributions of files must retain the above copyright notice. |
10
|
|
|
* |
11
|
|
|
* @copyright (c) Multidimension.al (http://multidimension.al) |
12
|
|
|
* @link https://github.com/multidimension-al/cakephpify CakePHPify Github |
13
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Multidimensional\Shopify\Auth; |
17
|
|
|
|
18
|
|
|
use Cake\Core\Configure; |
19
|
|
|
use Cake\Routing\Router; |
20
|
|
|
use Cake\Controller\ComponentRegistry; |
21
|
|
|
use Cake\Auth\BaseAuthenticate; |
22
|
|
|
use Cake\Network\Request; |
23
|
|
|
use Cake\Network\Response; |
24
|
|
|
use Cake\Network\Session; |
25
|
|
|
|
26
|
|
|
use Multidimensional\Shopify\Auth\Event; |
27
|
|
|
|
28
|
|
|
class ShopifyAuthAuthenticate extends BaseAuthenticate { |
|
|
|
|
29
|
|
|
|
30
|
|
|
public $api_key; |
|
|
|
|
31
|
|
|
private $ShopifyAPI; |
|
|
|
|
32
|
|
|
private $ShopifyDatabase; |
|
|
|
|
33
|
|
|
|
34
|
|
|
public function __construct($registry, array $config = []) { |
35
|
|
|
parent::__construct($registry, $config); |
36
|
|
|
|
37
|
|
|
$this->api_key = isset($config['api_key']) ? $config['api_key'] : ''; |
38
|
|
|
|
39
|
|
|
if (empty($this->api_key)) { |
40
|
|
|
|
41
|
|
|
$controller = $this->_registry->getController(); |
42
|
|
|
|
43
|
|
|
if (isset($controller->request->api_key)) { |
44
|
|
|
$this->api_key = $controller->request->api_key; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$this->ShopifyAPI = $registry->load('Multidimensional/Shopify.ShopifyAPI', [ |
50
|
|
|
'api_key' => $this->api_key |
51
|
|
|
]); |
52
|
|
|
$this->ShopifyDatabase = $registry->load('Multidimensional/Shopify.ShopifyDatabase'); |
53
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function authenticate(Request $request, Response $response) { |
57
|
|
|
|
58
|
|
|
return $this->getUser($request); |
59
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function unauthenticated(Request $request, Response $response) { |
63
|
|
|
|
64
|
|
|
if (isset($request->query['hmac']) |
65
|
|
|
&& isset($request->query['shop'])) { |
66
|
|
|
return null; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (empty($this->api_key)) { |
70
|
|
|
return null; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if (!empty($request->session()->read('shopify_access_token_' . $this->api_key)) |
74
|
|
|
&& !empty($request->session()->read('shopify_shop_domain_' . $this->api_key))) { |
75
|
|
|
return null; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$request->session()->delete('shopify_access_token_' . $this->api_key); |
79
|
|
|
$request->session()->delete('shopify_shop_domain_' . $this->api_key); |
80
|
|
|
|
81
|
|
|
return $response->location($this->_generateLoginUrl()); |
82
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function getUser(Request $request) { |
86
|
|
|
|
87
|
|
|
$accessToken = $request->session()->read('shopify_access_token_' . $this->api_key); |
88
|
|
|
$shopDomain = $request->session()->read('shopify_shop_domain_' . $this->api_key); |
89
|
|
|
|
90
|
|
|
if ($shopDomain) { |
91
|
|
|
$this->ShopifyAPI->setShopDomain($shopDomain); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if ((isset($request->query['hmac']) && isset($request->query['shop'])) |
95
|
|
|
&& (!$shopDomain || $request->query['shop'] != $shopDomain)) { |
96
|
|
|
|
97
|
|
|
$is_valid = $this->ShopifyAPI->validateHMAC($request->query); |
|
|
|
|
98
|
|
|
if ($is_valid) { |
|
|
|
|
99
|
|
|
$shopDomain = $this->ShopifyAPI->setShopDomain($request->query['shop']); |
100
|
|
|
|
101
|
|
|
if (isset($request->query['code'])) { |
102
|
|
|
$accessToken = $this->ShopifyAPI->getAccessToken($shopDomain, $request->query['code']); |
103
|
|
|
} else { |
104
|
|
|
$accessToken = $this->ShopifyDatabase->getAccessTokenFromShopDomain($shopDomain, $this->api_key); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if ($accessToken) { |
111
|
|
|
|
112
|
|
|
$this->ShopifyAPI->setAccessToken($accessToken); |
113
|
|
|
$this->ShopifyAPI->setShopDomain($shopDomain); |
114
|
|
|
|
115
|
|
|
$request->session()->write('shopify_access_token_' . $this->api_key, $accessToken); |
116
|
|
|
$request->session()->write('shopify_shop_domain_' . $this->api_key, $shopDomain); |
117
|
|
|
|
118
|
|
|
$shop = $this->ShopifyDatabase->getShopDataFromAccessToken($accessToken, $this->api_key); |
119
|
|
|
|
120
|
|
|
if($shop && is_array($shop)){ |
121
|
|
|
return ['id' => $shop['id'], 'username' => $shop['myshopify_domain']]; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return false; |
127
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
protected function _authenticate(Request $request) { |
|
|
|
|
131
|
|
|
|
132
|
|
|
|
133
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function implementedEvents() { |
137
|
|
|
return [ |
138
|
|
|
'Auth.afterIdentify' => 'afterIdentify', |
139
|
|
|
'Auth.logout' => 'logout' |
140
|
|
|
]; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function afterIdentify(Event $event, array $user) { |
|
|
|
|
144
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function logout(Event $event, array $user) { |
|
|
|
|
148
|
|
|
|
149
|
|
|
//$request->session()->delete('shopify_access_token_' . $this->api_key); |
150
|
|
|
//$request->session()->delete('shopify_shop_domain_' . $this->api_key); |
151
|
|
|
|
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
private function _generateLoginUrl() { |
155
|
|
|
return Router::url(['controller' => 'Install', 'action' => 'index', 'plugin' => 'Multidimensional/Shopify']); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
} |
159
|
|
|
|
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString
.