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

KeycloakEntitlements::hasResourceSetId()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.8437

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 5
cts 8
cp 0.625
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 4
nop 1
crap 4.8437
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
       return !$this->hasExpired() &&
39
           ($this->getNotBefore() != null && time() > $this->getNotBefore());
40
    }
41
42
    /**
43
     *
44
     * @return bool
45
     */
46
    public function hasExpired() {
47
        return isset($this->data->exp) && time() > $this->data->exp;
48
    }
49
50
    /**
51
     * Used to check age - may return null or a timestamp
52
     *
53
     * @return mixed null|timestamp when token was issued, if present
54
     */
55
    public function getIssuedAt() {
56
        return $this->data->iat;
57
    }
58
59
    /**
60
     * @return mixed null|timestamp
61
     */
62
    public function getNotBefore() {
63
        return $this->data->nbf;
64
    }
65
66
    /**
67
     * Check for permission on a resource by the resource's ID
68
     *
69
     * @param string $id Resource Set ID - this should be the UUID generated by Keycloak for your resource
70
     * @return bool
71
     */
72 2
    public function hasResourceSetId($id) {
73 2
        if (! isset($this->data->authorization->permissions)) {
74
            return false;
75
        }
76
77 2
        foreach ($this->data->authorization->permissions as $permission) {
78 2
            if ($permission->resource_set_id == $id) {
79 2
                return true;
80
            }
81
        }
82
83
        return false;
84
    }
85
86
    /**
87
     * Check for permission on a resource by the resource's name
88
     *
89
     * @param string $name Resource Set ID - this should be the name provided for the resource within Keycloak
90
     * @return bool
91
     */
92 2
    public function hasResourceSetName($name) {
93 2
        if (! isset($this->data->authorization->permissions)) {
94
            return false;
95
        }
96
97 2
        foreach ($this->data->authorization->permissions as $permission) {
98 2
            if ($permission->resource_set_name == $name) {
99 2
                return true;
100
            }
101 1
        }
102
103
        return false;
104
    }
105
106
    /**
107
     * 
108
     * @return array List of resource ids
109
     */
110 4
    public function listResourcesById() {
111 4
        $answer = [];
112
        
113 4
        if (isset($this->data->authorization->permissions)) {
114 2
            foreach ($this->data->authorization->permissions as $permission) {
115 2
                $answer[] = $permission->resource_set_id;
116 1
            }
117 1
        }
118
119 4
        return $answer;
120
    }
121
122
    /**
123
     *
124
     * @return array List of resource names
125
     */
126 4
    public function listResourcesByName() {
127 4
        $answer = [];
128
129 4
        if (isset($this->data->authorization->permissions)) {
130 2
            foreach ($this->data->authorization->permissions as $permission) {
131 2
                $answer[] = $permission->resource_set_name;
132 1
            }
133 1
        }
134
135 4
        return $answer;
136
    }
137
}