|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the login-cidadao project or it's bundles. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Guilherme Donato <guilhermednt on github> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace LoginCidadao\OpenIDBundle\Storage; |
|
12
|
|
|
|
|
13
|
|
|
use LoginCidadao\CoreBundle\Model\PersonInterface; |
|
14
|
|
|
use LoginCidadao\OAuthBundle\Model\ClientInterface; |
|
15
|
|
|
use OAuth2\ServerBundle\Storage\RefreshToken as BaseClass; |
|
16
|
|
|
use OAuth2\Storage\RefreshTokenInterface; |
|
17
|
|
|
use Doctrine\ORM\EntityManager; |
|
18
|
|
|
|
|
19
|
|
|
class RefreshToken extends BaseClass implements RefreshTokenInterface |
|
20
|
|
|
{ |
|
21
|
|
|
private $em; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct(EntityManager $EntityManager) |
|
|
|
|
|
|
24
|
|
|
{ |
|
25
|
|
|
parent::__construct($EntityManager); |
|
|
|
|
|
|
26
|
|
|
$this->em = $EntityManager; |
|
|
|
|
|
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Grant refresh access tokens. |
|
31
|
|
|
* |
|
32
|
|
|
* Retrieve the stored data for the given refresh token. |
|
33
|
|
|
* |
|
34
|
|
|
* Required for OAuth2::GRANT_TYPE_REFRESH_TOKEN. |
|
35
|
|
|
* |
|
36
|
|
|
* @param $refresh_token |
|
37
|
|
|
* Refresh token to be check with. |
|
38
|
|
|
* |
|
39
|
|
|
* @return array |
|
|
|
|
|
|
40
|
|
|
* An associative array as below, and NULL if the refresh_token is |
|
41
|
|
|
* invalid: |
|
42
|
|
|
* - refresh_token: Stored refresh token identifier. |
|
43
|
|
|
* - client_id: Stored client identifier. |
|
44
|
|
|
* - user_id: Stored user identifier. |
|
45
|
|
|
* - expires: Stored expiration unix timestamp. |
|
46
|
|
|
* - scope: (optional) Stored scope values in space-separated string. |
|
47
|
|
|
* |
|
48
|
|
|
* @see http://tools.ietf.org/html/rfc6749#section-6 |
|
49
|
|
|
* |
|
50
|
|
|
* @ingroup oauth2_section_6 |
|
51
|
|
|
*/ |
|
52
|
|
|
public function getRefreshToken($refresh_token) |
|
|
|
|
|
|
53
|
|
|
{ |
|
54
|
|
|
/** @var \LoginCidadao\OAuthBundle\Entity\RefreshToken $refreshToken */ |
|
55
|
|
|
$refreshToken = $this->em->getRepository('LoginCidadaoOAuthBundle:RefreshToken') |
|
56
|
|
|
->findOneBy(array('token' => $refresh_token)); |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
if (!$refreshToken) { |
|
59
|
|
|
return null; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
// Get Client |
|
63
|
|
|
/** @var ClientInterface $client */ |
|
64
|
|
|
$client = $refreshToken->getClient(); |
|
65
|
|
|
|
|
66
|
|
|
/** @var PersonInterface $user */ |
|
67
|
|
|
$user = $refreshToken->getUser(); |
|
68
|
|
|
|
|
69
|
|
|
return [ |
|
70
|
|
|
'refresh_token' => $refreshToken->getToken(), |
|
71
|
|
|
'client_id' => $client->getPublicId(), |
|
72
|
|
|
'user_id' => $user->getId(), |
|
73
|
|
|
'expires' => $refreshToken->getExpiresAt(), |
|
74
|
|
|
'scope' => $refreshToken->getScope(), |
|
75
|
|
|
]; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Take the provided refresh token values and store them somewhere. |
|
80
|
|
|
* |
|
81
|
|
|
* This function should be the storage counterpart to getRefreshToken(). |
|
82
|
|
|
* |
|
83
|
|
|
* If storage fails for some reason, we're not currently checking for |
|
84
|
|
|
* any sort of success/failure, so you should bail out of the script |
|
85
|
|
|
* and provide a descriptive fail message. |
|
86
|
|
|
* |
|
87
|
|
|
* Required for OAuth2::GRANT_TYPE_REFRESH_TOKEN. |
|
88
|
|
|
* |
|
89
|
|
|
* @param $refresh_token |
|
90
|
|
|
* Refresh token to be stored. |
|
91
|
|
|
* @param $client_id |
|
92
|
|
|
* Client identifier to be stored. |
|
93
|
|
|
* @param $user_id |
|
94
|
|
|
* User identifier to be stored. |
|
95
|
|
|
* @param $expires |
|
96
|
|
|
* expires to be stored. |
|
97
|
|
|
* @param $scope |
|
98
|
|
|
* (optional) Scopes to be stored in space-separated string. |
|
99
|
|
|
* |
|
100
|
|
|
* @ingroup oauth2_section_6 |
|
101
|
|
|
* @return null|void |
|
102
|
|
|
*/ |
|
103
|
|
|
public function setRefreshToken( |
|
|
|
|
|
|
104
|
|
|
$refresh_token, |
|
105
|
|
|
$client_id, |
|
106
|
|
|
$user_id, |
|
107
|
|
|
$expires, |
|
108
|
|
|
$scope = null |
|
109
|
|
|
) { |
|
110
|
|
|
// Get Client Entity |
|
111
|
|
|
$id = explode('_', $client_id); |
|
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
/** @var ClientInterface $client */ |
|
114
|
|
|
$client = $this->em->getRepository('LoginCidadaoOAuthBundle:Client') |
|
115
|
|
|
->find($id[0]); |
|
116
|
|
|
|
|
117
|
|
|
if (!$client) { |
|
118
|
|
|
return null; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
if ($user_id === null) { |
|
|
|
|
|
|
122
|
|
|
return null; |
|
123
|
|
|
} else { |
|
124
|
|
|
/** @var PersonInterface $user */ |
|
125
|
|
|
$user = $this->em->getRepository('LoginCidadaoCoreBundle:Person') |
|
126
|
|
|
->find($user_id); |
|
|
|
|
|
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
// Create Refresh Token |
|
130
|
|
|
$refreshToken = new \LoginCidadao\OAuthBundle\Entity\RefreshToken(); |
|
131
|
|
|
$refreshToken->setToken($refresh_token); |
|
|
|
|
|
|
132
|
|
|
$refreshToken->setClient($client); |
|
133
|
|
|
$refreshToken->setUser($user); |
|
134
|
|
|
$refreshToken->setExpiresAt($expires); |
|
135
|
|
|
$refreshToken->setScope($scope); |
|
136
|
|
|
|
|
137
|
|
|
// Store Refresh Token |
|
138
|
|
|
$this->em->persist($refreshToken); |
|
139
|
|
|
$this->em->flush(); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Expire a used refresh token. |
|
144
|
|
|
* |
|
145
|
|
|
* This is not explicitly required in the spec, but is almost implied. |
|
146
|
|
|
* After granting a new refresh token, the old one is no longer useful and |
|
147
|
|
|
* so should be forcibly expired in the data store so it can't be used again. |
|
148
|
|
|
* |
|
149
|
|
|
* If storage fails for some reason, we're not currently checking for |
|
150
|
|
|
* any sort of success/failure, so you should bail out of the script |
|
151
|
|
|
* and provide a descriptive fail message. |
|
152
|
|
|
* |
|
153
|
|
|
* @param $refresh_token |
|
154
|
|
|
* Refresh token to be expirse. |
|
155
|
|
|
* |
|
156
|
|
|
* @ingroup oauth2_section_6 |
|
157
|
|
|
*/ |
|
158
|
|
|
public function unsetRefreshToken($refresh_token) |
|
|
|
|
|
|
159
|
|
|
{ |
|
160
|
|
|
$refreshToken = $this->em->getRepository('LoginCidadaoOAuthBundle:RefreshToken') |
|
161
|
|
|
->findOneBy(['token' => $refresh_token]); |
|
|
|
|
|
|
162
|
|
|
$this->em->remove($refreshToken); |
|
|
|
|
|
|
163
|
|
|
$this->em->flush(); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.