|
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\ResponseType; |
|
12
|
|
|
|
|
13
|
|
|
use LoginCidadao\CoreBundle\Model\PersonInterface; |
|
14
|
|
|
use LoginCidadao\CoreBundle\Security\User\Manager\UserManager; |
|
15
|
|
|
use LoginCidadao\OpenIDBundle\Entity\ClientMetadata; |
|
16
|
|
|
use LoginCidadao\OpenIDBundle\Manager\ClientManager; |
|
17
|
|
|
use LoginCidadao\OpenIDBundle\Service\SubjectIdentifierService; |
|
18
|
|
|
use OAuth2\OpenID\ResponseType\IdToken as BaseIdToken; |
|
19
|
|
|
use OAuth2\Storage\PublicKeyInterface; |
|
20
|
|
|
use OAuth2\Encryption\EncryptionInterface; |
|
21
|
|
|
|
|
22
|
|
|
class IdToken extends BaseIdToken |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var PublicKeyInterface */ |
|
25
|
|
|
protected $publicKeyStorage; |
|
26
|
|
|
|
|
27
|
|
|
/** @var EncryptionInterface */ |
|
28
|
|
|
protected $encryptionUtil; |
|
29
|
|
|
|
|
30
|
|
|
/** @var SubjectIdentifierService */ |
|
31
|
|
|
private $subjectIdentifierService; |
|
32
|
|
|
|
|
33
|
|
|
/** @var ClientManager */ |
|
34
|
|
|
private $clientManager; |
|
35
|
|
|
|
|
36
|
|
|
/** @var UserManager */ |
|
37
|
|
|
private $userManager; |
|
38
|
|
|
|
|
39
|
1 |
|
protected function encodeToken(array $token, $client_id = null) |
|
40
|
|
|
{ |
|
41
|
1 |
|
$private_key = $this->publicKeyStorage->getPrivateKey($client_id); |
|
42
|
1 |
|
$algorithm = $this->publicKeyStorage->getEncryptionAlgorithm($client_id); |
|
43
|
|
|
|
|
44
|
1 |
|
$token['kid'] = 'pub'; |
|
45
|
|
|
|
|
46
|
1 |
|
return $this->encryptionUtil->encode($token, $private_key, $algorithm); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Create id token |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $client_id |
|
53
|
|
|
* @param mixed $userInfo |
|
54
|
|
|
* @param mixed $nonce |
|
55
|
|
|
* @param mixed $userClaims |
|
56
|
|
|
* @param mixed $access_token |
|
57
|
|
|
* @return mixed|string |
|
58
|
|
|
*/ |
|
59
|
1 |
|
public function createIdToken($client_id, $userInfo, $nonce = null, $userClaims = null, $access_token = null) |
|
60
|
|
|
{ |
|
61
|
1 |
|
$userInfo = $this->handleSubjectIdentifier($userInfo, $client_id); |
|
62
|
|
|
|
|
63
|
1 |
|
return parent::createIdToken($client_id, $userInfo, $nonce, $userClaims, $access_token); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
1 |
|
private function handleSubjectIdentifier($userInfo, $clientId) |
|
67
|
|
|
{ |
|
68
|
1 |
|
$client = $this->clientManager->getClientById($clientId); |
|
69
|
1 |
|
$metadata = $client->getMetadata(); |
|
70
|
|
|
|
|
71
|
1 |
|
if (is_array($userInfo)) { |
|
72
|
|
|
if (!isset($userInfo['user_id'])) { |
|
73
|
|
|
throw new \LogicException('if $user_id argument is an array, user_id index must be set'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$userInfo['user_id'] = $this->getSub($metadata, $userInfo['user_id']); |
|
77
|
|
|
} else { |
|
78
|
1 |
|
$userInfo = $this->getSub($metadata, $userInfo); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
1 |
|
return $userInfo; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
1 |
|
private function getSub(ClientMetadata $metadata, $userId) |
|
85
|
|
|
{ |
|
86
|
|
|
/** @var PersonInterface $person */ |
|
87
|
1 |
|
$person = $this->userManager->findUserBy(['id' => $userId]); |
|
88
|
|
|
|
|
89
|
1 |
|
return $this->subjectIdentifierService->getSubjectIdentifier($person, $metadata); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
1 |
|
public function setUserManager(UserManager $userManager) |
|
93
|
|
|
{ |
|
94
|
1 |
|
$this->userManager = $userManager; |
|
95
|
1 |
|
} |
|
96
|
|
|
|
|
97
|
1 |
|
public function setClientManager(ClientManager $clientManager) |
|
98
|
|
|
{ |
|
99
|
1 |
|
$this->clientManager = $clientManager; |
|
100
|
1 |
|
} |
|
101
|
|
|
|
|
102
|
1 |
|
public function setSubjectIdentifierService(SubjectIdentifierService $subjectIdentifierService) |
|
103
|
|
|
{ |
|
104
|
1 |
|
$this->subjectIdentifierService = $subjectIdentifierService; |
|
105
|
1 |
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|