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