Skip to content
Open
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
4 changes: 4 additions & 0 deletions core/Controller/TwoFactorChallengeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ public function showChallenge(string $challengeProviderId, ?string $redirect_url
$backupProvider = null;
}

[$regularProviders] = $this->splitProvidersAndBackupCodes($providerSet->getProviders());
$otherProviders = array_filter($regularProviders, fn (IProvider $p) => $p->getId() !== $provider->getId());

$errorMessage = '';
$error = false;
if ($this->session->exists('two_factor_auth_error')) {
Expand All @@ -128,6 +131,7 @@ public function showChallenge(string $challengeProviderId, ?string $redirect_url
'error_message' => $errorMessage,
'provider' => $provider,
'backupProvider' => $backupProvider,
'hasOtherProviders' => $otherProviders !== [],
'logout_url' => $this->getLogoutUrl(),
'redirect_url' => $redirect_url,
'template' => $tmpl->fetchPage(),
Expand Down
11 changes: 11 additions & 0 deletions core/templates/twofactorshowchallenge.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@
<?php endif; ?>
<?php endif; ?>
<?php print_unescaped($template); ?>
<?php if ($_['hasOtherProviders']): ?>
<p>
<a class="two-factor-secondary" href="<?php p(\OCP\Server::get(\OCP\IURLGenerator::class)->linkToRoute('core.TwoFactorChallenge.selectChallenge',
[
'redirect_url' => $_['redirect_url'],
]
)) ?>">
<?php p($l->t('Use another method')) ?>
</a>
</p>
<?php endif; ?>
<?php if (!is_null($_['backupProvider'])): ?>
<p>
<a class="two-factor-secondary" href="<?php p(\OCP\Server::get(\OCP\IURLGenerator::class)->linkToRoute('core.TwoFactorChallenge.showChallenge',
Expand Down
41 changes: 35 additions & 6 deletions tests/Core/Controller/TwoFactorChallengeControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use OCP\IUser;
use OCP\IUserSession;
use OCP\Template\ITemplate;
use PHPUnit\Framework\Attributes\DataProvider;
use Psr\Log\LoggerInterface;
use Test\TestCase;

Expand Down Expand Up @@ -126,12 +127,6 @@ public function testShowChallenge(): void {
->method('getProviderSet')
->with($user)
->willReturn($providerSet);
$provider->expects($this->once())
->method('getId')
->willReturn('u2f');
$backupProvider->expects($this->once())
->method('getId')
->willReturn('backup_codes');

$this->session->expects($this->once())
->method('exists')
Expand All @@ -152,6 +147,7 @@ public function testShowChallenge(): void {
'error' => true,
'provider' => $provider,
'backupProvider' => $backupProvider,
'hasOtherProviders' => false,
'logout_url' => 'logoutAttribute',
'template' => '<html/>',
'redirect_url' => '/re/dir/ect/url',
Expand All @@ -161,6 +157,39 @@ public function testShowChallenge(): void {
$this->assertEquals($expected, $this->controller->showChallenge('myprovider', '/re/dir/ect/url'));
}

public static function dataShowChallengeOtherProviders(): array {
return [
'single provider' => [['totp'], 'totp', false],
'single provider with backup codes' => [['totp', 'backup_codes'], 'totp', false],
'two providers' => [['totp', 'email', 'backup_codes'], 'totp', true],
'backup codes with a regular provider' => [['totp', 'backup_codes'], 'backup_codes', true],
'backup codes only' => [['backup_codes'], 'backup_codes', false],
];
}

#[DataProvider('dataShowChallengeOtherProviders')]
public function testShowChallengeOtherProviders(array $providerIds, string $challengeProviderId, bool $expected): void {
$user = $this->createMock(IUser::class);
$providers = array_map(function (string $id): IProvider {
$provider = $this->createMock(IProvider::class);
$provider->method('getId')->willReturn($id);
$tmpl = $this->createMock(ITemplate::class);
$tmpl->method('fetchPage')->willReturn('<html/>');
$provider->method('getTemplate')->willReturn($tmpl);
return $provider;
}, $providerIds);

$this->userSession->method('getUser')->willReturn($user);
$this->twoFactorManager->method('getProviderSet')
->with($user)
->willReturn(new ProviderSet($providers, false));

$response = $this->controller->showChallenge($challengeProviderId);

$this->assertInstanceOf(StandaloneTemplateResponse::class, $response);
$this->assertSame($expected, $response->getParams()['hasOtherProviders']);
}

public function testShowInvalidChallenge(): void {
$user = $this->createMock(IUser::class);
$providerSet = new ProviderSet([], false);
Expand Down
Loading