Skip to content

Commit

Permalink
fix(migration): rename 'period' column to 'seconds' to avoid MariaDB …
Browse files Browse the repository at this point in the history
…syntax conflict

Signed-off-by: ernolf <raphael.gradenwitz@googlemail.com>
  • Loading branch information
ernolf committed Aug 14, 2024
1 parent 75c29c2 commit 4f05f3f
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
8 changes: 4 additions & 4 deletions lib/Db/TotpSecret.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
* @method void setAlgorithm(int $algorithm)
* @method int getDigits()
* @method void setDigits(int $length)
* @method int getPeriod()
* @method void setPeriod(int $algorithm)
* @method int getSeconds()
* @method void setSeconds(int $seconds)
*/
class TotpSecret extends Entity {

Expand All @@ -63,7 +63,7 @@ class TotpSecret extends Entity {
protected $digits;

/** @var int */
protected $period;
protected $seconds;

public function __construct() {
$this->addType('userId', 'string');
Expand All @@ -72,6 +72,6 @@ public function __construct() {
$this->addType('lastCounter', 'int');
$this->addType('algorithm', 'int');
$this->addType('digits', 'int');
$this->addType('period', 'int');
$this->addType('seconds', 'int');
}
}
2 changes: 1 addition & 1 deletion lib/Db/TotpSecretMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function getSecret(IUser $user): TotpSecret {
/* @var $qb IQueryBuilder */
$qb = $this->db->getQueryBuilder();

$qb->select('id', 'user_id', 'secret', 'state', 'last_counter', 'algorithm', 'digits', 'period')
$qb->select('id', 'user_id', 'secret', 'state', 'last_counter', 'algorithm', 'digits', 'seconds')
->from($this->getTableName())
->from('twofactor_totp_secrets')
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user->getUID())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

class Version12000Date20240722120051 extends SimpleMigrationStep {
class Version120000Date20240722120051 extends SimpleMigrationStep {

/**
* @param IOutput $output
Expand All @@ -58,7 +58,7 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
'notnull' => true,
'default' => 6,
]);
$table->addColumn('period', Types::INTEGER, [
$table->addColumn('seconds', Types::INTEGER, [
'notnull' => true,
'default' => 30,
]);
Expand Down
14 changes: 7 additions & 7 deletions lib/Service/Totp.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,11 @@ public function getDigits(IUser $user): int {
public function getPeriod(IUser $user): int {
try {
$secret = $this->secretMapper->getSecret($user);
$digits = (int)$secret->getPeriod();
$this->logger->debug("Period in seconds from secret: " . $digits);
return $digits;
$period = (int)$secret->getSeconds();
$this->logger->debug("Period in seconds from secret: " . $period);
return $period;
} catch (DoesNotExistException $ex) {
$this->logger->debug("Period in seconds not found, defaulting to " . ITotp::DEFAULT_DIGITS);
$this->logger->debug("Period in seconds not found, defaulting to " . ITotp::DEFAULT_PERIOD);
return ITotp::DEFAULT_PERIOD; // Default value
}
}
Expand All @@ -170,7 +170,7 @@ public function updateSettings(IUser $user, string $customSecret = null, int $al
$dbSecret = $this->secretMapper->getSecret($user);
$dbSecret->setAlgorithm($algorithm);
$dbSecret->setDigits($digits);
$dbSecret->setPeriod($period);
$dbSecret->setSeconds($period);
if ($customSecret !== null) {
$dbSecret->setSecret($this->crypto->encrypt($customSecret));
}
Expand Down Expand Up @@ -224,10 +224,10 @@ public function createSecret(IUser $user, string $customSecret = null, int $algo
$dbSecret->setState(ITotp::STATE_CREATED);
$dbSecret->setAlgorithm($algorithm);
$dbSecret->setDigits($digits);
$dbSecret->setPeriod($period);
$dbSecret->setSeconds($period);

$this->secretMapper->insert($dbSecret);
$this->logger->debug("Created new secret for user {$user->getUID()} with Token Length: $digits and Hash Algorithm: $algorithm");
$this->logger->debug("Created new secret for user {$user->getUID()} with Hash Algorithm: $algorithm Token Length: $digits and Period: $period");
return $secret;
}

Expand Down

0 comments on commit 4f05f3f

Please sign in to comment.