-
Notifications
You must be signed in to change notification settings - Fork 26
/
AuthUserProvider.php
180 lines (149 loc) · 4.51 KB
/
AuthUserProvider.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
namespace Kitar\Dynamodb\Model;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\UserProvider as BaseUserProvider;
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
class AuthUserProvider implements BaseUserProvider
{
/**
* The hasher implementation.
*
* @var \Illuminate\Contracts\Hashing\Hasher
*/
protected $hasher;
/**
* The Eloquent user model.
*
* @var string
*/
protected $model;
/**
* The column name of the api token.
*
* @var string
*/
protected $apiTokenName;
/**
* The index name to use when querying by api token.
*
* @var string
*/
protected $apiTokenIndex;
public function __construct(HasherContract $hasher, $model, $apiTokenName = null, $apiTokenIndex = null)
{
$this->model = $model;
$this->hasher = $hasher;
$this->apiTokenName = $apiTokenName;
$this->apiTokenIndex = $apiTokenIndex;
}
/**
* Retrieve a user by their unique identifier.
*
* @param mixed $identifier
* @return \Illuminate\Contracts\Auth\Authenticatable|null
*/
public function retrieveById($identifier)
{
$model = $this->createModel();
return $model->find($identifier);
}
/**
* Retrieve a user by their unique identifier and "remember me" token.
*
* @param mixed $identifier
* @param string $token
* @return \Illuminate\Contracts\Auth\Authenticatable|null
*/
public function retrieveByToken($identifier, $token)
{
$user = $this->retrieveById($identifier);
if (! $user) {
return;
}
$rememberToken = $user->getRememberToken();
return $rememberToken && hash_equals($rememberToken, $token)
? $user : null;
}
/**
* Update the "remember me" token for the given user in storage.
*
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @param string $token
* @return void
*/
public function updateRememberToken(Authenticatable $user, $token)
{
$user->setRememberToken($token);
$timestamps = $user->timestamps;
$user->timestamps = false;
$user->save();
$user->timestamps = $timestamps;
}
/**
* Retrieve a user by the given credentials.
* Identifier or API Token are supported.
*
* @param array $credentials
* @return \Illuminate\Contracts\Auth\Authenticatable|null
*/
public function retrieveByCredentials(array $credentials)
{
if (isset($credentials['password'])) {
unset($credentials['password']);
}
if (count($credentials) !== 1) {
return;
}
$model = $this->createModel();
$id = $credentials[$model->getAuthIdentifierName()] ?? null;
if ($id) {
return $this->retrieveById($id);
}
$apiToken = $this->apiTokenName ? $credentials[$this->apiTokenName] ?? null : null;
if ($apiToken && $this->apiTokenIndex) {
return $model->index($this->apiTokenIndex)
->keyCondition($this->apiTokenName, '=', $apiToken)
->query()
->first();
}
}
/**
* Validate a user against the given credentials.
*
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @param array $credentials
* @return bool
*/
public function validateCredentials(Authenticatable $user, array $credentials)
{
$plain = $credentials['password'];
return $this->hasher->check($plain, $user->getAuthPassword());
}
/**
* Rehash the user's password if required and supported.
*
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @param array $credentials
* @param bool $force
* @return void
*/
public function rehashPasswordIfRequired(Authenticatable $user, array $credentials, bool $force = false)
{
if (! $this->hasher->needsRehash($user->getAuthPassword()) && ! $force) {
return;
}
$user->forceFill([
$user->getAuthPasswordName() => $this->hasher->make($credentials['password']),
])->save();
}
/**
* Create a new instance of the model.
*
* @return \Kitar\Dynamodb\Model\Model
*/
public function createModel()
{
$class = '\\'.ltrim($this->model, '\\');
return new $class;
}
}