Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/Provider/TotpProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace OCA\TwoFactorTOTP\Provider;

use OCA\TwoFactorTOTP\AppInfo\Application;
use OCA\TwoFactorTOTP\Exception\NoTotpSecretFoundException;
use OCA\TwoFactorTOTP\Service\ITotp;
use OCA\TwoFactorTOTP\Settings\Personal;
use OCP\AppFramework\IAppContainer;
Expand Down Expand Up @@ -70,7 +71,12 @@ public function getTemplate(IUser $user): Template {
*/
public function verifyChallenge(IUser $user, string $challenge): bool {
$challenge = preg_replace('/[^0-9]/', '', $challenge);
return $this->totp->validateSecret($user, $challenge);
try {
$secret = $this->totp->getSecret($user);
} catch (NoTotpSecretFoundException $e) {
return false;
}
return $this->totp->validateSecret($secret, $challenge);
}

/**
Expand Down
8 changes: 7 additions & 1 deletion lib/Service/ITotp.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace OCA\TwoFactorTOTP\Service;

use OCA\TwoFactorTOTP\Db\TotpSecret;
use OCA\TwoFactorTOTP\Exception\NoTotpSecretFoundException;
use OCA\TwoFactorTOTP\Exception\TotpSecretAlreadySet;
use OCP\AppFramework\Db\DoesNotExistException;
Expand All @@ -33,6 +34,11 @@ public function hasSecret(IUser $user): bool;
*/
public function createSecret(IUser $user): string;

/**
* @throws NoTotpSecretFoundException
*/
public function getSecret(IUser $user): TotpSecret;

/**
* Enable OTP for the given user. The secret has to be generated
* beforehand, using ITotp::createSecret
Expand All @@ -47,5 +53,5 @@ public function enable(IUser $user, $key): bool;

public function deleteSecret(IUser $user, bool $byAdmin = false): void;

public function validateSecret(IUser $user, string $key): bool;
public function validateSecret(TotpSecret $secret, string $key): bool;
}
34 changes: 20 additions & 14 deletions lib/Service/Totp.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,23 @@ public function createSecret(IUser $user): string {
return $secret;
}

public function getSecret(IUser $user): TotpSecret {
try {
return $this->secretMapper->getSecret($user);
} catch (DoesNotExistException $e) {
throw new NoTotpSecretFoundException(
$e->getMessage(),
$e->getCode(),
$e,
);
}
}

public function enable(IUser $user, $key): bool {
if (!$this->validateSecret($user, $key)) {
$dbSecret = $this->secretMapper->getSecret($user);
if (!$this->validateSecret($dbSecret, $key)) {
return false;
}
$dbSecret = $this->secretMapper->getSecret($user);
$dbSecret->setState(ITotp::STATE_ENABLED);
$this->secretMapper->update($dbSecret);

Expand All @@ -99,26 +111,20 @@ public function deleteSecret(IUser $user, bool $byAdmin = false): void {
}
}

public function validateSecret(IUser $user, string $key): bool {
try {
$dbSecret = $this->secretMapper->getSecret($user);
} catch (DoesNotExistException $ex) {
throw new NoTotpSecretFoundException();
}

$secret = $this->crypto->decrypt($dbSecret->getSecret());
$otp = Factory::getTOTP(Base32::decode($secret), 30, 6);
public function validateSecret(TotpSecret $secret, string $key): bool {
$decryptedSecret = $this->crypto->decrypt($secret->getSecret());
$otp = Factory::getTOTP(Base32::decode($decryptedSecret), 30, 6);

$counter = null;
$lastCounter = $dbSecret->getLastCounter();
$lastCounter = $secret->getLastCounter();
if ($lastCounter !== -1) {
$counter = $lastCounter;
}

$result = $otp->verify($key, 3, $counter);
if ($result instanceof TOTPValidResultInterface) {
$dbSecret->setLastCounter($result->getCounter());
$this->secretMapper->update($dbSecret);
$secret->setLastCounter($result->getCounter());
$this->secretMapper->update($secret);

return true;
}
Expand Down
31 changes: 31 additions & 0 deletions tests/Unit/Provider/TotpProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
namespace OCA\TwoFactorTOTP\Test\Unit\Provider;

use ChristophWurst\Nextcloud\Testing\TestCase;
use OCA\TwoFactorTOTP\Db\TotpSecret;
use OCA\TwoFactorTOTP\Exception\NoTotpSecretFoundException;
use OCA\TwoFactorTOTP\Provider\AtLoginProvider;
use OCA\TwoFactorTOTP\Provider\TotpProvider;
use OCA\TwoFactorTOTP\Service\ITotp;
Expand Down Expand Up @@ -128,6 +130,35 @@ public function testGetPersonalSettings(): void {
self::assertEquals($expected, $actual);
}

public function testVerifyChallengeSecretNotFound(): void {
$user = $this->createMock(IUser::class);
$this->totp->expects($this->once())
->method('getSecret')
->with($user)
->willThrowException(new NoTotpSecretFoundException());

$result = $this->provider->verifyChallenge($user, '123456');

$this->assertFalse($result);
}

public function testVerifyChallengeStripNonDigits(): void {
$user = $this->createMock(IUser::class);
$secret = new TotpSecret();
$this->totp->expects(self::once())
->method('getSecret')
->with($user)
->willReturn($secret);
$this->totp->expects(self::once())
->method('validateSecret')
->with($secret, '123456')
->willReturn(true);

$result = $this->provider->verifyChallenge($user, ' 123456 a ');

$this->assertTrue($result);
}

public function testDeactivate(): void {
$user = $this->createMock(IUser::class);
$this->totp->expects($this->once())
Expand Down