|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Rinvex\Oauth\Repositories; |
|
6
|
|
|
|
|
7
|
|
|
use RuntimeException; |
|
8
|
|
|
use Illuminate\Support\Str; |
|
9
|
|
|
use Rinvex\Oauth\Bridge\User; |
|
10
|
|
|
use Illuminate\Contracts\Hashing\Hasher; |
|
11
|
|
|
use League\OAuth2\Server\Entities\ClientEntityInterface; |
|
12
|
|
|
use League\OAuth2\Server\Repositories\UserRepositoryInterface; |
|
13
|
|
|
|
|
14
|
|
|
class UserRepository implements UserRepositoryInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* The hasher implementation. |
|
18
|
|
|
* |
|
19
|
|
|
* @var \Illuminate\Contracts\Hashing\Hasher |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $hasher; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Create a new repository instance. |
|
25
|
|
|
* |
|
26
|
|
|
* @param \Illuminate\Contracts\Hashing\Hasher $hasher |
|
27
|
|
|
* |
|
28
|
|
|
* @return void |
|
|
|
|
|
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(Hasher $hasher) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->hasher = $hasher; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* {@inheritdoc} |
|
37
|
|
|
*/ |
|
38
|
|
|
public function getUserEntityByUserCredentials($username, $password, $grantType, ClientEntityInterface $clientEntity) |
|
39
|
|
|
{ |
|
40
|
|
|
if (is_null($model = config('auth.providers.'.Str::plural($clientEntity->user_type).'.model'))) { |
|
|
|
|
|
|
41
|
|
|
throw new RuntimeException('Unable to determine authentication model from configuration.'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
if (method_exists($model, 'findAndValidateForOAuth')) { |
|
45
|
|
|
$user = (new $model())->findAndValidateForOAuth($username, $password); |
|
46
|
|
|
|
|
47
|
|
|
if (! $user) { |
|
48
|
|
|
return; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return new User($user->getAuthIdentifier()); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
if (method_exists($model, 'findForOAuth')) { |
|
55
|
|
|
$user = (new $model())->findForOAuth($username); |
|
56
|
|
|
} else { |
|
57
|
|
|
$user = (new $model())->where('email', $username)->first(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if (! $user) { |
|
61
|
|
|
return; |
|
62
|
|
|
} elseif (method_exists($user, 'validateForOAuthPasswordGrant')) { |
|
63
|
|
|
if (! $user->validateForOAuthPasswordGrant($password)) { |
|
64
|
|
|
return; |
|
65
|
|
|
} |
|
66
|
|
|
} elseif (! $this->hasher->check($password, $user->getAuthPassword())) { |
|
67
|
|
|
return; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return new User($user->getAuthIdentifier()); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.