|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by IntelliJ IDEA. |
|
4
|
|
|
* User: jgreen |
|
5
|
|
|
* Date: 11/08/2017 |
|
6
|
|
|
* Time: 2:16 PM |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Stevenmaguire\OAuth2\Client\Provider; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class KeycloakEntitlements |
|
13
|
|
|
* |
|
14
|
|
|
* Represents the result of a response from the Keycloak Entitlement API. |
|
15
|
|
|
* |
|
16
|
|
|
* @package Stevenmaguire\OAuth2\Client\Provider |
|
17
|
|
|
*/ |
|
18
|
|
|
class KeycloakEntitlements |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
private $data; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* KeycloakEntitlements constructor. |
|
25
|
|
|
* @param object $resultOfJwtDecode |
|
26
|
|
|
*/ |
|
27
|
4 |
|
public function __construct($resultOfJwtDecode) |
|
28
|
|
|
{ |
|
29
|
4 |
|
$this->data = $resultOfJwtDecode; |
|
30
|
4 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Basic checks should there be data that can be examined. |
|
34
|
|
|
* |
|
35
|
|
|
* @return bool |
|
36
|
|
|
*/ |
|
37
|
|
|
public function isValid() |
|
38
|
|
|
{ |
|
39
|
|
|
return !$this->hasExpired() && |
|
40
|
|
|
($this->getNotBefore() != null && time() > $this->getNotBefore()); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* |
|
45
|
|
|
* @return bool |
|
46
|
|
|
*/ |
|
47
|
|
|
public function hasExpired() |
|
48
|
|
|
{ |
|
49
|
|
|
return isset($this->data->exp) && time() > $this->data->exp; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Used to check age - may return null or a timestamp |
|
54
|
|
|
* |
|
55
|
|
|
* @return mixed null|timestamp when token was issued, if present |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getIssuedAt() |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->data->iat; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return mixed null|timestamp |
|
64
|
|
|
*/ |
|
65
|
|
|
public function getNotBefore() |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->data->nbf; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Check for permission on a resource by the resource's ID |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $id Resource Set ID - this should be the UUID generated by Keycloak for your resource |
|
74
|
|
|
* @return bool |
|
75
|
|
|
*/ |
|
76
|
2 |
|
public function hasResourceSetId($id) |
|
77
|
|
|
{ |
|
78
|
2 |
|
if (!isset($this->data->authorization->permissions)) { |
|
79
|
|
|
return false; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
2 |
|
foreach ($this->data->authorization->permissions as $permission) { |
|
83
|
2 |
|
if ($permission->resource_set_id == $id) { |
|
84
|
2 |
|
return true; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return false; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Check for permission on a resource by the resource's name |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $name Resource Set ID - this should be the name provided for the resource within Keycloak |
|
95
|
|
|
* @return bool |
|
96
|
|
|
*/ |
|
97
|
2 |
|
public function hasResourceSetName($name) |
|
98
|
|
|
{ |
|
99
|
2 |
|
if (!isset($this->data->authorization->permissions)) { |
|
100
|
|
|
return false; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
2 |
|
foreach ($this->data->authorization->permissions as $permission) { |
|
104
|
2 |
|
if ($permission->resource_set_name == $name) { |
|
105
|
2 |
|
return true; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return false; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* |
|
114
|
|
|
* @return array List of resource ids |
|
115
|
|
|
*/ |
|
116
|
4 |
|
public function listResourcesById() |
|
117
|
|
|
{ |
|
118
|
4 |
|
$answer = []; |
|
119
|
|
|
|
|
120
|
4 |
|
if (isset($this->data->authorization->permissions)) { |
|
121
|
2 |
|
foreach ($this->data->authorization->permissions as $permission) { |
|
122
|
2 |
|
$answer[] = $permission->resource_set_id; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
4 |
|
return $answer; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* |
|
131
|
|
|
* @return array List of resource names |
|
132
|
|
|
*/ |
|
133
|
4 |
|
public function listResourcesByName() |
|
134
|
|
|
{ |
|
135
|
4 |
|
$answer = []; |
|
136
|
|
|
|
|
137
|
4 |
|
if (isset($this->data->authorization->permissions)) { |
|
138
|
2 |
|
foreach ($this->data->authorization->permissions as $permission) { |
|
139
|
2 |
|
$answer[] = $permission->resource_set_name; |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
4 |
|
return $answer; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|