-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOAuthFactory.php
148 lines (129 loc) · 3.29 KB
/
OAuthFactory.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
<?php
/**
* @author Drajat Hasan
* @email drajathasan20@gmail.com
* @create date 2022-05-18 14:42:27
* @modify date 2022-05-21 11:01:02
* @license GPLv3
* @desc [description]
*/
namespace SLiMSOAuth;
use Exception,Closure,ReflectionClass;
use SLiMSOAuth\Provider\Contract;
final class OAuthFactory
{
/**
* OAuthFactory error property
*
* @var string
*/
private static string $error = '';
/**
* Provider name
*
* @var string
*/
private static string $currentProvider = '';
/**
* Provider instance
*
* @var null|object
*/
private static $providerInstance = null;
/**
* Create provider instance
*
* @param string $provider
* @return void
*/
public static function use(string $provider)
{
if (!class_exists($provider)) throw new Exception("Provider {$provider} not found!");
if (!is_null(self::$providerInstance)) return;
try {
$providerInstance = new $provider;
if ($providerInstance instanceof Contract)
{
self::$currentProvider = (new ReflectionClass($providerInstance))->getShortName();
self::$providerInstance = $providerInstance;
}
else
{
throw new Exception("Provider {$provider} is not extends SLiMSOAuth\Provider\Contract");
}
} catch (Exception $e) {
self::$error = $e->getMessage();
}
}
/**
* Authentication process
* with callback handle by user;
*
* @param Closure $next
* @return void
*/
public static function auth(Closure $next)
{
$OAuthFactory = new Static;
try {
// Provider authentication and Create user
self::$providerInstance->auth()->createIfNotExists();
// Next process
$next($OAuthFactory);
} catch (Exception $e) {
self::$error = $e->getMessage();
}
}
/**
* Logout and revoke
* last user login
*
* @return void
*/
public static function revoke()
{
self::$providerInstance->logout($_SESSION['accessToken']);
}
/**
* Getter for OAuth provider URL
*
* @return void
*/
public static function getAuthUrl()
{
try {
$url = self::$providerInstance->getAuthUrl();
return self::$providerInstance->createButtonAuth($url);
} catch (Exception $e) {
self::$error = self::getProvider() . ' OAuth config error : <strong>' . $e->getMessage() . '</strong>';
}
}
/**
* Get current provider name
*
* @return void
*/
public static function getProvider()
{
return self::$currentProvider;
}
/**
* Get some error from Factory
* or Provider error
*
* @return void
*/
public static function getError()
{
$error = '';
if (!empty(self::$error))
{
$error .= self::$error;
}
if (!is_null(self::$providerInstance) && !empty(self::$providerInstance->getError()))
{
$error .= self::$providerInstance->getError();
}
return $error;
}
}