1 | <?php |
||
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 |
||
|
|||
91 | */ |
||
92 | public function __construct(AbstractServer $server) |
||
93 | { |
||
94 | $this->server = $server; |
||
95 | |||
96 | return $this; |
||
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() |
||
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)); |
||
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 = []) |
||
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() |
||
275 | |||
276 | /** |
||
277 | * Return session owner type |
||
278 | * |
||
279 | * @return string |
||
280 | */ |
||
281 | public function getOwnerType() |
||
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(), |
||
304 | |||
305 | /** |
||
306 | * Apply any scope changes |
||
307 | * |
||
308 | * @return void |
||
309 | */ |
||
310 | public function applyScopes() { |
||
315 | } |
||
316 |
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.