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
|
|
|
public function __construct($obj) |
42
|
|
|
{ |
43
|
|
|
if (isset($obj->realm_access->roles)) { |
44
|
|
|
$this->realmAccess = $obj->realm_access->roles; |
45
|
|
|
} |
46
|
|
|
if (isset($obj->resource_access)) { |
47
|
|
|
foreach ($obj->resource_access as $resource => $roles) { |
48
|
|
|
$list = []; |
49
|
|
|
foreach ($roles->roles as $role) { |
50
|
|
|
$list[] = $role; |
51
|
|
|
} |
52
|
|
|
$resourceRoles = new KeycloakResourceRoles($resource, $list); |
53
|
|
|
$this->resourcesAndRoles[$resource] = $resourceRoles; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function hasResourceNamed($name) |
59
|
|
|
{ |
60
|
|
|
return $this->resourcesAndRoles != null && array_key_exists($name, $this->resourcesAndRoles); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function getResourceNamesFound() |
64
|
|
|
{ |
65
|
|
|
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
|
|
|
public function getRealmRoles() |
74
|
|
|
{ |
75
|
|
|
return $this->realmAccess; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param $name |
80
|
|
|
* @return KeyCloakResourceRoles |
81
|
|
|
*/ |
82
|
|
|
public function getRolesOfResourceNamed($name) |
83
|
|
|
{ |
84
|
|
|
return $this->resourcesAndRoles[$name]; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|