use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList;
use OCP\IRequest;
+use OCP\Security\Bruteforce\IThrottler;
use OCP\Security\ISecureRandom;
use Psr\Log\LoggerInterface;
private DbHandler $dbHandler;
private LoggerInterface $logger;
private ITimeFactory $timeFactory;
+ private IThrottler $throttler;
public function __construct(
string $appName,
TrustedServers $trustedServers,
DbHandler $dbHandler,
LoggerInterface $logger,
- ITimeFactory $timeFactory
+ ITimeFactory $timeFactory,
+ IThrottler $throttler
) {
parent::__construct($appName, $request);
$this->dbHandler = $dbHandler;
$this->logger = $logger;
$this->timeFactory = $timeFactory;
+ $this->throttler = $throttler;
}
/**
*
* @NoCSRFRequired
* @PublicPage
+ * @BruteForceProtection(action=federationSharedSecret)
*
* @param string $url URL of the server
* @param string $token Token of the server
*
* @NoCSRFRequired
* @PublicPage
+ * @BruteForceProtection(action=federationSharedSecret)
*
* @param string $url URL of the server
* @param string $token Token of the server
*
* @NoCSRFRequired
* @PublicPage
+ * @BruteForceProtection(action=federationSharedSecret)
*
* @param string $url URL of the server
* @param string $token Token of the server
*/
public function requestSharedSecret(string $url, string $token): DataResponse {
if ($this->trustedServers->isTrustedServer($url) === false) {
+ $this->throttler->registerAttempt('federationSharedSecret', $this->request->getRemoteAddress());
$this->logger->error('remote server not trusted (' . $url . ') while requesting shared secret', ['app' => 'federation']);
throw new OCSForbiddenException();
}
*
* @NoCSRFRequired
* @PublicPage
+ * @BruteForceProtection(action=federationSharedSecret)
*
* @param string $url URL of the server
* @param string $token Token of the server
*/
public function getSharedSecret(string $url, string $token): DataResponse {
if ($this->trustedServers->isTrustedServer($url) === false) {
+ $this->throttler->registerAttempt('federationSharedSecret', $this->request->getRemoteAddress());
$this->logger->error('remote server not trusted (' . $url . ') while getting shared secret', ['app' => 'federation']);
throw new OCSForbiddenException();
}
if ($this->isValidToken($url, $token) === false) {
+ $this->throttler->registerAttempt('federationSharedSecret', $this->request->getRemoteAddress());
$expectedToken = $this->dbHandler->getToken($url);
$this->logger->error(
'remote server (' . $url . ') didn\'t send a valid token (got "' . $token . '" but expected "'. $expectedToken . '") while getting shared secret',
use OCP\AppFramework\OCS\OCSForbiddenException;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IRequest;
+use OCP\Security\Bruteforce\IThrottler;
use OCP\Security\ISecureRandom;
use Psr\Log\LoggerInterface;
use Test\TestCase;
/** @var \PHPUnit\Framework\MockObject\MockObject|ITimeFactory */
private $timeFactory;
+ /** @var \PHPUnit\Framework\MockObject\MockObject|IThrottler */
+ private $throttler;
+
private OCSAuthAPIController $ocsAuthApi;
/** @var int simulated timestamp */
$this->jobList = $this->createMock(JobList::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->timeFactory = $this->createMock(ITimeFactory::class);
+ $this->throttler = $this->createMock(IThrottler::class);
$this->ocsAuthApi = new OCSAuthAPIController(
'federation',
$this->trustedServers,
$this->dbHandler,
$this->logger,
- $this->timeFactory
+ $this->timeFactory,
+ $this->throttler
);
$this->timeFactory->method('getTime')
} else {
$this->jobList->expects($this->never())->method('add');
$this->jobList->expects($this->never())->method('remove');
+ if (!$isTrustedServer) {
+ $this->throttler->expects($this->once())
+ ->method('registerAttempt')
+ ->with('federationSharedSecret');
+ }
}
+
try {
$this->ocsAuthApi->requestSharedSecret($url, $token);
$this->assertTrue($ok);
$this->trustedServers,
$this->dbHandler,
$this->logger,
- $this->timeFactory
+ $this->timeFactory,
+ $this->throttler
]
)->setMethods(['isValidToken'])->getMock();
} else {
$this->secureRandom->expects($this->never())->method('generate');
$this->trustedServers->expects($this->never())->method('addSharedSecret');
+ $this->throttler->expects($this->once())
+ ->method('registerAttempt')
+ ->with('federationSharedSecret');
}
try {