forked from mricon/totp-cgi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
193 lines (153 loc) · 6.62 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/python -tt
import unittest
import totpcgi
import pyotp
import time
import json
import sys
import os
secrets_dir = 'test/secrets'
status_dir = 'test/status'
class GATest(unittest.TestCase):
def cleanStatus(self, user='valid'):
status_file = os.path.join(status_dir, '%s.json' % user)
if os.access(status_file, os.R_OK):
os.unlink(status_file)
def setCustomStatus(self, status, user='valid'):
status_file = os.path.join(status_dir, '%s.json' % user)
fh = open(status_file, 'w')
json.dump(status, fh, indent=4)
fh.close()
def setUp(self):
# Remove any existing status files for user "valid"
self.cleanStatus()
def tearDown(self):
self.cleanStatus()
def getValidUser(self):
return totpcgi.GAUser('valid', secrets_dir, status_dir)
def testValidSecretParsing(self):
gau = self.getValidUser()
self.assertEqual(gau.totp.secret, 'VN7J5UVLZEP7ZAGM',
'Secret read from valid.totp did not match')
self.assertEqual(gau.user, 'valid',
'User did not match')
self.assertEqual(gau.rate_limit, (4, 40),
'RATE_LIMIT did not parse correctly')
self.assertEqual(gau.window_size, 18,
'WINDOW_SIZE did not parse correctly')
scratch_tokens = [88709766,11488461,27893432,60474774,10449492]
self.assertItemsEqual(scratch_tokens, gau.scratch_tokens)
def testInvalidSecretParsing(self):
with self.assertRaises(totpcgi.UserFileError):
totpcgi.GAUser('invalid', secrets_dir, status_dir)
def testInvalidUsername(self):
with self.assertRaisesRegexp(totpcgi.VerifyFailed,
'invalid characters'):
gau = totpcgi.GAUser('../../etc/passwd', secrets_dir, status_dir)
def testNonExistentValidUser(self):
with self.assertRaises(totpcgi.UserNotFound):
gau = totpcgi.GAUser('[email protected]', secrets_dir, status_dir)
def testValidToken(self):
gau = self.getValidUser()
totp = pyotp.TOTP(gau.totp.secret)
token = totp.now()
self.assertEqual(gau.verify_token(token), 'Valid token used')
# try using it again
with self.assertRaisesRegexp(totpcgi.VerifyFailed, 'been used once'):
gau.verify_token(token)
# and again, to make sure it is preserved in status
with self.assertRaisesRegexp(totpcgi.VerifyFailed, 'been used once'):
gau.verify_token(token)
def testWindowSize(self):
gau = self.getValidUser()
totp = pyotp.TOTP(gau.totp.secret)
# get a token from 60 seconds ago
past_token = totp.at(int(time.time())-60)
future_token = totp.at(int(time.time())+60)
# this should fail
gau.window_size = 0
with self.assertRaisesRegexp(totpcgi.VerifyFailed, 'Not a valid token'):
gau.verify_token(past_token)
with self.assertRaisesRegexp(totpcgi.VerifyFailed, 'Not a valid token'):
gau.verify_token(future_token)
# this should work
gau.window_size = 10
self.assertEqual(gau.verify_token(past_token),
'Valid token within window size used')
self.assertEqual(gau.verify_token(future_token),
'Valid token within window size used')
def testRateLimit(self):
gau = self.getValidUser()
# just in case the lightning strikes at that very number
if gau.now_token == 555555:
token = '555556'
else:
token = '555555'
# We now fail 4 times consecutively
with self.assertRaisesRegexp(totpcgi.VerifyFailed, 'Not a valid token'):
gau.verify_token(token)
with self.assertRaisesRegexp(totpcgi.VerifyFailed, 'Not a valid token'):
gau.verify_token(token)
with self.assertRaisesRegexp(totpcgi.VerifyFailed, 'Not a valid token'):
gau.verify_token(token)
with self.assertRaisesRegexp(totpcgi.VerifyFailed, 'Not a valid token'):
gau.verify_token(token)
# We should now get a rate-limited error
with self.assertRaisesRegexp(totpcgi.VerifyFailed, 'Rate-limit'):
gau.verify_token(token)
# Same with a valid token
with self.assertRaisesRegexp(totpcgi.VerifyFailed, 'Rate-limit'):
gau.verify_token(gau.now_token)
# Make sure we recover from rate-limiting correctly
old_timestamp = gau.now_timestamp-(31+(gau.rate_limit[1]*10))
status = {
'fail_timestamps': [
old_timestamp,
old_timestamp,
old_timestamp,
old_timestamp
],
'success_timestamps': [],
'used_scratch_tokens': []
}
self.setCustomStatus(status)
with self.assertRaisesRegexp(totpcgi.VerifyFailed, 'Not a valid token'):
gau.verify_token(token)
# Valid token should work, too
self.setCustomStatus(status)
self.assertEqual(gau.verify_token(gau.now_token), 'Valid token used')
def testInvalidToken(self):
gau = self.getValidUser()
# just in case the lightning strikes at that very number
if gau.now_token == 555555:
token = '555556'
else:
token = '555555'
with self.assertRaisesRegexp(totpcgi.VerifyFailed, 'Not a valid token'):
gau.verify_token(token)
with self.assertRaisesRegexp(totpcgi.VerifyFailed, 'too long'):
self.cleanStatus()
gau.verify_token('12345678910')
with self.assertRaisesRegexp(totpcgi.VerifyFailed, 'not an integer'):
self.cleanStatus()
gau.verify_token('WAKKA')
with self.assertRaisesRegexp(totpcgi.VerifyFailed,
'Not a valid scratch-token'):
gau.verify_token('11112222')
def testScratchTokens(self):
gau = self.getValidUser()
ret = gau.verify_token('88709766')
self.assertEqual(ret, 'Scratch-token used')
# try using it again
with self.assertRaisesRegexp(totpcgi.VerifyFailed,
'Scratch-token already used once'):
gau.verify_token('88709766')
# try using another token
ret = gau.verify_token('11488461')
self.assertEqual(ret, 'Scratch-token used')
# use first one again to make sure it's preserved in the status file
with self.assertRaisesRegexp(totpcgi.VerifyFailed,
'Scratch-token already used once'):
gau.verify_token('88709766')
if __name__ == '__main__':
unittest.main()