-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.py
More file actions
37 lines (29 loc) · 1.06 KB
/
Copy pathauth.py
File metadata and controls
37 lines (29 loc) · 1.06 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
import pyotp
from dotenv import load_dotenv
import os
import traceback
import sys
load_dotenv()
class TOTPctl:
def __init__(self):
print("TOTPctl initializing...", end="")
try:
self.secret = os.getenv("TOTP_SECRET")
if not self.secret:
print("secret will be generated and saved to .env file")
self.secret = pyotp.random_base32()
with open(".env", "a") as f:
f.write(f"\nTOTP_SECRET={self.secret}\n")
self.totp = pyotp.TOTP(self.secret)
except Exception as e:
print(f" [FAIL] - {e}")
traceback.print_exc()
sys.exit(1)
else:
print(" [OK]")
def get_uri(self, user):
print(f"SECURITY LOG: generating TOTP URI for user {user}")
return self.totp.provisioning_uri(name=user, issuer_name="CPWB theSunset")
def verify(self, code):
print(f"SECURITY LOG: verifying TOTP code")
return self.totp.verify(code)