|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the login-cidadao project or it's bundles. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Guilherme Donato <guilhermednt on github> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace LoginCidadao\CoreBundle\Tests\Security; |
|
12
|
|
|
|
|
13
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
14
|
|
|
use LoginCidadao\CoreBundle\Entity\BackupCode; |
|
15
|
|
|
use LoginCidadao\CoreBundle\Model\PersonInterface; |
|
16
|
|
|
use LoginCidadao\CoreBundle\Security\TwoFactorAuthenticationService; |
|
17
|
|
|
use Google\Authenticator\GoogleAuthenticator as Google; |
|
18
|
|
|
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Google\GoogleAuthenticator; |
|
19
|
|
|
|
|
20
|
|
|
class TwoFactorAuthenticationServiceTest extends \PHPUnit_Framework_TestCase |
|
21
|
|
|
{ |
|
22
|
|
|
public function testEnable() |
|
23
|
|
|
{ |
|
24
|
|
|
$person = $this->getPerson(); |
|
25
|
|
|
|
|
26
|
|
|
$em = $this->getEntityManager(); |
|
27
|
|
|
$em->expects($this->exactly(11))->method('persist'); // 11 times: 10 BackupCodes and 1 PersonInterface |
|
|
|
|
|
|
28
|
|
|
$em->expects($this->once())->method('flush'); |
|
29
|
|
|
|
|
30
|
|
|
$google = new Google(); |
|
31
|
|
|
$twoFactor = new TwoFactorAuthenticationService($em, $this->getGoogleAuthenticator($google)); |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
$secret = $twoFactor->generateSecret(); |
|
34
|
|
|
$person->expects($this->once())->method('getGoogleAuthenticatorSecret') |
|
|
|
|
|
|
35
|
|
|
->willReturn($secret); |
|
36
|
|
|
|
|
37
|
|
|
$twoFactor->enable($person, $google->getCode($secret)); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function testEnableWrongCode() |
|
41
|
|
|
{ |
|
42
|
|
|
$this->setExpectedException('\InvalidArgumentException'); |
|
43
|
|
|
|
|
44
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|PersonInterface $person */ |
|
45
|
|
|
$person = $this->getMock('LoginCidadao\CoreBundle\Model\PersonInterface'); |
|
46
|
|
|
|
|
47
|
|
|
$twoFactor = new TwoFactorAuthenticationService($this->getEntityManager(), $this->getGoogleAuthenticator()); |
|
|
|
|
|
|
48
|
|
|
$person->expects($this->once())->method('getGoogleAuthenticatorSecret') |
|
|
|
|
|
|
49
|
|
|
->willReturn($twoFactor->generateSecret()); |
|
50
|
|
|
$twoFactor->enable($person, 'WRONG'); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function testDisable() |
|
54
|
|
|
{ |
|
55
|
|
|
$person = $this->getPerson(); |
|
56
|
|
|
$person->expects($this->once())->method('setGoogleAuthenticatorSecret')->with(null); |
|
|
|
|
|
|
57
|
|
|
$person->expects($this->once())->method('getBackupCodes')->willReturn($this->getBackupCodes($person)); |
|
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
$em = $this->getEntityManager(); |
|
60
|
|
|
$em->expects($this->exactly(10))->method('remove') |
|
|
|
|
|
|
61
|
|
|
->with($this->isInstanceOf('LoginCidadao\CoreBundle\Entity\BackupCode')); |
|
62
|
|
|
$em->expects($this->once())->method('persist')->with($person); |
|
63
|
|
|
$em->expects($this->once())->method('flush'); |
|
64
|
|
|
|
|
65
|
|
|
$twoFactor = new TwoFactorAuthenticationService($em, $this->getGoogleAuthenticator()); |
|
|
|
|
|
|
66
|
|
|
$this->assertTrue($twoFactor->disable($person)); |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function testGenerateBackupCodes() |
|
70
|
|
|
{ |
|
71
|
|
|
$person = $this->getPerson(); |
|
72
|
|
|
|
|
73
|
|
|
$em = $this->getEntityManager(); |
|
74
|
|
|
$em->expects($this->exactly(10))->method('persist') |
|
|
|
|
|
|
75
|
|
|
->with($this->isInstanceOf('LoginCidadao\CoreBundle\Entity\BackupCode')); |
|
76
|
|
|
|
|
77
|
|
|
$twoFactor = new TwoFactorAuthenticationService($em, $this->getGoogleAuthenticator()); |
|
|
|
|
|
|
78
|
|
|
$backupCodes = $twoFactor->generateBackupCodes($person); |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
foreach ($backupCodes as $backupCode) { |
|
81
|
|
|
$this->assertEquals($person, $backupCode->getPerson()); |
|
82
|
|
|
$this->assertNotNull($backupCode->getCode()); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function testRemoveBackupCodes() |
|
87
|
|
|
{ |
|
88
|
|
|
$person = $this->getPerson(); |
|
89
|
|
|
|
|
90
|
|
|
$person->expects($this->once())->method('getBackupCodes')->willReturn($this->getBackupCodes($person)); |
|
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
$em = $this->getEntityManager(); |
|
93
|
|
|
$em->expects($this->exactly(10))->method('remove') |
|
|
|
|
|
|
94
|
|
|
->with($this->isInstanceOf('LoginCidadao\CoreBundle\Entity\BackupCode')); |
|
95
|
|
|
|
|
96
|
|
|
$twoFactor = new TwoFactorAuthenticationService($em, $this->getGoogleAuthenticator()); |
|
|
|
|
|
|
97
|
|
|
$twoFactor->removeBackupCodes($person); |
|
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function testGetSecretUrl() |
|
101
|
|
|
{ |
|
102
|
|
|
$url = 'https://secret.url'; |
|
103
|
|
|
$person = $this->getPerson(); |
|
104
|
|
|
|
|
105
|
|
|
$googleAuthClass = 'Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Google\GoogleAuthenticator'; |
|
106
|
|
|
$googleAuth = $this->getMockBuilder($googleAuthClass)->disableOriginalConstructor()->getMock(); |
|
107
|
|
|
$googleAuth->expects($this->once())->method('getUrl')->with($person)->willReturn($url); |
|
108
|
|
|
|
|
109
|
|
|
$twoFactor = new TwoFactorAuthenticationService($this->getEntityManager(), $googleAuth); |
|
|
|
|
|
|
110
|
|
|
$this->assertEquals($url, $twoFactor->getSecretUrl($person)); |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|EntityManagerInterface |
|
115
|
|
|
*/ |
|
116
|
|
|
private function getEntityManager() |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->getMock('Doctrine\ORM\EntityManagerInterface'); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
private function getGoogleAuthenticator($googleAuth = null) |
|
122
|
|
|
{ |
|
123
|
|
|
if (!$googleAuth) { |
|
124
|
|
|
$googleAuth = new Google(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return new GoogleAuthenticator($googleAuth, 'server', 'issuer'); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|PersonInterface |
|
132
|
|
|
*/ |
|
133
|
|
|
private function getPerson() |
|
134
|
|
|
{ |
|
135
|
|
|
return $this->getMock('LoginCidadao\CoreBundle\Model\PersonInterface'); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @param PersonInterface $person |
|
140
|
|
|
* @return BackupCode[] |
|
141
|
|
|
*/ |
|
142
|
|
|
private function getBackupCodes(PersonInterface $person) |
|
143
|
|
|
{ |
|
144
|
|
|
$backupCodes = []; |
|
145
|
|
|
while (count($backupCodes) < 10) { |
|
146
|
|
|
$backupCodes[] = (new BackupCode()) |
|
147
|
|
|
->setPerson($person) |
|
148
|
|
|
->setCode(random_int(1111, 9999)); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
return $backupCodes; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: