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