1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace EcodevTests\Felix\Model\Traits; |
6
|
|
|
|
7
|
|
|
use Ecodev\Felix\Model\Traits\HasOtp; |
8
|
|
|
use OTPHP\Factory; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
|
11
|
|
|
final class HasOtpTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
private \Ecodev\Felix\Model\HasOtp $user; |
14
|
|
|
|
15
|
|
|
protected function setUp(): void |
16
|
|
|
{ |
17
|
|
|
$this->user = new class() implements \Ecodev\Felix\Model\HasOtp { |
18
|
|
|
use HasOtp; |
19
|
|
|
|
20
|
|
|
public function getLogin(): ?string |
21
|
|
|
{ |
22
|
|
|
return '[email protected]'; |
23
|
|
|
} |
24
|
|
|
}; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testCreateOtpSecret(): void |
28
|
|
|
{ |
29
|
|
|
self::assertNull($this->user->getOtpUri(), 'should have no OTP secret at first'); |
30
|
|
|
self::assertFalse($this->user->isOtp(), 'should have OTP disabled at first'); |
31
|
|
|
|
32
|
|
|
self::expectExceptionMessage('Cannot enable OTP without a secret'); |
|
|
|
|
33
|
|
|
$this->user->setOtp(true); |
34
|
|
|
|
35
|
|
|
$this->user->createOtpSecret('felix.lan'); |
36
|
|
|
$otp1 = $this->user->getOtpUri(); |
37
|
|
|
self::assertIsString($otp1); |
38
|
|
|
self::assertStringStartsWith('otpauth://totp/', $otp1, 'TOTP provisionning URI was generated and stored'); |
|
|
|
|
39
|
|
|
|
40
|
|
|
$this->user->createOtpSecret('felix.lan'); |
41
|
|
|
$otp2 = $this->user->getOtpUri(); |
42
|
|
|
self::assertIsString($otp2); |
43
|
|
|
self::assertNotSame($otp1, $otp2, 'TOTP provisionning URI was changed'); |
44
|
|
|
|
45
|
|
|
$this->user->setOtp(true); |
46
|
|
|
self::assertTrue($this->user->isOtp()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testRevokeSecret(): void |
50
|
|
|
{ |
51
|
|
|
$this->user->createOtpSecret('felix.lan'); |
52
|
|
|
$this->user->revokeOtpSecret(); |
53
|
|
|
|
54
|
|
|
self::assertFalse($this->user->isOtp()); |
55
|
|
|
self::assertNull($this->user->getOtpUri()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testVerifySecret(): void |
59
|
|
|
{ |
60
|
|
|
$this->user->setOtp(false); |
61
|
|
|
self::assertFalse($this->user->verifyOtp('123456'), 'Cannot verify OTP with 2FA disabled'); |
62
|
|
|
|
63
|
|
|
$this->user->createOtpSecret('felix.lan'); |
64
|
|
|
$this->user->setOtp(true); |
65
|
|
|
|
66
|
|
|
self::assertFalse($this->user->verifyOtp('123456'), 'Wrong OTP given'); |
67
|
|
|
|
68
|
|
|
$otp = Factory::loadFromProvisioningUri($this->user->getOtpUri()); |
|
|
|
|
69
|
|
|
|
70
|
|
|
self::assertTrue($this->user->verifyOtp($otp->now()), 'Correct OTP given'); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|