-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuthFactory.php
160 lines (142 loc) · 4.05 KB
/
AuthFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
/**
* Arikaim
*
* @link http://www.arikaim.com
* @copyright Copyright (c) Konstantin Atanasov <[email protected]>
* @license http://www.arikaim.com/license
* @package Access
*/
namespace Arikaim\Core\Access;
use Arikaim\Core\Access\Middleware\AuthMiddleware;
/**
* Auth factory class.
*/
class AuthFactory
{
// auth type id
const AUTH_BASIC = 'basic';
const AUTH_SESSION = 'session';
const AUTH_JWT = 'jwt';
const AUTH_TOKEN = 'token';
const CSRF_TOKEN = 'csrf';
const OAUTH_TOKEN = 'oauth';
const AUTH_PUBLIC = 'public';
/**
* Providers object pool
*
* @var array
*/
private static $providers = [];
/**
* Auth name
*
* @var array
*/
private static $authNames = [
Self::AUTH_BASIC,
Self::AUTH_SESSION,
Self::AUTH_JWT,
Self::AUTH_TOKEN,
Self::CSRF_TOKEN,
Self::OAUTH_TOKEN,
Self::AUTH_PUBLIC
];
/**
* Auth provider classes
*
* @var array
*/
private static $providerClasses = [
Self::AUTH_BASIC => 'Arikaim\\Core\\Access\\Provider\\BasicAuthProvider',
Self::AUTH_SESSION => 'Arikaim\\Core\\Access\\Provider\\SessionAuthProvider',
Self::AUTH_JWT => 'Arikaim\\Core\\Access\\Provider\\JwtAuthProvider',
Self::AUTH_TOKEN => 'Arikaim\\Core\\Access\\Provider\\TokenAuthProvider',
Self::OAUTH_TOKEN => 'Arikaim\\Core\\Access\\Provider\\OauthProvider',
Self::AUTH_PUBLIC => 'Arikaim\\Core\\Access\\Provider\\PublicAuthProvider'
];
/**
* Create auth provider
*
* @param string $name
* @param array $params
* @return object|null
*/
public static function createProvider(string $name, array $params = []): ?object
{
if (isset(Self::$providers[$name]) == true) {
return Self::$providers[$name];
}
$class = Self::$providerClasses[$name] ?? $name;
if (empty($class) == true) {
return null;
}
Self::$providers[$name] = new $class($params);
return Self::$providers[$name];
}
/**
* Create auth middleware
*
* @param string $authName
* @param object|null $container
* @param array $options
* @return object|null
*/
public static function createMiddleware(string $authName, ?object $container = null, array $options = [])
{
$options['authProviders'] = Self::createAuthProviders($authName,$options);
return (\count($options['authProviders']) == 0) ? null : new AuthMiddleware($container,$options);
}
/**
* Create auth providers
*
* @param string|array $authName
* @param array $params
* @return array
*/
public static function createAuthProviders($authName, array $params = []): array
{
$providers = (\is_array($authName) == false) ? \explode(',',$authName) : $authName;
$result = [];
foreach ($providers as $item) {
$result[$item] = Self::createProvider($item,$params);
}
return $result;
}
/**
* Check if auth name is valid
*
* @param string $name
* @return boolean
*/
public static function isValidAuthName(string $name): bool
{
return (\array_search($name,Self::$authNames) !== false);
}
/**
* Resolve auth type
*
* @param string|null|array $type
* @return null|string
*/
public static function resolveAuthType($type): ?string
{
if (\is_array($type) == true) {
return \implode(',',$type);
}
if (empty($type) == true) {
return null;
}
return \trim((string)$type ?? '');
}
/**
* Get auth provider class
*
* @param string $name
* @return string
*/
public static function getAuthProviderClass(string $name): string
{
return Self::$providerClasses[$name] ?? '';
}
}