diff options
author | Roeland Jago Douma <roeland@famdouma.nl> | 2018-01-14 11:47:00 +0100 |
---|---|---|
committer | Morris Jobke <hey@morrisjobke.de> | 2018-01-14 21:08:23 +0100 |
commit | cf0a3399970eb00621e822923f17d3d52845e0a6 (patch) | |
tree | c7d0603711f70b29341e802e98821ca9089fa25c /lib/private/Security/RateLimiting/Backend | |
parent | 60f38d37fe5ca505258510adc1e106da54426510 (diff) | |
download | nextcloud-server-cf0a3399970eb00621e822923f17d3d52845e0a6.tar.gz nextcloud-server-cf0a3399970eb00621e822923f17d3d52845e0a6.zip |
Make OC\Security\RateLimiting strict
* Add return types
* Add scalar argument types
* Made strict
* Cleaned up phpstorm inspections
Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
Diffstat (limited to 'lib/private/Security/RateLimiting/Backend')
-rw-r--r-- | lib/private/Security/RateLimiting/Backend/IBackend.php | 13 | ||||
-rw-r--r-- | lib/private/Security/RateLimiting/Backend/MemoryCache.php | 28 |
2 files changed, 24 insertions, 17 deletions
diff --git a/lib/private/Security/RateLimiting/Backend/IBackend.php b/lib/private/Security/RateLimiting/Backend/IBackend.php index b20d27af42b..88c10fbbc8d 100644 --- a/lib/private/Security/RateLimiting/Backend/IBackend.php +++ b/lib/private/Security/RateLimiting/Backend/IBackend.php @@ -1,4 +1,5 @@ <?php +declare(strict_types=1); /** * @copyright Copyright (c) 2017 Lukas Reschke <lukas@statuscode.ch> * @@ -39,9 +40,9 @@ interface IBackend { * @param int $seconds Seconds to look back at * @return int */ - public function getAttempts($methodIdentifier, - $userIdentifier, - $seconds); + public function getAttempts(string $methodIdentifier, + string $userIdentifier, + int $seconds): int; /** * Registers an attempt @@ -50,7 +51,7 @@ interface IBackend { * @param string $userIdentifier Identifier for the user * @param int $period Period in seconds how long this attempt should be stored */ - public function registerAttempt($methodIdentifier, - $userIdentifier, - $period); + public function registerAttempt(string $methodIdentifier, + string $userIdentifier, + int $period); } diff --git a/lib/private/Security/RateLimiting/Backend/MemoryCache.php b/lib/private/Security/RateLimiting/Backend/MemoryCache.php index 700fa624ed4..a8fb7b87d10 100644 --- a/lib/private/Security/RateLimiting/Backend/MemoryCache.php +++ b/lib/private/Security/RateLimiting/Backend/MemoryCache.php @@ -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(); |