From 9b7b9d05c20f187dbf6cc2d9884c88adc6a621e3 Mon Sep 17 00:00:00 2001 From: Shane Mc Cormack Date: Sun, 10 Mar 2019 23:55:13 +0000 Subject: [PATCH] Authy does technically also validate as a regular code... #35 --- admin/init_functions.php | 8 ++++++++ classes/twofactorkey.php | 31 ++++++++++++++++++++++++++++--- web/1.0/index.php | 2 +- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/admin/init_functions.php b/admin/init_functions.php index 252283d..d8dc00b 100644 --- a/admin/init_functions.php +++ b/admin/init_functions.php @@ -431,6 +431,14 @@ public function run($pdo) { $dataChanges[26] = new DBChange(<< 0, 'expires' => 0, 'active' => false, + 'code' => true, 'push' => false, 'type' => 'rfc6238', 'onetime' => false, @@ -158,6 +159,10 @@ public function setType($value) { return $this->setData('type', strtolower($value)); } + public function setCode($value) { + return $this->setData('code', parseBool($value) ? 'true' : 'false'); + } + public function setPush($value) { return $this->setData('push', parseBool($value) ? 'true' : 'false'); } @@ -210,6 +215,10 @@ public function getType() { return $this->getData('type'); } + public function isCode() { + return parseBool($this->getData('code')); + } + public function isPush() { return parseBool($this->getData('push')); } @@ -304,8 +313,8 @@ public function validate() { } public function verify($code, $discrepancy = 1) { - // Push-Based tokens don't verify with a code. - if ($this->isPush()) { return FALSE; } + // Only allow code-based tokens. + if (!$this->isCode()) { return FALSE; } $type = $this->getType(); switch ($type) { @@ -318,6 +327,9 @@ public function verify($code, $discrepancy = 1) { case "yubikeyotp": return $this->verify_yubikey($code); + case "authy": + return $this->verify_authycode($code); + default: throw new Exception('Unknown key type: ' . $type); } @@ -419,8 +431,21 @@ private function verify_authypush($message) { return FALSE; } - public function postDelete() { + private function verify_authycode($code) { global $config; + if (!self::canUseAuthy()) { return FALSE; } + + $authy_api = new Authy\AuthyApi($config['twofactor']['authy']['apikey']); + + $verification = $authy_api->verifyToken($this->getKey(), $code); + + return $verification->ok(); + } + + public function postDelete($result) { + global $config; + + if (!$result) { return; } if ($this->getType() == 'authy' && self::canUseAuthy()) { $authy_api = new Authy\AuthyApi($config['twofactor']['authy']['apikey']); diff --git a/web/1.0/index.php b/web/1.0/index.php index f9660c9..3868d6e 100644 --- a/web/1.0/index.php +++ b/web/1.0/index.php @@ -186,7 +186,7 @@ if ($testCode !== NULL) { foreach ($keys as $key) { - if (!$key->isPush() && $key->verify($testCode, 1)) { + if ($key->isCode() && $key->verify($testCode, 1)) { $valid = true; $key->setLastUsed(time())->save();