Passed
Pull Request — master (#1)
by Joao
04:25
created

ServiceAbstractBase   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 49
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A requireAuthenticated() 0 8 2
A requireRole() 0 7 2
A createToken() 0 5 1
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;
0 ignored issues
show
Bug introduced by
The type ByJG\RestServer\ServiceAbstract was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $token is correct as it would always require null to be passed?
Loading history...
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
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $token is correct as it would always require null to be passed?
Loading history...
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