|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of OAuth 2.0 Laravel. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Luca Degasperi <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace LucaDegasperi\OAuth2Server\Storage; |
|
13
|
|
|
|
|
14
|
|
|
use Carbon\Carbon; |
|
15
|
|
|
use League\OAuth2\Server\Entity\AccessTokenEntity; |
|
16
|
|
|
use League\OAuth2\Server\Entity\AuthCodeEntity; |
|
17
|
|
|
use League\OAuth2\Server\Entity\ScopeEntity; |
|
18
|
|
|
use League\OAuth2\Server\Entity\SessionEntity; |
|
19
|
|
|
use League\OAuth2\Server\Storage\SessionInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* This is the fluent session class. |
|
23
|
|
|
* |
|
24
|
|
|
* @author Luca Degasperi <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class FluentSession extends AbstractFluentAdapter implements SessionInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* Get a session from it's identifier. |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $sessionId |
|
32
|
|
|
* |
|
33
|
|
|
* @return \League\OAuth2\Server\Entity\SessionEntity |
|
|
|
|
|
|
34
|
|
|
*/ |
|
35
|
6 |
|
public function get($sessionId) |
|
36
|
|
|
{ |
|
37
|
6 |
|
$result = $this->getConnection()->table('oauth_sessions') |
|
38
|
6 |
|
->where('oauth_sessions.id', $sessionId) |
|
39
|
6 |
|
->first(); |
|
40
|
|
|
|
|
41
|
6 |
|
if (is_null($result)) { |
|
42
|
3 |
|
return; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
3 |
|
return (new SessionEntity($this->getServer())) |
|
46
|
3 |
|
->setId($result->id) |
|
47
|
3 |
|
->setOwner($result->owner_type, $result->owner_id); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Get a session from an access token. |
|
52
|
|
|
* |
|
53
|
|
|
* @param \League\OAuth2\Server\Entity\AccessTokenEntity $accessToken The access token |
|
54
|
|
|
* |
|
55
|
|
|
* @return \League\OAuth2\Server\Entity\SessionEntity |
|
|
|
|
|
|
56
|
|
|
*/ |
|
57
|
6 |
View Code Duplication |
public function getByAccessToken(AccessTokenEntity $accessToken) |
|
|
|
|
|
|
58
|
|
|
{ |
|
59
|
6 |
|
$result = $this->getConnection()->table('oauth_sessions') |
|
60
|
6 |
|
->select('oauth_sessions.*') |
|
61
|
6 |
|
->join('oauth_access_tokens', 'oauth_sessions.id', '=', 'oauth_access_tokens.session_id') |
|
62
|
6 |
|
->where('oauth_access_tokens.id', $accessToken->getId()) |
|
63
|
6 |
|
->first(); |
|
64
|
|
|
|
|
65
|
6 |
|
if (is_null($result)) { |
|
66
|
3 |
|
return; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
3 |
|
return (new SessionEntity($this->getServer())) |
|
70
|
3 |
|
->setId($result->id) |
|
71
|
3 |
|
->setOwner($result->owner_type, $result->owner_id); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Get a session's scopes. |
|
76
|
|
|
* |
|
77
|
|
|
* @param \League\OAuth2\Server\Entity\SessionEntity |
|
78
|
|
|
* |
|
79
|
|
|
* @return array Array of \League\OAuth2\Server\Entity\ScopeEntity |
|
80
|
|
|
*/ |
|
81
|
3 |
View Code Duplication |
public function getScopes(SessionEntity $session) |
|
|
|
|
|
|
82
|
|
|
{ |
|
83
|
|
|
// TODO: Check this before pushing |
|
84
|
3 |
|
$result = $this->getConnection()->table('oauth_session_scopes') |
|
85
|
3 |
|
->select('oauth_scopes.*') |
|
86
|
3 |
|
->join('oauth_scopes', 'oauth_session_scopes.scope_id', '=', 'oauth_scopes.id') |
|
87
|
3 |
|
->where('oauth_session_scopes.session_id', $session->getId()) |
|
88
|
3 |
|
->get(); |
|
89
|
|
|
|
|
90
|
3 |
|
$scopes = []; |
|
91
|
|
|
|
|
92
|
3 |
|
foreach ($result as $scope) { |
|
93
|
3 |
|
$scopes[] = (new ScopeEntity($this->getServer()))->hydrate([ |
|
94
|
3 |
|
'id' => $scope->id, |
|
95
|
3 |
|
'description' => $scope->description, |
|
96
|
3 |
|
]); |
|
97
|
3 |
|
} |
|
98
|
|
|
|
|
99
|
3 |
|
return $scopes; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Create a new session. |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $ownerType Session owner's type (user, client) |
|
106
|
|
|
* @param string $ownerId Session owner's ID |
|
107
|
|
|
* @param string $clientId Client ID |
|
108
|
|
|
* @param string $clientRedirectUri Client redirect URI (default = null) |
|
|
|
|
|
|
109
|
|
|
* |
|
110
|
|
|
* @return int The session's ID |
|
|
|
|
|
|
111
|
|
|
*/ |
|
112
|
3 |
|
public function create($ownerType, $ownerId, $clientId, $clientRedirectUri = null) |
|
113
|
|
|
{ |
|
114
|
3 |
|
return $this->getConnection()->table('oauth_sessions')->insertGetId([ |
|
115
|
3 |
|
'client_id' => $clientId, |
|
116
|
3 |
|
'owner_type' => $ownerType, |
|
117
|
3 |
|
'owner_id' => $ownerId, |
|
118
|
3 |
|
'client_redirect_uri' => $clientRedirectUri, |
|
119
|
3 |
|
'created_at' => Carbon::now(), |
|
120
|
3 |
|
'updated_at' => Carbon::now(), |
|
121
|
3 |
|
]); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Associate a scope with a session. |
|
126
|
|
|
* |
|
127
|
|
|
* @param \League\OAuth2\Server\Entity\SessionEntity $session |
|
128
|
|
|
* @param \League\OAuth2\Server\Entity\ScopeEntity $scope The scopes ID might be an integer or string |
|
129
|
|
|
* |
|
130
|
|
|
* @return void |
|
131
|
|
|
*/ |
|
132
|
3 |
View Code Duplication |
public function associateScope(SessionEntity $session, ScopeEntity $scope) |
|
|
|
|
|
|
133
|
|
|
{ |
|
134
|
3 |
|
$this->getConnection()->table('oauth_session_scopes')->insert([ |
|
135
|
3 |
|
'session_id' => $session->getId(), |
|
136
|
3 |
|
'scope_id' => $scope->getId(), |
|
137
|
3 |
|
'created_at' => Carbon::now(), |
|
138
|
3 |
|
'updated_at' => Carbon::now(), |
|
139
|
3 |
|
]); |
|
140
|
3 |
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Get a session from an auth code. |
|
144
|
|
|
* |
|
145
|
|
|
* @param \League\OAuth2\Server\Entity\AuthCodeEntity $authCode The auth code |
|
146
|
|
|
* |
|
147
|
|
|
* @return \League\OAuth2\Server\Entity\SessionEntity |
|
|
|
|
|
|
148
|
|
|
*/ |
|
149
|
6 |
View Code Duplication |
public function getByAuthCode(AuthCodeEntity $authCode) |
|
|
|
|
|
|
150
|
|
|
{ |
|
151
|
6 |
|
$result = $this->getConnection()->table('oauth_sessions') |
|
152
|
6 |
|
->select('oauth_sessions.*') |
|
153
|
6 |
|
->join('oauth_auth_codes', 'oauth_sessions.id', '=', 'oauth_auth_codes.session_id') |
|
154
|
6 |
|
->where('oauth_auth_codes.id', $authCode->getId()) |
|
155
|
6 |
|
->first(); |
|
156
|
|
|
|
|
157
|
6 |
|
if (is_null($result)) { |
|
158
|
3 |
|
return; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
3 |
|
return (new SessionEntity($this->getServer())) |
|
162
|
3 |
|
->setId($result->id) |
|
163
|
3 |
|
->setOwner($result->owner_type, $result->owner_id); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.