Skip to content

Commit 3b9be2d

Browse files
committed
OXDEV-9078 Add a challenge state dto
1 parent ec80222 commit 3b9be2d

3 files changed

Lines changed: 134 additions & 0 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/**
4+
* Copyright © OXID eSales AG. All rights reserved.
5+
* See LICENSE file for license details.
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace OxidEsales\SecurityModule\Authentication\TwoFactorAuth\OTP\DTO;
11+
12+
use DateTimeImmutable;
13+
14+
class OtpChallengeState implements OtpChallengeStateInterface
15+
{
16+
public function __construct(
17+
private string $userId,
18+
private string $codeHash,
19+
private int $attempts,
20+
private ?DateTimeImmutable $lastSentAt,
21+
private DateTimeImmutable $expiresAt,
22+
private ?DateTimeImmutable $verifiedAt,
23+
) {
24+
}
25+
26+
public function getUserId(): string
27+
{
28+
return $this->userId;
29+
}
30+
31+
public function getCodeHash(): string
32+
{
33+
return $this->codeHash;
34+
}
35+
36+
public function getAttempts(): int
37+
{
38+
return $this->attempts;
39+
}
40+
41+
public function getLastSentAt(): ?DateTimeImmutable
42+
{
43+
return $this->lastSentAt;
44+
}
45+
46+
public function getExpiresAt(): DateTimeImmutable
47+
{
48+
return $this->expiresAt;
49+
}
50+
51+
public function getVerifiedAt(): ?DateTimeImmutable
52+
{
53+
return $this->verifiedAt;
54+
}
55+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/**
4+
* Copyright © OXID eSales AG. All rights reserved.
5+
* See LICENSE file for license details.
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace OxidEsales\SecurityModule\Authentication\TwoFactorAuth\OTP\DTO;
11+
12+
use DateTimeImmutable;
13+
14+
interface OtpChallengeStateInterface
15+
{
16+
public function getUserId(): string;
17+
18+
public function getCodeHash(): string;
19+
20+
public function getAttempts(): int;
21+
22+
public function getLastSentAt(): ?DateTimeImmutable;
23+
24+
public function getExpiresAt(): DateTimeImmutable;
25+
26+
public function getVerifiedAt(): ?DateTimeImmutable;
27+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
/**
4+
* Copyright © OXID eSales AG. All rights reserved.
5+
* See LICENSE file for license details.
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace OxidEsales\SecurityModule\Tests\Unit\Authentication\TwoFactorAuth\OTP\DTO;
11+
12+
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\OTP\DTO\OtpChallengeState;
13+
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\OTP\DTO\OtpChallengeStateInterface;
14+
use PHPUnit\Framework\TestCase;
15+
16+
class OtpChallengeStateTest extends TestCase
17+
{
18+
public function testInitializeAndReadProperties(): void
19+
{
20+
$sut = new OtpChallengeState(
21+
userId: $userId = uniqid(),
22+
codeHash: $codeHash = uniqid(),
23+
attempts: $attempts = rand(),
24+
lastSentAt: $lastSentAt = new \DateTimeImmutable(),
25+
expiresAt: $expiresAt = new \DateTimeImmutable(),
26+
verifiedAt: $verifiedAt = new \DateTimeImmutable(),
27+
);
28+
29+
$this->assertInstanceOf(OtpChallengeStateInterface::class, $sut);
30+
$this->assertSame($userId, $sut->getUserId());
31+
$this->assertSame($codeHash, $sut->getCodeHash());
32+
$this->assertSame($attempts, $sut->getAttempts());
33+
$this->assertSame($lastSentAt, $sut->getLastSentAt());
34+
$this->assertSame($expiresAt, $sut->getExpiresAt());
35+
$this->assertSame($verifiedAt, $sut->getVerifiedAt());
36+
}
37+
38+
public function testLastSentAtAndVerifiedAtAreNullable(): void
39+
{
40+
$sut = new OtpChallengeState(
41+
userId: uniqid(),
42+
codeHash: uniqid(),
43+
attempts: 0,
44+
lastSentAt: null,
45+
expiresAt: new \DateTimeImmutable(),
46+
verifiedAt: null,
47+
);
48+
49+
$this->assertNull($sut->getLastSentAt());
50+
$this->assertNull($sut->getVerifiedAt());
51+
}
52+
}

0 commit comments

Comments
 (0)