2019-07-28 23:00:30 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This Software is the property of Data Development and is protected
|
|
|
|
* by copyright law - it is NOT Freeware.
|
|
|
|
* Any unauthorized use of this software without a valid license
|
|
|
|
* is a violation of the license agreement and will be prosecuted by
|
|
|
|
* civil and criminal law.
|
|
|
|
* http://www.shopmodule.com
|
|
|
|
*
|
|
|
|
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
|
|
|
|
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
|
|
|
|
* @link http://www.oxidmodule.com
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace D3\Totp\Application\Model;
|
|
|
|
|
2019-07-31 22:43:34 +02:00
|
|
|
use OxidEsales\Eshop\Application\Model\User;
|
2019-07-28 23:00:30 +02:00
|
|
|
use OxidEsales\Eshop\Core\DatabaseProvider;
|
|
|
|
use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException;
|
|
|
|
use OxidEsales\Eshop\Core\Model\BaseModel;
|
2019-07-31 22:43:34 +02:00
|
|
|
use OxidEsales\Eshop\Core\Registry;
|
2019-07-28 23:00:30 +02:00
|
|
|
use RandomLib\Factory;
|
|
|
|
use RandomLib\Generator;
|
|
|
|
|
|
|
|
class d3backupcode extends BaseModel
|
|
|
|
{
|
|
|
|
protected $_sCoreTable = 'd3totp_backupcodes';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $sUserId
|
|
|
|
* @return string
|
|
|
|
* @throws DatabaseConnectionException
|
|
|
|
*/
|
|
|
|
public function generateCode($sUserId)
|
|
|
|
{
|
|
|
|
$factory = new Factory();
|
|
|
|
$generator = $factory->getLowStrengthGenerator();
|
|
|
|
|
|
|
|
$sCode = $generator->generateString(6, Generator::CHAR_DIGITS);
|
|
|
|
$this->assign(
|
2019-08-02 00:23:21 +02:00
|
|
|
[
|
2019-07-28 23:00:30 +02:00
|
|
|
'oxuserid' => $sUserId,
|
|
|
|
'backupcode' => $this->d3EncodeBC($sCode),
|
2019-08-02 00:23:21 +02:00
|
|
|
]
|
2019-07-28 23:00:30 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
return $sCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $code
|
|
|
|
* @return false|string
|
|
|
|
* @throws DatabaseConnectionException
|
|
|
|
*/
|
|
|
|
public function d3EncodeBC($code)
|
|
|
|
{
|
|
|
|
$oDb = DatabaseProvider::getDb();
|
2019-07-31 22:43:34 +02:00
|
|
|
$salt = $this->d3GetUser()->getFieldData('oxpasssalt');
|
2019-07-28 23:00:30 +02:00
|
|
|
$sSelect = "SELECT BINARY MD5( CONCAT( " . $oDb->quote($code) . ", UNHEX( ".$oDb->quote($salt)." ) ) )";
|
|
|
|
|
|
|
|
return $oDb->getOne($sSelect);
|
|
|
|
}
|
2019-07-31 22:43:34 +02:00
|
|
|
|
|
|
|
public function d3GetUser()
|
|
|
|
{
|
|
|
|
if ($this->getUser()) {
|
|
|
|
return $this->getUser();
|
|
|
|
}
|
|
|
|
|
2019-08-02 00:23:21 +02:00
|
|
|
$sUserId = Registry::getSession()->getVariable(d3totp::TOTP_SESSION_CURRENTUSER);
|
2019-07-31 22:43:34 +02:00
|
|
|
$oUser = oxNew(User::class);
|
|
|
|
$oUser->load($sUserId);
|
|
|
|
return $oUser;
|
|
|
|
}
|
2019-07-28 23:00:30 +02:00
|
|
|
}
|