|
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\ScopeEntity; |
|
17
|
|
|
use League\OAuth2\Server\Storage\AccessTokenInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* This is the fluent access token class. |
|
21
|
|
|
* |
|
22
|
|
|
* @author Luca Degasperi <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class FluentAccessToken extends AbstractFluentAdapter implements AccessTokenInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Get an instance of Entities\AccessToken. |
|
28
|
|
|
* |
|
29
|
|
|
* @param string $token The access token |
|
30
|
|
|
* |
|
31
|
|
|
* @return null|AbstractTokenEntity |
|
|
|
|
|
|
32
|
|
|
*/ |
|
33
|
9 |
|
public function get($token) |
|
34
|
|
|
{ |
|
35
|
9 |
|
$result = $this->getConnection()->table('oauth_access_tokens') |
|
36
|
9 |
|
->where('oauth_access_tokens.id', $token) |
|
37
|
9 |
|
->first(); |
|
38
|
|
|
|
|
39
|
9 |
|
if (is_null($result)) { |
|
40
|
6 |
|
return; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
3 |
|
return (new AccessTokenEntity($this->getServer())) |
|
44
|
3 |
|
->setId($result->id) |
|
45
|
3 |
|
->setExpireTime((int) $result->expire_time); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/* |
|
|
|
|
|
|
49
|
|
|
public function getByRefreshToken(RefreshTokenEntity $refreshToken) |
|
50
|
|
|
{ |
|
51
|
|
|
$result = $this->getConnection()->table('oauth_access_tokens') |
|
52
|
|
|
->select('oauth_access_tokens.*') |
|
53
|
|
|
->join('oauth_refresh_tokens', 'oauth_access_tokens.id', '=', 'oauth_refresh_tokens.access_token_id') |
|
54
|
|
|
->where('oauth_refresh_tokens.id', $refreshToken->getId()) |
|
55
|
|
|
->first(); |
|
56
|
|
|
|
|
57
|
|
|
if (is_null($result)) { |
|
58
|
|
|
return null; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return (new AccessTokenEntity($this->getServer())) |
|
62
|
|
|
->setId($result->id) |
|
63
|
|
|
->setExpireTime((int)$result->expire_time); |
|
64
|
|
|
} |
|
65
|
|
|
*/ |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Get the scopes for an access token. |
|
69
|
|
|
* |
|
70
|
|
|
* @param \League\OAuth2\Server\Entity\AccessTokenEntity $token The access token |
|
71
|
|
|
* |
|
72
|
|
|
* @return array Array of \League\OAuth2\Server\Entity\ScopeEntity |
|
73
|
|
|
*/ |
|
74
|
3 |
View Code Duplication |
public function getScopes(AccessTokenEntity $token) |
|
|
|
|
|
|
75
|
|
|
{ |
|
76
|
3 |
|
$result = $this->getConnection()->table('oauth_access_token_scopes') |
|
77
|
3 |
|
->select('oauth_scopes.*') |
|
78
|
3 |
|
->join('oauth_scopes', 'oauth_access_token_scopes.scope_id', '=', 'oauth_scopes.id') |
|
79
|
3 |
|
->where('oauth_access_token_scopes.access_token_id', $token->getId()) |
|
80
|
3 |
|
->get(); |
|
81
|
|
|
|
|
82
|
3 |
|
$scopes = []; |
|
83
|
|
|
|
|
84
|
3 |
|
foreach ($result as $scope) { |
|
85
|
3 |
|
$scopes[] = (new ScopeEntity($this->getServer()))->hydrate([ |
|
86
|
3 |
|
'id' => $scope->id, |
|
87
|
3 |
|
'description' => $scope->description, |
|
88
|
3 |
|
]); |
|
89
|
3 |
|
} |
|
90
|
|
|
|
|
91
|
3 |
|
return $scopes; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Creates a new access token. |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $token The access token |
|
98
|
|
|
* @param int $expireTime The expire time expressed as a unix timestamp |
|
99
|
|
|
* @param string|int $sessionId The session ID |
|
100
|
|
|
* |
|
101
|
|
|
* @return \League\OAuth2\Server\Entity\AccessTokenEntity |
|
102
|
|
|
*/ |
|
103
|
3 |
View Code Duplication |
public function create($token, $expireTime, $sessionId) |
|
|
|
|
|
|
104
|
|
|
{ |
|
105
|
3 |
|
$this->getConnection()->table('oauth_access_tokens')->insert([ |
|
106
|
3 |
|
'id' => $token, |
|
107
|
3 |
|
'expire_time' => $expireTime, |
|
108
|
3 |
|
'session_id' => $sessionId, |
|
109
|
3 |
|
'created_at' => Carbon::now(), |
|
110
|
3 |
|
'updated_at' => Carbon::now(), |
|
111
|
3 |
|
]); |
|
112
|
|
|
|
|
113
|
3 |
|
return (new AccessTokenEntity($this->getServer())) |
|
114
|
3 |
|
->setId($token) |
|
115
|
3 |
|
->setExpireTime((int) $expireTime); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Associate a scope with an access token. |
|
120
|
|
|
* |
|
121
|
|
|
* @param \League\OAuth2\Server\Entity\AccessTokenEntity $token The access token |
|
122
|
|
|
* @param \League\OAuth2\Server\Entity\ScopeEntity $scope The scope |
|
123
|
|
|
* |
|
124
|
|
|
* @return void |
|
125
|
|
|
*/ |
|
126
|
3 |
View Code Duplication |
public function associateScope(AccessTokenEntity $token, ScopeEntity $scope) |
|
|
|
|
|
|
127
|
|
|
{ |
|
128
|
3 |
|
$this->getConnection()->table('oauth_access_token_scopes')->insert([ |
|
129
|
3 |
|
'access_token_id' => $token->getId(), |
|
130
|
3 |
|
'scope_id' => $scope->getId(), |
|
131
|
3 |
|
'created_at' => Carbon::now(), |
|
132
|
3 |
|
'updated_at' => Carbon::now(), |
|
133
|
3 |
|
]); |
|
134
|
3 |
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Delete an access token. |
|
138
|
|
|
* |
|
139
|
|
|
* @param \League\OAuth2\Server\Entity\AccessTokenEntity $token The access token to delete |
|
140
|
|
|
* |
|
141
|
|
|
* @return void |
|
142
|
|
|
*/ |
|
143
|
3 |
|
public function delete(AccessTokenEntity $token) |
|
144
|
|
|
{ |
|
145
|
3 |
|
$this->getConnection()->table('oauth_access_tokens') |
|
146
|
3 |
|
->where('oauth_access_tokens.id', $token->getId()) |
|
147
|
3 |
|
->delete(); |
|
148
|
3 |
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
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.