From 658a2219000f08fbadde0f097dbf1daf06bf4787 Mon Sep 17 00:00:00 2001 From: Fredrik Wahlberg Date: Sat, 14 Jul 2018 11:47:15 +0200 Subject: [PATCH] =?UTF-8?q?St=C3=A4dar=20upp=20i=20filen,=20tar=20bort=20e?= =?UTF-8?q?n=20del=20redundant?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- alla.py | 55 ++++++++++++++++++++++++++----------------------------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/alla.py b/alla.py index 7aa1e36..578241c 100755 --- a/alla.py +++ b/alla.py @@ -1,17 +1,16 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- from __future__ import print_function +import argparse import base64 +import ConfigParser import hashlib import hmac +import json +import os import struct -import sys import time -import json -import argparse -import ConfigParser -import os def die(reason): @@ -20,6 +19,7 @@ def die(reason): print("Error message -", reason) exit(1) + def get_arguments(): # Get input from the command line parser = argparse.ArgumentParser() @@ -30,7 +30,9 @@ def get_arguments(): args = parser.parse_args() return args + def menu(data): + # Print a pretty menu to choose from keynum = 1 print(" ---------------- Available keys ----------------") print(" |") @@ -45,18 +47,22 @@ def menu(data): print() return keynum + def print_OTP(secret): + # Generate the key and pretty print it value = TOTP(secret).generate() - # Formatera svaret som XXX XXX - # De först 3 tecknen och sedan resten + # Format response like XXX XXX print(value[:3], value[3:]) def read_config(args): # Read from the config file config = ConfigParser.ConfigParser() - if not config.read(args.config): - # Felaktig sökväg till configfilen + + try: + config.read(args.config) + except: + # Error in config file die("Could not read %s" % args.config) # Verify that the config is correct @@ -66,11 +72,11 @@ def read_config(args): die("Could not find path to 'andOTPfile' in %s" % args.config) if not os.path.isfile(config.get('totp', 'andOTPfile')): - # Hittar inte die("The file %s does not exist" % config.get('totp', 'andOTPfile')) return config + def read_file(config): # Open and parse the data file try: @@ -121,34 +127,25 @@ class TOTP(): raise TOTP.TOTPException('Invalid secret') -def main(): - for p in data: - print (TOTP(data["secret"]).generate()) - if __name__ == '__main__': args = get_arguments() # Get args from cmdline cfg = read_config(args) # Read from the cfg file data = read_file(cfg) # Open the data file - if not args.site: - waiting = True - while waiting: - menu(data) - site = input("Key #: ") - if site: - print_OTP(data[site-1]["secret"]) # -1 since index start with 0 and the menu with 1 - waiting = False - exit(0) + if not args.site: # Show a menu if no input + menu(data) + site = input("Key #: ") + if site: + print_OTP(data[site-1]["secret"]) # -1 since index start with 0 and the menu with 1 + exit(0) - - found = False - for p in data: + for p in data: # Try to find a matching site if p["label"].strip().lower() == args.site[0].lower(): print_OTP(p["secret"]) - found = True + exit(0) - if not found: - die("Could not find %s in the andOTP file" % args.site[0]) + + die("Could not find '%s' in the andOTP file" % args.site[0])