Skip to content

Commit

Permalink
Merge pull request #10588 from nextcloud/fix/single-2fa-provider-logi…
Browse files Browse the repository at this point in the history
…n-redirect

Fix login redirection if only one 2FA provider is active
  • Loading branch information
rullzer committed Aug 9, 2018
2 parents def2bf2 + d8197f2 commit 0757c52
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
2 changes: 1 addition & 1 deletion core/Controller/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ public function tryLogin($user, $password, $redirect_url, $remember_login = true
if ($this->twoFactorManager->isTwoFactorAuthenticated($loginResult)) {
$this->twoFactorManager->prepareTwoFactorLogin($loginResult, $remember_login);

$providers = $this->twoFactorManager->getProviderSet($loginResult)->getProviders();
$providers = $this->twoFactorManager->getProviderSet($loginResult)->getPrimaryProviders();
if (count($providers) === 1) {
// Single provider, hence we can redirect to that provider's challenge page directly
/* @var $provider IProvider */
Expand Down
11 changes: 11 additions & 0 deletions lib/private/Authentication/TwoFactorAuth/ProviderSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

namespace OC\Authentication\TwoFactorAuth;

use function array_filter;
use OCA\TwoFactorBackupCodes\Provider\BackupCodesProvider;
use OCP\Authentication\TwoFactorAuth\IProvider;

/**
Expand Down Expand Up @@ -65,6 +67,15 @@ public function getProviders(): array {
return $this->providers;
}

/**
* @return IProvider[]
*/
public function getPrimaryProviders(): array {
return array_filter($this->providers, function(IProvider $provider) {
return !($provider instanceof BackupCodesProvider);
});
}

public function isProviderMissing(): bool {
return $this->providerMissing;
}
Expand Down
11 changes: 6 additions & 5 deletions tests/Core/Controller/LoginControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use OC\Core\Controller\LoginController;
use OC\Security\Bruteforce\Throttler;
use OC\User\Session;
use OCA\TwoFactorBackupCodes\Provider\BackupCodesProvider;
use OCP\AppFramework\Http\RedirectResponse;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\Authentication\TwoFactorAuth\IProvider;
Expand Down Expand Up @@ -594,7 +595,10 @@ public function testLoginWithOneTwoFactorProvider() {
->will($this->returnValue('john'));
$password = 'secret';
$challengeUrl = 'challenge/url';
$provider = $this->createMock(IProvider::class);
$provider1 = $this->createMock(IProvider::class);
$provider1->method('getId')->willReturn('u2f');
$provider2 = $this->createMock(BackupCodesProvider::class);
$provider2->method('getId')->willReturn('backup');

$this->request
->expects($this->once())
Expand All @@ -616,14 +620,11 @@ public function testLoginWithOneTwoFactorProvider() {
$this->twoFactorManager->expects($this->once())
->method('prepareTwoFactorLogin')
->with($user);
$providerSet = new ProviderSet([$provider], false);
$providerSet = new ProviderSet([$provider1, $provider2], false);
$this->twoFactorManager->expects($this->once())
->method('getProviderSet')
->with($user)
->willReturn($providerSet);
$provider->expects($this->once())
->method('getId')
->will($this->returnValue('u2f'));
$this->urlGenerator->expects($this->once())
->method('linkToRoute')
->with('core.TwoFactorChallenge.showChallenge', [
Expand Down
18 changes: 18 additions & 0 deletions tests/lib/Authentication/TwoFactorAuth/ProviderSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
namespace Test\Authentication\TwoFactorAuth;

use OC\Authentication\TwoFactorAuth\ProviderSet;
use OCA\TwoFactorBackupCodes\Provider\BackupCodesProvider;
use OCP\Authentication\TwoFactorAuth\IProvider;
use Test\TestCase;

Expand All @@ -49,6 +50,23 @@ public function testIndexesProviders() {
$this->assertEquals($expected, $set->getProviders());
}

public function testGet3rdPartyProviders() {
$p1 = $this->createMock(IProvider::class);
$p1->method('getId')->willReturn('p1');
$p2 = $this->createMock(IProvider::class);
$p2->method('getId')->willReturn('p2');
$p3 = $this->createMock(BackupCodesProvider::class);
$p3->method('getId')->willReturn('p3');
$expected = [
'p1' => $p1,
'p2' => $p2,
];

$set = new ProviderSet([$p2, $p1], false);

$this->assertEquals($expected, $set->getPrimaryProviders());
}

public function testGetProvider() {
$p1 = $this->createMock(IProvider::class);
$p1->method('getId')->willReturn('p1');
Expand Down

0 comments on commit 0757c52

Please sign in to comment.