Completed
Push — master ( dfef9a...bfe41e )
by Michał
06:35
created

Token::matches()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.6737
c 0
b 0
f 0
cc 6
eloc 10
nc 5
nop 1
1
<?php namespace nyx\auth\id\protocols\oauth2;
2
3
// External dependencies
4
use nyx\core;
5
6
// Internal dependencies
7
use nyx\auth\id\protocols\oauth2;
8
use nyx\auth;
9
10
/**
11
 * OAuth 2.0 Access Token
12
 *
13
 * @package     Nyx\Auth
14
 * @version     0.1.0
15
 * @author      Michal Chojnacki <[email protected]>
16
 * @copyright   2012-2017 Nyx Dev Team
17
 * @link        https://github.com/unyx/nyx
18
 */
19
class Token extends auth\Token
20
{
21
    /**
22
     * The traits of a OAuth 2.0 Access Token instance.
23
     */
24
    use core\traits\Serializable;
25
26
    /**
27
     * @var auth\Token  The refresh Token for this Access Token.
28
     */
29
    protected $refreshToken;
30
31
    /**
32
     * @var int     The expiry time of this Token in seconds.
33
     */
34
    protected $expiry;
35
36
    /**
37
     * @var array   The scopes this Token is known to grant access to, if applicable.
38
     *              Note: For faster lookups the actual scopes are stored as keys of this array.
39
     */
40
    protected $scopes = [];
41
42
    /**
43
     * {@inheritDoc}
44
     */
45
    public function matches(auth\interfaces\Token $that) : bool
46
    {
47
        // The interface alone won't do us much good here. We do need access to specific properties.
48
        if (!$that instanceof static) {
49
            return false;
50
        }
51
52
        // Base class(es) will perform common comparisons.
53
        if (!parent::matches($that)) {
54
            return false;
55
        }
56
57
        if ($this->expiry !== $that->expiry) {
58
            return false;
59
        }
60
61
        if (isset($this->refreshToken) && !$this->refreshToken->matches($that->refreshToken)) {
62
            return false;
63
        }
64
65
        return $this->scopes === $that->scopes;
66
    }
67
68
    /**
69
     * Determines whether this Token is known to grant access to a specific scope.
70
     *
71
     * @param   string  $scope  The name of the scope to check for.
72
     * @return  bool
73
     */
74
    public function authorizes(string $scope) : bool
75
    {
76
        return isset($this->scopes[$scope]);
77
    }
78
79
    /**
80
     * Returns the scopes this Token is known to grant access to.
81
     *
82
     * @return  array
83
     */
84
    public function getScopes() : array
85
    {
86
        return $this->scopes;
87
    }
88
89
    /**
90
     * Sets the scopes this Token is known to grant access to.
91
     *
92
     * @param   array   $scopes The scopes to set.
93
     * @return  $this
94
     */
95
    public function setScopes(array $scopes) : oauth2\Token
96
    {
97
        $this->scopes = [];
98
99
        foreach ($scopes as $scope) {
100
            $this->scopes[$scope] = true;
101
        }
102
103
        return $this;
104
    }
105
106
    /**
107
     * Returns the refresh Token for this Access Token.
108
     *
109
     * @return  auth\Token
110
     */
111
    public function getRefreshToken() : ?auth\Token
112
    {
113
        return $this->refreshToken;
114
    }
115
116
    /**
117
     * Sets the refresh Token for this Access Token.
118
     *
119
     * @param   auth\Token|string   $token
120
     * @return  $this
121
     */
122
    public function setRefreshToken($token) : oauth2\Token
123
    {
124
        // For any type other than an auth\Token instance, instantiate a auth\Token.
125
        // However, only strings are actually accepted in this scenario (the constructor will throw
126
        // on invalid types).
127
        if (!$token instanceof auth\Token) {
128
            $token = new auth\Token($token);
129
        }
130
131
        $this->refreshToken = $token;
132
133
        return $this;
134
    }
135
136
    /**
137
     * Returns the expiry time of this Token in seconds.
138
     *
139
     * @return  int
140
     */
141
    public function getExpiry() : ?int
142
    {
143
        return $this->expiry;
144
    }
145
146
    /**
147
     * Sets the expiry time of this Token in seconds.
148
     *
149
     * @param   int $time   The expiry time in seconds.
150
     * @return  $this
151
     */
152
    public function setExpiry(int $time) : oauth2\Token
153
    {
154
        $this->expiry = $time;
155
156
        return $this;
157
    }
158
159
    /**
160
     * {@inheritDoc}
161
     */
162
    public function unserialize($data)
163
    {
164
        $data = unserialize($data);
165
166
        $this->id           = $data['id'];
167
        $this->refreshToken = $data['refresh'];
168
        $this->expiry       = $data['expiry'];
169
170
        $this->setScopes($data['scopes']);
171
    }
172
173
    /**
174
     * {@inheritDoc}
175
     */
176
    public function toString() : string
177
    {
178
        return $this->id;
179
    }
180
181
    /**
182
     * {@inheritDoc}
183
     */
184
    public function toArray() : array
185
    {
186
        return [
187
            'id'      => $this->id,
188
            'refresh' => $this->refreshToken,
189
            'expiry'  => $this->expiry,
190
            'scopes'  => array_keys($this->scopes)
191
        ];
192
    }
193
}
194