Merge pull request #16795 from nextcloud/enh/phpstan/limiter
Fix report of phpstan in Limiter
This commit is contained in:
commit
40edabaf61
|
@ -28,9 +28,7 @@ use OC\Security\Normalizer\IpAddress;
|
||||||
use OC\Security\RateLimiting\Backend\IBackend;
|
use OC\Security\RateLimiting\Backend\IBackend;
|
||||||
use OC\Security\RateLimiting\Exception\RateLimitExceededException;
|
use OC\Security\RateLimiting\Exception\RateLimitExceededException;
|
||||||
use OCP\AppFramework\Utility\ITimeFactory;
|
use OCP\AppFramework\Utility\ITimeFactory;
|
||||||
use OCP\IRequest;
|
|
||||||
use OCP\IUser;
|
use OCP\IUser;
|
||||||
use OCP\IUserSession;
|
|
||||||
|
|
||||||
class Limiter {
|
class Limiter {
|
||||||
/** @var IBackend */
|
/** @var IBackend */
|
||||||
|
@ -39,14 +37,10 @@ class Limiter {
|
||||||
private $timeFactory;
|
private $timeFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param IUserSession $userSession
|
|
||||||
* @param IRequest $request
|
|
||||||
* @param ITimeFactory $timeFactory
|
* @param ITimeFactory $timeFactory
|
||||||
* @param IBackend $backend
|
* @param IBackend $backend
|
||||||
*/
|
*/
|
||||||
public function __construct(IUserSession $userSession,
|
public function __construct(ITimeFactory $timeFactory,
|
||||||
IRequest $request,
|
|
||||||
ITimeFactory $timeFactory,
|
|
||||||
IBackend $backend) {
|
IBackend $backend) {
|
||||||
$this->backend = $backend;
|
$this->backend = $backend;
|
||||||
$this->timeFactory = $timeFactory;
|
$this->timeFactory = $timeFactory;
|
||||||
|
@ -62,7 +56,7 @@ class Limiter {
|
||||||
private function register(string $methodIdentifier,
|
private function register(string $methodIdentifier,
|
||||||
string $userIdentifier,
|
string $userIdentifier,
|
||||||
int $period,
|
int $period,
|
||||||
int $limit) {
|
int $limit): void {
|
||||||
$existingAttempts = $this->backend->getAttempts($methodIdentifier, $userIdentifier, $period);
|
$existingAttempts = $this->backend->getAttempts($methodIdentifier, $userIdentifier, $period);
|
||||||
if ($existingAttempts >= $limit) {
|
if ($existingAttempts >= $limit) {
|
||||||
throw new RateLimitExceededException();
|
throw new RateLimitExceededException();
|
||||||
|
@ -83,7 +77,7 @@ class Limiter {
|
||||||
public function registerAnonRequest(string $identifier,
|
public function registerAnonRequest(string $identifier,
|
||||||
int $anonLimit,
|
int $anonLimit,
|
||||||
int $anonPeriod,
|
int $anonPeriod,
|
||||||
string $ip) {
|
string $ip): void {
|
||||||
$ipSubnet = (new IpAddress($ip))->getSubnet();
|
$ipSubnet = (new IpAddress($ip))->getSubnet();
|
||||||
|
|
||||||
$anonHashIdentifier = hash('sha512', 'anon::' . $identifier . $ipSubnet);
|
$anonHashIdentifier = hash('sha512', 'anon::' . $identifier . $ipSubnet);
|
||||||
|
@ -102,7 +96,7 @@ class Limiter {
|
||||||
public function registerUserRequest(string $identifier,
|
public function registerUserRequest(string $identifier,
|
||||||
int $userLimit,
|
int $userLimit,
|
||||||
int $userPeriod,
|
int $userPeriod,
|
||||||
IUser $user) {
|
IUser $user): void {
|
||||||
$userHashIdentifier = hash('sha512', 'user::' . $identifier . $user->getUID());
|
$userHashIdentifier = hash('sha512', 'user::' . $identifier . $user->getUID());
|
||||||
$this->register($identifier, $userHashIdentifier, $userPeriod, $userLimit);
|
$this->register($identifier, $userHashIdentifier, $userPeriod, $userLimit);
|
||||||
}
|
}
|
||||||
|
|
|
@ -597,14 +597,6 @@ class Server extends ServerContainer implements IServerContainer {
|
||||||
});
|
});
|
||||||
$this->registerAlias('Search', \OCP\ISearch::class);
|
$this->registerAlias('Search', \OCP\ISearch::class);
|
||||||
|
|
||||||
$this->registerService(\OC\Security\RateLimiting\Limiter::class, function (Server $c) {
|
|
||||||
return new \OC\Security\RateLimiting\Limiter(
|
|
||||||
$this->getUserSession(),
|
|
||||||
$this->getRequest(),
|
|
||||||
new \OC\AppFramework\Utility\TimeFactory(),
|
|
||||||
$c->query(\OC\Security\RateLimiting\Backend\IBackend::class)
|
|
||||||
);
|
|
||||||
});
|
|
||||||
$this->registerService(\OC\Security\RateLimiting\Backend\IBackend::class, function ($c) {
|
$this->registerService(\OC\Security\RateLimiting\Backend\IBackend::class, function ($c) {
|
||||||
return new \OC\Security\RateLimiting\Backend\MemoryCache(
|
return new \OC\Security\RateLimiting\Backend\MemoryCache(
|
||||||
$this->getMemCacheFactory(),
|
$this->getMemCacheFactory(),
|
||||||
|
|
|
@ -31,10 +31,6 @@ use OCP\IUserSession;
|
||||||
use Test\TestCase;
|
use Test\TestCase;
|
||||||
|
|
||||||
class LimiterTest extends TestCase {
|
class LimiterTest extends TestCase {
|
||||||
/** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */
|
|
||||||
private $userSession;
|
|
||||||
/** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */
|
|
||||||
private $request;
|
|
||||||
/** @var ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */
|
/** @var ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */
|
||||||
private $timeFactory;
|
private $timeFactory;
|
||||||
/** @var IBackend|\PHPUnit_Framework_MockObject_MockObject */
|
/** @var IBackend|\PHPUnit_Framework_MockObject_MockObject */
|
||||||
|
@ -45,14 +41,10 @@ class LimiterTest extends TestCase {
|
||||||
public function setUp() {
|
public function setUp() {
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
|
||||||
$this->userSession = $this->createMock(IUserSession::class);
|
|
||||||
$this->request = $this->createMock(IRequest::class);
|
|
||||||
$this->timeFactory = $this->createMock(ITimeFactory::class);
|
$this->timeFactory = $this->createMock(ITimeFactory::class);
|
||||||
$this->backend = $this->createMock(IBackend::class);
|
$this->backend = $this->createMock(IBackend::class);
|
||||||
|
|
||||||
$this->limiter = new Limiter(
|
$this->limiter = new Limiter(
|
||||||
$this->userSession,
|
|
||||||
$this->request,
|
|
||||||
$this->timeFactory,
|
$this->timeFactory,
|
||||||
$this->backend
|
$this->backend
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in New Issue