Skip to content

Commit

Permalink
Replace RemoteAddress with UserIpReader in EmailAuthenticator. (vufin…
Browse files Browse the repository at this point in the history
  • Loading branch information
EreMaijala authored Nov 1, 2024
1 parent d5523b3 commit e8161e4
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 93 deletions.
1 change: 0 additions & 1 deletion module/VuFind/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,6 @@
'VuFindHttp\HttpService' => 'VuFind\Service\HttpServiceFactory',
'VuFindSearch\Service' => 'VuFind\Service\SearchServiceFactory',
'Laminas\Db\Adapter\Adapter' => 'VuFind\Db\AdapterFactory',
'Laminas\Http\PhpEnvironment\RemoteAddress' => 'VuFind\Http\PhpEnvironment\RemoteAddressFactory',
'Laminas\Session\SessionManager' => 'VuFind\Session\ManagerFactory',
],
'delegators' => [
Expand Down
10 changes: 5 additions & 5 deletions module/VuFind/src/VuFind/Auth/EmailAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@

namespace VuFind\Auth;

use Laminas\Http\PhpEnvironment\RemoteAddress;
use Laminas\Http\Request;
use Laminas\View\Renderer\PhpRenderer;
use VuFind\Db\Service\AuthHashServiceInterface;
use VuFind\Exception\Auth as AuthException;
use VuFind\Net\UserIpReader;
use VuFind\Validator\CsrfInterface;

/**
Expand Down Expand Up @@ -66,7 +66,7 @@ class EmailAuthenticator implements \VuFind\I18n\Translator\TranslatorAwareInter
* @param CsrfInterface $csrf CSRF Validator
* @param \VuFind\Mailer\Mailer $mailer Mailer
* @param PhpRenderer $viewRenderer View Renderer
* @param RemoteAddress $remoteAddress Remote address
* @param UserIpReader $userIpReader User IP address reader
* @param \Laminas\Config\Config $config Configuration
* @param AuthHashServiceInterface $authHashService AuthHash database service
*/
Expand All @@ -75,7 +75,7 @@ public function __construct(
protected CsrfInterface $csrf,
protected \VuFind\Mailer\Mailer $mailer,
protected PhpRenderer $viewRenderer,
protected RemoteAddress $remoteAddress,
protected UserIpReader $userIpReader,
protected \Laminas\Config\Config $config,
protected AuthHashServiceInterface $authHashService
) {
Expand Down Expand Up @@ -121,7 +121,7 @@ public function sendAuthenticationLink(
'timestamp' => time(),
'data' => $data,
'email' => $email,
'ip' => $this->remoteAddress->getIpAddress(),
'ip' => $this->userIpReader->getUserIp(),
];
$hash = $this->csrf->getHash(true);

Expand Down Expand Up @@ -171,7 +171,7 @@ public function authenticate($hash)
$sessionId = $this->sessionManager->getId();
if (
$row->getSessionId() !== $sessionId
&& $linkData['ip'] !== $this->remoteAddress->getIpAddress()
&& $linkData['ip'] !== $this->userIpReader->getUserIp()
) {
throw new AuthException('authentication_error_session_ip_mismatch');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function __invoke(
$container->get(\VuFind\Validator\CsrfInterface::class),
$container->get(\VuFind\Mailer\Mailer::class),
$container->get('ViewRenderer'),
$container->get(\Laminas\Http\PhpEnvironment\RemoteAddress::class),
$container->get(\VuFind\Net\UserIpReader::class),
$container->get(\VuFind\Config\PluginManager::class)->get('config'),
$container->get(\VuFind\Db\Service\PluginManager::class)
->get(\VuFind\Db\Service\AuthHashServiceInterface::class)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

use DateTime;
use Laminas\Config\Config;
use Laminas\Http\PhpEnvironment\RemoteAddress;
use Laminas\Http\Request;
use Laminas\I18n\Translator\TranslatorInterface;
use Laminas\Session\SessionManager;
Expand All @@ -43,6 +42,7 @@
use VuFind\Db\Entity\AuthHashEntityInterface;
use VuFind\Db\Service\AuthHashServiceInterface;
use VuFind\Mailer\Mailer;
use VuFind\Net\UserIpReader;
use VuFind\Validator\CsrfInterface;

/**
Expand All @@ -63,7 +63,7 @@ class EmailAuthenticatorTest extends \PHPUnit\Framework\TestCase
* @param ?CsrfInterface $csrf CSRF validator
* @param ?Mailer $mailer Mailer service
* @param ?PhpRenderer $renderer View renderer
* @param ?RemoteAddress $remoteAddress Remote address details
* @param ?userIpReader $userIpReader User IP reader
* @param array $config Configuration settings
* @param ?AuthHashServiceInterface $authHashService AuthHash database service
*
Expand All @@ -77,7 +77,7 @@ protected function getEmailAuthenticator(
CsrfInterface $csrf = null,
Mailer $mailer = null,
PhpRenderer $renderer = null,
RemoteAddress $remoteAddress = null,
UserIpReader $userIpReader = null,
array $config = [],
AuthHashServiceInterface $authHashService = null
): EmailAuthenticator {
Expand All @@ -86,7 +86,7 @@ protected function getEmailAuthenticator(
$csrf ?? $this->createMock(CsrfInterface::class),
$mailer ?? $this->createMock(Mailer::class),
$renderer ?? $this->createMock(PhpRenderer::class),
$remoteAddress ?? $this->createMock(RemoteAddress::class),
$userIpReader ?? $this->createMock(UserIpReader::class),
new Config($config),
$authHashService ?? $this->createMock(AuthHashServiceInterface::class)
);
Expand Down Expand Up @@ -186,14 +186,14 @@ function ($name) use ($mockServerUrl, $mockUrl) {
$renderer->expects($this->once())->method('render')
->with('Email/login-link.phtml', $this->callback($checkViewParams))
->willReturn('foo-message');
$remoteAddress = $this->createMock(RemoteAddress::class);
$remoteAddress->expects($this->once())->method('getIpAddress')->willReturn('foo-ip');
$userIpReader = $this->createMock(userIpReader::class);
$userIpReader->expects($this->once())->method('getUserIp')->willReturn('foo-ip');
$authenticator = $this->getEmailAuthenticator(
sessionManager: $sessionManager,
csrf: $csrf,
mailer: $mailer,
renderer: $renderer,
remoteAddress: $remoteAddress,
userIpReader: $userIpReader,
config: ['Site' => ['title' => 'foo-site-title', 'email' => '[email protected]']],
authHashService: $authHashService
);
Expand Down

0 comments on commit e8161e4

Please sign in to comment.