Improve error handling and user feedback in OTP key generation and reading functions
This commit is contained in:
parent
5ce8ac58c5
commit
3d17016823
29
ft_otp.py
29
ft_otp.py
@ -26,7 +26,6 @@ def main():
|
|||||||
elif args.q:
|
elif args.q:
|
||||||
# Call the function to generate a QR code for the OTP key
|
# Call the function to generate a QR code for the OTP key
|
||||||
generate_qr_code()
|
generate_qr_code()
|
||||||
print("QR code generated successfully.")
|
|
||||||
else:
|
else:
|
||||||
print("No action specified. Use -g to generate a new OTP key or -k to generate a OTP from a key file.")
|
print("No action specified. Use -g to generate a new OTP key or -k to generate a OTP from a key file.")
|
||||||
parser.print_help()
|
parser.print_help()
|
||||||
@ -41,8 +40,15 @@ def is_hex(s):
|
|||||||
def generate_secret_key(secret_file):
|
def generate_secret_key(secret_file):
|
||||||
print(f"Generating OTP key from {secret_file}...")
|
print(f"Generating OTP key from {secret_file}...")
|
||||||
machine_id = get_machine_hash()
|
machine_id = get_machine_hash()
|
||||||
with open(secret_file, 'rb') as f:
|
try:
|
||||||
|
with open(secret_file, 'r') as f:
|
||||||
secret = f.read().strip()
|
secret = f.read().strip()
|
||||||
|
except FileNotFoundError:
|
||||||
|
print(f"Secret file {secret_file} not found. Please provide a valid file.")
|
||||||
|
return
|
||||||
|
if not secret:
|
||||||
|
print("The provided secret file is empty. Please provide a valid secret.")
|
||||||
|
return
|
||||||
if not is_hex(secret.decode()):
|
if not is_hex(secret.decode()):
|
||||||
print("The provided secret is not a valid hexadecimal string.")
|
print("The provided secret is not a valid hexadecimal string.")
|
||||||
return
|
return
|
||||||
@ -51,14 +57,29 @@ def generate_secret_key(secret_file):
|
|||||||
return
|
return
|
||||||
fernet = Fernet(machine_id)
|
fernet = Fernet(machine_id)
|
||||||
secret_enc = fernet.encrypt(secret)
|
secret_enc = fernet.encrypt(secret)
|
||||||
|
try:
|
||||||
with open('./ft_otp.key', 'wb') as f:
|
with open('./ft_otp.key', 'wb') as f:
|
||||||
f.write(secret_enc)
|
f.write(secret_enc)
|
||||||
|
print("OTP key generated and saved to ft_otp.key")
|
||||||
|
except IOError as e:
|
||||||
|
print(f"Error writing to file: {e}")
|
||||||
|
return
|
||||||
|
|
||||||
def read_secret_key(keyfile):
|
def read_secret_key(keyfile):
|
||||||
"""Read the secret key from the specified file."""
|
"""Read the secret key from the specified file."""
|
||||||
machine_id = get_machine_hash()
|
machine_id = get_machine_hash()
|
||||||
|
if machine_id is None:
|
||||||
|
print("Machine ID could not be retrieved. Exiting.")
|
||||||
|
return None
|
||||||
|
try:
|
||||||
with open(keyfile, 'rb') as f:
|
with open(keyfile, 'rb') as f:
|
||||||
secret_enc = f.read().strip()
|
secret_enc = f.read().strip()
|
||||||
|
except FileNotFoundError:
|
||||||
|
print(f"Key file {keyfile} not found. Please provide a valid file.")
|
||||||
|
return None
|
||||||
|
if not secret_enc:
|
||||||
|
print("The provided key file is empty. Please provide a valid key file.")
|
||||||
|
return None
|
||||||
fernet = Fernet(machine_id)
|
fernet = Fernet(machine_id)
|
||||||
secret = fernet.decrypt(secret_enc)
|
secret = fernet.decrypt(secret_enc)
|
||||||
return secret
|
return secret
|
||||||
@ -67,10 +88,8 @@ def generate_qr_code():
|
|||||||
"""Generate a QR code for the OTP key."""
|
"""Generate a QR code for the OTP key."""
|
||||||
keyfile = './ft_otp.key'
|
keyfile = './ft_otp.key'
|
||||||
secret = read_secret_key(keyfile)
|
secret = read_secret_key(keyfile)
|
||||||
print(f"Secret key read from {keyfile}: {secret.decode()}")
|
|
||||||
base32 = base64.b32encode(base64.b16decode(secret.upper())).decode().strip('=')
|
base32 = base64.b32encode(base64.b16decode(secret.upper())).decode().strip('=')
|
||||||
print(str(base32))
|
otp_uri = f"otpauth://totp/FTOTP?secret={base32}&issuer=whaffman&algo=SHA1&digits=6&period=30"
|
||||||
otp_uri = f"otpauth://totp/FTOTP?secret={base32}&issuer=FTOTP&algo=SHA1&digits=6&period=30"
|
|
||||||
qrcode_terminal.draw(otp_uri)
|
qrcode_terminal.draw(otp_uri)
|
||||||
|
|
||||||
def generate_otp_from_secret_key(keyfile):
|
def generate_otp_from_secret_key(keyfile):
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user