forked from kleo/pico-2fa-totp
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
79 lines (63 loc) · 2.24 KB
/
main.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
from machine import Pin
from time import sleep_ms,time
import json
from totp import totp
import password
import PicoOled13
import cryptor
from synchronised_time import create_synchronised_time, do_connect
lcd = PicoOled13.get()
# Unlock secrets
key=password.get(lcd)
codes = json.loads(cryptor.decrypt("codes.json.encoded",key))
selected_idx = 0
code = None
# Do all required init i.e. manual time setting vs ntp
synchronised_time = create_synchronised_time(lcd,key)
# Begin,End markers for timer bar
lcd.fill_rect(0,61,4,2,lcd.white)
lcd.fill_rect(124,61,4,2,lcd.white)
# Size of text area to be wiped first time: full screen minus timer bar height of 4
# (Set this correctly and let the text clearing code handle this instead
# of lcd.clear() or lcd.fill().)
loc=(lcd.width,lcd.height-4)
screenawake=1
while True:
# Is a button pressed
if lcd.is_pressed(lcd.KEY0):
selected_idx = (selected_idx + 1) % len(codes)
if lcd.is_pressed(lcd.KEY1):
if screenawake:
screenawake=0
else:
# Empty the timer bar in the framebuffer
lcd.fill_rect(4,60,120,4,0x0000)
screenawake=1
if screenawake:
# Did the selection change from last time
if code != codes[selected_idx]:
code = codes[selected_idx]
(passwd, expiry) = totp(synchronised_time(),
code['key'],
step_secs=code['step'],
digits=code['digits'])
# is it time to recalculate things yet?
s=time()%30
if s == 0:
(passwd, expiry) = totp(synchronised_time(),
code['key'],
step_secs=code['step'],
digits=code['digits'])
# Wipe previous text (first run wipes full screen except timer bar)
lcd.fill_rect(0,0,128,loc[1],0x0000)
loc=lcd.text(code['name'],0,0,0xffff)
loc=lcd.text(passwd,0,loc[1],0xffff)
# Time left bar
if s == 0:
lcd.fill_rect(4,60,120,4,0x0000)
lcd.fill_rect(4,60,(s+1)<<2,4,0xffff)
lcd.on()
lcd.show()
else:
lcd.off()
sleep_ms(250)