1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Firesphere\YubiAuth\Tests; |
4
|
|
|
|
5
|
|
|
use Firesphere\YubiAuth\Providers\YubikeyAuthProvider; |
6
|
|
|
use SilverStripe\Core\Config\Config; |
7
|
|
|
use SilverStripe\Core\Injector\Injector; |
8
|
|
|
use SilverStripe\Dev\Debug; |
9
|
|
|
use SilverStripe\Dev\SapphireTest; |
10
|
|
|
use SilverStripe\ORM\ValidationResult; |
11
|
|
|
use SilverStripe\Security\Member; |
12
|
|
|
|
13
|
|
|
class YubikeyAuthProviderTest extends SapphireTest |
14
|
|
|
{ |
15
|
|
|
protected static $fixture_file = '../fixtures/Member.yml'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var ValidationResult |
19
|
|
|
*/ |
20
|
|
|
protected $result; |
21
|
|
|
/** |
22
|
|
|
* @var YubikeyAuthProvider |
23
|
|
|
*/ |
24
|
|
|
protected $provider; |
25
|
|
|
|
26
|
|
|
public function testCheckNoYubikeyDaysZero() |
27
|
|
|
{ |
28
|
|
|
Config::modify()->set(YubikeyAuthProvider::class, 'MaxNoYubiLoginDays', 0); |
29
|
|
|
/** @var Member $member */ |
30
|
|
|
$member = Member::get()->filter(['Email' => '[email protected]'])->first(); |
31
|
|
|
$member->Created = date('Y-m-d', strtotime('-1 year')); |
32
|
|
|
$member->MFAEnabled = false; |
33
|
|
|
$member->write(); |
34
|
|
|
|
35
|
|
|
$result = $this->provider->checkNoYubiDays($member); |
36
|
|
|
$this->assertInstanceOf(Member::class, $result); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testCheckNoYubikeyDaysError() |
40
|
|
|
{ |
41
|
|
|
/** @var Member $member */ |
42
|
|
|
$member = Member::get()->filter(['Email' => '[email protected]'])->first(); |
43
|
|
|
$member->Created = date('Y-m-d', strtotime('-1 year')); |
44
|
|
|
$member->MFAEnabled = false; |
45
|
|
|
$member->write(); |
46
|
|
|
|
47
|
|
|
$result = $this->provider->checkNoYubiDays($member); |
48
|
|
|
$this->assertInstanceOf(ValidationResult::class, $result); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testvalidateTokenDuplicate() |
52
|
|
|
{ |
53
|
|
|
$member1 = Member::create([ |
54
|
|
|
'Email' => 'user' . uniqid('', false) . '[email protected]', |
55
|
|
|
'Yubikey' => '1234567890', |
56
|
|
|
'MFAEnabled' => true |
57
|
|
|
]); |
58
|
|
|
$member1->write(); |
59
|
|
|
$member2 = Member::create([ |
60
|
|
|
'Email' => 'user' . uniqid('', false) . '[email protected]', |
61
|
|
|
'Yubikey' => '1234567890', |
62
|
|
|
'MFAEnabled' => true |
63
|
|
|
]); |
64
|
|
|
$member2->write(); |
65
|
|
|
|
66
|
|
|
$this->provider->validateToken($member1, '1234567890', $this->result); |
67
|
|
|
|
68
|
|
|
$this->assertInstanceOf(ValidationResult::class, $this->result); |
69
|
|
|
$this->assertFalse($this->result->isValid()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testvalidateTokenID() |
73
|
|
|
{ |
74
|
|
|
$member1 = Member::create([ |
75
|
|
|
'Email' => 'user' . uniqid('', false) . '[email protected]', |
76
|
|
|
'Yubikey' => '0987654321', |
77
|
|
|
'MFAEnabled' => true |
78
|
|
|
]); |
79
|
|
|
$member1->write(); |
80
|
|
|
$member2 = Member::create([ |
81
|
|
|
'Email' => 'user' . uniqid('', false) . '[email protected]', |
82
|
|
|
'Yubikey' => '1234567890', |
83
|
|
|
'MFAEnabled' => true |
84
|
|
|
]); |
85
|
|
|
$member2->write(); |
86
|
|
|
|
87
|
|
|
$this->provider->validateToken($member1, '1234567890', $this->result); |
88
|
|
|
|
89
|
|
|
$this->assertInstanceOf(ValidationResult::class, $this->result); |
90
|
|
|
$this->assertFalse($this->result->isValid()); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testvalidateTokenNotMatchesMember() |
94
|
|
|
{ |
95
|
|
|
$member1 = Member::create([ |
96
|
|
|
'Email' => 'user' . uniqid('', false) . '[email protected]', |
97
|
|
|
'Yubikey' => 'abcdefghij', |
98
|
|
|
'MFAEnabled' => true |
99
|
|
|
]); |
100
|
|
|
$member1->write(); |
101
|
|
|
|
102
|
|
|
$this->provider->validateToken($member1, '1234567890', $this->result); |
103
|
|
|
|
104
|
|
|
$this->assertInstanceOf(ValidationResult::class, $this->result); |
105
|
|
|
$this->assertFalse($this->result->isValid()); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testvalidateTokenUnique() |
109
|
|
|
{ |
110
|
|
|
$member1 = Member::create([ |
111
|
|
|
'Email' => 'user' . uniqid('', false) . '[email protected]', |
112
|
|
|
'Yubikey' => 'abcdefghij', |
113
|
|
|
'MFAEnabled' => true |
114
|
|
|
]); |
115
|
|
|
$member1->write(); |
116
|
|
|
$member2 = Member::create([ |
117
|
|
|
'Email' => 'user' . uniqid('', false) . '[email protected]', |
118
|
|
|
'Yubikey' => '1234567890', |
119
|
|
|
'MFAEnabled' => true |
120
|
|
|
]); |
121
|
|
|
$member2->write(); |
122
|
|
|
|
123
|
|
|
$this->provider->validateToken($member1, 'abcdefghij', $this->result); |
124
|
|
|
|
125
|
|
|
$this->assertInstanceOf(ValidationResult::class, $this->result); |
126
|
|
|
$this->assertTrue($this->result->isValid()); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function testSingleHost() |
130
|
|
|
{ |
131
|
|
|
Config::modify()->set(YubikeyAuthProvider::class, 'AuthURL', 'localhost'); |
132
|
|
|
|
133
|
|
|
/** @var YubikeyAuthProvider $provider */ |
134
|
|
|
$provider = Injector::inst()->get(YubikeyAuthProvider::class); |
135
|
|
|
|
136
|
|
|
$url = $provider->getService()->getHost(); |
137
|
|
|
|
138
|
|
|
$this->assertEquals('localhost', $url); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function testHost() |
142
|
|
|
{ |
143
|
|
|
Config::modify()->set(YubikeyAuthProvider::class, 'AuthURL', ['localhost-1', 'localhost-2']); |
144
|
|
|
|
145
|
|
|
/** @var YubikeyAuthProvider $provider */ |
146
|
|
|
$provider = Injector::inst()->get(YubikeyAuthProvider::class); |
147
|
|
|
|
148
|
|
|
$url = $provider->getService()->getHost(); |
149
|
|
|
|
150
|
|
|
$this->assertContains('localhost', $url); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
protected function setUp() |
154
|
|
|
{ |
155
|
|
|
$this->provider = Injector::inst()->get(YubikeyAuthProvider::class); |
156
|
|
|
$this->result = Injector::inst()->get(ValidationResult::class); |
157
|
|
|
return parent::setUp(); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|