|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of web-stack |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Slick\WebStack\Domain\Security\Http; |
|
13
|
|
|
|
|
14
|
|
|
use Slick\WebStack\Domain\Security\Authentication\Token\TokenStorageInterface; |
|
15
|
|
|
use Slick\WebStack\Domain\Security\PasswordHasher\Hasher\PhpPasswordHasher; |
|
16
|
|
|
use Slick\WebStack\Domain\Security\PasswordHasher\PasswordHasherInterface; |
|
17
|
|
|
use Slick\WebStack\Domain\Security\User\UserProviderInterface; |
|
18
|
|
|
use Slick\WebStack\Domain\Security\UserInterface; |
|
19
|
|
|
use Psr\Container\ContainerExceptionInterface; |
|
20
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
|
21
|
|
|
use Slick\Http\Session\SessionDriverInterface; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* ProfileFactoryTrait |
|
25
|
|
|
* |
|
26
|
|
|
* @package Slick\WebStack\Domain\Security\Http |
|
27
|
|
|
* @template-covariant TUser of UserInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
trait ProfileFactoryTrait |
|
30
|
|
|
{ |
|
31
|
|
|
|
|
32
|
|
|
/** @var array<string, mixed> */ |
|
33
|
|
|
protected static array $defaultOptions = [ |
|
34
|
|
|
"secured" => true, |
|
35
|
|
|
"passwordHasher" => PhpPasswordHasher::class, |
|
36
|
|
|
"authenticators" => [], |
|
37
|
|
|
"stateless" => true, |
|
38
|
|
|
"tokenStorage" => TokenStorageInterface::class, |
|
39
|
|
|
"sessionDriver" => SessionDriverInterface::class, |
|
40
|
|
|
"entryPoint" => null, |
|
41
|
|
|
"accessControl" => [] |
|
42
|
|
|
]; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Create a session driver based on a given profile modules. |
|
46
|
|
|
* |
|
47
|
|
|
* @param array<string, mixed> $profile The profile configuration. |
|
48
|
|
|
* @return SessionDriverInterface The created session driver. |
|
49
|
|
|
* |
|
50
|
|
|
* @throws ContainerExceptionInterface |
|
51
|
|
|
* @throws NotFoundExceptionInterface |
|
52
|
|
|
*/ |
|
53
|
|
|
private function createSessionDriver(array $profile): SessionDriverInterface |
|
54
|
|
|
{ |
|
55
|
|
|
$config = array_merge(self::$defaultOptions, $profile); |
|
56
|
|
|
return $this->container->get($config['sessionDriver']); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Create a token storage based on a given profile. |
|
61
|
|
|
* |
|
62
|
|
|
* @param array<string, mixed> $profile The profile configuration. |
|
63
|
|
|
* @return TokenStorageInterface<UserInterface> The created token storage. |
|
64
|
|
|
* @throws ContainerExceptionInterface |
|
65
|
|
|
* @throws NotFoundExceptionInterface |
|
66
|
|
|
*/ |
|
67
|
|
|
private function createTokenStorage(array $profile): TokenStorageInterface |
|
68
|
|
|
{ |
|
69
|
|
|
$config = array_merge(self::$defaultOptions, $profile); |
|
70
|
|
|
return $this->container->get($config['tokenStorage']); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Creates a password hasher. |
|
75
|
|
|
* |
|
76
|
|
|
* @param array<string, mixed> $profile The profile array containing the configuration options. |
|
77
|
|
|
* @return PasswordHasherInterface The created password hasher. |
|
78
|
|
|
* @throws ContainerExceptionInterface |
|
79
|
|
|
* @throws NotFoundExceptionInterface |
|
80
|
|
|
*/ |
|
81
|
|
|
private function createPasswordHasher(array $profile): PasswordHasherInterface |
|
82
|
|
|
{ |
|
83
|
|
|
$config = array_merge(self::$defaultOptions, $profile); |
|
84
|
|
|
return $this->container->get($config['passwordHasher']); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Creates a user provider instance based on a given provider class. |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $providerClass The class name of the user provider |
|
91
|
|
|
* |
|
92
|
|
|
* @return UserProviderInterface<UserInterface> The user provider instance |
|
93
|
|
|
* |
|
94
|
|
|
* @throws ContainerExceptionInterface |
|
95
|
|
|
* @throws NotFoundExceptionInterface |
|
96
|
|
|
*/ |
|
97
|
|
|
private function createUserProvider(string $providerClass): UserProviderInterface |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->container->get($providerClass); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|