-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathAuthentication.php
More file actions
79 lines (63 loc) · 2.08 KB
/
Copy pathAuthentication.php
File metadata and controls
79 lines (63 loc) · 2.08 KB
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
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter Shield.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Shield\Authentication;
use CodeIgniter\Shield\Config\Auth as AuthConfig;
use CodeIgniter\Shield\Models\UserModel;
/**
* Factory for Authenticators.
*/
class Authentication
{
/**
* Instantiated Authenticator objects,
* stored by Authenticator alias.
*
* @var array<string, AuthenticatorInterface> [Authenticator_alias => Authenticator_instance]
*/
protected array $instances = [];
protected ?UserModel $userProvider = null;
public function __construct(protected AuthConfig $config)
{
}
/**
* Creates and returns the shared instance of the specified Authenticator.
*
* @param string|null $alias Authenticator alias. Passing `null` returns the
* default authenticator.
*
* @throws AuthenticationException
*/
public function factory(?string $alias = null): AuthenticatorInterface
{
// Determine actual Authenticator alias
$alias ??= $this->config->defaultAuthenticator;
// Return the cached instance if we have it
if (! empty($this->instances[$alias])) {
return $this->instances[$alias];
}
// Otherwise, try to create a new instance.
if (! array_key_exists($alias, $this->config->authenticators)) {
throw AuthenticationException::forUnknownAuthenticator($alias);
}
$className = $this->config->authenticators[$alias];
assert($this->userProvider !== null, 'You must set $this->userProvider.');
$this->instances[$alias] = new $className($this->userProvider);
return $this->instances[$alias];
}
/**
* Sets the User Provider to use.
*/
public function setProvider(UserModel $provider): self
{
$this->userProvider = $provider;
return $this;
}
}