PDF rausgenommen
This commit is contained in:
@ -0,0 +1,87 @@
|
||||
<?php
|
||||
/**
|
||||
* Matomo - free/libre analytics platform
|
||||
*
|
||||
* @link https://matomo.org
|
||||
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
|
||||
*/
|
||||
namespace Piwik\Plugins\TwoFactorAuth\Dao;
|
||||
|
||||
use Piwik\Common;
|
||||
use Piwik\Db;
|
||||
|
||||
class RecoveryCodeDao
|
||||
{
|
||||
protected $table = 'twofactor_recovery_code';
|
||||
protected $tablePrefixed = '';
|
||||
|
||||
/**
|
||||
* @var RecoveryCodeRandomGenerator $generator
|
||||
*/
|
||||
private $generator;
|
||||
|
||||
public function __construct(RecoveryCodeRandomGenerator $generator)
|
||||
{
|
||||
$this->tablePrefixed = Common::prefixTable($this->table);
|
||||
$this->generator = $generator;
|
||||
}
|
||||
|
||||
public function getPrefixedTableName()
|
||||
{
|
||||
return $this->tablePrefixed;
|
||||
}
|
||||
|
||||
public function createRecoveryCodesForLogin($login)
|
||||
{
|
||||
$codes = array();
|
||||
$this->deleteAllRecoveryCodesForLogin($login);
|
||||
|
||||
for ($i = 0; $i < 10; $i++) {
|
||||
$code = $this->generator->generateCode();
|
||||
$code = Common::mb_strtoupper($code);
|
||||
$this->insertRecoveryCode($login, $code);
|
||||
$codes[] = $code;
|
||||
}
|
||||
return $codes;
|
||||
}
|
||||
|
||||
public function insertRecoveryCode($login, $recoveryCode)
|
||||
{
|
||||
// we do not really care about duplicates as it is very unlikely to happen, that's why we don't even use a
|
||||
// unique login,recovery_code index
|
||||
$sql = sprintf('INSERT INTO %s (`login`, `recovery_code`) VALUES(?,?)', $this->tablePrefixed);
|
||||
Db::query($sql, array($login, $recoveryCode));
|
||||
}
|
||||
|
||||
public function useRecoveryCode($login, $recoveryCode)
|
||||
{
|
||||
if ($this->deleteRecoveryCode($login, $recoveryCode)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getAllRecoveryCodesForLogin($login)
|
||||
{
|
||||
$sql = sprintf('SELECT recovery_code FROM %s WHERE login = ?', $this->tablePrefixed);
|
||||
$rows = Db::fetchAll($sql, array($login));
|
||||
$codes = array_column($rows, 'recovery_code');
|
||||
return $codes;
|
||||
}
|
||||
|
||||
public function deleteRecoveryCode($login, $recoveryCode)
|
||||
{
|
||||
$sql = sprintf('DELETE FROM %s WHERE login = ? and recovery_code = ?', $this->tablePrefixed);
|
||||
$query = Db::query($sql, array($login, $recoveryCode));
|
||||
return $query->rowCount();
|
||||
}
|
||||
|
||||
public function deleteAllRecoveryCodesForLogin($login)
|
||||
{
|
||||
$query = sprintf('DELETE FROM %s WHERE login = ?', $this->tablePrefixed);
|
||||
|
||||
Db::query($query, array($login));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
/**
|
||||
* Matomo - free/libre analytics platform
|
||||
*
|
||||
* @link https://matomo.org
|
||||
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
|
||||
*/
|
||||
namespace Piwik\Plugins\TwoFactorAuth\Dao;
|
||||
|
||||
use Piwik\Common;
|
||||
|
||||
class RecoveryCodeRandomGenerator
|
||||
{
|
||||
public function generateCode()
|
||||
{
|
||||
return Common::getRandomString(16);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
/**
|
||||
* Matomo - free/libre analytics platform
|
||||
*
|
||||
* @link https://matomo.org
|
||||
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
|
||||
*/
|
||||
namespace Piwik\Plugins\TwoFactorAuth\Dao;
|
||||
|
||||
class RecoveryCodeStaticGenerator extends RecoveryCodeRandomGenerator
|
||||
{
|
||||
private $index = 10;
|
||||
|
||||
public function generateCode()
|
||||
{
|
||||
$this->index++;
|
||||
return str_pad($this->index, 16, '0');
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
/**
|
||||
* Matomo - free/libre analytics platform
|
||||
*
|
||||
* @link https://matomo.org
|
||||
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
|
||||
*/
|
||||
namespace Piwik\Plugins\TwoFactorAuth\Dao;
|
||||
|
||||
require_once PIWIK_DOCUMENT_ROOT . '/libs/Authenticator/TwoFactorAuthenticator.php';
|
||||
|
||||
class TwoFaSecretRandomGenerator
|
||||
{
|
||||
public function generateSecret()
|
||||
{
|
||||
$authenticator = new \TwoFactorAuthenticator();
|
||||
return $authenticator->createSecret(16);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
/**
|
||||
* Matomo - free/libre analytics platform
|
||||
*
|
||||
* @link https://matomo.org
|
||||
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
|
||||
*/
|
||||
namespace Piwik\Plugins\TwoFactorAuth\Dao;
|
||||
|
||||
class TwoFaSecretStaticGenerator extends TwoFaSecretRandomGenerator
|
||||
{
|
||||
public function generateSecret()
|
||||
{
|
||||
return str_pad('1', 16, '1');
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user