Completed
Pull Request — master (#7)
by
unknown
02:02
created

KeycloakRoles   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 84%

Importance

Changes 0
Metric Value
wmc 12
lcom 2
cbo 1
dl 0
loc 64
ccs 21
cts 25
cp 0.84
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 16 5
A hasResourceNamed() 0 4 2
A getResourceNamesFound() 0 4 1
A hasRealmRoleNamed() 0 4 2
A getRealmRoles() 0 4 1
A getRolesOfResourceNamed() 0 4 1
1
<?php
2
/**
3
 * Created by IntelliJ IDEA.
4
 * User: jgreen
5
 * Date: 10/08/2017
6
 * Time: 11:43 AM
7
 */
8
9
namespace Stevenmaguire\OAuth2\Client\Provider;
10
11
use Firebase\JWT\JWT;
12
use League\OAuth2\Client\Token\AccessToken;
13
14
/**
15
 * Class KeycloakRoles
16
 *
17
 * Container for the two known sets of roles that can be detected inside an access token.
18
 *
19
 * There are roles, which are within the realm, then roles specific within individual named resources.
20
 *
21
 * @package Stevenmaguire\OAuth2\Client\Provider
22
 */
23
class KeycloakRoles
24
{
25
26
    /**
27
     * @var array a list of roles associated with the realm
28
     */
29
    protected $realmAccess = [];
30
    /**
31
     * @var array An associative array of KeycloakResourceRoles keyed by resource name
32
     */
33
    protected $resourcesAndRoles = [];
34
35
    /**
36
     * KeycloakRoles constructor.
37
     *
38
     * @param $obj Object from JWT::decode
39
     *
40
     */
41 6
    public function __construct($obj)
42
    {
43 6
        if (isset($obj->realm_access->roles)) {
44 2
            $this->realmAccess = $obj->realm_access->roles;
45 1
        }
46 6
        if (isset($obj->resource_access)) {
47 2
            foreach ($obj->resource_access as $resource => $roles) {
48 2
                $list = [];
49 2
                foreach ($roles->roles as $role) {
50 2
                    $list[] = $role;
51 1
                }
52 2
                $resourceRoles = new KeycloakResourceRoles($resource, $list);
53 2
                $this->resourcesAndRoles[$resource] = $resourceRoles;
54 1
            }
55 1
        }
56 6
    }
57
58
    public function hasResourceNamed($name)
59
    {
60
        return $this->resourcesAndRoles != null && array_key_exists($name, $this->resourcesAndRoles);
61
    }
62
63 6
    public function getResourceNamesFound()
64
    {
65 6
        return array_keys($this->resourcesAndRoles);
66
    }
67
68
    public function hasRealmRoleNamed($name)
69
    {
70
        return $this->realmAccess != null && in_array($name, $this->realmAccess->roles);
71
    }
72
73 6
    public function getRealmRoles()
74
    {
75 6
        return $this->realmAccess;
76
    }
77
78
    /**
79
     * @param $name
80
     * @return KeyCloakResourceRoles
81
     */
82 2
    public function getRolesOfResourceNamed($name)
83
    {
84 2
        return $this->resourcesAndRoles[$name];
85
    }
86
}
87