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

KeycloakEntitlements::listResourcesByName()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 2
nop 0
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
    public function __construct($resultOfJwtDecode)
28
    {
29
        $this->data = $resultOfJwtDecode;
30
    }
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
    public function hasResourceSetId($id)
77
    {
78
        if (!isset($this->data->authorization->permissions)) {
79
            return false;
80
        }
81
82
        foreach ($this->data->authorization->permissions as $permission) {
83
            if ($permission->resource_set_id == $id) {
84
                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
    public function hasResourceSetName($name)
98
    {
99
        if (!isset($this->data->authorization->permissions)) {
100
            return false;
101
        }
102
103
        foreach ($this->data->authorization->permissions as $permission) {
104
            if ($permission->resource_set_name == $name) {
105
                return true;
106
            }
107
        }
108
109
        return false;
110
    }
111
112
    /**
113
     *
114
     * @return array List of resource ids
115
     */
116
    public function listResourcesById()
117
    {
118
        $answer = [];
119
120
        if (isset($this->data->authorization->permissions)) {
121
            foreach ($this->data->authorization->permissions as $permission) {
122
                $answer[] = $permission->resource_set_id;
123
            }
124
        }
125
126
        return $answer;
127
    }
128
129
    /**
130
     *
131
     * @return array List of resource names
132
     */
133
    public function listResourcesByName()
134
    {
135
        $answer = [];
136
137
        if (isset($this->data->authorization->permissions)) {
138
            foreach ($this->data->authorization->permissions as $permission) {
139
                $answer[] = $permission->resource_set_name;
140
            }
141
        }
142
143
        return $answer;
144
    }
145
}
146