Skip to content

Commit 8d7dd40

Browse files
committed
Update
- Fixed cache validation - Fixed token validation
1 parent abdbc59 commit 8d7dd40

File tree

8 files changed

+66
-35
lines changed

8 files changed

+66
-35
lines changed

src/Connection/DataTable.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,6 @@ protected function getData() : array
371371

372372
if ( $this->useCache ) {
373373

374-
Cache::$debug = false;
375374
$cache = new Cache();
376375
$key = $cache->getKey($this->table, [
377376
'start' => $this->getStart(),
@@ -386,10 +385,7 @@ protected function getData() : array
386385
$data = $cache->get($key, $status);
387386
if ( !$status ) {
388387
$data = $this->prepare();
389-
// Cache on success
390-
if ( $data ) {
391-
$cache->set($key, $data, 3600, $this->table);
392-
}
388+
$cache->validate()->set($key, $data, 0, $this->table);
393389
}
394390
return $data;
395391
}

src/Connection/Transient.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ public function __construct(string $row = self::ROW, string $driver = self::DRIV
5656

5757
// Init cache
5858
if ( $this->useCache ) {
59-
Cache::$debug = false;
6059
$this->cache = new Cache($driver);
6160
}
6261
}
@@ -76,7 +75,7 @@ public function noCache() : self
7675
/**
7776
* Get temp value from database through cache,
7877
* Get cached value if persistent.
79-
*
78+
*
8079
* @access public
8180
* @param string $key
8281
* @param mixed $default
@@ -94,7 +93,7 @@ public function get(string $key, $default = null)
9493
/**
9594
* Set temp value in database,
9695
* Add cache layer if persistent.
97-
*
96+
*
9897
* @access public
9998
* @param string $key
10099
* @param mixed $value
@@ -114,7 +113,7 @@ public function set(string $key, $value = true, int $ttl = self::TTL) : bool
114113

115114
/**
116115
* Delete temp value from database and cache.
117-
*
116+
*
118117
* @access public
119118
* @param string $key
120119
* @return bool
@@ -127,7 +126,7 @@ public function delete(string $key) : bool
127126

128127
/**
129128
* Get temp value from cache.
130-
*
129+
*
131130
* @access public
132131
* @param string $key
133132
* @return mixed
@@ -142,7 +141,7 @@ public function getTemp(string $key)
142141

143142
/**
144143
* Set temp value in cache.
145-
*
144+
*
146145
* @access public
147146
* @param string $key
148147
* @param mixed $value
@@ -159,7 +158,7 @@ public function setTemp(string $key, $value = true, int $ttl = self::TTL) : bool
159158

160159
/**
161160
* Delete temp value from cache.
162-
*
161+
*
163162
* @access public
164163
* @param string $key
165164
* @return bool
@@ -174,7 +173,7 @@ public function deleteTemp(string $key) : bool
174173

175174
/**
176175
* Get temp value from database.
177-
*
176+
*
178177
* @access public
179178
* @param string $key
180179
* @param mixed $default
@@ -220,7 +219,7 @@ public function getBaseTemp(string $key, $default = null, bool $persistent = fal
220219

221220
/**
222221
* Set temp value in database.
223-
*
222+
*
224223
* @access public
225224
* @param string $key
226225
* @param mixed $value
@@ -250,7 +249,7 @@ public function setBaseTemp(string $key, $value = true, int $ttl = self::TTL) :
250249

251250
/**
252251
* Delete temp value from database.
253-
*
252+
*
254253
* @access public
255254
* @param string $key
256255
* @param mixed $value
@@ -279,7 +278,7 @@ public function deleteBaseTemp(string $key) : bool
279278

280279
/**
281280
* Reset database temp row.
282-
*
281+
*
283282
* @access public
284283
* @return bool
285284
*/

src/Connection/User.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function getUser(string $user) : array
4141
$sql .= "WHERE `username` = :username OR `email` = :email;";
4242
return $this->getRow($sql);
4343
}
44-
44+
4545
/**
4646
* Check user secret.
4747
*

src/Filesystem/Cache.php

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,16 @@ class Cache
2828
use \FloatPHP\Helpers\Framework\inc\TraitFormattable;
2929

3030
/**
31-
* @access private
31+
* @access protected
3232
* @var object $instance, Cache instance
33-
* @var object DRIVERS, Cache drivers
34-
*/
35-
private static $instance;
36-
private const DRIVERS = ['File', 'Redis'];
37-
38-
/**
39-
* @access public
4033
* @var bool $validate, Validate cache value
4134
* @var bool $debug, Cache debug
35+
* @var object DRIVERS, Cache drivers
4236
*/
43-
public static $validate = false;
44-
public static $debug = false;
37+
protected static $instance;
38+
protected $validate = false;
39+
protected $debug = false;
40+
protected const DRIVERS = ['File', 'Redis'];
4541

4642
/**
4743
* Instance cache driver.
@@ -99,7 +95,7 @@ public function has(string $key) : bool
9995
*/
10096
public function set(string $key, $value, ?int $ttl = null, ?string $group = null) : bool
10197
{
102-
if ( (static::$validate && !$value) || static::$debug ) {
98+
if ( ($this->validate && !$value) || $this->debug ) {
10399
return false;
104100
}
105101
$key = $this->slugify($key);
@@ -123,6 +119,29 @@ public function purge(?string $group = null) : bool
123119
return self::$instance->purge($group);
124120
}
125121

122+
/**
123+
* Disable cache.
124+
*
125+
* @inheritdoc
126+
*/
127+
public function debug(?string $group = null) : self
128+
{
129+
$this->debug = true;
130+
$this->purge($group);
131+
return $this;
132+
}
133+
134+
/**
135+
* Validate cache value.
136+
*
137+
* @inheritdoc
138+
*/
139+
public function validate() : self
140+
{
141+
$this->validate = true;
142+
return $this;
143+
}
144+
126145
/**
127146
* Get cache key.
128147
*

src/Framework/hook/Security.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function useTokenOnly() : self
6161
public function useLimitedAttempt(int $max = 3) : self
6262
{
6363
// Log failed authentication
64-
$this->addAction('authenticate-failed', function($username) {
64+
$this->addAction('auth-failed', function($username) {
6565
if ( !empty($username) ) {
6666
$transient = new Transient();
6767
$key = "auth-{$username}";

src/Framework/inc/TraitAuthenticatable.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function isAuthenticated() : bool
4141
protected function isValidSession() : bool
4242
{
4343
return ( $this->isSessionRegistered()
44-
&& !$this->isSessionExpired() );
44+
&& !$this->isSessionExpired() );
4545
}
4646

4747
/**
@@ -55,7 +55,7 @@ protected function isValidSession() : bool
5555
protected function getAccessToken(string $token, ?string $secret = null) : array
5656
{
5757
$encryption = new Encryption($token, $secret);
58-
return $encryption->setPrefix()->decrypt() ?: [];
58+
return $encryption->setPrefix()->decrypt() ?: [];
5959
}
6060

6161
/**
@@ -71,6 +71,6 @@ protected function setAccessToken(string $user, string $pswd, ?string $secret =
7171
{
7272
$data = ['user' => $user, 'pswd' => $pswd];
7373
$encryption = new Encryption($data, $secret);
74-
return $encryption->setPrefix()->encrypt();
74+
return $encryption->setPrefix()->encrypt();
7575
}
7676
}

src/Framework/inc/TraitSecurable.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,27 @@ protected function getHashObject($data, ?string $key = 'v6t1pQ97JS', ?string $ve
4949
* @access protected
5050
* @inheritdoc
5151
*/
52-
protected function generateToken($length = 32, $special = false) : string
52+
protected function generateToken(int $length = 32) : string
5353
{
54-
return Tokenizer::generate($length, $special);
54+
return Tokenizer::generate($length);
55+
}
56+
57+
/**
58+
* @access protected
59+
* @inheritdoc
60+
*/
61+
protected function generateHash($value) : string
62+
{
63+
return Tokenizer::hash($value);
64+
}
65+
66+
/**
67+
* @access protected
68+
* @inheritdoc
69+
*/
70+
protected function verifyHash(string $hash, $value) : bool
71+
{
72+
return Tokenizer::verify($hash, $value);
5573
}
5674

5775
/**

src/Html/Menu.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ public function prepare() : Menu
124124
{
125125
if ( $this->useCache && !$this->debug ) {
126126

127-
Cache::$debug = false;
128127
$cache = new Cache();
129128
$key = $cache->getKey('menu', [
130129
'user' => $this->user,

0 commit comments

Comments
 (0)