|
|
|
|
@ -1,4 +1,5 @@ |
|
|
|
|
<?php |
|
|
|
|
declare(strict_types=1); |
|
|
|
|
/** |
|
|
|
|
* @copyright Copyright (c) 2017 Lukas Reschke <lukas@statuscode.ch> |
|
|
|
|
* |
|
|
|
|
@ -54,8 +55,8 @@ class MemoryCache implements IBackend { |
|
|
|
|
* @param string $userIdentifier |
|
|
|
|
* @return string |
|
|
|
|
*/ |
|
|
|
|
private function hash($methodIdentifier, |
|
|
|
|
$userIdentifier) { |
|
|
|
|
private function hash(string $methodIdentifier, |
|
|
|
|
string $userIdentifier): string { |
|
|
|
|
return hash('sha512', $methodIdentifier . $userIdentifier); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -63,9 +64,14 @@ class MemoryCache implements IBackend { |
|
|
|
|
* @param string $identifier |
|
|
|
|
* @return array |
|
|
|
|
*/ |
|
|
|
|
private function getExistingAttempts($identifier) { |
|
|
|
|
$cachedAttempts = json_decode($this->cache->get($identifier), true); |
|
|
|
|
if(is_array($cachedAttempts)) { |
|
|
|
|
private function getExistingAttempts(string $identifier): array { |
|
|
|
|
$cachedAttempts = $this->cache->get($identifier); |
|
|
|
|
if ($cachedAttempts === null) { |
|
|
|
|
return []; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$cachedAttempts = json_decode($cachedAttempts, true); |
|
|
|
|
if(\is_array($cachedAttempts)) { |
|
|
|
|
return $cachedAttempts; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -75,9 +81,9 @@ class MemoryCache implements IBackend { |
|
|
|
|
/** |
|
|
|
|
* {@inheritDoc} |
|
|
|
|
*/ |
|
|
|
|
public function getAttempts($methodIdentifier, |
|
|
|
|
$userIdentifier, |
|
|
|
|
$seconds) { |
|
|
|
|
public function getAttempts(string $methodIdentifier, |
|
|
|
|
string $userIdentifier, |
|
|
|
|
int $seconds): int { |
|
|
|
|
$identifier = $this->hash($methodIdentifier, $userIdentifier); |
|
|
|
|
$existingAttempts = $this->getExistingAttempts($identifier); |
|
|
|
|
|
|
|
|
|
@ -96,9 +102,9 @@ class MemoryCache implements IBackend { |
|
|
|
|
/** |
|
|
|
|
* {@inheritDoc} |
|
|
|
|
*/ |
|
|
|
|
public function registerAttempt($methodIdentifier, |
|
|
|
|
$userIdentifier, |
|
|
|
|
$period) { |
|
|
|
|
public function registerAttempt(string $methodIdentifier, |
|
|
|
|
string $userIdentifier, |
|
|
|
|
int $period) { |
|
|
|
|
$identifier = $this->hash($methodIdentifier, $userIdentifier); |
|
|
|
|
$existingAttempts = $this->getExistingAttempts($identifier); |
|
|
|
|
$currentTime = $this->timeFactory->getTime(); |
|
|
|
|
|