forked from tilkinsc/LuaOTP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtotp.lua
More file actions
42 lines (33 loc) · 1.09 KB
/
totp.lua
File metadata and controls
42 lines (33 loc) · 1.09 KB
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
-- please point this to proper location
local otp = require("otp")
local totp = {}
totp.at = function(instance, for_time, counter_offset)
if (for_time == nil) then
error("No for_time supplied.")
end
return otp.generate_otp(instance, totp.timecode(instance, tonumber(for_time)) + (counter_offset or 0))
end
totp.now = function(instance)
return otp.generate_otp(instance, totp.timecode(instance, os.time()))
end
totp.valid_until = function(instance, for_time, valid_window)
valid_window = valid_window or 0
return for_time + ((self.interval + 1) * valid_window)
end
totp.verify = function(instance, key, for_time, valid_window)
valid_window = valid_window or 0
for_time = for_time or os.time()
if (valid_window > 0) then
for i=-valid_window, valid_window, 1 do
if (tostring(key) == tostring(totp.at(instance, for_time, i))) then
return true
end
end
return false
end
return tostring(key) == tostring(totp.at(instance, for_time))
end
totp.timecode = function(instance, for_time)
return math.floor(for_time/instance.interval)
end
return totp