22 lines
703 B
Python
22 lines
703 B
Python
import pyotp
|
|
import qrcode
|
|
from io import BytesIO
|
|
import base64
|
|
|
|
def generate_mfa_secret() -> str:
|
|
return pyotp.random_base32()
|
|
|
|
def get_mfa_uri(email: str, secret: str, issuer: str = "iFixKart") -> str:
|
|
return pyotp.totp.TOTP(secret).provisioning_uri(name=email, issuer_name=issuer)
|
|
|
|
def verify_mfa_token(secret: str, token: str) -> bool:
|
|
if not secret or not token:
|
|
return False
|
|
totp = pyotp.TOTP(secret)
|
|
return totp.verify(token, valid_window=1)
|
|
|
|
def qr_code_png_base64(otpauth_uri: str) -> str:
|
|
img = qrcode.make(otpauth_uri)
|
|
buffer = BytesIO()
|
|
img.save(buffer, format="PNG")
|
|
return "data:image/png;base64," + base64.b64encode(buffer.getvalue()).decode()
|