-
Notifications
You must be signed in to change notification settings - Fork 784
/
Copy pathClientRepository.php
107 lines (94 loc) · 3.04 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
namespace Laravel\Passport\Bridge;
use Laravel\Passport\ClientRepository as ClientModelRepository;
use Laravel\Passport\Passport;
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
class ClientRepository implements ClientRepositoryInterface
{
/**
* The client model repository.
*
* @var \Laravel\Passport\ClientRepository
*/
protected $clients;
/**
* Create a new repository instance.
*
* @param \Laravel\Passport\ClientRepository $clients
* @return void
*/
public function __construct(ClientModelRepository $clients)
{
$this->clients = $clients;
}
/**
* {@inheritdoc}
*/
public function getClientEntity($clientIdentifier)
{
$record = $this->clients->findActive($clientIdentifier);
if (! $record) {
return;
}
return new Client(
$clientIdentifier,
$record->name,
$record->redirect,
$record->confidential(),
$record->provider
);
}
/**
* {@inheritdoc}
*/
public function validateClient($clientIdentifier, $clientSecret, $grantType)
{
// 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((string) $clientSecret, $record->secret, $grantType);
}
/**
* Determine if the given client can handle the given grant type.
*
* @param \Laravel\Passport\Client $record
* @param string $grantType
* @return bool
*/
protected function handlesGrant($record, $grantType)
{
if (is_array($record->grant_types) && ! in_array($grantType, $record->grant_types)) {
return false;
}
switch ($grantType) {
case 'authorization_code':
return ! $record->firstParty();
case 'personal_access':
return $record->personal_access_client && $record->confidential();
case 'password':
return $record->password_client;
case 'client_credentials':
return $record->confidential();
default:
return true;
}
}
/**
* Verify the client secret is valid.
*
* @param string $clientSecret
* @param string $storedHash
* @param string $grantType
* @return bool
*/
protected function verifySecret($clientSecret, $storedHash, $grantType)
{
return Passport::$hashesClientSecrets && $grantType !== 'personal_access'
? password_verify($clientSecret, $storedHash)
: hash_equals($storedHash, $clientSecret);
}
}