|
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\Entity\Client; |
|
15
|
|
|
use LoginCidadao\OAuthBundle\Model\ClientInterface; |
|
16
|
|
|
use LoginCidadao\OpenIDBundle\Service\SubjectIdentifierService; |
|
17
|
|
|
use OAuth2\ServerBundle\Storage\AccessToken as BaseClass; |
|
18
|
|
|
use OAuth2\Storage\AccessTokenInterface; |
|
19
|
|
|
use Doctrine\ORM\EntityManager; |
|
20
|
|
|
|
|
21
|
|
|
class AccessToken extends BaseClass implements AccessTokenInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var EntityManager */ |
|
24
|
|
|
private $em; |
|
25
|
|
|
|
|
26
|
|
|
/** @var SubjectIdentifierService */ |
|
27
|
|
|
private $subjectIdentifierService; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct(EntityManager $EntityManager) |
|
|
|
|
|
|
30
|
|
|
{ |
|
31
|
|
|
parent::__construct($EntityManager); |
|
|
|
|
|
|
32
|
|
|
$this->em = $EntityManager; |
|
|
|
|
|
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Look up the supplied oauth_token from storage. |
|
37
|
|
|
* |
|
38
|
|
|
* We need to retrieve access token data as we create and verify tokens. |
|
39
|
|
|
* |
|
40
|
|
|
* @param $oauth_token |
|
41
|
|
|
* oauth_token to be check with. |
|
42
|
|
|
* |
|
43
|
|
|
* @return array|null |
|
|
|
|
|
|
44
|
|
|
* An associative array as below, and return NULL if the supplied oauth_token |
|
45
|
|
|
* is invalid: |
|
46
|
|
|
* - client_id: Stored client identifier. |
|
47
|
|
|
* - expires: Stored expiration in unix timestamp. |
|
48
|
|
|
* - scope: (optional) Stored scope values in space-separated string. |
|
49
|
|
|
* |
|
50
|
|
|
* @ingroup oauth2_section_7 |
|
51
|
|
|
*/ |
|
52
|
|
|
public function getAccessToken($oauth_token) |
|
|
|
|
|
|
53
|
|
|
{ |
|
54
|
|
|
/** @var \LoginCidadao\OAuthBundle\Entity\AccessToken $accessToken */ |
|
55
|
|
|
$accessToken = $this->em->getRepository('LoginCidadaoOAuthBundle:AccessToken') |
|
56
|
|
|
->findOneBy(['token' => $oauth_token]); |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
if (!$accessToken) { |
|
59
|
|
|
return null; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** @var Client $client */ |
|
63
|
|
|
$client = $accessToken->getClient(); |
|
64
|
|
|
|
|
65
|
|
|
/** @var PersonInterface $person */ |
|
66
|
|
|
$person = $accessToken->getUser(); |
|
67
|
|
|
|
|
68
|
|
|
return [ |
|
69
|
|
|
'client_id' => $client->getClientId(), |
|
70
|
|
|
'user_id' => $this->subjectIdentifierService->getSubjectIdentifier($person, $client), |
|
|
|
|
|
|
71
|
|
|
'expires' => $accessToken->getExpiresAt(), |
|
72
|
|
|
'scope' => $accessToken->getScope(), |
|
73
|
|
|
'id_token' => $accessToken->getIdToken(), |
|
74
|
|
|
]; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Store the supplied access token values to storage. |
|
79
|
|
|
* |
|
80
|
|
|
* We need to store access token data as we create and verify tokens. |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $oauth_token |
|
83
|
|
|
* oauth_token to be stored. |
|
84
|
|
|
* @param string $client_id |
|
85
|
|
|
* Client identifier to be stored. |
|
86
|
|
|
* @param string $user_id |
|
87
|
|
|
* User identifier to be stored. |
|
88
|
|
|
* @param int $expires Expiration to be stored as a Unix timestamp. |
|
89
|
|
|
* @param string $scope (optional) Scopes to be stored in space-separated string. |
|
|
|
|
|
|
90
|
|
|
* @param null|string $id_token |
|
91
|
|
|
* @return null|void |
|
92
|
|
|
* @ingroup oauth2_section_4 |
|
93
|
|
|
*/ |
|
94
|
|
|
public function setAccessToken($oauth_token, $client_id, $user_id, $expires, $scope = null, $id_token = null) |
|
|
|
|
|
|
95
|
|
|
{ |
|
96
|
|
|
// Get Client Entity |
|
97
|
|
|
$id = explode('_', $client_id); |
|
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
/** @var ClientInterface $client */ |
|
100
|
|
|
$client = $this->em->getRepository('LoginCidadaoOAuthBundle:Client')->find($id[0]); |
|
101
|
|
|
|
|
102
|
|
|
if (!$client) { |
|
103
|
|
|
return null; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
if ($user_id === null) { |
|
|
|
|
|
|
107
|
|
|
return null; |
|
108
|
|
|
} else { |
|
109
|
|
|
/** @var PersonInterface $user */ |
|
110
|
|
|
$user = $this->em->getRepository('LoginCidadaoCoreBundle:Person')->find($user_id); |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
// Create Access Token |
|
114
|
|
|
$accessToken = new \LoginCidadao\OAuthBundle\Entity\AccessToken(); |
|
115
|
|
|
$accessToken->setToken($oauth_token); |
|
|
|
|
|
|
116
|
|
|
$accessToken->setClient($client); |
|
117
|
|
|
$accessToken->setUser($user); |
|
118
|
|
|
$accessToken->setExpiresAt($expires); |
|
119
|
|
|
$accessToken->setScope($scope); |
|
120
|
|
|
$accessToken->setIdToken($id_token); |
|
|
|
|
|
|
121
|
|
|
|
|
122
|
|
|
// Store Access Token and Authorization |
|
123
|
|
|
$this->em->persist($accessToken); |
|
124
|
|
|
$this->em->flush(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public function setSubjectIdentifierService(SubjectIdentifierService $subjectIdentifierService) |
|
128
|
|
|
{ |
|
129
|
|
|
$this->subjectIdentifierService = $subjectIdentifierService; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
This check marks parameter names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString.