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

KeycloakRoles::hasRealmRoleNamed()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 1
crap 6
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
12
use Firebase\JWT\JWT;
13
use League\OAuth2\Client\Token\AccessToken;
14
15
/**
16
 * Class KeycloakRoles
17
 *
18
 * Container for the two known sets of roles that can be detected inside an access token.
19
 *
20
 * There are roles, which are within the realm, then roles specific within individual named resources.
21
 *
22
 * @package Stevenmaguire\OAuth2\Client\Provider
23
 */
24
class KeycloakRoles
25
{
26
27
    /**
28
     * @var array a list of roles associated with the realm
29
     */
30
    protected $realmAccess = [];
31
    /**
32
     * @var array An associative array of KeycloakResourceRoles keyed by resource name
33
     */
34
    protected $resourcesAndRoles = [];
35
36
    /**
37
     * KeycloakRoles constructor.
38
     *
39
     * Will decode the JWT access token hidden within this OAuth `AccessToken` yielding additional information
40
     * provided by KeyCloak.
41
     *
42
     */
43 6
    public function __construct($obj)
44
    {
45 6
        if (isset($obj->realm_access->roles)) {
46 2
            $this->realmAccess = $obj->realm_access->roles;
47 1
        }
48 6
        if (isset($obj->resource_access)) {
49 2
            foreach ($obj->resource_access as $resource => $roles) {
50 2
                $list = [];
51 2
                foreach ($roles->roles as $role) {
52 2
                    $list[] = $role;
53 1
                }
54 2
                $resourceRoles = new KeycloakResourceRoles($resource, $list);
55 2
                $this->resourcesAndRoles[$resource] = $resourceRoles;
56 1
            }
57 1
        }
58 6
    }
59
60
    /**
61
     *
62
     * @param AccessToken $accessToken The token received within which the `access_token` exists (yes, really)
63
     * @param string $encryptionKey For signature checking purposes
64
     * @param string $encryptionAlgorithm For signature checking purposes
65
     * @return KeycloakRoles
66
     */
67
    public static function fromToken(AccessToken $accessToken, $encryptionKey, $encryptionAlgorithm) {
68
        $obj = JWT::decode($accessToken->getToken(), $encryptionKey, array($encryptionAlgorithm));
69
        return new KeycloakRoles($obj);
70
    }
71
72
    public function hasResourceNamed($name) {
73
        return $this->resourcesAndRoles != null && array_key_exists($name, $this->resourcesAndRoles);
74
    }
75 6
    public function getResourceNamesFound() {
76 6
        return array_keys($this->resourcesAndRoles);
77
    }
78
79
    public function hasRealmRoleNamed($name) {
80
        return $this->realmAccess != null && in_array($name, $this->realmAccess->roles);
81
    }
82 6
    public function getRealmRoles() {
83 6
        return $this->realmAccess;
84
    }
85
86
    /**
87
     * @param $name
88
     * @return KeyCloakResourceRoles
89
     */
90 2
    public function getRolesOfResourceNamed($name) {
91 2
        return $this->resourcesAndRoles[$name];
92
    }
93
}