forked from laravel/passport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClientRepository.php
84 lines (71 loc) · 2.33 KB
/
ClientRepository.php
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
<?php
namespace Laravel\Passport\Bridge;
use Illuminate\Contracts\Hashing\Hasher;
use Laravel\Passport\Client as ClientModel;
use Laravel\Passport\ClientRepository as ClientModelRepository;
use League\OAuth2\Server\Entities\ClientEntityInterface;
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
class ClientRepository implements ClientRepositoryInterface
{
/**
* The client model repository.
*/
protected ClientModelRepository $clients;
/**
* The hasher implementation.
*/
protected Hasher $hasher;
/**
* Create a new repository instance.
*/
public function __construct(ClientModelRepository $clients, Hasher $hasher)
{
$this->clients = $clients;
$this->hasher = $hasher;
}
/**
* {@inheritdoc}
*/
public function getClientEntity(string $clientIdentifier): ?ClientEntityInterface
{
$record = $this->clients->findActive($clientIdentifier);
if (! $record) {
return null;
}
return new Client(
$clientIdentifier,
$record->name,
$record->redirect_uris,
$record->confidential(),
$record->provider
);
}
/**
* {@inheritdoc}
*/
public function validateClient(string $clientIdentifier, ?string $clientSecret, ?string $grantType): bool
{
// First, we will verify that the client exists and is authorized to create personal
// access tokens. Generally personal access tokens are only generated by the user
// from the main interface. We'll only let certain clients generate the tokens.
$record = $this->clients->findActive($clientIdentifier);
if (! $record || ! $this->handlesGrant($record, $grantType)) {
return false;
}
return ! $record->confidential() || $this->verifySecret($clientSecret, $record->secret);
}
/**
* Determine if the given client can handle the given grant type.
*/
protected function handlesGrant(ClientModel $record, string $grantType): bool
{
return $record->hasGrantType($grantType);
}
/**
* Verify the client secret is valid.
*/
protected function verifySecret(string $clientSecret, string $storedHash): bool
{
return $this->hasher->check($clientSecret, $storedHash);
}
}