Skip to content

Commit

Permalink
Merge pull request #40079 from nextcloud/techdebt/noid/user-IThrottle…
Browse files Browse the repository at this point in the history
…r-interface-for-DI

techdebt(DI): Use public IThrottler interface which exists since 25
  • Loading branch information
nickvergessen authored Aug 28, 2023
2 parents fec5ede + 25309bc commit 943f4c2
Show file tree
Hide file tree
Showing 22 changed files with 67 additions and 67 deletions.
6 changes: 3 additions & 3 deletions apps/dav/lib/Connector/PublicAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
*/
namespace OCA\DAV\Connector;

use OC\Security\Bruteforce\Throttler;
use OCP\IRequest;
use OCP\ISession;
use OCP\Security\Bruteforce\IThrottler;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IManager;
use OCP\Share\IShare;
Expand All @@ -48,12 +48,12 @@ class PublicAuth extends AbstractBasic {
private IManager $shareManager;
private ISession $session;
private IRequest $request;
private Throttler $throttler;
private IThrottler $throttler;

public function __construct(IRequest $request,
IManager $shareManager,
ISession $session,
Throttler $throttler) {
IThrottler $throttler) {
$this->request = $request;
$this->shareManager = $shareManager;
$this->session = $session;
Expand Down
6 changes: 3 additions & 3 deletions apps/dav/lib/Connector/Sabre/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@
use Exception;
use OC\Authentication\Exceptions\PasswordLoginForbiddenException;
use OC\Authentication\TwoFactorAuth\Manager;
use OC\Security\Bruteforce\Throttler;
use OC\User\Session;
use OCA\DAV\Connector\Sabre\Exception\PasswordLoginForbidden;
use OCA\DAV\Connector\Sabre\Exception\TooManyRequests;
use OCP\IRequest;
use OCP\ISession;
use OCP\Security\Bruteforce\IThrottler;
use OCP\Security\Bruteforce\MaxDelayReached;
use Psr\Log\LoggerInterface;
use Sabre\DAV\Auth\Backend\AbstractBasic;
Expand All @@ -58,13 +58,13 @@ class Auth extends AbstractBasic {
private IRequest $request;
private ?string $currentUser = null;
private Manager $twoFactorManager;
private Throttler $throttler;
private IThrottler $throttler;

public function __construct(ISession $session,
Session $userSession,
IRequest $request,
Manager $twoFactorManager,
Throttler $throttler,
IThrottler $throttler,
string $principalPrefix = 'principals/users/') {
$this->session = $session;
$this->userSession = $userSession;
Expand Down
8 changes: 5 additions & 3 deletions apps/dav/lib/Direct/DirectHome.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
*/
namespace OCA\DAV\Direct;

use OC\Security\Bruteforce\Throttler;
use OCA\DAV\Db\DirectMapper;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\IRootFolder;
use OCP\IRequest;
use OCP\Security\Bruteforce\IThrottler;
use Sabre\DAV\Exception\Forbidden;
use Sabre\DAV\Exception\MethodNotAllowed;
use Sabre\DAV\Exception\NotFound;
Expand All @@ -49,18 +49,20 @@ class DirectHome implements ICollection {
/** @var ITimeFactory */
private $timeFactory;

/** @var Throttler */
/** @var IThrottler */
private $throttler;

/** @var IRequest */
private $request;

/** @var IEventDispatcher */
private $eventDispatcher;

public function __construct(
IRootFolder $rootFolder,
DirectMapper $mapper,
ITimeFactory $timeFactory,
Throttler $throttler,
IThrottler $throttler,
IRequest $request,
IEventDispatcher $eventDispatcher
) {
Expand Down
5 changes: 3 additions & 2 deletions apps/dav/lib/Direct/ServerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
*/
namespace OCA\DAV\Direct;

use OC\Security\Bruteforce\Throttler;
use OCA\DAV\Connector\Sabre\MaintenancePlugin;
use OCA\DAV\Db\DirectMapper;
use OCP\AppFramework\Utility\ITimeFactory;
Expand All @@ -37,12 +36,14 @@
use OCP\IL10N;
use OCP\IRequest;
use OCP\L10N\IFactory;
use OCP\Security\Bruteforce\IThrottler;

class ServerFactory {
/** @var IConfig */
private $config;
/** @var IL10N */
private $l10n;
/** @var IEventDispatcher */
private $eventDispatcher;

public function __construct(IConfig $config, IFactory $l10nFactory, IEventDispatcher $eventDispatcher) {
Expand All @@ -56,7 +57,7 @@ public function createServer(string $baseURI,
IRootFolder $rootFolder,
DirectMapper $mapper,
ITimeFactory $timeFactory,
Throttler $throttler,
IThrottler $throttler,
IRequest $request): Server {
$home = new DirectHome($rootFolder, $mapper, $timeFactory, $throttler, $request, $this->eventDispatcher);
$server = new Server($home);
Expand Down
6 changes: 3 additions & 3 deletions apps/dav/tests/unit/Connector/PublicAuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
*/
namespace OCA\DAV\Tests\unit\Connector;

use OC\Security\Bruteforce\Throttler;
use OCP\IRequest;
use OCP\ISession;
use OCP\Security\Bruteforce\IThrottler;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IManager;
use OCP\Share\IShare;
Expand All @@ -50,7 +50,7 @@ class PublicAuthTest extends \Test\TestCase {
private $shareManager;
/** @var \OCA\DAV\Connector\PublicAuth */
private $auth;
/** @var Throttler|\PHPUnit\Framework\MockObject\MockObject */
/** @var IThrottler|\PHPUnit\Framework\MockObject\MockObject */
private $throttler;

/** @var string */
Expand All @@ -68,7 +68,7 @@ protected function setUp(): void {
$this->shareManager = $this->getMockBuilder(IManager::class)
->disableOriginalConstructor()
->getMock();
$this->throttler = $this->getMockBuilder(Throttler::class)
$this->throttler = $this->getMockBuilder(IThrottler::class)
->disableOriginalConstructor()
->getMock();

Expand Down
6 changes: 3 additions & 3 deletions apps/dav/tests/unit/Connector/Sabre/AuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
namespace OCA\DAV\Tests\unit\Connector\Sabre;

use OC\Authentication\TwoFactorAuth\Manager;
use OC\Security\Bruteforce\Throttler;
use OC\User\Session;
use OCP\IRequest;
use OCP\ISession;
use OCP\IUser;
use OCP\Security\Bruteforce\IThrottler;
use Sabre\DAV\Server;
use Sabre\HTTP\RequestInterface;
use Sabre\HTTP\ResponseInterface;
Expand All @@ -57,7 +57,7 @@ class AuthTest extends TestCase {
private $request;
/** @var Manager */
private $twoFactorManager;
/** @var Throttler */
/** @var IThrottler */
private $throttler;

protected function setUp(): void {
Expand All @@ -71,7 +71,7 @@ protected function setUp(): void {
$this->twoFactorManager = $this->getMockBuilder(Manager::class)
->disableOriginalConstructor()
->getMock();
$this->throttler = $this->getMockBuilder(Throttler::class)
$this->throttler = $this->getMockBuilder(IThrottler::class)
->disableOriginalConstructor()
->getMock();
$this->auth = new \OCA\DAV\Connector\Sabre\Auth(
Expand Down
6 changes: 3 additions & 3 deletions apps/dav/tests/unit/Direct/DirectHomeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
*/
namespace OCA\DAV\Tests\Unit\Direct;

use OC\Security\Bruteforce\Throttler;
use OCA\DAV\Db\Direct;
use OCA\DAV\Db\DirectMapper;
use OCA\DAV\Direct\DirectFile;
Expand All @@ -37,6 +36,7 @@
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\IRootFolder;
use OCP\IRequest;
use OCP\Security\Bruteforce\IThrottler;
use Sabre\DAV\Exception\Forbidden;
use Sabre\DAV\Exception\MethodNotAllowed;
use Sabre\DAV\Exception\NotFound;
Expand All @@ -53,7 +53,7 @@ class DirectHomeTest extends TestCase {
/** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
private $timeFactory;

/** @var Throttler|\PHPUnit\Framework\MockObject\MockObject */
/** @var IThrottler|\PHPUnit\Framework\MockObject\MockObject */
private $throttler;

/** @var IRequest */
Expand All @@ -71,7 +71,7 @@ protected function setUp(): void {
$this->directMapper = $this->createMock(DirectMapper::class);
$this->rootFolder = $this->createMock(IRootFolder::class);
$this->timeFactory = $this->createMock(ITimeFactory::class);
$this->throttler = $this->createMock(Throttler::class);
$this->throttler = $this->createMock(IThrottler::class);
$this->request = $this->createMock(IRequest::class);
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);

Expand Down
4 changes: 2 additions & 2 deletions apps/oauth2/lib/Controller/OauthApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
use OC\Authentication\Exceptions\ExpiredTokenException;
use OC\Authentication\Exceptions\InvalidTokenException;
use OC\Authentication\Token\IProvider as TokenProvider;
use OC\Security\Bruteforce\Throttler;
use OCA\OAuth2\Db\AccessTokenMapper;
use OCA\OAuth2\Db\ClientMapper;
use OCA\OAuth2\Exceptions\AccessTokenNotFoundException;
Expand All @@ -41,6 +40,7 @@
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IRequest;
use OCP\Security\Bruteforce\IThrottler;
use OCP\Security\ICrypto;
use OCP\Security\ISecureRandom;
use Psr\Log\LoggerInterface;
Expand All @@ -57,7 +57,7 @@ public function __construct(
private ISecureRandom $secureRandom,
private ITimeFactory $time,
private LoggerInterface $logger,
private Throttler $throttler
private IThrottler $throttler
) {
parent::__construct($appName, $request);
}
Expand Down
6 changes: 3 additions & 3 deletions apps/oauth2/tests/Controller/OauthApiControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
use OC\Authentication\Exceptions\InvalidTokenException;
use OC\Authentication\Token\IProvider as TokenProvider;
use OC\Authentication\Token\PublicKeyToken;
use OC\Security\Bruteforce\Throttler;
use OCA\OAuth2\Controller\OauthApiController;
use OCA\OAuth2\Db\AccessToken;
use OCA\OAuth2\Db\AccessTokenMapper;
Expand All @@ -41,6 +40,7 @@
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IRequest;
use OCP\Security\Bruteforce\IThrottler;
use OCP\Security\ICrypto;
use OCP\Security\ISecureRandom;
use Psr\Log\LoggerInterface;
Expand All @@ -66,7 +66,7 @@ class OauthApiControllerTest extends TestCase {
private $secureRandom;
/** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
private $time;
/** @var Throttler|\PHPUnit\Framework\MockObject\MockObject */
/** @var IThrottler|\PHPUnit\Framework\MockObject\MockObject */
private $throttler;
/** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
private $logger;
Expand All @@ -83,7 +83,7 @@ protected function setUp(): void {
$this->tokenProvider = $this->createMock(TokenProvider::class);
$this->secureRandom = $this->createMock(ISecureRandom::class);
$this->time = $this->createMock(ITimeFactory::class);
$this->throttler = $this->createMock(Throttler::class);
$this->throttler = $this->createMock(IThrottler::class);
$this->logger = $this->createMock(LoggerInterface::class);

$this->oauthApiController = new OauthApiController(
Expand Down
4 changes: 2 additions & 2 deletions core/Controller/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
use OC\Authentication\Login\Chain;
use OC\Authentication\Login\LoginData;
use OC\Authentication\WebAuthn\Manager as WebAuthnManager;
use OC\Security\Bruteforce\Throttler;
use OC\User\Session;
use OC_App;
use OCP\AppFramework\Controller;
Expand All @@ -58,6 +57,7 @@
use OCP\IUser;
use OCP\IUserManager;
use OCP\Notification\IManager;
use OCP\Security\Bruteforce\IThrottler;
use OCP\Util;

#[IgnoreOpenAPI]
Expand All @@ -74,7 +74,7 @@ public function __construct(
private Session $userSession,
private IURLGenerator $urlGenerator,
private Defaults $defaults,
private Throttler $throttler,
private IThrottler $throttler,
private IInitialStateService $initialStateService,
private WebAuthnManager $webAuthnManager,
private IManager $manager,
Expand Down
5 changes: 3 additions & 2 deletions lib/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\IUserSession;
use OCP\Security\Bruteforce\IThrottler;
use OCP\Server;
use OCP\Share;
use OCP\User\Events\UserChangedEvent;
Expand Down Expand Up @@ -871,7 +872,7 @@ public static function registerCleanupHooks(\OC\SystemConfig $systemConfig): voi
// reset brute force delay for this IP address and username
$uid = $userSession->getUser()->getUID();
$request = Server::get(IRequest::class);
$throttler = Server::get(\OC\Security\Bruteforce\Throttler::class);
$throttler = Server::get(IThrottler::class);
$throttler->resetDelay($request->getRemoteAddress(), 'login', ['user' => $uid]);
}

Expand Down Expand Up @@ -1149,7 +1150,7 @@ public static function handleLogin(OCP\IRequest $request): bool {
&& $userSession->loginWithCookie($_COOKIE['nc_username'], $_COOKIE['nc_token'], $_COOKIE['nc_session_id'])) {
return true;
}
if ($userSession->tryBasicAuthLogin($request, Server::get(\OC\Security\Bruteforce\Throttler::class))) {
if ($userSession->tryBasicAuthLogin($request, Server::get(IThrottler::class))) {
return true;
}
return false;
Expand Down
7 changes: 4 additions & 3 deletions lib/private/AppFramework/DependencyInjection/DIContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
use OCP\ISession;
use OCP\IURLGenerator;
use OCP\IUserSession;
use OCP\Security\Bruteforce\IThrottler;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

Expand Down Expand Up @@ -233,7 +234,7 @@ public function __construct(string $appName, array $urlParams = [], ServerContai
$c->get(IRequest::class),
$c->get(IControllerMethodReflector::class),
$c->get(IUserSession::class),
$c->get(OC\Security\Bruteforce\Throttler::class)
$c->get(IThrottler::class)
)
);
$dispatcher->registerMiddleware(
Expand Down Expand Up @@ -291,7 +292,7 @@ public function __construct(string $appName, array $urlParams = [], ServerContai
$dispatcher->registerMiddleware(
new OC\AppFramework\Middleware\Security\BruteForceMiddleware(
$c->get(IControllerMethodReflector::class),
$c->get(OC\Security\Bruteforce\Throttler::class),
$c->get(IThrottler::class),
$c->get(IRequest::class),
$c->get(LoggerInterface::class)
)
Expand All @@ -309,7 +310,7 @@ public function __construct(string $appName, array $urlParams = [], ServerContai
$c->get(IRequest::class),
$c->get(ISession::class),
$c->get(\OCP\IConfig::class),
$c->get(OC\Security\Bruteforce\Throttler::class)
$c->get(IThrottler::class)
)
);
$dispatcher->registerMiddleware(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
namespace OC\AppFramework\Middleware\PublicShare;

use OC\AppFramework\Middleware\PublicShare\Exceptions\NeedAuthenticationException;
use OC\Security\Bruteforce\Throttler;
use OCP\AppFramework\AuthPublicShareController;
use OCP\AppFramework\Http\NotFoundResponse;
use OCP\AppFramework\Middleware;
Expand All @@ -33,6 +32,7 @@
use OCP\IConfig;
use OCP\IRequest;
use OCP\ISession;
use OCP\Security\Bruteforce\IThrottler;

class PublicShareMiddleware extends Middleware {
/** @var IRequest */
Expand All @@ -44,10 +44,10 @@ class PublicShareMiddleware extends Middleware {
/** @var IConfig */
private $config;

/** @var Throttler */
/** @var IThrottler */
private $throttler;

public function __construct(IRequest $request, ISession $session, IConfig $config, Throttler $throttler) {
public function __construct(IRequest $request, ISession $session, IConfig $config, IThrottler $throttler) {
$this->request = $request;
$this->session = $session;
$this->config = $config;
Expand Down
Loading

0 comments on commit 943f4c2

Please sign in to comment.