1
|
<?php
|
2
|
|
3
|
namespace Illuminate\Auth;
|
4
|
|
5
|
use Closure;
|
6
|
use InvalidArgumentException;
|
7
|
use Illuminate\Contracts\Auth\Factory as FactoryContract;
|
8
|
|
9
|
class AuthManager implements FactoryContract
|
10
|
{
|
11
|
use CreatesUserProviders;
|
12
|
|
13
|
/**
|
14
|
* The application instance.
|
15
|
*
|
16
|
* @var \Illuminate\Foundation\Application
|
17
|
*/
|
18
|
protected $app;
|
19
|
|
20
|
/**
|
21
|
* The registered custom driver creators.
|
22
|
*
|
23
|
* @var array
|
24
|
*/
|
25
|
protected $customCreators = [];
|
26
|
|
27
|
/**
|
28
|
* The array of created "drivers".
|
29
|
*
|
30
|
* @var array
|
31
|
*/
|
32
|
protected $guards = [];
|
33
|
|
34
|
/**
|
35
|
* The user resolver shared by various services.
|
36
|
*
|
37
|
* Determines the default user for Gate, Request, and the Authenticatable contract.
|
38
|
*
|
39
|
* @var \Closure
|
40
|
*/
|
41
|
protected $userResolver;
|
42
|
|
43
|
/**
|
44
|
* Create a new Auth manager instance.
|
45
|
*
|
46
|
* @param \Illuminate\Foundation\Application $app
|
47
|
* @return void
|
48
|
*/
|
49
|
public function __construct($app)
|
50
|
{
|
51
|
$this->app = $app;
|
52
|
|
53
|
$this->userResolver = function ($guard = null) {
|
54
|
return $this->guard($guard)->user();
|
55
|
};
|
56
|
}
|
57
|
|
58
|
/**
|
59
|
* Attempt to get the guard from the local cache.
|
60
|
*
|
61
|
* @param string $name
|
62
|
* @return \Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard
|
63
|
*/
|
64
|
public function guard($name = null)
|
65
|
{
|
66
|
$name = $name ?: $this->getDefaultDriver();
|
67
|
|
68
|
return isset($this->guards[$name])
|
69
|
? $this->guards[$name]
|
70
|
: $this->guards[$name] = $this->resolve($name);
|
71
|
}
|
72
|
|
73
|
/**
|
74
|
* Resolve the given guard.
|
75
|
*
|
76
|
* @param string $name
|
77
|
* @return \Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard
|
78
|
*
|
79
|
* @throws \InvalidArgumentException
|
80
|
*/
|
81
|
protected function resolve($name)
|
82
|
{
|
83
|
$config = $this->getConfig($name);
|
84
|
|
85
|
if (is_null($config)) {
|
86
|
throw new InvalidArgumentException("Auth guard [{$name}] is not defined.");
|
87
|
}
|
88
|
|
89
|
if (isset($this->customCreators[$config['driver']])) {
|
90
|
return $this->callCustomCreator($name, $config);
|
91
|
} else {
|
92
|
$driverMethod = 'create'.ucfirst($config['driver']).'Driver';
|
93
|
|
94
|
if (method_exists($this, $driverMethod)) {
|
95
|
return $this->{$driverMethod}($name, $config);
|
96
|
} else {
|
97
|
throw new InvalidArgumentException("Auth guard driver [{$name}] is not defined.");
|
98
|
}
|
99
|
}
|
100
|
}
|
101
|
|
102
|
/**
|
103
|
* Call a custom driver creator.
|
104
|
*
|
105
|
* @param string $name
|
106
|
* @param array $config
|
107
|
* @return mixed
|
108
|
*/
|
109
|
protected function callCustomCreator($name, array $config)
|
110
|
{
|
111
|
return $this->customCreators[$config['driver']]($this->app, $name, $config);
|
112
|
}
|
113
|
|
114
|
/**
|
115
|
* Create a session based authentication guard.
|
116
|
*
|
117
|
* @param string $name
|
118
|
* @param array $config
|
119
|
* @return \Illuminate\Auth\SessionGuard
|
120
|
*/
|
121
|
public function createSessionDriver($name, $config)
|
122
|
{
|
123
|
$provider = $this->createUserProvider($config['provider']);
|
124
|
|
125
|
$guard = new SessionGuard($name, $provider, $this->app['session.store']);
|
126
|
|
127
|
// When using the remember me functionality of the authentication services we
|
128
|
// will need to be set the encryption instance of the guard, which allows
|
129
|
// secure, encrypted cookie values to get generated for those cookies.
|
130
|
if (method_exists($guard, 'setCookieJar')) {
|
131
|
$guard->setCookieJar($this->app['cookie']);
|
132
|
}
|
133
|
|
134
|
if (method_exists($guard, 'setDispatcher')) {
|
135
|
$guard->setDispatcher($this->app['events']);
|
136
|
}
|
137
|
|
138
|
if (method_exists($guard, 'setRequest')) {
|
139
|
$guard->setRequest($this->app->refresh('request', $guard, 'setRequest'));
|
140
|
}
|
141
|
|
142
|
return $guard;
|
143
|
}
|
144
|
|
145
|
/**
|
146
|
* Create a token based authentication guard.
|
147
|
*
|
148
|
* @param string $name
|
149
|
* @param array $config
|
150
|
* @return \Illuminate\Auth\TokenGuard
|
151
|
*/
|
152
|
public function createTokenDriver($name, $config)
|
153
|
{
|
154
|
// The token guard implements a basic API token based guard implementation
|
155
|
// that takes an API token field from the request and matches it to the
|
156
|
// user in the database or another persistence layer where users are.
|
157
|
$guard = new TokenGuard(
|
158
|
$this->createUserProvider($config['provider']),
|
159
|
$this->app['request']
|
160
|
);
|
161
|
|
162
|
$this->app->refresh('request', $guard, 'setRequest');
|
163
|
|
164
|
return $guard;
|
165
|
}
|
166
|
|
167
|
/**
|
168
|
* Get the guard configuration.
|
169
|
*
|
170
|
* @param string $name
|
171
|
* @return array
|
172
|
*/
|
173
|
protected function getConfig($name)
|
174
|
{
|
175
|
return $this->app['config']["auth.guards.{$name}"];
|
176
|
}
|
177
|
|
178
|
/**
|
179
|
* Get the default authentication driver name.
|
180
|
*
|
181
|
* @return string
|
182
|
*/
|
183
|
public function getDefaultDriver()
|
184
|
{
|
185
|
return $this->app['config']['auth.defaults.guard'];
|
186
|
}
|
187
|
|
188
|
/**
|
189
|
* Set the default guard driver the factory should serve.
|
190
|
*
|
191
|
* @param string $name
|
192
|
* @return void
|
193
|
*/
|
194
|
public function shouldUse($name)
|
195
|
{
|
196
|
$this->setDefaultDriver($name);
|
197
|
|
198
|
$this->userResolver = function ($name = null) {
|
199
|
return $this->guard($name)->user();
|
200
|
};
|
201
|
}
|
202
|
|
203
|
/**
|
204
|
* Set the default authentication driver name.
|
205
|
*
|
206
|
* @param string $name
|
207
|
* @return void
|
208
|
*/
|
209
|
public function setDefaultDriver($name)
|
210
|
{
|
211
|
$this->app['config']['auth.defaults.guard'] = $name;
|
212
|
}
|
213
|
|
214
|
/**
|
215
|
* Register a new callback based request guard.
|
216
|
*
|
217
|
* @param string $driver
|
218
|
* @param callable $callback
|
219
|
* @return $this
|
220
|
*/
|
221
|
public function viaRequest($driver, callable $callback)
|
222
|
{
|
223
|
return $this->extend($driver, function () use ($callback) {
|
224
|
$guard = new RequestGuard($callback, $this->app['request']);
|
225
|
|
226
|
$this->app->refresh('request', $guard, 'setRequest');
|
227
|
|
228
|
return $guard;
|
229
|
});
|
230
|
}
|
231
|
|
232
|
/**
|
233
|
* Get the user resolver callback.
|
234
|
*
|
235
|
* @return \Closure
|
236
|
*/
|
237
|
public function userResolver()
|
238
|
{
|
239
|
return $this->userResolver;
|
240
|
}
|
241
|
|
242
|
/**
|
243
|
* Set the callback to be used to resolve users.
|
244
|
*
|
245
|
* @param \Closure $userResolver
|
246
|
* @return $this
|
247
|
*/
|
248
|
public function resolveUsersUsing(Closure $userResolver)
|
249
|
{
|
250
|
$this->userResolver = $userResolver;
|
251
|
|
252
|
return $this;
|
253
|
}
|
254
|
|
255
|
/**
|
256
|
* Register a custom driver creator Closure.
|
257
|
*
|
258
|
* @param string $driver
|
259
|
* @param \Closure $callback
|
260
|
* @return $this
|
261
|
*/
|
262
|
public function extend($driver, Closure $callback)
|
263
|
{
|
264
|
$this->customCreators[$driver] = $callback;
|
265
|
|
266
|
return $this;
|
267
|
}
|
268
|
|
269
|
/**
|
270
|
* Register a custom provider creator Closure.
|
271
|
*
|
272
|
* @param string $name
|
273
|
* @param \Closure $callback
|
274
|
* @return $this
|
275
|
*/
|
276
|
public function provider($name, Closure $callback)
|
277
|
{
|
278
|
$this->customProviderCreators[$name] = $callback;
|
279
|
|
280
|
return $this;
|
281
|
}
|
282
|
|
283
|
/**
|
284
|
* Dynamically call the default driver instance.
|
285
|
*
|
286
|
* @param string $method
|
287
|
* @param array $parameters
|
288
|
* @return mixed
|
289
|
*/
|
290
|
public function __call($method, $parameters)
|
291
|
{
|
292
|
return call_user_func_array([$this->guard(), $method], $parameters);
|
293
|
}
|
294
|
}
|