|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\controllers\rest; |
|
4
|
|
|
|
|
5
|
|
|
use Ubiquity\cache\system\ArrayCache; |
|
6
|
|
|
use Ubiquity\utils\base\UArray; |
|
7
|
|
|
|
|
8
|
|
|
class ApiTokens { |
|
9
|
|
|
private $tokens; |
|
10
|
|
|
private $length; |
|
11
|
|
|
private $duration; |
|
12
|
|
|
private static $cache; |
|
13
|
|
|
|
|
14
|
2 |
|
public function __construct($length=10,$duration=3600,$tokens=[]){ |
|
15
|
2 |
|
$this->length=$length; |
|
16
|
2 |
|
$this->duration=$duration; |
|
17
|
2 |
|
$this->tokens=$tokens; |
|
18
|
2 |
|
} |
|
19
|
|
|
|
|
20
|
1 |
|
protected function generateToken(){ |
|
21
|
|
|
do{ |
|
22
|
1 |
|
$token= \bin2hex(\random_bytes($this->length)); |
|
23
|
1 |
|
}while (\array_search($token, $this->tokens,true)===true); |
|
24
|
1 |
|
return $token; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function getTokens() { |
|
28
|
|
|
return $this->tokens; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
1 |
|
public function getDuration() { |
|
32
|
1 |
|
return $this->duration; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
1 |
|
public function getToken($key){ |
|
36
|
1 |
|
if(isset($this->tokens[$key])) |
|
37
|
1 |
|
return $this->tokens[$key]; |
|
38
|
|
|
return false; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
1 |
|
public function isExpired($key){ |
|
42
|
1 |
|
$token=$this->getToken($key); |
|
43
|
1 |
|
if($token!==false) |
|
44
|
1 |
|
return \time() - $token["creationTime"] > $this->duration; |
|
45
|
|
|
return true; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
public function addToken(){ |
|
49
|
1 |
|
$key=$this->generateToken(); |
|
50
|
1 |
|
$this->tokens[$key]=["creationTime"=>\time()]; |
|
51
|
1 |
|
return $key; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function clearAll(){ |
|
55
|
|
|
$this->tokens=[]; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
1 |
|
public function removeExpireds(){ |
|
59
|
1 |
|
$tokens=$this->tokens; |
|
60
|
1 |
|
foreach ($tokens as $key=>$value){ |
|
61
|
1 |
|
if($this->isExpired($key)){ |
|
62
|
|
|
unset($this->tokens[$key]); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
1 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function remove($key){ |
|
68
|
|
|
if(isset($this->tokens[$key])){ |
|
69
|
|
|
unset($this->tokens[$key]); |
|
70
|
|
|
return true; |
|
71
|
|
|
} |
|
72
|
|
|
return false; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
1 |
|
public function storeToCache($key="_apiTokens"){ |
|
76
|
1 |
|
$fileContent=["duration"=>$this->duration,"length"=>$this->length,"tokens"=>$this->tokens]; |
|
77
|
1 |
|
self::$cache->store($key, "return " . UArray::asPhpArray($fileContent,"array").";"); |
|
78
|
1 |
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param $folder |
|
82
|
|
|
* @param string $key |
|
83
|
|
|
* @param number $length |
|
84
|
|
|
* @param number $duration |
|
85
|
|
|
* @return ApiTokens |
|
86
|
|
|
*/ |
|
87
|
2 |
|
public static function getFromCache($folder,$key="_apiTokens",$length=10,$duration=3600){ |
|
88
|
2 |
|
if(!isset(self::$cache)){ |
|
89
|
2 |
|
self::$cache=new ArrayCache($folder."rest/tokens",".rest"); |
|
90
|
|
|
} |
|
91
|
2 |
|
$tokens=[]; |
|
92
|
2 |
|
if(self::$cache->exists($key)){ |
|
93
|
1 |
|
$filecontent=self::$cache->fetch($key); |
|
94
|
1 |
|
if(isset($filecontent["tokens"])){ |
|
95
|
1 |
|
$tokens=$filecontent["tokens"]; |
|
96
|
|
|
} |
|
97
|
1 |
|
if(isset($filecontent["length"])){ |
|
98
|
1 |
|
$length=$filecontent["length"]; |
|
99
|
|
|
} |
|
100
|
1 |
|
if(isset($filecontent["duration"])){ |
|
101
|
1 |
|
$duration=$filecontent["duration"]; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
2 |
|
$apiTokens=new ApiTokens($length,$duration,$tokens); |
|
105
|
2 |
|
return $apiTokens; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|