|
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.5 |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Quantum\Libraries\Auth; |
|
16
|
|
|
|
|
17
|
|
|
use Quantum\Libraries\Mailer\MailerException; |
|
18
|
|
|
use Quantum\Exceptions\ServiceException; |
|
19
|
|
|
use Quantum\Exceptions\ConfigException; |
|
20
|
|
|
use Quantum\Libraries\JWToken\JWToken; |
|
21
|
|
|
use Quantum\Exceptions\LangException; |
|
22
|
|
|
use Quantum\Libraries\Hasher\Hasher; |
|
23
|
|
|
use Quantum\Exceptions\DiException; |
|
24
|
|
|
use Quantum\Factory\ServiceFactory; |
|
25
|
|
|
use Quantum\Loader\Setup; |
|
26
|
|
|
use ReflectionException; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Class AuthManager |
|
30
|
|
|
* @package Quantum\Libraries\Auth |
|
31
|
|
|
*/ |
|
32
|
|
|
class AuthManager |
|
33
|
|
|
{ |
|
34
|
|
|
|
|
35
|
|
|
const ADAPTERS = [ |
|
36
|
|
|
'web', |
|
37
|
|
|
'api', |
|
38
|
|
|
]; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var AuthenticatableInterface |
|
42
|
|
|
*/ |
|
43
|
|
|
private static $adapter; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Get Handler |
|
47
|
|
|
* @return AuthenticatableInterface |
|
48
|
|
|
* @throws AuthException |
|
49
|
|
|
* @throws ConfigException |
|
50
|
|
|
* @throws DiException |
|
51
|
|
|
* @throws ReflectionException |
|
52
|
|
|
* @throws LangException |
|
53
|
|
|
* @throws ServiceException |
|
54
|
|
|
* @throws MailerException |
|
55
|
|
|
*/ |
|
56
|
|
|
public static function getHandler(): AuthenticatableInterface |
|
57
|
|
|
{ |
|
58
|
|
|
if (self::$adapter !== null) { |
|
59
|
|
|
return self::$adapter; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
self::loadConfigs(); |
|
63
|
|
|
|
|
64
|
|
|
$authAdapter = config()->get('auth.type'); |
|
65
|
|
|
|
|
66
|
|
|
if (!in_array($authAdapter, self::ADAPTERS)) { |
|
67
|
|
|
throw AuthException::undefinedAuthType($authAdapter); |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$authService = ServiceFactory::create(config()->get('auth.service')); |
|
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
if (!($authService instanceof AuthServiceInterface)) { |
|
73
|
|
|
throw AuthException::incorrectAuthService(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$mailer = mailer(); |
|
77
|
|
|
$hasher = new Hasher(); |
|
78
|
|
|
$jwt = null; |
|
79
|
|
|
|
|
80
|
|
|
if ($authAdapter == 'api') { |
|
81
|
|
|
$jwt = (new JWToken()) |
|
82
|
|
|
->setLeeway(1) |
|
83
|
|
|
->setClaims((array)config()->get('auth.claims')); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$authAdapterClassName = __NAMESPACE__ . '\\Adapters\\' . ucfirst($authAdapter) . 'Adapter'; |
|
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
return self::$adapter = $authAdapterClassName::getInstance($authService, $mailer, $hasher, $jwt); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @throws AuthException |
|
93
|
|
|
* @throws ConfigException |
|
94
|
|
|
* @throws DiException |
|
95
|
|
|
* @throws LangException |
|
96
|
|
|
* @throws ReflectionException |
|
97
|
|
|
*/ |
|
98
|
|
|
private static function loadConfigs() |
|
99
|
|
|
{ |
|
100
|
|
|
if (!config()->has('auth')) { |
|
101
|
|
|
config()->import(new Setup('Config', 'auth')); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
if (!config()->has('auth.type')) { |
|
105
|
|
|
throw AuthException::misconfiguredAuthConfig(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
if (!config()->has('mailer') || !config()->has('mailer.current')) { |
|
109
|
|
|
config()->import(new Setup('config', 'mailer')); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|