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.8.0 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Quantum\Libraries\Auth; |
16
|
|
|
|
17
|
|
|
use Quantum\Libraries\JWToken\JWToken; |
18
|
|
|
use Quantum\Exceptions\AuthException; |
19
|
|
|
use Quantum\Libraries\Mailer\Mailer; |
20
|
|
|
use Quantum\Libraries\Hasher\Hasher; |
21
|
|
|
use Quantum\Factory\ServiceFactory; |
22
|
|
|
use Quantum\Mvc\QtService; |
23
|
|
|
use Quantum\Loader\Setup; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class AuthManager |
27
|
|
|
* @package Quantum\Libraries\Auth |
28
|
|
|
*/ |
29
|
|
|
class AuthManager |
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Get Handler |
34
|
|
|
* @return \Quantum\Libraries\Auth\ApiAuth|\Quantum\Libraries\Auth\WebAuth |
35
|
|
|
* @throws \Quantum\Exceptions\AuthException |
36
|
|
|
* @throws \Quantum\Exceptions\ConfigException |
37
|
|
|
* @throws \Quantum\Exceptions\DiException |
38
|
|
|
* @throws \ReflectionException |
39
|
|
|
*/ |
40
|
|
|
public static function getHandler() |
41
|
|
|
{ |
42
|
|
|
self::loadConfigs(); |
43
|
|
|
|
44
|
|
|
switch (config()->has('auth.type')) { |
45
|
|
|
case 'web': |
46
|
|
|
return WebAuth::getInstance(self::getAuthService(), new Mailer, new Hasher); |
|
|
|
|
47
|
|
|
case 'api': |
48
|
|
|
$jwt = (new JWToken())->setLeeway(1)->setClaims((array) config()->get('auth.claims')); |
49
|
|
|
return ApiAuth::getInstance(self::getAuthService(), new Mailer, new Hasher, $jwt); |
|
|
|
|
50
|
|
|
default : |
51
|
|
|
AuthException::undefinedAuthType(); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Gets the auth service |
57
|
|
|
* @return \Quantum\Mvc\QtService |
58
|
|
|
* @throws \Quantum\Exceptions\ConfigException |
59
|
|
|
*/ |
60
|
|
|
public static function getAuthService(): QtService |
61
|
|
|
{ |
62
|
|
|
return ServiceFactory::create(config()->get('auth.service')); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private static function loadConfigs() |
66
|
|
|
{ |
67
|
|
|
if (!config()->has('auth')) { |
68
|
|
|
config()->import(new Setup('Config', 'auth')); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if (!config()->has('auth.type') && !config()->has('auth.type')) { |
72
|
|
|
throw AuthException::misconfiguredAuthConfig(); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
} |
77
|
|
|
|