|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Quantum PHP Framework |
|
5
|
|
|
* |
|
6
|
|
|
* An open source software development framework for PHP |
|
7
|
|
|
* |
|
8
|
|
|
* @package Quantum |
|
9
|
|
|
* @author Arman Ag. <[email protected]> |
|
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
|
11
|
|
|
* @link http://quantum.softberg.org/ |
|
12
|
|
|
* @since 2.9.6 |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Quantum\Libraries\Auth\Factories; |
|
16
|
|
|
|
|
17
|
|
|
use Quantum\Libraries\Auth\Contracts\AuthServiceInterface; |
|
18
|
|
|
use Quantum\Libraries\Config\Exceptions\ConfigException; |
|
19
|
|
|
use Quantum\Libraries\Auth\Adapters\SessionAuthAdapter; |
|
20
|
|
|
use Quantum\Libraries\Auth\Adapters\JwtAuthAdapter; |
|
21
|
|
|
use Quantum\Libraries\Auth\Exceptions\AuthException; |
|
22
|
|
|
use Quantum\Exceptions\ServiceException; |
|
23
|
|
|
use Quantum\Di\Exceptions\DiException; |
|
24
|
|
|
use Quantum\Exceptions\BaseException; |
|
25
|
|
|
use Quantum\Libraries\Hasher\Hasher; |
|
26
|
|
|
use Quantum\Libraries\Jwt\JwtToken; |
|
27
|
|
|
use Quantum\Factory\ServiceFactory; |
|
28
|
|
|
use Quantum\Libraries\Auth\Auth; |
|
29
|
|
|
use Quantum\Loader\Setup; |
|
30
|
|
|
use ReflectionException; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Class AuthFactory |
|
34
|
|
|
* @package Quantum\Libraries\Auth |
|
35
|
|
|
*/ |
|
36
|
|
|
class AuthFactory |
|
37
|
|
|
{ |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Supported adapters |
|
41
|
|
|
*/ |
|
42
|
|
|
const ADAPTERS = [ |
|
43
|
|
|
Auth::SESSION => SessionAuthAdapter::class, |
|
44
|
|
|
Auth::JWT => JwtAuthAdapter::class, |
|
45
|
|
|
]; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var Auth|null |
|
49
|
|
|
*/ |
|
50
|
|
|
private static $instances = []; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param string|null $adapter |
|
54
|
|
|
* @return Auth |
|
55
|
|
|
* @throws AuthException |
|
56
|
|
|
* @throws BaseException |
|
57
|
|
|
* @throws ConfigException |
|
58
|
|
|
* @throws DiException |
|
59
|
|
|
* @throws ReflectionException |
|
60
|
|
|
* @throws ServiceException |
|
61
|
|
|
*/ |
|
62
|
|
|
public static function get(?string $adapter = null): Auth |
|
63
|
|
|
{ |
|
64
|
|
|
if (!config()->has('auth')) { |
|
65
|
|
|
config()->import(new Setup('Config', 'auth')); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$adapter = $adapter ?? config()->get('auth.default'); |
|
69
|
|
|
|
|
70
|
|
|
$adapterClass = self::getAdapterClass($adapter); |
|
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
if (!isset(self::$instances[$adapter])) { |
|
73
|
|
|
self::$instances[$adapter] = self::createInstance($adapterClass, $adapter); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return self::$instances[$adapter]; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param string $adapterClass |
|
81
|
|
|
* @param string $adapter |
|
82
|
|
|
* @return Auth |
|
83
|
|
|
* @throws AuthException |
|
84
|
|
|
* @throws BaseException |
|
85
|
|
|
* @throws ConfigException |
|
86
|
|
|
* @throws DiException |
|
87
|
|
|
* @throws ReflectionException |
|
88
|
|
|
* @throws ServiceException |
|
89
|
|
|
*/ |
|
90
|
|
|
private static function createInstance(string $adapterClass, string $adapter): Auth |
|
91
|
|
|
{ |
|
92
|
|
|
return new Auth(new $adapterClass( |
|
93
|
|
|
self::createAuthService($adapter), |
|
94
|
|
|
mailer(), |
|
95
|
|
|
new Hasher(), |
|
96
|
|
|
self::createJwtInstance($adapter) |
|
97
|
|
|
)); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param string $adapter |
|
102
|
|
|
* @return string |
|
103
|
|
|
* @throws BaseException |
|
104
|
|
|
*/ |
|
105
|
|
|
private static function getAdapterClass(string $adapter): string |
|
106
|
|
|
{ |
|
107
|
|
|
if (!array_key_exists($adapter, self::ADAPTERS)) { |
|
108
|
|
|
throw AuthException::adapterNotSupported($adapter); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return self::ADAPTERS[$adapter]; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param string $adapter |
|
116
|
|
|
* @return AuthServiceInterface |
|
117
|
|
|
* @throws AuthException |
|
118
|
|
|
* @throws DiException |
|
119
|
|
|
* @throws ReflectionException |
|
120
|
|
|
* @throws ServiceException |
|
121
|
|
|
*/ |
|
122
|
|
|
private static function createAuthService(string $adapter): AuthServiceInterface |
|
123
|
|
|
{ |
|
124
|
|
|
$authService = ServiceFactory::create(config()->get('auth.' . $adapter . '.service')); |
|
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
if (!$authService instanceof AuthServiceInterface) { |
|
127
|
|
|
throw AuthException::incorrectAuthService(); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return $authService; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param string $adapter |
|
135
|
|
|
* @return JwtToken|null |
|
136
|
|
|
*/ |
|
137
|
|
|
private static function createJwtInstance(string $adapter): ?JwtToken |
|
138
|
|
|
{ |
|
139
|
|
|
return $adapter === Auth::JWT ? (new JwtToken())->setLeeway(1)->setClaims((array)config()->get('auth.claims')) : null; |
|
140
|
|
|
} |
|
141
|
|
|
} |