From 0a47684a7448d7c7398fa29acfd9ca992ccae662 Mon Sep 17 00:00:00 2001 From: Brady McDonough Date: Tue, 30 Jan 2024 00:22:34 -0700 Subject: [PATCH] phpmd, the great naming standards refactor --- .gitignore | 3 +- src/AntiCSRF/Base.php | 9 +++-- src/AntiCSRF/None.php | 6 ++-- src/AntiCSRFInterface.php | 4 +-- src/Clock/Base.php | 2 +- src/Factory.php | 18 +++++----- src/Hash/HMAC_SHA1.php | 4 +-- src/HashInterface.php | 4 +-- src/RFC/_4226.php | 8 ++--- src/RFC/_6238.php | 4 +-- src/Request/base.php | 6 ++-- src/Required/PersistenceInterface.php | 10 +++--- src/Required/RequestInterface.php | 4 +-- src/Session/Base.php | 2 +- src/SessionInterface.php | 4 +-- src/URI/otpauth.php | 2 +- src/Workflow/Authenticate.php | 18 +++++----- src/Workflow/UserManagement.php | 48 +++++++++++++-------------- src/WorkflowInterface.php | 2 +- 19 files changed, 79 insertions(+), 79 deletions(-) diff --git a/.gitignore b/.gitignore index fd6cdfb..14c4c40 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ *~ \#*\# .\#* -.projectile \ No newline at end of file +.projectile +TAGS \ No newline at end of file diff --git a/src/AntiCSRF/Base.php b/src/AntiCSRF/Base.php index f678525..38b3984 100644 --- a/src/AntiCSRF/Base.php +++ b/src/AntiCSRF/Base.php @@ -10,7 +10,6 @@ define("CSRF_EXPIRY_IDX", "antiCSRF_TAATP_Expiry"); class Base implements AntiCSRFInterface { private int $token; - private int $expiry; public function __construct( private SessionInterface $session, @@ -35,7 +34,7 @@ class Base implements AntiCSRFInterface return $this->clock->now()->getTimestamp() >= $this->session->get(CSRF_EXPIRY_IDX); } - public function emit_str(): string + public function emitStr(): string { return 'emit_str(); + echo $this->emitStr(); } public function generate() @@ -38,4 +38,4 @@ class None implements AntiCSRFInterface {} } -?> \ No newline at end of file +?> diff --git a/src/AntiCSRFInterface.php b/src/AntiCSRFInterface.php index 92f0a35..0073940 100644 --- a/src/AntiCSRFInterface.php +++ b/src/AntiCSRFInterface.php @@ -13,10 +13,10 @@ interface AntiCSRFInterface public function match(): bool; public function expired(): bool; public function display(); - public function emit_str(): string; + public function emitStr(): string; public function generate(); public function regenerate(); } -?> \ No newline at end of file +?> diff --git a/src/Clock/Base.php b/src/Clock/Base.php index 5cbd01c..3579420 100644 --- a/src/Clock/Base.php +++ b/src/Clock/Base.php @@ -10,4 +10,4 @@ class Base implements \Psr\Clock\ClockInterface } } -?> \ No newline at end of file +?> diff --git a/src/Factory.php b/src/Factory.php index e2c0055..52205ac 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -17,33 +17,33 @@ class Factory private PersistenceInterface $db, private RequestInterface $ri, private null|SessionInterface $session, - private null|\PSR\Clock\ClockInterface $csrf_clock, + private null|\PSR\Clock\ClockInterface $csrfClock, private null|AntiCSRFInterface $csrf, - private null|\PSR\Clock\ClockInterface $totp_clock, + private null|\PSR\Clock\ClockInterface $totpClock, private null|HashInterface $hash, ) { $this->session ? : $this->session = new Session\Base(); - $this->csrf_clock ? : $this->csrf_clock = new Clock\Request(); + $this->csrf_clock ? : $this->csrfClock = new Clock\Request(); $this->csrf ? : $this->csrf = new AntiCSRF\Base( $this->session, $this->csrf_clock ); - $this->totp_clock ? : $this->totp_clock = new Clock\Base(); + $this->totp_clock ? : $this->totpClock = new Clock\Base(); $this->$hash ? : $this->hash = new Hash\HMAC_SHA1(); } - public function user_management(mixed $user_index): UserManagement + public function userManagement(mixed $userIndex): UserManagement { - return new UserManagement($this->db, $this->ri, $this->csrf, $this->session, $user_index); + return new UserManagement($this->db, $this->ri, $this->csrf, $this->session, $userIndex); } - public function authenticate(mixed $user_index): null|Authenticate + public function authenticate(mixed $userIndex): null|Authenticate { - if(\is_null($db->get_secret($user_index))) + if(\is_null($this->db->getSecret($userIndex))) return null; else - return new Authenticate($this->db, $this->ri, $this->csrf, $this->session, $user_index) + return new Authenticate($this->db, $this->ri, $this->csrf, $this->session, $userIndex); } } diff --git a/src/Hash/HMAC_SHA1.php b/src/Hash/HMAC_SHA1.php index 18374aa..27d8767 100644 --- a/src/Hash/HMAC_SHA1.php +++ b/src/Hash/HMAC_SHA1.php @@ -13,12 +13,12 @@ class HMAC_SHA1 implements HashInterface return \hash_hmac("sha1", \hex2bin($v), $key, true); } - public function hash_numeric(string $k, int $v): string + public function hashNumeric(string $k, int $v): string { return $this->hash($k, \dechex($v)); } - public function hash_type(): string + public function hashType(): string { return "SHA1"; } diff --git a/src/HashInterface.php b/src/HashInterface.php index 9963709..389c4c4 100644 --- a/src/HashInterface.php +++ b/src/HashInterface.php @@ -5,8 +5,8 @@ namespace BradyMcD\TAATP; interface HashInterface { public function hash(string $k, string $v): string; - public function hash_numeric(string $k, int $v): string; - public function hash_type(): string; + public function hashNumeric(string $k, int $v): string; + public function hashType(): string; } ?> diff --git a/src/RFC/_4226.php b/src/RFC/_4226.php index 2f58adf..ba2fd34 100644 --- a/src/RFC/_4226.php +++ b/src/RFC/_4226.php @@ -15,20 +15,20 @@ class _4226 public function validate(string $q): int { - $valid_count = false; + $validCount = false; foreach (range($this->n, $this->n + ($this->driftModifier * $this->grace), $this->driftModifier) as $c) { $expected = - \bindec($this->hash->hash_numeric($this->key, $c)) % + \bindec($this->hash->hashNumeric($this->key, $c)) % \pow(10, $this->digits); if (\hash_equals((string)$expected, $q)) { - $valid_count = $c; + $validCount = $c; break; } } - return $valid_count; + return $validCount; } } diff --git a/src/RFC/_6238.php b/src/RFC/_6238.php index 1c81575..d3d5bbb 100644 --- a/src/RFC/_6238.php +++ b/src/RFC/_6238.php @@ -18,10 +18,10 @@ class _6238 public function validate(string $q): bool|int { - $window_n = $this->clock->now()->getTimestamp()/$this->window; + $windowNum = $this->clock->now()->getTimestamp()/$this->window; $hotp = new _4226( $this->key, - $window_n, + $windowNum, $this->grace, $this->digits, $this->hash, diff --git a/src/Request/base.php b/src/Request/base.php index 89222bb..cf0bf3d 100644 --- a/src/Request/base.php +++ b/src/Request/base.php @@ -23,7 +23,7 @@ class Base implements RequestInterface } } - public function form_props(string $place): string + public function formProps(string $place): string { $method = 'method="post"'; if(\is_array($this->paths)) @@ -37,9 +37,9 @@ class Base implements RequestInterface return $action . ' ' . $method; } - public function get_resp(string $k): string + public function getResp(string $key): string { - return $__REQUEST[$k]; + return $_REQUEST[$key]; } } diff --git a/src/Required/PersistenceInterface.php b/src/Required/PersistenceInterface.php index 7fbc208..26d7068 100644 --- a/src/Required/PersistenceInterface.php +++ b/src/Required/PersistenceInterface.php @@ -16,13 +16,13 @@ interface PersistenceInterface * @param string $secret The secret datastring used to seed the TOTP rolling hash * @return void */ - public function store_secret(mixed $index, string $secret); + public function storeSecret(mixed $index, string $secret); /** * Removes the secret key for the user associated with the given indexing data. * @param mixed $index Whatever data needed to index into your database and identify a particular user */ - public function strip_secret(mixed $index); + public function stripSecret(mixed $index); /** * As the name suggests One-Time-Passwords should only be usable one time! @@ -32,7 +32,7 @@ interface PersistenceInterface * Only codes generated at a time greater than the indicated time will be deemed valid. * @return bool */ - public function store_last_time(mixed $index, int $timestamp): bool; + public function storeLastTime(mixed $index, int $timestamp): bool; /** * Gets and returns the otpauth URI for the user associated with the given user_id. @@ -40,14 +40,14 @@ interface PersistenceInterface * @param mixed $index Whatever data needed to index into your database and identify a particular user * @return null|string */ - public function get_secret(mixed $index): null|string; + public function getSecret(mixed $index): null|string; /** * Gets and returns the last successful challenge timestamp to enforce the One-Time aspect of a TOTP. * @param mixed $index Whatever data needed to index into your database and identify a particular user * @return int */ - public function get_last_time(mixed $index): int; + public function getLastTime(mixed $index): int; } ?> diff --git a/src/Required/RequestInterface.php b/src/Required/RequestInterface.php index 855c22c..4b54438 100644 --- a/src/Required/RequestInterface.php +++ b/src/Required/RequestInterface.php @@ -16,14 +16,14 @@ interface RequestInterface * @param string $place One of "enroll", "unenroll" or "authenticate" * @return string */ - public function form_props(string $place): string; + public function formProps(string $place): string; /** * Returns a referred user response variable. * @param string $k The key the user is sending * @return string */ - public function get_resp(string $k): string; + public function getResp(string $k): string; } diff --git a/src/Session/Base.php b/src/Session/Base.php index d50a66c..0878e6c 100644 --- a/src/Session/Base.php +++ b/src/Session/Base.php @@ -15,7 +15,7 @@ class Base implements SessionInterface return session_status() === PHP_SESSION_ACTIVE; } - public function try_store(string $k, mixed $val): bool + public function tryStore(string $k, mixed $val): bool { $key = $this->ns($k); if (!isset($_SESSION[$key])) diff --git a/src/SessionInterface.php b/src/SessionInterface.php index ef00893..dd4d197 100644 --- a/src/SessionInterface.php +++ b/src/SessionInterface.php @@ -9,9 +9,9 @@ namespace BradyMcD\TAATP; interface SessionInterface { function live(): bool; - function try_store(string $key, mixed $val): bool; + function tryStore(string $key, mixed $val): bool; function store(string $key, mixed $val); function get(string $key); } -?> \ No newline at end of file +?> diff --git a/src/URI/otpauth.php b/src/URI/otpauth.php index a7aca86..938a7a8 100644 --- a/src/URI/otpauth.php +++ b/src/URI/otpauth.php @@ -111,7 +111,7 @@ class Otpauth return new self($issuer, $user, $secret, $algo); } - public function emit_str(): string + public function emitStr(): string { $label = $this->provider . ":" . $this->userid; $provider = "provider=" . $this->provider; diff --git a/src/Workflow/Authenticate.php b/src/Workflow/Authenticate.php index c2dbb7b..9af1021 100644 --- a/src/Workflow/Authenticate.php +++ b/src/Workflow/Authenticate.php @@ -15,7 +15,7 @@ class Authenticate implements WorkflowInterface private RequestInterface $ri, private AntiCSRFInterface $csrf, private SessionInterface $session, - private mixed $user_index, + private mixed $userIndex, private HashInterface $hash, ) { @@ -23,20 +23,20 @@ class Authenticate implements WorkflowInterface function display() { - echo $this->emit_str(); + echo $this->emitStr(); } - function emit_str(): string + function emitStr(): string { $html = "
"; - $html .= $this->csrf->emit_str(); + $html .= $this->csrf->emitStr(); $html .= "

Please enter the code showing on your authenticator

"; $html .= ""; $html .= ""; $html .= "
"; $values = [ - "%frm" => $this->ri->form_props("authenticate"), + "%frm" => $this->ri->formProps("authenticate"), ]; return \str_replace(\array_keys($values), $values, $html); @@ -49,22 +49,22 @@ class Authenticate implements WorkflowInterface return false; } - $p_uri = $this->db->get_secret($this->user_index); + $p_uri = $this->db->getSecret($this->userIndex); $totp = _6238( $p_uri.secret, $p_uri.period, - $this->db->get_last_time($this->user_index), + $this->db->getLastTime($this->userIndex), 2, $p_uri.digits, $this->clock, $this->hash ); - $flag = $totp.validate($this->ri->get_resp("totp_challenge")); + $flag = $totp.validate($this->ri->getResp("totp_challenge")); if($flag) { - $this->db->store_last_time($this->user_index, $flag); + $this->db->storeLastTime($this->userIndex, $flag); return true; } return false; diff --git a/src/Workflow/UserManagement.php b/src/Workflow/UserManagement.php index ca4f521..7463da4 100644 --- a/src/Workflow/UserManagement.php +++ b/src/Workflow/UserManagement.php @@ -17,7 +17,7 @@ class UserManagement implements WorkflowInterface private RequestInterface $ri, private AntiCSRFInterface $csrf, private SessionInterface $session, - private mixed $user_index, + private mixed $userIndex, private HashInterface $hash, ) { @@ -25,62 +25,62 @@ class UserManagement implements WorkflowInterface function display() { - echo $this->emit_str(); + echo $this->emitStr(); } - private function view_enroll_form(): string + private function viewEnrollForm(): string { $html = "
"; - $html .= $this->csrf->emit_str(); + $html .= $this->csrf->emitStr(); $html .= "

To add an authenticator to your account, scan the QR code

"; $html .= "\"qr-code\""; - $html .= "" + $html .= ""; $html .= ""; $html .= ""; $html .= "
"; - $provisioning_uri = (new Otpauth( + $provisioningUri = (new Otpauth( "taatp", $this->userIndex, $this->hash->keygen(), "SHA1", 30, 6 - ))->emit_str(); - $this->session->store("secret", $provisioning_uri); + ))->emitStr(); + $this->session->store("secret", $provisioningUri); $values = [ - "%frm" => $this->ri->form_props("enroll"), - "%qr" => (new QRCode)->render($provisioning_uri), - ] + "%frm" => $this->ri->formProps("enroll"), + "%qr" => (new QRCode)->render($provisioningUri), + ]; return \str_replace(\array_keys($values), $values, $html); } - private function view_unenroll_form(): string + private function viewUnenrollForm(): string { $html = "
"; - $html .= $this->csrf->emit_str(); + $html .= $this->csrf->emitStr(); $html .= ""; $html .= ""; $html .= ""; $html .= "
"; $values = [ - "%frm" => $this->ri->form_props("unenroll"), + "%frm" => $this->ri->formProps("unenroll"), ]; return \str_replace(\array_keys($values), $values, $html); } - function emit_str(): string + function emitStr(): string { - if (\is_null($this->db->get_secret($this->user_index))) + if (\is_null($this->db->getSecret($this->userIndex))) { - $this->view_enroll_form(); + $this->viewEnrollForm(); } else { - $this->view_unenroll_form(); + $this->viewUnenrollForm(); } } @@ -91,30 +91,30 @@ class UserManagement implements WorkflowInterface return false; } - $p_uri = $this->db->get_secret($this->user_index); + $p_uri = $this->db->getSecret($this->userIndex); $enroll_flag = \is_null($p_uri); $enroll_flag && $enroll_flag = $this->session->get('secret'); $totp = _6238( $p_uri.secret, $p_uri.period, - $enroll_flag? 0:$this->db->get_last_time($this->user_index), + $enroll_flag? 0:$this->db->getLastTime($this->userIndex), 2, $p_uri.digits, $this->clock, $this->hash ); - $flag = $totp.validate($this->ri->get_resp("totp_challenge")); + $flag = $totp.validate($this->ri->getResp("totp_challenge")); if($flag && $enroll_flag) { - $this->db->store_last_time($this->user_index, $flag); - $this->db->store_secret($this->user_index, $p_uri); + $this->db->storeLastTime($this->userIndex, $flag); + $this->db->storeSecret($this->userIndex, $p_uri); return true; } else if($flag) { - $this->db->strip_secret($this->user_index); + $this->db->stripSecret($this->userIndex); return true; } return false; diff --git a/src/WorkflowInterface.php b/src/WorkflowInterface.php index bf890c4..0ee0172 100644 --- a/src/WorkflowInterface.php +++ b/src/WorkflowInterface.php @@ -13,7 +13,7 @@ interface WorkflowInterface * returns the workflow's relevant form. * @return string */ - public function emit_str():string; + public function emitStr():string; /** * Handles any response given to the workflow's form. * @return bool