|
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\Factories; |
|
16
|
|
|
|
|
17
|
|
|
use Quantum\Libraries\Auth\Contracts\AuthServiceInterface; |
|
18
|
|
|
use Quantum\Libraries\Config\Exceptions\ConfigException; |
|
19
|
|
|
use Quantum\Libraries\Mailer\Exceptions\MailerException; |
|
20
|
|
|
use Quantum\Libraries\Auth\Exceptions\AuthException; |
|
21
|
|
|
use Quantum\Libraries\Lang\Exceptions\LangException; |
|
22
|
|
|
use Quantum\Libraries\Auth\Adapters\WebAdapter; |
|
23
|
|
|
use Quantum\Libraries\Auth\Adapters\ApiAdapter; |
|
24
|
|
|
use Quantum\Exceptions\ServiceException; |
|
25
|
|
|
use Quantum\Di\Exceptions\DiException; |
|
26
|
|
|
use Quantum\Exceptions\BaseException; |
|
27
|
|
|
use Quantum\Libraries\Hasher\Hasher; |
|
28
|
|
|
use Quantum\Libraries\Jwt\JwtToken; |
|
29
|
|
|
use Quantum\Factory\ServiceFactory; |
|
30
|
|
|
use Quantum\Libraries\Auth\Auth; |
|
31
|
|
|
use Quantum\Loader\Setup; |
|
32
|
|
|
use ReflectionException; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Class AuthFactory |
|
36
|
|
|
* @package Quantum\Libraries\Auth |
|
37
|
|
|
*/ |
|
38
|
|
|
class AuthFactory |
|
39
|
|
|
{ |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Supported adapters |
|
43
|
|
|
*/ |
|
44
|
|
|
const ADAPTERS = [ |
|
45
|
|
|
Auth::WEB => WebAdapter::class, |
|
46
|
|
|
Auth::API => ApiAdapter::class, |
|
47
|
|
|
]; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var Auth|null |
|
51
|
|
|
*/ |
|
52
|
|
|
private static $instance = null; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return Auth |
|
56
|
|
|
* @throws BaseException |
|
57
|
|
|
* @throws AuthException |
|
58
|
|
|
* @throws ConfigException |
|
59
|
|
|
* @throws DiException |
|
60
|
|
|
* @throws LangException |
|
61
|
|
|
* @throws MailerException |
|
62
|
|
|
* @throws ReflectionException |
|
63
|
|
|
* @throws ServiceException |
|
64
|
|
|
*/ |
|
65
|
|
|
public static function get(): Auth |
|
66
|
|
|
{ |
|
67
|
|
|
if (self::$instance === null) { |
|
68
|
|
|
return self::$instance = self::createInstance(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return self::$instance; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return Auth |
|
76
|
|
|
* @throws BaseException |
|
77
|
|
|
* @throws AuthException |
|
78
|
|
|
* @throws ConfigException |
|
79
|
|
|
* @throws DiException |
|
80
|
|
|
* @throws LangException |
|
81
|
|
|
* @throws MailerException |
|
82
|
|
|
* @throws ReflectionException |
|
83
|
|
|
* @throws ServiceException |
|
84
|
|
|
*/ |
|
85
|
|
|
private static function createInstance(): Auth |
|
86
|
|
|
{ |
|
87
|
|
|
if (!config()->has('auth')) { |
|
88
|
|
|
config()->import(new Setup('Config', 'auth')); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$adapter = config()->get('auth.type'); |
|
92
|
|
|
$adapterClass = self::getAdapterClass($adapter); |
|
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
$authService = self::createAuthService(); |
|
95
|
|
|
$mailer = mailer(); |
|
96
|
|
|
$hasher = new Hasher(); |
|
97
|
|
|
$jwt = null; |
|
98
|
|
|
|
|
99
|
|
|
if ($adapter == Auth::API) { |
|
100
|
|
|
$jwt = (new JwtToken()) |
|
101
|
|
|
->setLeeway(1) |
|
102
|
|
|
->setClaims((array)config()->get('auth.claims')); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return new Auth(new $adapterClass($authService, $mailer, $hasher, $jwt)); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param string $adapter |
|
110
|
|
|
* @return string |
|
111
|
|
|
* @throws BaseException |
|
112
|
|
|
*/ |
|
113
|
|
|
private static function getAdapterClass(string $adapter): string |
|
114
|
|
|
{ |
|
115
|
|
|
if (!array_key_exists($adapter, self::ADAPTERS)) { |
|
116
|
|
|
throw AuthException::adapterNotSupported($adapter); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return self::ADAPTERS[$adapter]; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @return AuthServiceInterface |
|
124
|
|
|
* @throws AuthException |
|
125
|
|
|
* @throws DiException |
|
126
|
|
|
* @throws ReflectionException |
|
127
|
|
|
* @throws ServiceException |
|
128
|
|
|
*/ |
|
129
|
|
|
private static function createAuthService(): AuthServiceInterface |
|
130
|
|
|
{ |
|
131
|
|
|
$authService = ServiceFactory::create(config()->get('auth.service')); |
|
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
if (!$authService instanceof AuthServiceInterface) { |
|
134
|
|
|
throw AuthException::incorrectAuthService(); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
return $authService; |
|
138
|
|
|
} |
|
139
|
|
|
} |