From a350d7dbec716ce4cc817d09fca5cf4968effd7e Mon Sep 17 00:00:00 2001 From: Olov Danielson Date: Mon, 18 Jan 2010 10:06:03 +0000 Subject: [PATCH] Added systemtest functionality. System tests unders systemtests/ --- systemtests/setupTest.php | 60 +++++++++++++++++++ ykval-otpgen.php | 118 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 178 insertions(+) create mode 100644 systemtests/setupTest.php create mode 100644 ykval-otpgen.php diff --git a/systemtests/setupTest.php b/systemtests/setupTest.php new file mode 100644 index 0000000..ff761c7 --- /dev/null +++ b/systemtests/setupTest.php @@ -0,0 +1,60 @@ +yubi = &new Auth_Yubico('1', null); + $this->yubi->setURLPart("api2.yubico.com/wsapi/verify"); + } + + function microtime_float() + { + list($usec, $sec) = explode(" ", microtime()); + return ((float)$usec + (float)$sec); + } + + function testStandardValidation() + { + $myKey=new otpgen("mysql:dbname=ykval_systemtest;host=127.0.0.1", + "ykval-systester", + "lab", + array(), + "ykval-systemtest", + "ccccccccgchv"); + $otp=$myKey->getOtp(); + $this->assertTrue(is_string($otp), "getOtp should return a string"); + $this->assertEquals(44, strlen($otp), "OTP should have length 32"); + + $auth=$this->yubi->verify($otp); + + + if (PEAR::isError($auth)) { + echo "\nERROR MESSAGE IS " . $auth->getMessage() . "\n"; + } + $this->assertFalse(PEAR::isError($auth), "An error should not have been raised by this OTP."); + + $validation_pool=array("api3.yubico.com/wsapi/verify", + "api4.yubico.com/wsapi/verify", + "api5.yubico.com/wsapi/verify"); + + // We except the calls to these to fail with replayed_otp. + + foreach ($validation_pool as $server){ + $this->yubi->setURLPart($server); + $auth=$this->yubi->verify($otp); + $this->assertTrue(PEAR::isError($auth), "An error should have been raised by this OTP."); + $this->assertEquals("REPLAYED_OTP", $auth->getMessage(), "OTP should be reported as replayed."); + } + } + +} +?> \ No newline at end of file diff --git a/ykval-otpgen.php b/ykval-otpgen.php new file mode 100644 index 0000000..bd3f548 --- /dev/null +++ b/ykval-otpgen.php @@ -0,0 +1,118 @@ + + * @copyright 2010 Yubico + * @license http://opensource.org/licenses/bsd-license.php New BSD License + * @link http://www.yubico.com/ + * @link http://code.google.com/p/yubikey-val-server-php/ + */ + +class OtpGen +{ + + + public function __construct($db_dsn, $db_username, $db_password, $db_options, $name='ykval-otpgen', $yk_publicname) + { + + $this->myLog = new Log($name); + $this->db=new Db($db_dsn, $db_username, $db_password, $db_options, $name . ':db'); + $this->isConnected=$this->db->connect(); + + // First obtain private ID and AES-key + if($yubikey=$this->db->findBy('yubikeys', 'yk_publicname', $yk_publicname, 1)) { + $this->yk_internalname=$yubikey['yk_internalname']; + $this->yk_aeskey=$yubikey['yk_aeskey']; + } else { + $this->myLog->log(LOG_WARNING, 'Failed to obtain data for yubikey ' . $yk_publicname); + } + + + $this->yk_publicname = $yk_publicname; + $this->yk_counter = $this->stepYkCounter(); + $this->yk_use = 0; + $this->yk_low = rand(0,65535); + $this->yk_high = rand(0,255); + // Store start time as well so we can step yk_low, yk_high correctly + $this->start_time=time(); + + } + + + public function getOtp() + { + # TODO. Add the rest of the values to string and execute. ! + $execstring=sprintf("ykgenerate %s %s %04x %04x %02x %02x" , + $this->yk_aeskey, + $this->yk_internalname, + $this->yk_counter, + $this->yk_low, + $this->yk_high, + $this->yk_use++); + if ($this->yk_use>=256) { + $this->yk_use=0; + $this->yk_counter=$this->stepYkCounter(); + } + echo $execstring . "\n"; + $otp=system($execstring); + return $this->yk_publicname . $otp; + } + + + private function stepYkCounter() + { + if ($this->yk_publicname) { + if($yubikey=$this->db->findBy('yubikeys', 'yk_publicname', $this->yk_publicname, 1)) { + $new_counter = $yubikey['yk_counter'] + 1; + if ($this->db->updateBy('yubikeys', + 'yk_publicname', + $this->yk_publicname, + array('yk_counter'=>$new_counter))) { + $this->myLog->log(LOG_NOTICE, "Yubikey " . $this->yk_publicname . " stepped counter value to " . $new_counter); + return $new_counter; + } else { + $this->myLog->log(LOG_WARNING, "Failed to update counter value for yubikey " . $this->yk_publicname); + } + } else { + $this->myLog->log(LOG_WARNING, "Failed to get data for yubikey " . $this->yk_publicname); + } + } else { + $this->myLog->log(LOG_WARNING, "yk_publicname not set up correctly for class ykval-otpgen.php. We shouldn't be here."); + } + return false; + } +} + +