Final linting pass, declare(strict_types=1)

master
Brady McDonough 2 years ago
parent 345b43d950
commit faabdb9aee

@ -1,4 +1,4 @@
<?php <?php declare(strict_types=1);
namespace BradyMcD\TAATP\AntiCSRF; namespace BradyMcD\TAATP\AntiCSRF;
use BradyMcD\TAATP\AntiCSRFInterface; use BradyMcD\TAATP\AntiCSRFInterface;
@ -20,6 +20,7 @@ class Base implements AntiCSRFInterface
$this->generate(); $this->generate();
} }
/** @SuppressWarnings(PHPMD.Superglobals) */
public function match(): bool public function match(): bool
{ {
if (\hash_equals($this->session->get(CSRF_TOKEN_IDX), $_REQUEST[CSRF_TOKEN_IDX])) if (\hash_equals($this->session->get(CSRF_TOKEN_IDX), $_REQUEST[CSRF_TOKEN_IDX]))

@ -1,4 +1,4 @@
<?php <?php declare(strict_types=1);
namespace BradyMcD\TAATP\AntiCSRF; namespace BradyMcD\TAATP\AntiCSRF;
use BradyMcD\TAATP\AntiCSRFInterface; use BradyMcD\TAATP\AntiCSRFInterface;

@ -1,4 +1,4 @@
<?php <?php declare(strict_types=1);
namespace BradyMcD\TAATP; namespace BradyMcD\TAATP;

@ -1,12 +1,14 @@
<?php <?php declare(strict_types=1);
namespace BradyMcD\TAATP\Clock; namespace BradyMcD\TAATP\Clock;
use DateTimeImmutable;
class Base implements \Psr\Clock\ClockInterface class Base implements \Psr\Clock\ClockInterface
{ {
function now(): \DateTimeImmutable function now(): DateTimeImmutable
{ {
return new \DateTimeImmutable("now"); return new DateTimeImmutable("now");
} }
} }

@ -1,7 +1,9 @@
<?php <?php declare(strict_types=1);
namespace BradyMcD\TAATP\Clock; namespace BradyMcD\TAATP\Clock;
use DateTimeImmutable;
class Request implements \Psr\Clock\ClockInterface class Request implements \Psr\Clock\ClockInterface
{ {
private $time; private $time;
@ -11,10 +13,10 @@ class Request implements \Psr\Clock\ClockInterface
$this->time = $_SERVER["REQUEST_TIME"]; $this->time = $_SERVER["REQUEST_TIME"];
} }
public function now(): \DateTimeImmutable public function now(): DateTimeImmutable
{ {
return new \DateTimeImmutable($this->time); return new DateTimeImmutable($this->time);
} }
} }
?> ?>

@ -1,4 +1,4 @@
<?php <?php declare(strict_types=1);
namespace BradyMcD\TAATP; namespace BradyMcD\TAATP;
use BradyMcD\TAATP\Required\PersistenceInterface; use BradyMcD\TAATP\Required\PersistenceInterface;
@ -10,6 +10,7 @@ use BradyMcD\TAATP\Workflow\Authenticate;
/** /**
* The primary entrypoint of the module. * The primary entrypoint of the module.
* @SuppressWarnings(PHPMD.ShortVariable)
*/ */
class Factory class Factory
{ {

@ -1,12 +1,14 @@
<?php <?php declare(strict_types=1);
namespace BradyMcD\TAATP\Hash; namespace BradyMcD\TAATP\Hash;
use BradyMcD\TAATP\HashInterface; use BradyMcD\TAATP\HashInterface;
use ParagonIE\ConstantTime\Base32; use ParagonIE\ConstantTime\Base32;
/** @SuppressWarnings(PHPMD.CamelCaseClassName)*/
class HMAC_SHA1 implements HashInterface class HMAC_SHA1 implements HashInterface
{ {
const DEFAULT_SECRET_SIZE = 32; const DEFAULT_SECRET_SIZE = 32;
/** @SuppressWarnings(PHPMD.StaticAccess) */
public function hash(string $key, string $val): string public function hash(string $key, string $val): string
{ {
$key = Base32::decode($key); $key = Base32::decode($key);
@ -23,6 +25,7 @@ class HMAC_SHA1 implements HashInterface
return "SHA1"; return "SHA1";
} }
/** @SuppressWarnings(PHPMD.StaticAccess) */
public static function keygen(): string public static function keygen(): string
{ {
return Base32::encodeUpper(random_bytes(this.DEFAULT_SECRET_SIZE)); return Base32::encodeUpper(random_bytes(this.DEFAULT_SECRET_SIZE));

@ -1,11 +1,11 @@
<?php <?php declare(strict_types=1);
namespace BradyMcD\TAATP; namespace BradyMcD\TAATP;
interface HashInterface interface HashInterface
{ {
public function hash(string $k, string $v): string; public function hash(string $key, string $val): string;
public function hashNumeric(string $k, int $v): string; public function hashNumeric(string $key, int $val): string;
public function hashType(): string; public function hashType(): string;
} }

@ -1,11 +1,12 @@
<?php <?php declare(strict_types=1);
namespace BradyMcD\TAATP\RFC; namespace BradyMcD\TAATP\RFC;
/** @SuppressWarnings(CamelCaseClassName)*/
class _4226 class _4226
{ {
public function __construct( public function __construct(
private string $key, private string $key,
private int $n, private int $count,
private int $grace, private int $grace,
private int $digits, private int $digits,
private \BradyMcD\TAATP\HashInterface $hash, private \BradyMcD\TAATP\HashInterface $hash,
@ -13,18 +14,18 @@ class _4226
) )
{} {}
public function validate(string $q): int public function validate(string $query): int
{ {
$validCount = false; $validCount = false;
foreach (range($this->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 = $expected =
\bindec($this->hash->hashNumeric($this->key, $c)) % \bindec($this->hash->hashNumeric($this->key, $count)) %
\pow(10, $this->digits); \pow(10, $this->digits);
if (\hash_equals((string)$expected, $q)) if (\hash_equals((string)$expected, $query))
{ {
$validCount = $c; $validCount = $count;
break; break;
} }
} }

@ -1,8 +1,11 @@
<?php <?php declare(strict_types=1);
namespace BradyMcD\TAATP\RFC; namespace BradyMcD\TAATP\RFC;
use Psr\Clock\ClockInterface; use Psr\Clock\ClockInterface;
/**
* @SuppressWarnings(PHPMD.CamelCaseClassName)
*/
class _6238 class _6238
{ {
public function __construct( public function __construct(
@ -16,7 +19,7 @@ class _6238
) )
{} {}
public function validate(string $q): bool|int public function validate(string $query): bool|int
{ {
$windowNum = $this->clock->now()->getTimestamp()/$this->window; $windowNum = $this->clock->now()->getTimestamp()/$this->window;
$hotp = new _4226( $hotp = new _4226(
@ -28,7 +31,7 @@ class _6238
-1 -1
); );
$valid = $hotp->validate($q) * $this->window; $valid = $hotp->validate($query) * $this->window;
if ($valid != false && $valid <= $this->floor) if ($valid != false && $valid <= $this->floor)
{ {
$valid = false; $valid = false;

@ -1,17 +1,24 @@
<?php <?php declare(strict_types=1);
namespace BradyMcD\TAATP\Request; namespace BradyMcD\TAATP\Request;
use IllegalArgumentException;
use BradyMcD\TAATP\Required\RequestInterface; use BradyMcD\TAATP\Required\RequestInterface;
use BradyMcD\TAATP\Required\RequestTargets; use BradyMcD\TAATP\Required\RequestTargets;
class Base implements RequestInterface class Base implements RequestInterface
{ {
public function __construct(private mixed $paths) /**
* @SuppressWarnings(PHPMD.StaticAccess)
* @SuppressWarnings(PHPMD.CamelCaseVariableName)
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
*/
public function __construct(private readonly mixed $paths)
{ {
if(\is_array($paths)) if(\is_array($paths))
{ {
foreach($paths as $r => $p) foreach($paths as $r => $_)
{ {
// ::from will throw a ValueError if invalid // ::from will throw a ValueError if invalid
RequestTargets::from($r); RequestTargets::from($r);
@ -19,24 +26,20 @@ class Base implements RequestInterface
} }
else if(!\is_string($paths)) 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 public function formProps(string $place): string
{ {
$method = 'method="post"'; $method = 'method="post"';
if(\is_array($this->paths)) $path = \is_array($this->paths)? $this->paths[$place] : $this->paths;
{ $action = 'action="' . $path . '"';
$action = 'action="' . $this->paths[$place] . '"';
}
else
{
$action = 'action="' . $this->paths . '"';
}
return $action . ' ' . $method; return $action . ' ' . $method;
} }
/** @SuppressWarnings(PHPMD.Superglobals) */
public function getResp(string $key): string public function getResp(string $key): string
{ {
return $_REQUEST[$key]; return $_REQUEST[$key];

@ -1,4 +1,4 @@
<?php <?php declare(strict_types=1);
/** /**
* *
*/ */

@ -1,4 +1,4 @@
<?php <?php declare(strict_types=1);
/** /**
* *
*/ */
@ -23,7 +23,7 @@ interface RequestInterface
* @param string $k The key the user is sending * @param string $k The key the user is sending
* @return string * @return string
*/ */
public function getResp(string $k): string; public function getResp(string $key): string;
} }

@ -1,4 +1,4 @@
<?php <?php declare(strict_types=1);
namespace BradyMcD\TAATP\Required; namespace BradyMcD\TAATP\Required;

@ -1,11 +1,13 @@
<?php <?php declare(strict_types=1);
namespace BradyMcD\TAATP\Session; namespace BradyMcD\TAATP\Session;
use BradyMcD\TAATP\SessionInterface; use BradyMcD\TAATP\SessionInterface;
/** @SuppressWarnings(PHPMD.Superglobals) */
class Base implements SessionInterface class Base implements SessionInterface
{ {
private function ns($key): string private function prefix($key): string
{ {
return "taatp_" . $key; return "taatp_" . $key;
} }
@ -17,7 +19,7 @@ class Base implements SessionInterface
public function tryStore(string $key, mixed $val): bool public function tryStore(string $key, mixed $val): bool
{ {
$key = $this->ns($key); $key = $this->prefix($key);
if (!isset($_SESSION[$key])) if (!isset($_SESSION[$key]))
{ {
$_SESSION[$key] = $val; $_SESSION[$key] = $val;
@ -28,12 +30,12 @@ class Base implements SessionInterface
public function store(string $key, mixed $val) public function store(string $key, mixed $val)
{ {
$_SESSION[$this->ns($key)] = $val; $_SESSION[$this->prefix($key)] = $val;
} }
public function get(string $key): mixed public function get(string $key): mixed
{ {
return $_SESSION[$this->ns($key)]; return $_SESSION[$this->prefix($key)];
} }
} }

@ -1,4 +1,4 @@
<?php <?php declare(strict_types=1);
namespace BradyMcD\TAATP; namespace BradyMcD\TAATP;

@ -1,7 +1,8 @@
<?php <?php declare(strict_types=1);
namespace BradyMcD\TAATP\URI; namespace BradyMcD\TAATP\URI;
use InvalidArgumentException;
class Otpauth class Otpauth
{ {
@ -15,17 +16,17 @@ class Otpauth
) )
{} {}
public static function from_string(string $uri): self public static function fromString(string $uri): self
{ {
// This is definitely from writing Scheme. // This is definitely from writing Scheme.
$run_check = function(array $check_array, $target) $runCheck = function(array $checkArray, $target)
{ {
foreach($check_array as $c) foreach($checkArray as $c)
{ {
!($c[0]($target))? :throw new \InvalidArgumentException($c[1]); !($c[0]($target))? :throw new InvalidArgumentException($c[1]);
} }
} };
$uri_checks = [ $uriChecks = [
[ function($arr) [ function($arr)
{ return $arr !== false; }, { return $arr !== false; },
$uri . " is not a valid URI."], $uri . " is not a valid URI."],
@ -40,32 +41,32 @@ class Otpauth
$uri . " lacks query information."] $uri . " lacks query information."]
]; ];
$parsed_uri = \parse_url($uri); $parsedUri = \parse_url($uri);
$run_check($uri_checks, $parsed_uri) $runCheck($uriChecks, $parsedUri);
$query_checks = [ $queryChecks = [
[ function($que) [ function($que)
{ return \array_key_exists('issuer', $que);}, { return \array_key_exists('issuer', $que);},
$parsed_uri['query'] . " has no issuer information."], $parsedUri['query'] . " has no issuer information."],
[ function($que) [ function($que)
{ return \array_key_exists('secret', $que);}, { return \array_key_exists('secret', $que);},
$parsed_uri['query'] . "has no secret key."] $parsedUri['query'] . "has no secret key."]
]; ];
$parsed_query = []; $parsedQuery = [];
\parse_str($parsed['query'], $parsed_query); \parse_str($parsedUri['query'], $parsedQuery);
$run_check($query_checks, $parsed_query); $runCheck($queryChecks, $parsedQuery);
$label_checks = [ $labelChecks = [
[ function($lab) [ function($lab)
{ return \count($lab) === 2;}, { return \count($lab) === 2;},
$parsed_uri['path'] . " doesn't have the correct number of components."] $parsedUri['path'] . " doesn't have the correct number of components."]
]; ];
$label = \explode(":", $parsed['path']); $label = \explode(":", $parsedUri['path']);
$run_check($label_checks, $label); $runCheck($labelChecks, $label);
$apply_defaults = function(array &$arr, array $defaults) { $applyDefaults = function(array &$arr, array $defaults) {
foreach($defaults as $k => $v) foreach($defaults as $k => $v)
{ {
if(\array_key_exists($k, $arr)) if(\array_key_exists($k, $arr))
@ -73,44 +74,28 @@ class Otpauth
$arr[$k] = $v; $arr[$k] = $v;
} }
} }
} };
$query_defaults = [ $queryDefaults = [
"algorithm" => "SHA1", "algorithm" => "SHA1",
"period" => 30, "period" => 30,
"digits" => 6, "digits" => 6,
]; ];
$applyDefaults($queryDefaults, $parsedQuery);
$apply_defaults($query_defaults, $parsed_query)
// END SCHEMEING // 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( return self(
$parsed_query['issuer'], $parsedQuery['issuer'],
$label[1], $label[1],
$parsed_query['secret'], $parsedQuery['secret'],
$parsed_query['algorithm'], $parsedQuery['algorithm'],
$parsed_query['period'], $parsedQuery['period'],
$parsed_query['digits'] $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 public function emitStr(): string
{ {
$label = $this->provider . ":" . $this->userid; $label = $this->provider . ":" . $this->userid;
@ -119,9 +104,9 @@ class Otpauth
$digits = "digits=" . $this->digits; $digits = "digits=" . $this->digits;
$period = "period=" . $this->period; $period = "period=" . $this->period;
$secret = "secret=" . $this->secret; $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;
} }
} }

@ -1,4 +1,4 @@
<?php <?php declare(strict_types=1);
namespace BradyMcD\TAATP\Workflow; namespace BradyMcD\TAATP\Workflow;
use BradyMcD\TAATP\AntiCSRFInterface; use BradyMcD\TAATP\AntiCSRFInterface;

@ -1,12 +1,14 @@
<?php <?php declare(strict_types=1);
namespace BradyMcD\TAATP; namespace BradyMcD\TAATP;
use BradyMcD\TAATP\AntiCSRFInterface; use BradyMcD\TAATP\AntiCSRFInterface;
use BradyMcD\TAATP\Required\PersistenceInterface; use BradyMcD\TAATP\Required\PersistenceInterface;
use BradyMcD\TAATP\SessionInterface; use BradyMcD\TAATP\SessionInterface;
use BradyMcD\TAATP\HashInterface; use BradyMcD\TAATP\HashInterface;
use BradyMcD\TAATP\URI\Otpauth; use BradyMcD\TAATP\URI\Otpauth;
use BradyMcD\RFC\_6238; use BradyMcD\RFC\_6238;
use Chillerlan\QRCode; use Chillerlan\QRCode;

@ -1,4 +1,4 @@
<?php <?php declare(strict_types=1);
namespace BradyMcD\TAATP; namespace BradyMcD\TAATP;

Loading…
Cancel
Save