Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
ApiTokens.php
Go to the documentation of this file.
1<?php
2
4
5use Ubiquity\cache\system\ArrayCache;
6
16class ApiTokens {
17 protected $tokens;
18 protected $length;
19 protected $duration;
20 protected static $cache;
21
22 public function __construct($length = 10, $duration = 3600, $tokens = [ ]) {
23 $this->length = $length ?? 10;
24 $this->duration = $duration ?? 3600;
25 $this->tokens = $tokens;
26 }
27
28 protected function generateToken() {
29 do {
30 $token = $this->tokenGenerator ();
31 } while ( \array_search ( $token, $this->tokens, true ) === true );
32 return $token;
33 }
34
35 protected function tokenGenerator() {
36 return \bin2hex ( \random_bytes ( $this->length ) );
37 }
38
39 public function getTokens() {
40 return $this->tokens;
41 }
42
43 public function getDuration() {
44 return $this->duration;
45 }
46
47 public function getToken($key) {
48 return $this->tokens [$key]??false;
49 }
50
51 public function isExpired($key) {
52 $token = $this->getToken ( $key );
53 if ($token !== false) {
54 return \time() - $token ['creationTime'] > $this->duration;
55 }
56 return true;
57 }
58
59 public function addToken($datas=null) {
60 $key = $this->generateToken ();
61 $token=[ 'creationTime' => \time () ];
62 if(isset($datas)){
63 $token['datas']=$datas;
64 }
65 $this->tokens [$key] = $token;
66 return $key;
67 }
68
74 public function refreshToken(string $key) {
75 if($token=$this->getToken($key)){
76 $token=$this->addToken($token['datas']??null);
77 $this->remove($key);
78 return $token;
79 }
80 return false;
81 }
82
83 public function clearAll() {
84 $this->tokens = [ ];
85 }
86
87 public function removeExpireds() {
89 foreach ( $tokens as $key => $value ) {
90 if ($this->isExpired ( $key )) {
91 unset ( $this->tokens [$key] );
92 }
93 }
94 }
95
96 public function remove($key) {
97 if (isset ( $this->tokens [$key] )) {
98 unset ( $this->tokens [$key] );
99 return true;
100 }
101 return false;
102 }
103
104 public function storeToCache($key = '_apiTokens') {
105 $fileContent = [ 'duration' => $this->duration,'length' => $this->length,'tokens' => $this->tokens ];
106 self::$cache->store ( $key, $fileContent );
107 }
108
115 public function getFromCache($folder, $key = '_apiTokens') {
116 if (! isset ( self::$cache )) {
117 self::$cache = new ArrayCache ( $folder . 'rest/tokens', '.rest' );
118 }
119 if (self::$cache->exists ( $key )) {
120 $filecontent = self::$cache->fetch ( $key );
121 if (isset ( $filecontent ['tokens'] )) {
122 $this->tokens = $filecontent ['tokens'];
123 }
124 if (isset ( $filecontent ['length'] )) {
125 $this->length = $filecontent ['length'];
126 }
127 if (isset ( $filecontent ['duration'] )) {
128 $this->duration = $filecontent ["duration"];
129 }
130 }
131 return $this;
132 }
133}
Manage the token api for the Rest part.
Definition ApiTokens.php:16
getFromCache($folder, $key='_apiTokens')
__construct($length=10, $duration=3600, $tokens=[])
Definition ApiTokens.php:22
refreshToken(string $key)
Refresh the token using an existing one.
Definition ApiTokens.php:74