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