From faabdb9aee6a6f303f47cc0310c389a55eac9d2e Mon Sep 17 00:00:00 2001 From: Brady McDonough Date: Wed, 31 Jan 2024 00:17:39 -0700 Subject: [PATCH] Final linting pass, declare(strict_types=1) --- src/AntiCSRF/Base.php | 3 +- src/AntiCSRF/None.php | 2 +- src/AntiCSRFInterface.php | 2 +- src/Clock/Base.php | 8 ++- src/Clock/Request.php | 10 ++-- src/Factory.php | 3 +- src/Hash/HMAC_SHA1.php | 5 +- src/HashInterface.php | 6 +- src/RFC/_4226.php | 15 ++--- src/RFC/_6238.php | 9 ++- src/Request/base.php | 27 +++++---- src/Required/PersistenceInterface.php | 2 +- src/Required/RequestInterface.php | 4 +- src/Required/RequestTargets.php | 2 +- src/Session/Base.php | 12 ++-- src/SessionInterface.php | 2 +- src/URI/otpauth.php | 83 +++++++++++---------------- src/Workflow/Authenticate.php | 2 +- src/Workflow/UserManagement.php | 4 +- src/WorkflowInterface.php | 2 +- 20 files changed, 104 insertions(+), 99 deletions(-) diff --git a/src/AntiCSRF/Base.php b/src/AntiCSRF/Base.php index 38b3984..1206f9a 100644 --- a/src/AntiCSRF/Base.php +++ b/src/AntiCSRF/Base.php @@ -1,4 +1,4 @@ -generate(); } + /** @SuppressWarnings(PHPMD.Superglobals) */ public function match(): bool { if (\hash_equals($this->session->get(CSRF_TOKEN_IDX), $_REQUEST[CSRF_TOKEN_IDX])) diff --git a/src/AntiCSRF/None.php b/src/AntiCSRF/None.php index c9faf15..0336c71 100644 --- a/src/AntiCSRF/None.php +++ b/src/AntiCSRF/None.php @@ -1,4 +1,4 @@ -time = $_SERVER["REQUEST_TIME"]; } - public function now(): \DateTimeImmutable + public function now(): DateTimeImmutable { - return new \DateTimeImmutable($this->time); + return new DateTimeImmutable($this->time); } } -?> \ No newline at end of file +?> diff --git a/src/Factory.php b/src/Factory.php index d229d38..c85b3cd 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -1,4 +1,4 @@ -n, $this->n + ($this->driftModifier * $this->grace), $this->driftModifier) as $c) + foreach (range($this->count, $this->count + ($this->driftModifier * $this->grace), $this->driftModifier) as $count) { $expected = - \bindec($this->hash->hashNumeric($this->key, $c)) % + \bindec($this->hash->hashNumeric($this->key, $count)) % \pow(10, $this->digits); - if (\hash_equals((string)$expected, $q)) + if (\hash_equals((string)$expected, $query)) { - $validCount = $c; + $validCount = $count; break; } } diff --git a/src/RFC/_6238.php b/src/RFC/_6238.php index d3d5bbb..48dc525 100644 --- a/src/RFC/_6238.php +++ b/src/RFC/_6238.php @@ -1,8 +1,11 @@ -clock->now()->getTimestamp()/$this->window; $hotp = new _4226( @@ -28,7 +31,7 @@ class _6238 -1 ); - $valid = $hotp->validate($q) * $this->window; + $valid = $hotp->validate($query) * $this->window; if ($valid != false && $valid <= $this->floor) { $valid = false; diff --git a/src/Request/base.php b/src/Request/base.php index cf0bf3d..62fba8a 100644 --- a/src/Request/base.php +++ b/src/Request/base.php @@ -1,17 +1,24 @@ - $p) + foreach($paths as $r => $_) { // ::from will throw a ValueError if invalid RequestTargets::from($r); @@ -19,24 +26,20 @@ class Base implements RequestInterface } else if(!\is_string($paths)) { - throw new \IllegalArgumentException("You must either provide a single path as a string or a list of RequestTargets as keys to paths."); + throw new IllegalArgumentException("You must either provide a single path as a string or a list of RequestTargets as keys to paths."); } } public function formProps(string $place): string { $method = 'method="post"'; - if(\is_array($this->paths)) - { - $action = 'action="' . $this->paths[$place] . '"'; - } - else - { - $action = 'action="' . $this->paths . '"'; - } + $path = \is_array($this->paths)? $this->paths[$place] : $this->paths; + $action = 'action="' . $path . '"'; + return $action . ' ' . $method; } + /** @SuppressWarnings(PHPMD.Superglobals) */ public function getResp(string $key): string { return $_REQUEST[$key]; diff --git a/src/Required/PersistenceInterface.php b/src/Required/PersistenceInterface.php index 26d7068..6807f61 100644 --- a/src/Required/PersistenceInterface.php +++ b/src/Required/PersistenceInterface.php @@ -1,4 +1,4 @@ -ns($key); + $key = $this->prefix($key); if (!isset($_SESSION[$key])) { $_SESSION[$key] = $val; @@ -28,12 +30,12 @@ class Base implements SessionInterface public function store(string $key, mixed $val) { - $_SESSION[$this->ns($key)] = $val; + $_SESSION[$this->prefix($key)] = $val; } public function get(string $key): mixed { - return $_SESSION[$this->ns($key)]; + return $_SESSION[$this->prefix($key)]; } } diff --git a/src/SessionInterface.php b/src/SessionInterface.php index dd4d197..1797b90 100644 --- a/src/SessionInterface.php +++ b/src/SessionInterface.php @@ -1,4 +1,4 @@ - $v) { if(\array_key_exists($k, $arr)) @@ -73,44 +74,28 @@ class Otpauth $arr[$k] = $v; } } - } - $query_defaults = [ + }; + $queryDefaults = [ "algorithm" => "SHA1", "period" => 30, "digits" => 6, ]; - - $apply_defaults($query_defaults, $parsed_query) + $applyDefaults($queryDefaults, $parsedQuery); // END SCHEMEING - \ltrim($label[0], "/") !== $parsed_query['issuer'] - || throw new \InvalidArgumentException($uri . " has mismatching issuer information."); + + \ltrim($label[0], "/") !== $parsedQuery['issuer'] + || throw new InvalidArgumentException($uri . " has mismatching issuer information."); return self( - $parsed_query['issuer'], + $parsedQuery['issuer'], $label[1], - $parsed_query['secret'], - $parsed_query['algorithm'], - $parsed_query['period'], - $parsed_query['digits'] + $parsedQuery['secret'], + $parsedQuery['algorithm'], + $parsedQuery['period'], + $parsedQuery['digits'] ); } - public static function from_string(string $uri): self - { - $parsed = self._pre($uri); - $parsed_uri = $parsed[0]; - $parsed_query = $parsed[1]; - - $label = \explode(":", $parsed_uri['path']); - $issuer = \ltrim($label[0], "/"); - $user = $label[1]; - - $secret = $query['secret']; - - \array_key_exists('algorithm', $query) ? $algo = $query['algorithm'] : $algo = null; - return new self($issuer, $user, $secret, $algo); - } - public function emitStr(): string { $label = $this->provider . ":" . $this->userid; @@ -119,9 +104,9 @@ class Otpauth $digits = "digits=" . $this->digits; $period = "period=" . $this->period; $secret = "secret=" . $this->secret; - $query = \implode("&", [$secret, $provider, $period, $digits]) + $query = \implode("&", [$secret, $provider, $period, $digits, $algo]); - return "otpauth://totp/" $label . "?" . $query; + return "otpauth://totp/" . $label . "?" . $query; } } diff --git a/src/Workflow/Authenticate.php b/src/Workflow/Authenticate.php index 49a4e2a..231f36c 100644 --- a/src/Workflow/Authenticate.php +++ b/src/Workflow/Authenticate.php @@ -1,4 +1,4 @@ -