1 | <?php |
||
27 | class ShopifyAuthAuthenticate extends BaseAuthenticate |
||
|
|||
28 | { |
||
29 | |||
30 | public $api_key; |
||
31 | private $ShopifyAPI; |
||
32 | private $ShopifyDatabase; |
||
33 | |||
34 | public function __construct($registry, array $config = []) |
||
35 | { |
||
36 | parent::__construct($registry, $config); |
||
37 | |||
38 | $this->api_key = isset($config['api_key']) ? $config['api_key'] : ''; |
||
39 | |||
40 | if (empty($this->api_key)) { |
||
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 | $this->ShopifyAPI = $registry->load( |
||
49 | 'Multidimensional/Cakephpify.ShopifyAPI', [ |
||
50 | 'api_key' => $this->api_key |
||
51 | ] |
||
52 | ); |
||
53 | |||
54 | $this->ShopifyDatabase = $registry->load('Multidimensional/Cakephpify.ShopifyDatabase'); |
||
55 | } |
||
56 | |||
57 | public function authenticate(Request $request, Response $response) |
||
58 | { |
||
59 | return $this->getUser($request); |
||
60 | } |
||
61 | |||
62 | public function unauthenticated(Request $request, Response $response) |
||
63 | { |
||
64 | if (isset($request->query['hmac']) |
||
65 | && isset($request->query['shop']) |
||
66 | ) { |
||
67 | return null; |
||
68 | } |
||
69 | |||
70 | if (empty($this->api_key)) { |
||
71 | return null; |
||
72 | } |
||
73 | |||
74 | if (!empty($request->session()->read('shopify_access_token_' . $this->api_key)) |
||
75 | && !empty($request->session()->read('shopify_shop_domain_' . $this->api_key)) |
||
76 | ) { |
||
77 | return null; |
||
78 | } |
||
79 | |||
80 | $request->session()->delete('shopify_access_token_' . $this->api_key); |
||
81 | $request->session()->delete('shopify_shop_domain_' . $this->api_key); |
||
82 | |||
83 | return $response->location($this->_generateLoginUrl()); |
||
84 | } |
||
85 | |||
86 | public function getUser(Request $request) |
||
87 | { |
||
88 | $accessToken = $request->session()->read('shopify_access_token_' . $this->api_key); |
||
89 | $shopDomain = $request->session()->read('shopify_shop_domain_' . $this->api_key); |
||
90 | |||
91 | if ($shopDomain) { |
||
92 | $this->ShopifyAPI->setShopDomain($shopDomain); |
||
93 | } |
||
94 | |||
95 | if ((isset($request->query['hmac']) && isset($request->query['shop'])) |
||
96 | && (!$shopDomain || $request->query['shop'] != $shopDomain) |
||
97 | ) { |
||
98 | $isValid = $this->ShopifyAPI->validateHMAC($request->query); |
||
99 | if ($isValid) { |
||
100 | $shopDomain = $this->ShopifyAPI->setShopDomain($request->query['shop']); |
||
101 | |||
102 | if (isset($request->query['code'])) { |
||
103 | $accessToken = $this->ShopifyAPI->getAccessToken($shopDomain, $request->query['code']); |
||
104 | } else { |
||
105 | $accessToken = $this->ShopifyDatabase->getAccessTokenFromShopDomain($shopDomain, $this->api_key); |
||
106 | } |
||
107 | } |
||
108 | } |
||
109 | |||
110 | if ($accessToken) { |
||
111 | $this->ShopifyAPI->setAccessToken($accessToken); |
||
112 | $this->ShopifyAPI->setShopDomain($shopDomain); |
||
113 | |||
114 | $request->session()->write('shopify_access_token_' . $this->api_key, $accessToken); |
||
115 | $request->session()->write('shopify_shop_domain_' . $this->api_key, $shopDomain); |
||
116 | |||
117 | $shop = $this->ShopifyDatabase->getShopDataFromAccessToken($accessToken, $this->api_key); |
||
118 | |||
119 | if ($shop && is_array($shop)) { |
||
120 | return ['id' => $shop['id'], 'username' => $shop['myshopify_domain']]; |
||
121 | } |
||
122 | } |
||
123 | |||
124 | return false; |
||
125 | } |
||
126 | |||
127 | protected function _authenticate(Request $request) |
||
128 | { |
||
129 | } |
||
130 | |||
131 | public function implementedEvents() |
||
132 | { |
||
133 | return [ |
||
134 | 'Auth.afterIdentify' => 'afterIdentify', |
||
135 | 'Auth.logout' => 'logout' |
||
136 | ]; |
||
137 | } |
||
138 | |||
139 | public function afterIdentify(Event $event, array $user) |
||
140 | { |
||
141 | } |
||
142 | |||
143 | public function logout(Event $event, array $user) |
||
144 | { |
||
145 | //$request->session()->delete('shopify_access_token_' . $this->api_key); |
||
146 | //$request->session()->delete('shopify_shop_domain_' . $this->api_key); |
||
147 | } |
||
148 | |||
149 | private function _generateLoginUrl() |
||
153 | } |
||
154 |
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
.