Changeset View
Changeset View
Standalone View
Standalone View
src/include/Kolab2FA/Driver/HOTP.php
Show First 20 Lines • Show All 52 Lines • ▼ Show 20 Lines | public function init($config) | ||||
'type' => 'integer', | 'type' => 'integer', | ||||
'editable' => false, | 'editable' => false, | ||||
'hidden' => true, | 'hidden' => true, | ||||
'generator' => 'random_counter', | 'generator' => 'random_counter', | ||||
), | ), | ||||
); | ); | ||||
// copy config options | // copy config options | ||||
$this->backend = new \Kolab2FA\OTP\HOTP(); | $this->backend = \OTPHP\HOTP::create( | ||||
$this->backend | null, | ||||
->setDigits($this->config['digits']) | 0, | ||||
->setDigest($this->config['digest']) | $this->config['digest'], | ||||
->setIssuer($this->config['issuer']) | $this->config['digits'] | ||||
->setIssuerIncludedAsParameter(true); | ); | ||||
$this->backend->setIssuer($this->config['issuer']); | |||||
$this->backend->setIssuerIncludedAsParameter(true); | |||||
} | } | ||||
/** | /** | ||||
* | * | ||||
*/ | */ | ||||
public function verify($code, $timestamp = null) | public function verify($code, $timestamp = null) | ||||
{ | { | ||||
// get my secret from the user storage | // get my secret from the user storage | ||||
$secret = $this->get('secret'); | $secret = $this->get('secret'); | ||||
$counter = $this->get('counter'); | $counter = (int) $this->get('counter'); | ||||
if (!strlen($secret)) { | if (!strlen($secret)) { | ||||
// LOG: "no secret set for user $this->username" | // LOG: "no secret set for user $this->username" | ||||
// rcube::console("VERIFY HOTP: no secret set for user $this->username"); | // rcube::console("VERIFY HOTP: no secret set for user $this->username"); | ||||
return false; | return false; | ||||
} | } | ||||
try { | try { | ||||
$this->backend->setLabel($this->username)->setSecret($secret)->setCounter(intval($this->get('counter'))); | $this->backend->setLabel($this->username); | ||||
$this->backend->setSecret($secret); | |||||
$this->backend->setParameter('counter', $counter); | |||||
$pass = $this->backend->verify($code, $counter, $this->config['window']); | $pass = $this->backend->verify($code, $counter, $this->config['window']); | ||||
// store incremented counter value | // store incremented counter value | ||||
$this->set('counter', $this->backend->getCounter()); | $this->set('counter', $this->backend->getCounter()); | ||||
$this->commit(); | $this->commit(); | ||||
} | } | ||||
catch (\Exception $e) { | catch (\Exception $e) { | ||||
// LOG: exception | // LOG: exception | ||||
Show All 15 Lines | public function get_provisioning_uri() | ||||
$this->set('secret', $this->get('secret', true)); | $this->set('secret', $this->get('secret', true)); | ||||
$this->set('counter', $this->get('counter', true)); | $this->set('counter', $this->get('counter', true)); | ||||
$this->set('created', $this->get('created', true)); | $this->set('created', $this->get('created', true)); | ||||
$this->commit(); | $this->commit(); | ||||
} | } | ||||
// TODO: deny call if already active? | // TODO: deny call if already active? | ||||
$this->backend->setLabel($this->username)->setSecret($this->secret)->setCounter(intval($this->get('counter'))); | $this->backend->setLabel($this->username); | ||||
$this->backend->setSecret($this->secret); | |||||
$this->backend->setParameter('counter', (int) $this->get('counter')); | |||||
return $this->backend->getProvisioningUri(); | return $this->backend->getProvisioningUri(); | ||||
} | } | ||||
/** | /** | ||||
* Generate a random counter value | * Generate a random counter value | ||||
*/ | */ | ||||
public function random_counter() | public function random_counter() | ||||
{ | { | ||||
return mt_rand(1, 999); | return mt_rand(1, 999); | ||||
} | } | ||||
} | } |