Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uml 1533 hashing logged email #2460

Merged
merged 13 commits into from
Dec 19, 2023
19 changes: 19 additions & 0 deletions service-api/app/src/App/src/Service/Log/Output/Email.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace App\Service\Log\Output;

use function hash;

class Email
{
public function __construct(private string $email)
{
}

public function __toString(): string
{
return hash('sha256', $this->email);
}
}
3 changes: 2 additions & 1 deletion service-api/app/src/App/src/Service/User/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use App\Exception\GoneException;
use App\Exception\NotFoundException;
use App\Exception\UnauthorizedException;
use App\Service\Log\Output\Email;
use DateTime;
use DateTimeInterface;
use Exception;
Expand Down Expand Up @@ -190,7 +191,7 @@ public function requestPasswordReset(string $email): array
} catch (Exception $e) {
$this->logger->notice(
'Attempt made to reset password for non-existent account',
['email' => $email]
['email' => new Email($email)]
Lbagg1 marked this conversation as resolved.
Show resolved Hide resolved
);

throw $e;
Expand Down
19 changes: 19 additions & 0 deletions service-api/app/test/AppTest/Service/Log/Output/EmailTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace AppTest\Service\Log\Output;

use App\Service\Log\Output\Email;
use PHPUnit\Framework\TestCase;

class EmailTest extends TestCase
{
/** @test */
public function it_hides_a_string()
{
$email = new Email('[email protected]');
$this->assertMatchesRegularExpression('/.*/', (string)$email);
$this->assertStringNotContainsString('[email protected]', (string)$email);
}
}
31 changes: 31 additions & 0 deletions service-api/app/test/AppTest/Service/User/UserServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use App\Exception\GoneException;
use App\Exception\NotFoundException;
use App\Exception\UnauthorizedException;
use App\Service\Log\Output\Email;
use App\Service\User\UserService;
use DateTime;
use Exception;
Expand Down Expand Up @@ -189,6 +190,36 @@ public function cannot_add_existing_user_as_email_used_in_reset()
$us->add($userData);
}

/** @test */
public function logs_Notice_When_Password_Reset_Is_Requested_For_Non_Existent_Account(): void
{
$email = '[email protected]';
$hashed_email = hash('sha256', $email);

$repoProphecy = $this->prophesize(ActorUsersInterface::class);
$loggerProphecy = $this->prophesize(LoggerInterface::class);

$repoProphecy
->recordPasswordResetRequest(Argument::cetera())
->willThrow(Exception::class);

$loggerProphecy
->notice(
'Attempt made to reset password for non-existent account',
Argument::that(function ($arg) use ($hashed_email) {
return $arg['email'] instanceof Email && (string)($arg['email']) == $hashed_email;
})
)
->shouldBeCalled();

$userService = new UserService($repoProphecy->reveal(), $loggerProphecy->reveal());

try {
$userService->requestPasswordReset($email);
} catch (Exception) {
}
}

/** @test */
public function can_get_a_user_from_storage(): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function authenticate(string $credential, ?string $password = null): ?Use
'Authentication failed for {email} with code {code}',
[
'code' => $e->getCode(),
'email' => $credential,
'email' =>new Email($credential),
]
);
if ($e->getCode() === StatusCodeInterface::STATUS_UNAUTHORIZED) {
Expand Down
Loading