1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* User: jg |
4
|
|
|
* Date: 30/09/17 |
5
|
|
|
* Time: 18:10 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace RestTemplate\Rest; |
9
|
|
|
|
10
|
|
|
use ByJG\RestServer\Exception\Error401Exception; |
11
|
|
|
use ByJG\RestServer\ServiceAbstract; |
|
|
|
|
12
|
|
|
use ByJG\Util\JwtWrapper; |
13
|
|
|
use Builder\Psr11; |
14
|
|
|
|
15
|
|
|
class ServiceAbstractBase |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param array $properties |
20
|
|
|
* @return mixed |
21
|
|
|
* @throws \Psr\Container\ContainerExceptionInterface |
22
|
|
|
* @throws \Psr\Container\NotFoundExceptionInterface |
23
|
|
|
*/ |
24
|
|
|
public function createToken($properties = []) |
25
|
|
|
{ |
26
|
|
|
$jwt = new JwtWrapper(Psr11::container()->get('API_SERVER'), Psr11::container()->get('JWT_SECRET')); |
27
|
|
|
$jwtData = $jwt->createJwtData($properties, 1800); |
28
|
|
|
return $jwt->generateToken($jwtData); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param null $token |
|
|
|
|
33
|
|
|
* @return mixed |
34
|
|
|
* @throws \ByJG\RestServer\Exception\Error401Exception |
35
|
|
|
* @throws \Psr\Container\ContainerExceptionInterface |
36
|
|
|
* @throws \Psr\Container\NotFoundExceptionInterface |
37
|
|
|
*/ |
38
|
|
|
public function requireAuthenticated($token = null) |
39
|
|
|
{ |
40
|
|
|
try { |
41
|
|
|
$jwt = new JwtWrapper(Psr11::container()->get('API_SERVER'), Psr11::container()->get('JWT_SECRET')); |
42
|
|
|
$tokenInfo = json_decode(json_encode($jwt->extractData($token)), true); |
43
|
|
|
return $tokenInfo['data']; |
44
|
|
|
} catch (\Exception $ex) { |
45
|
|
|
throw new Error401Exception($ex->getMessage()); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param $role |
51
|
|
|
* @param null $token |
|
|
|
|
52
|
|
|
* @return mixed |
53
|
|
|
* @throws \ByJG\RestServer\Exception\Error401Exception |
54
|
|
|
* @throws \Psr\Container\ContainerExceptionInterface |
55
|
|
|
* @throws \Psr\Container\NotFoundExceptionInterface |
56
|
|
|
*/ |
57
|
|
|
public function requireRole($role, $token = null) |
58
|
|
|
{ |
59
|
|
|
$data = $this->requireAuthenticated($token); |
60
|
|
|
if ($data['role'] !== $role) { |
61
|
|
|
throw new Error401Exception('Insufficient privileges - ' . print_r($data, true)); |
62
|
|
|
} |
63
|
|
|
return $data; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
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