1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Canvas\Middleware; |
6
|
|
|
|
7
|
|
|
use Baka\Auth\Models\Sessions; |
|
|
|
|
8
|
|
|
use Canvas\Constants\Flags; |
9
|
|
|
use Canvas\Http\Exception\UnauthorizedException; |
10
|
|
|
use Canvas\Models\Apps; |
11
|
|
|
use Canvas\Models\AppsKeys; |
12
|
|
|
use Canvas\Models\Users; |
13
|
|
|
use Phalcon\Config; |
14
|
|
|
use Phalcon\Http\RequestInterface; |
15
|
|
|
use Phalcon\Mvc\Micro; |
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class AuthenticationMiddleware. |
19
|
|
|
* |
20
|
|
|
* @package Canvas\Middleware |
21
|
|
|
*/ |
22
|
|
|
class AuthenticationMiddleware extends TokenBase |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Call me. |
26
|
|
|
* |
27
|
|
|
* @param Micro $api |
28
|
|
|
* |
29
|
|
|
* @todo need to check section for auth here |
30
|
|
|
* |
31
|
|
|
* @return bool |
32
|
|
|
*/ |
33
|
|
|
public function call(Micro $api) |
34
|
|
|
{ |
35
|
|
|
$config = $api->getService('config'); |
36
|
|
|
$request = $api->getService('request'); |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* This is where we will find if the user exists based on |
40
|
|
|
* the token passed using Bearer Authentication. |
41
|
|
|
*/ |
42
|
|
|
if (!empty($request->getBearerTokenFromHeader())) { |
43
|
|
|
$token = $this->getToken($request->getBearerTokenFromHeader()); |
44
|
|
|
} elseif ($request->hasHeader('Client-Id') && $request->hasHeader('Client-Secret-Id') && $request->hasHeader('KanvasKey')) { |
45
|
|
|
// Functions that authenticates user by client id, client secret id and app key |
46
|
|
|
$this->sessionSdk($api, $request); |
47
|
|
|
|
48
|
|
|
return true; |
49
|
|
|
} else { |
50
|
|
|
throw new UnauthorizedException('Missing Token'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$this->sessionUser($api, $config, $token, $request); |
54
|
|
|
|
55
|
|
|
return true; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get the real from the JWT Token. |
60
|
|
|
* |
61
|
|
|
* @param Micro $api |
62
|
|
|
* @param Config $config |
63
|
|
|
* @param string $token |
64
|
|
|
* @param RequestInterface $request |
65
|
|
|
* |
66
|
|
|
* @throws UnauthorizedException |
67
|
|
|
* |
68
|
|
|
* @return void |
69
|
|
|
*/ |
70
|
|
|
protected function sessionUser(Micro $api, Config $config, object $token, RequestInterface $request) : void |
71
|
|
|
{ |
72
|
|
|
$api->getDI()->setShared( |
73
|
|
|
'userData', |
74
|
|
|
function () use ($config, $token, $request) { |
75
|
|
|
$session = new Sessions(); |
76
|
|
|
|
77
|
|
|
//all is empty and is dev, ok take use the first user |
78
|
|
|
if (empty($token->getClaim('sessionId')) && strtolower($config->app->env) == Flags::DEVELOPMENT) { |
79
|
|
|
return Users::findFirst(1); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if (!empty($token->getClaim('sessionId'))) { |
83
|
|
|
//user |
84
|
|
|
if (!$user = Users::getByEmail($token->getClaim('email'))) { |
85
|
|
|
throw new UnauthorizedException('User not found'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$ip = !defined('API_TESTS') ? $request->getClientAddress() : '127.0.0.1'; |
89
|
|
|
return $session->check($user, $token->getClaim('sessionId'), (string) $ip, 1); |
90
|
|
|
} else { |
91
|
|
|
throw new UnauthorizedException('User not found'); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* This is where we will validate the token that was sent to us |
98
|
|
|
* using Bearer Authentication. |
99
|
|
|
* |
100
|
|
|
* Find the user attached to this token |
101
|
|
|
*/ |
102
|
|
|
if (!$token->validate(Users::getValidationData($token->getHeader('jti')))) { |
103
|
|
|
throw new UnauthorizedException('Invalid Token'); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Anonymous user from token. |
109
|
|
|
* |
110
|
|
|
* @param Micro $api |
111
|
|
|
* @param Config $config |
112
|
|
|
* @param mixed $token |
113
|
|
|
* @param RequestInterface $request |
114
|
|
|
* |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
|
|
protected function anonymousUser(Micro $api, Config $config, $token, RequestInterface $request) : void |
118
|
|
|
{ |
119
|
|
|
$api->getDI()->setShared( |
120
|
|
|
'userData', |
121
|
|
|
function () use ($config) { |
122
|
|
|
/** |
123
|
|
|
* @todo we need to track session for anonymous user |
124
|
|
|
*/ |
125
|
|
|
if ($anonymous = Users::findFirst('-1')) { |
126
|
|
|
return $anonymous; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
throw new UnauthorizedException( |
130
|
|
|
strtolower($config->app->env) == Flags::DEVELOPMENT ? |
131
|
|
|
'No anonymous user configured in the app' : |
132
|
|
|
'Missing Token' |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Authenticate Admin user by client id, client secret id and apps keys. |
140
|
|
|
* |
141
|
|
|
* @param Micro $api |
142
|
|
|
* @param RequestInterface $request |
143
|
|
|
* |
144
|
|
|
* @return void |
145
|
|
|
* |
146
|
|
|
* @todo Add users validation by client id, client secret id, apps_id |
147
|
|
|
*/ |
148
|
|
|
protected function sessionSdk(Micro $api, RequestInterface $request) : void |
149
|
|
|
{ |
150
|
|
|
$app = $api->getService('app'); |
151
|
|
|
|
152
|
|
|
$api->getDI()->setShared( |
153
|
|
|
'userData', |
154
|
|
|
function () use ($request, $app) { |
155
|
|
|
$appkeys = AppsKeys::validateAppsKeys( |
156
|
|
|
$request->getHeader('Client-Id'), |
157
|
|
|
$request->getHeader('Client-Secret-Id'), |
158
|
|
|
$app->getId() |
159
|
|
|
); |
160
|
|
|
|
161
|
|
|
return Users::findFirst($appkeys->users_id); |
162
|
|
|
} |
163
|
|
|
); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths