Completed
Pull Request — master (#308)
by Alex
43:42 queued 08:40
created

SessionEntity::getOwnerType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * OAuth 2.0 session entity
4
 *
5
 * @package     league/oauth2-server
6
 * @author      Alex Bilbie <[email protected]>
7
 * @copyright   Copyright (c) Alex Bilbie
8
 * @license     http://mit-license.org/
9
 * @link        https://github.com/thephpleague/oauth2-server
10
 */
11
12
namespace League\OAuth2\Server\Entity;
13
14
use League\OAuth2\Server\AbstractServer;
15
use League\OAuth2\Server\Event\SessionOwnerEvent;
16
17
/**
18
 * Session entity grant
19
 */
20
class SessionEntity
21
{
22
    /**
23
     * Session identifier
24
     *
25
     * @var string
26
     */
27
    protected $id;
28
29
    /**
30
     * Client identifier
31
     *
32
     * @var \League\OAuth2\Server\Entity\ClientEntity
33
     */
34
    protected $client;
35
36
    /**
37
     * Session owner identifier
38
     *
39
     * @var string
40
     */
41
    protected $ownerId;
42
43
    /**
44
     * Session owner type (e.g. "user")
45
     *
46
     * @var string
47
     */
48
    protected $ownerType;
49
50
    /**
51
     * Auth code
52
     *
53
     * @var \League\OAuth2\Server\Entity\AuthCodeEntity
54
     */
55
    protected $authCode;
56
57
    /**
58
     * Access token
59
     *
60
     * @var \League\OAuth2\Server\Entity\AccessTokenEntity
61
     */
62
    protected $accessToken;
63
64
    /**
65
     * Refresh token
66
     *
67
     * @var \League\OAuth2\Server\Entity\RefreshTokenEntity
68
     */
69
    protected $refreshToken;
70
71
    /**
72
     * Session scopes
73
     *
74
     * @var \Symfony\Component\HttpFoundation\ParameterBag
75
     */
76
    protected $scopes;
77
78
    /**
79
     * Authorization or resource server
80
     *
81
     * @var \League\OAuth2\Server\AuthorizationServer|\League\OAuth2\Server\ResourceServer
82
     */
83
    protected $server;
84
85
    /**
86
     * __construct
87
     *
88
     * @param \League\OAuth2\Server\AbstractServer $server
89
     *
90
     * @return self
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
91
     */
92
    public function __construct(AbstractServer $server)
93
    {
94
        $this->server = $server;
0 ignored issues
show
Documentation Bug introduced by
$server is of type object<League\OAuth2\Server\AbstractServer>, but the property $server was declared to be of type object<League\OAuth2\Server\ResourceServer>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
95
96
        return $this;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
97
    }
98
99
    /**
100
     * Set the session identifier
101
     *
102
     * @param string $id
103
     *
104
     * @return self
105
     */
106
    public function setId($id)
107
    {
108
        $this->id = $id;
109
110
        return $this;
111
    }
112
113
    /**
114
     * Return the session identifier
115
     *
116
     * @return string
117
     */
118
    public function getId()
119
    {
120
        return $this->id;
121
    }
122
123
    /**
124
     * Associate a scope
125
     *
126
     * @param \League\OAuth2\Server\Entity\ScopeEntity $scope
127
     *
128
     * @return self
129
     */
130
    public function associateScope(ScopeEntity $scope)
131
    {
132
        if (!isset($this->scopes[$scope->getId()])) {
133
            $this->scopes[$scope->getId()] = $scope;
134
        }
135
136
        return $this;
137
    }
138
139
    /**
140
     * Check if access token has an associated scope
141
     *
142
     * @param string $scope Scope to check
143
     *
144
     * @return bool
145
     */
146
    public function hasScope($scope)
147
    {
148
        if ($this->scopes === null) {
149
            $this->getScopes();
150
        }
151
152
        return isset($this->scopes[$scope]);
153
    }
154
155
    /**
156
     * Return all scopes associated with the session
157
     *
158
     * @return \League\OAuth2\Server\Entity\ScopeEntity[]
159
     */
160
    public function getScopes()
161
    {
162
        if ($this->scopes === null) {
163
            $this->scopes = $this->formatScopes($this->server->getSessionStorage()->getScopes($this));
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->formatScopes($thi...ge()->getScopes($this)) of type array is incompatible with the declared type object<Symfony\Component...oundation\ParameterBag> of property $scopes.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
164
        }
165
166
        return $this->scopes;
167
    }
168
169
    /**
170
     * Format the local scopes array
171
     *
172
     * @param  \League\OAuth2\Server\Entity\Scope[]
173
     *
174
     * @return array
175
     */
176
    private function formatScopes($unformatted = [])
177
    {
178
        $scopes = [];
179
        if (is_array($unformatted)) {
180
            foreach ($unformatted as $scope) {
181
                if ($scope instanceof ScopeEntity) {
182
                    $scopes[$scope->getId()] = $scope;
183
                }
184
            }
185
        }
186
187
        return $scopes;
188
    }
189
190
    /**
191
     * Associate an access token with the session
192
     *
193
     * @param \League\OAuth2\Server\Entity\AccessTokenEntity $accessToken
194
     *
195
     * @return self
196
     */
197
    public function associateAccessToken(AccessTokenEntity $accessToken)
198
    {
199
        $this->accessToken = $accessToken;
200
201
        return $this;
202
    }
203
204
    /**
205
     * Associate a refresh token with the session
206
     *
207
     * @param \League\OAuth2\Server\Entity\RefreshTokenEntity $refreshToken
208
     *
209
     * @return self
210
     */
211
    public function associateRefreshToken(RefreshTokenEntity $refreshToken)
212
    {
213
        $this->refreshToken = $refreshToken;
214
215
        return $this;
216
    }
217
218
    /**
219
     * Associate a client with the session
220
     *
221
     * @param \League\OAuth2\Server\Entity\ClientEntity $client The client
222
     *
223
     * @return self
224
     */
225
    public function associateClient(ClientEntity $client)
226
    {
227
        $this->client = $client;
228
229
        return $this;
230
    }
231
232
    /**
233
     * Return the session client
234
     *
235
     * @return \League\OAuth2\Server\Entity\ClientEntity
236
     */
237
    public function getClient()
238
    {
239
        if ($this->client instanceof ClientEntity) {
240
            return $this->client;
241
        }
242
243
        $this->client = $this->server->getClientStorage()->getBySession($this);
244
245
        return $this->client;
246
    }
247
248
    /**
249
     * Set the session owner
250
     *
251
     * @param string $type The type of the owner (e.g. user, app)
252
     * @param string $id   The identifier of the owner
253
     *
254
     * @return self
255
     */
256
    public function setOwner($type, $id)
257
    {
258
        $this->ownerType = $type;
259
        $this->ownerId = $id;
260
261
        $this->server->getEventEmitter()->emit(new SessionOwnerEvent($this));
262
263
        return $this;
264
    }
265
266
    /**
267
     * Return session owner identifier
268
     *
269
     * @return string
270
     */
271
    public function getOwnerId()
272
    {
273
        return $this->ownerId;
274
    }
275
276
    /**
277
     * Return session owner type
278
     *
279
     * @return string
280
     */
281
    public function getOwnerType()
282
    {
283
        return $this->ownerType;
284
    }
285
286
    /**
287
     * Save the session
288
     *
289
     * @return void
290
     */
291
    public function save()
292
    {
293
        // Save the session and get an identifier
294
        $id = $this->server->getSessionStorage()->create(
295
            $this->getOwnerType(),
296
            $this->getOwnerId(),
297
            $this->getClient()->getId(),
298
            $this->getClient()->getRedirectUri()
299
        );
300
301
        $this->setId($id);
302
        $this->applyScopes();
303
    }
304
305
    /**
306
     * Apply any scope changes
307
     *
308
     * @return void
309
     */
310
    public function applyScopes() {
311
        foreach ($this->getScopes() as $scope) {
312
            $this->server->getSessionStorage()->associateScope($this, $scope);
313
        }
314
    }
315
}
316