-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathClientRepository.php
More file actions
111 lines (85 loc) · 3.59 KB
/
ClientRepository.php
File metadata and controls
111 lines (85 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
declare(strict_types=1);
namespace League\Bundle\OAuth2ServerBundle\Repository;
use League\Bundle\OAuth2ServerBundle\Entity\Client as ClientEntity;
use League\Bundle\OAuth2ServerBundle\Manager\ClientManagerInterface;
use League\Bundle\OAuth2ServerBundle\Model\ClientInterface;
use League\OAuth2\Server\Entities\ClientEntityInterface;
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
use Symfony\Component\PasswordHasher\PasswordHasherInterface;
final class ClientRepository implements ClientRepositoryInterface
{
/**
* @var ClientManagerInterface
*/
private $clientManager;
private ?PasswordHasherInterface $passwordHasher;
public function __construct(ClientManagerInterface $clientManager, ?PasswordHasherInterface $passwordHasher = null)
{
$this->clientManager = $clientManager;
$this->passwordHasher = $passwordHasher;
if (null === $this->passwordHasher) {
trigger_deprecation('league/oauth2-server-bundle', '1.2', 'Not passing a "%s" to "%s" is deprecated since version 1.2 and will be required in 2.0.', PasswordHasherInterface::class, __CLASS__);
}
}
public function getClientEntity(string $clientIdentifier): ?ClientEntityInterface
{
$client = $this->clientManager->find($clientIdentifier);
if (null === $client) {
return null;
}
return $this->buildClientEntity($client);
}
public function validateClient(string $clientIdentifier, #[\SensitiveParameter] ?string $clientSecret, ?string $grantType): bool
{
$client = $this->clientManager->find($clientIdentifier);
if (null === $client) {
return false;
}
if (!$client->isActive()) {
return false;
}
if (!$this->isGrantSupported($client, $grantType)) {
return false;
}
if (!$client->isConfidential()) {
return true;
}
$storedSecret = (string) $client->getSecret();
$inputSecret = (string) $clientSecret;
if (null === $this->passwordHasher) {
return hash_equals($storedSecret, $inputSecret);
}
$secretIsValid = $this->passwordHasher->verify($storedSecret, $inputSecret);
if ($secretIsValid && $this->passwordHasher->needsRehash($storedSecret)) {
if (!method_exists($client, 'setSecret')) {
trigger_deprecation('league/oauth2-server-bundle', '1.2', 'Not implementing method "setSecret()" in client "%s" is deprecated. This method will be required in 2.0.', $client::class);
} else {
$client->setSecret($this->passwordHasher->hash($inputSecret));
$this->clientManager->save($client);
}
}
return $secretIsValid;
}
private function buildClientEntity(ClientInterface $client): ClientEntity
{
$clientEntity = new ClientEntity();
$clientEntity->setName($client->getName());
$clientEntity->setIdentifier($client->getIdentifier());
$clientEntity->setRedirectUri(array_map('strval', $client->getRedirectUris()));
$clientEntity->setConfidential($client->isConfidential());
$clientEntity->setAllowPlainTextPkce($client->isPlainTextPkceAllowed());
return $clientEntity;
}
private function isGrantSupported(ClientInterface $client, ?string $grant): bool
{
if (null === $grant) {
return true;
}
$grants = $client->getGrants();
if (empty($grants)) {
return true;
}
return \in_array($grant, $client->getGrants());
}
}