1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Jitamin. |
5
|
|
|
* |
6
|
|
|
* Copyright (C) Jitamin Team |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Jitamin\Http\Middleware; |
13
|
|
|
|
14
|
|
|
use Jitamin\Foundation\Base; |
15
|
|
|
use JsonRPC\Exception\AccessDeniedException; |
16
|
|
|
use JsonRPC\Exception\AuthenticationFailureException; |
17
|
|
|
use JsonRPC\MiddlewareInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class AuthenticationApiMiddleware. |
21
|
|
|
*/ |
22
|
|
|
class ApiAuthenticationMiddleware extends Base implements MiddlewareInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Execute Middleware. |
26
|
|
|
* |
27
|
|
|
* @param string $username |
28
|
|
|
* @param string $password |
29
|
|
|
* @param string $apiName |
30
|
|
|
* |
31
|
|
|
* @throws AccessDeniedException |
32
|
|
|
* @throws AuthenticationFailureException |
33
|
|
|
*/ |
34
|
|
|
public function execute($username, $password, $apiName) |
35
|
|
|
{ |
36
|
|
|
$this->dispatcher->dispatch('app.bootstrap'); |
|
|
|
|
37
|
|
|
$this->sessionStorage->scope = 'API'; |
|
|
|
|
38
|
|
|
|
39
|
|
|
if ($this->isUserAuthenticated($username, $password)) { |
40
|
|
|
$this->userSession->initialize($this->userModel->getByUsername($username)); |
|
|
|
|
41
|
|
|
} elseif (!$this->isAppAuthenticated($username, $password)) { |
42
|
|
|
$this->logger->error('API authentication failure for '.$username); |
|
|
|
|
43
|
|
|
|
44
|
|
|
throw new AuthenticationFailureException('Wrong credentials'); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Check user credentials. |
50
|
|
|
* |
51
|
|
|
* @param string $username |
52
|
|
|
* @param string $password |
53
|
|
|
* |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
|
|
private function isUserAuthenticated($username, $password) |
57
|
|
|
{ |
58
|
|
|
return $username !== 'jsonrpc' && |
59
|
|
|
!$this->userLockingModel->isLocked($username) && |
|
|
|
|
60
|
|
|
$this->authenticationManager->passwordAuthentication($username, $password); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Check administrative credentials. |
65
|
|
|
* |
66
|
|
|
* @param string $username |
67
|
|
|
* @param string $password |
68
|
|
|
* |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
|
|
private function isAppAuthenticated($username, $password) |
72
|
|
|
{ |
73
|
|
|
return $username === 'jsonrpc' && $password === $this->getApiToken(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get API Token. |
78
|
|
|
* |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
private function getApiToken() |
82
|
|
|
{ |
83
|
|
|
if (defined('API_AUTHENTICATION_TOKEN')) { |
84
|
|
|
return API_AUTHENTICATION_TOKEN; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $this->settingModel->get('api_token'); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.