1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RestTemplate\Rest; |
4
|
|
|
|
5
|
|
|
use ByJG\ApiTools\Base\Schema; |
6
|
|
|
use ByJG\Config\Exception\ConfigNotFoundException; |
7
|
|
|
use ByJG\Config\Exception\EnvironmentException; |
|
|
|
|
8
|
|
|
use ByJG\Config\Exception\KeyNotFoundException; |
9
|
|
|
use ByJG\RestServer\Exception\Error400Exception; |
10
|
|
|
use ByJG\RestServer\Exception\Error401Exception; |
11
|
|
|
use ByJG\RestServer\Exception\Error403Exception; |
12
|
|
|
use ByJG\RestServer\HttpRequest; |
13
|
|
|
use ByJG\Util\JwtWrapper; |
14
|
|
|
use RestTemplate\Psr11; |
15
|
|
|
use Exception; |
16
|
|
|
use Psr\SimpleCache\InvalidArgumentException; |
17
|
|
|
use ReflectionException; |
18
|
|
|
|
19
|
|
|
class ServiceAbstractBase |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param array $properties |
24
|
|
|
* @return mixed |
25
|
|
|
* @throws ConfigNotFoundException |
26
|
|
|
* @throws EnvironmentException |
27
|
|
|
* @throws InvalidArgumentException |
28
|
|
|
* @throws KeyNotFoundException |
29
|
|
|
* @throws ReflectionException |
30
|
|
|
*/ |
31
|
|
|
public function createToken($properties = []) |
32
|
|
|
{ |
33
|
|
|
$jwt = Psr11::container()->get(JwtWrapper::class); |
34
|
|
|
$jwtData = $jwt->createJwtData($properties, 1800); |
35
|
|
|
return $jwt->generateToken($jwtData); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param null $token |
|
|
|
|
40
|
|
|
* @param bool $fullToken |
41
|
|
|
* @return mixed |
42
|
|
|
* @throws Error401Exception |
43
|
|
|
* @throws InvalidArgumentException |
44
|
|
|
*/ |
45
|
|
|
public function requireAuthenticated($token = null, $fullToken = false) |
46
|
|
|
{ |
47
|
|
|
try { |
48
|
|
|
$jwt = Psr11::container()->get(JwtWrapper::class); |
49
|
|
|
$tokenInfo = json_decode(json_encode($jwt->extractData($token)), true); |
50
|
|
|
if ($fullToken) { |
51
|
|
|
return $tokenInfo; |
52
|
|
|
} else { |
53
|
|
|
return $tokenInfo['data']; |
54
|
|
|
} |
55
|
|
|
} catch (Exception $ex) { |
56
|
|
|
throw new Error401Exception($ex->getMessage()); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param $role |
62
|
|
|
* @param null $token |
|
|
|
|
63
|
|
|
* @return mixed |
64
|
|
|
* @throws Error401Exception |
65
|
|
|
* @throws InvalidArgumentException |
66
|
|
|
*/ |
67
|
|
|
public function requireRole($role, $token = null) |
68
|
|
|
{ |
69
|
|
|
$data = $this->requireAuthenticated($token); |
70
|
|
|
if ($data['role'] !== $role) { |
71
|
|
|
throw new Error403Exception('Insufficient privileges'); |
72
|
|
|
} |
73
|
|
|
return $data; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function validateRequest(HttpRequest $request) |
77
|
|
|
{ |
78
|
|
|
$schema = Psr11::container()->get(Schema::class); |
79
|
|
|
|
80
|
|
|
$path = $request->getRequestPath(); |
81
|
|
|
$method = $request->server('REQUEST_METHOD'); |
82
|
|
|
|
83
|
|
|
// Returns a SwaggerRequestBody instance |
84
|
|
|
$bodyRequestDef = $schema->getRequestParameters($path, $method); |
85
|
|
|
|
86
|
|
|
// Validate the request body (payload) |
87
|
|
|
$requestBody = json_decode($request->payload(), true); |
88
|
|
|
try { |
89
|
|
|
$bodyRequestDef->match($requestBody); |
90
|
|
|
} catch (Exception $ex) { |
91
|
|
|
throw new Error400Exception(explode("\n", $ex->getMessage())[0]); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $requestBody; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
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