Total Complexity | 42 |
Total Lines | 245 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 0 |
Complex classes like headerAuth often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use headerAuth, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class headerAuth extends header |
||
24 | { |
||
25 | use \responsible\core\traits\optionsTrait; |
||
26 | |||
27 | /** |
||
28 | * [__construct] |
||
29 | */ |
||
30 | public function __construct() |
||
31 | {} |
||
32 | |||
33 | /** |
||
34 | * [authorizationHeaders Scan for "Authorization" header] |
||
35 | * @return string|array [mixed: string / error] |
||
36 | */ |
||
37 | public function authorizationHeaders($skipError = false) |
||
38 | { |
||
39 | if ($grant = $this->isGrantRequest()) { |
||
40 | return $grant; |
||
41 | } |
||
42 | |||
43 | if ($clientToken = $this->hasBearerToken()) { |
||
44 | return $clientToken; |
||
45 | } |
||
46 | |||
47 | if (!$skipError) { |
||
48 | $this->setUnauthorised(); |
||
49 | } |
||
50 | // @codeCoverageIgnoreStart |
||
51 | } |
||
52 | // @codeCoverageIgnoreEnd |
||
53 | |||
54 | /** |
||
55 | * [hasBearerValue Check if Authorization headers has Bearer value] |
||
56 | * @throws Exception |
||
57 | * Unauthorised |
||
58 | * @return boolean |
||
59 | */ |
||
60 | private function hasBearerValue() |
||
61 | { |
||
62 | $auth_headers = $this->getHeaders(); |
||
63 | |||
64 | if (isset($auth_headers["Authorization"]) && !empty($auth_headers["Authorization"])) { |
||
65 | |||
66 | list($type, $clientToken) = explode(" ", $auth_headers["Authorization"], 2); |
||
67 | |||
68 | if (strcasecmp(trim($type), "Bearer") == 0) { |
||
69 | return true; |
||
70 | } |
||
71 | } |
||
72 | |||
73 | return false; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * [hasBearerToken Check if bearer token is present] |
||
78 | * @return string|null |
||
79 | */ |
||
80 | public function hasBearerToken() |
||
81 | { |
||
82 | $auth_headers = $this->getHeaders(); |
||
83 | |||
84 | if ($this->hasBearerValue()) { |
||
85 | |||
86 | list($type, $clientToken) = explode(" ", $auth_headers["Authorization"], 2); |
||
87 | |||
88 | if (strcasecmp(trim($type), "Bearer") == 0 && !empty($clientToken)) { |
||
89 | return $clientToken; |
||
90 | } |
||
91 | } |
||
92 | |||
93 | return; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Check if the request is a token grant |
||
98 | * @return array|boolean |
||
99 | */ |
||
100 | public function isGrantRequest() |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * [accessRefreshHeaders description] |
||
131 | * @return string|array [mixed: string / error] |
||
132 | */ |
||
133 | private function accessRefreshHeaders($auth_headers) |
||
134 | { |
||
135 | list($type, $clientToken) = explode(" ", $auth_headers["Authorization"], 2); |
||
136 | |||
137 | $server = new server([], $this->getOptions()); |
||
138 | $mockTest = $server->isMockTest(); |
||
139 | |||
140 | // @codeCoverageIgnoreStart |
||
141 | if (strcasecmp($type, "Bearer") == 0 && !empty($clientToken) && !$mockTest) { |
||
142 | |||
143 | $user = new user\user; |
||
144 | $account = $user |
||
145 | ->setOptions($this->options) |
||
146 | ->load( |
||
147 | $clientToken, |
||
148 | array( |
||
149 | 'loadBy' => 'refresh_token', |
||
150 | 'getJWT' => true, |
||
151 | 'authorizationRefresh' => true, |
||
152 | ) |
||
153 | ); |
||
154 | |||
155 | if (empty($account)) { |
||
156 | $this->setUnauthorised(); |
||
157 | } |
||
158 | |||
159 | $tokens = [ |
||
160 | 'token' => $account['JWT'], |
||
161 | 'refresh_token' => $account['refreshToken']['token'], |
||
162 | ]; |
||
163 | |||
164 | $account['refreshToken'] = $tokens; |
||
165 | |||
166 | return $account; |
||
167 | |||
168 | } else { |
||
169 | if ($mockTest) { |
||
170 | return [ |
||
171 | 'mock_access' => true |
||
172 | ]; |
||
173 | } |
||
174 | $this->setUnauthorised(); |
||
175 | } |
||
176 | } |
||
177 | // @codeCoverageIgnoreEnd |
||
178 | |||
179 | /** |
||
180 | * [accessCredentialHeaders Check if the credentials are correct] |
||
181 | * @param array $auth_headers |
||
182 | * @return string|array [mixed: string / error] |
||
183 | */ |
||
184 | private function accessCredentialHeaders($auth_headers) |
||
245 | } |
||
246 | } |
||
247 | // @codeCoverageIgnoreEnd |
||
248 | |||
249 | /** |
||
250 | * [unauthorised Set an unauthorised header] |
||
251 | * @throws Exception |
||
252 | * UNAUTHORIZED 401 |
||
253 | * @return void |
||
254 | */ |
||
255 | public function setUnauthorised() |
||
272 |