Completed
Push — master ( e78aa7...260413 )
by AJ
06:05
created

ShopifyAuthAuthenticate::implementedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
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
0 ignored issues
show
Coding Style introduced by
The property $api_key is not named in camelCase.

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.

Loading history...
29
{
30
31
    public $api_key;
0 ignored issues
show
Coding Style introduced by
$api_key does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
32
    private $ShopifyAPI;
0 ignored issues
show
Coding Style introduced by
$ShopifyAPI does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
33
    private $ShopifyDatabase;
0 ignored issues
show
Coding Style introduced by
$ShopifyDatabase does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
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
            $controller = $this->_registry->getController();
43
44
            if (isset($controller->request->api_key)) {
45
                $this->api_key = $controller->request->api_key;
46
            }
47
        }
48
49
        $this->ShopifyAPI = $registry->load('Multidimensional/Cakephpify.ShopifyAPI', [
50
            'api_key' => $this->api_key
51
        ]);
52
53
        $this->ShopifyDatabase = $registry->load('Multidimensional/Cakephpify.ShopifyDatabase');
54
    }
55
56
    public function authenticate(Request $request, Response $response)
57
    {
58
59
        return $this->getUser($request);
60
    }
61
62
    public function unauthenticated(Request $request, Response $response)
63
    {
64
65
        if (isset($request->query['hmac'])
66
            && isset($request->query['shop'])) {
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
            return null;
77
        }
78
79
        $request->session()->delete('shopify_access_token_' . $this->api_key);
80
        $request->session()->delete('shopify_shop_domain_' . $this->api_key);
81
82
        return $response->location($this->_generateLoginUrl());
83
    }
84
85
    public function getUser(Request $request)
86
    {
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
            $isValid = $this->ShopifyAPI->validateHMAC($request->query);
98
            if ($isValid) {
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
        if ($accessToken) {
110
            $this->ShopifyAPI->setAccessToken($accessToken);
111
            $this->ShopifyAPI->setShopDomain($shopDomain);
112
113
            $request->session()->write('shopify_access_token_' . $this->api_key, $accessToken);
114
            $request->session()->write('shopify_shop_domain_' . $this->api_key, $shopDomain);
115
116
            $shop = $this->ShopifyDatabase->getShopDataFromAccessToken($accessToken, $this->api_key);
117
118
            if ($shop && is_array($shop)) {
119
                return ['id' => $shop['id'], 'username' => $shop['myshopify_domain']];
120
            }
121
        }
122
123
        return false;
124
    }
125
126
    protected function _authenticate(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
127
    {
128
    }
129
130
    public function implementedEvents()
131
    {
132
        return [
133
            'Auth.afterIdentify' => 'afterIdentify',
134
            'Auth.logout' => 'logout'
135
        ];
136
    }
137
138
    public function afterIdentify(Event $event, array $user)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $user is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
139
    {
140
    }
141
142
    public function logout(Event $event, array $user)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $user is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
143
    {
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()
150
    {
151
        return Router::url(['controller' => 'Install', 'action' => 'index', 'plugin' => 'Multidimensional/Cakephpify']);
152
    }
153
}
154