authorise   A
last analyzed

Complexity

Total Complexity 32

Size/Duplication

Total Lines 184
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 79
c 3
b 0
f 0
dl 0
loc 184
rs 9.84
wmc 32

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isGrantType() 0 3 1
A user() 0 10 2
A __construct() 0 5 1
B getJWTObject() 0 34 11
C authorise() 0 97 17
1
<?php
2
3
/**
4
 * ==================================
5
 * Responsible PHP API
6
 * ==================================
7
 *
8
 * @link Git https://github.com/vince-scarpa/responsibleAPI.git
9
 *
10
 * @api Responible API
11
 * @package responsible\core\oauth
12
 *
13
 * @author Vince scarpa <[email protected]>
14
 *
15
 */
16
17
namespace responsible\core\auth;
18
19
use responsible\core\auth;
20
use responsible\core\configuration;
21
use responsible\core\user;
22
use responsible\core\server;
23
24
class authorise extends server
25
{
26
    /**
27
     * [$user]
28
     * @var object
29
     */
30
    public $user;
31
32
    /**
33
     * [__construct Inherit Responsible API options]
34
     */
35
    public function __construct($options)
36
    {
37
        $this->setOptions($options);
38
        $this->config = new configuration\config();
39
        $this->config->responsibleDefault($options);
40
    }
41
42
    /**
43
     * [auth]
44
     * @return boolean|object
45
     */
46
    public function authorise()
47
    {
48
        /**
49
         * Ignore if debug mode is initiated in Responsible API options
50
         */
51
        if ($this->getRequestType() == 'debug') {
52
            $this->grantAccess = true;
53
            return true;
54
        }
55
56
        /**
57
         * Check if a custom scope is set
58
         */
59
        if (
60
            isset($this->header->getMethod()->data['scope']) &&
61
            ($this->header->getMethod()->data['scope'] == 'anonymous')
62
        ) {
63
            $this->grantAccess = true;
64
            return true;
65
        }
66
67
        if (isset($this->getOptions()['systemUser']) && !empty($this->getOptions()['systemUser'])) {
68
            $this->header
69
                ->setHeader('Authorization', array(
70
                    'Bearer', $this->getOptions()['systemUser']['token'],
71
                ), "", "");
72
        }
73
74
        /**
75
         * Scan for a header Authorization Bearer Json Web Token
76
         * -- If not set header will return an unauthorised message
77
         */
78
        $token = $this->header->authorizationHeaders();
79
80
        if (isset($token['client_access_request']) && !empty($token['client_access_request'])) {
81
            $this->user = (object) $token['client_access_request'];
82
            $this->grantAccess = true;
83
        } else {
84
85
            /**
86
             * [$jwt Decode the JWT]
87
             * @var auth\jwt
88
             */
89
            $jwt = new auth\jwt();
90
            $decoded = $jwt
91
                ->setOptions($this->getOptions())
92
                ->token($token)
93
                ->key('payloadOnly')
94
                ->decode()
95
            ;
96
97
            if (isset($decoded['sub']) && !empty($decoded['sub'])) {
98
                $this->user = (object) (new user\user())
99
                    ->setOptions($this->getOptions())
100
                    ->load($decoded['sub'], ['refreshToken' => true])
101
                ;
102
103
                $secretKey = $this->user->secret;
104
                if (
105
                    isset($this->getOptions()['jwt']['signWith']) &&
106
                    !empty($this->getOptions()['jwt']['signWith'])
107
                ) {
108
                    $secretKey = $this->getOptions()['jwt']['signWith'];
109
                }
110
111
                if (!empty($this->user)) {
112
                    $jwt = new auth\jwt();
113
                    $decoded = $jwt
114
                        ->setOptions($this->getOptions())
115
                        ->token($token)
116
                        ->key($secretKey)
117
                        ->decode()
118
                    ;
119
                }
120
            } else {
121
                $this->header->unauthorised();
122
            }
123
        }
124
125
        /**
126
         * [$user Check user account]
127
         * @var [object]
128
         */
129
        if ((isset($decoded['sub']) && !empty($decoded['sub'])) && !$this->user) {
130
            $this->user = (object) (new user\user())
131
                ->setOptions($this->getOptions())
132
                ->load($decoded['sub'], ['refreshToken' => true])
133
            ;
134
        }
135
136
        /**
137
         *  Account not found / doesn't exist
138
         */
139
        if (empty($this->user)) {
140
            $this->header->unauthorised();
141
        }
142
        return false;
143
    }
144
145
    /**
146
     * [user]
147
     * @return object
148
     */
149
    public function user()
150
    {
151
        if ($this->isGrantType()) {
152
            return (object) [
153
                'uid' => -1,
154
                'account_id' => 0,
155
                'scope' => 'anonymous',
156
            ];
157
        }
158
        return $this->user;
159
    }
160
161
    /**
162
     * [isGrantType If grant type is set then allow system scope override]
163
     * @return boolean
164
     */
165
    public function isGrantType()
166
    {
167
        return $this->grantAccess;
168
    }
169
170
    /**
171
     * [getJWTToken Get the user JWT refresh object]
172
     * @return boolean|null
173
     */
174
    public function getJWTObject($objectKey, $array = null)
175
    {
176
        if ($this->getRequestType() == 'debug') {
177
            return;
178
        }
179
180
        if (
181
            isset($this->header->getMethod()->data['scope']) &&
182
            ($this->header->getMethod()->data['scope'] == 'anonymous')
183
        ) {
184
            return;
185
        }
186
187
        if (is_null($this->user)) {
188
            return;
189
        }
190
191
        $haystack = (is_null($array)) ? $this->user->refreshToken : $array;
192
193
        if (isset($haystack[$objectKey])) {
194
            return $haystack[$objectKey];
195
        }
196
197
        if (is_array($haystack)) {
198
            foreach ($haystack as $key => $value) {
199
                if (is_array($value)) {
200
                    return $this->getJWTObject($objectKey, $value);
201
                }
202
                if (false !== stripos($key, $objectKey)) {
203
                    return $haystack[$key];
204
                }
205
            }
206
        }
207
        return false;
208
    }
209
}
210