-
Notifications
You must be signed in to change notification settings - Fork 0
/
AuthProviderMemcached.php
108 lines (88 loc) · 3.4 KB
/
AuthProviderMemcached.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
<?php
namespace celmarket;
use celmarket\AuthProvider;
use PDO;
/**
* AuthProvider class example
* It saves the tokens by default in files based on provider ID set by user
* or uid generated from username and password
*
*
*/
class AuthProviderMemcached extends AuthProvider
{
private static $token;
private static $user;
private static $password;
private static $providerID;
private static $memcached;
private static $memcached_host = 'HOST';
private static $memcached_port = 'PORT';
public function __construct($userName = '', $password = '')
{
if (empty($userName) || empty($password)) {
throw new \Exception('Username or password is missing');
}
self::$user = trim($userName);
self::$password = trim($password);
// Memcached init
self::$memcached = new \Memcached;
self::$memcached->addServer(self::$memcached_host, self::$memcached_port);
}
public static function setProviderID($providerID = '')
{
self::$providerID = $providerID;
}
public static function setToken($token = '')
{
return self::$token = $token;
}
public static function checkToken()
{
$token = self::$memcached->get('celro_' . (!empty(self::$providerID) ? self::$providerID : md5(self::$user . self::$password)));
if ($token) {
self::setToken($token);
}
return $token ? static::$token : self::regenerateToken();
}
public static function getToken()
{
if (!empty(static::$token)) {
return static::$token;
}
$token = self::$memcached->get('celro_' . (!empty(self::$providerID) ? self::$providerID : md5(self::$user . self::$password)));
if ($token) {
return $token;
}
return self::regenerateToken();
}
public static function regenerateToken()
{
### 1. Sanity check ###
if (empty(self::$user) || empty(self::$password)) {
throw new \Exception('Username or password is missing');
}
$res = Dispatcher::send('login', 'actionLogin', array('username' => self::$user, 'password' => self::$password));
try {
if ($res === '') { // Token is null
throw new \Exception('Token-ul primit este null');
} elseif (is_string($res) && strstr($res, 'Raspuns invalid:') !== false) { // Invalid answer
throw new \Exception($res);
} else {
// Delete token for this providerID
self::$memcached->delete('celro_' . (!empty(self::$providerID) ? self::$providerID : md5(self::$user . self::$password)));
// Insert new token
$insert_token = self::$memcached->set('celro_' . (!empty(self::$providerID) ? self::$providerID : md5(self::$user . self::$password)), $res);
if (!$insert_token) {
throw new \Exception('Token-ul nu a fost salvat in baza de date');
}
}
// Token static attribute gets the value of the response
self::$token = $res;
return self::$token;
} catch (\Exception $e) {
// Catch all exceptions, add a message for clarification and re-throw them up the stack
throw new \Exception('Eroare in procesul de autentificare: ' . $e->getMessage());
}
}
}