From 6bf08e69c4820e9b33520a1eb28ac4f0790d7272 Mon Sep 17 00:00:00 2001 From: Fredrik Wahlberg Date: Sat, 14 Jul 2018 11:34:24 +0200 Subject: [PATCH] =?UTF-8?q?En=20hyfsat=20fungerande=20version=20p=C3=A5=20?= =?UTF-8?q?plats=20nu?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- alla.py | 123 ++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 92 insertions(+), 31 deletions(-) diff --git a/alla.py b/alla.py index 60fa482..7aa1e36 100755 --- a/alla.py +++ b/alla.py @@ -1,5 +1,5 @@ #!/usr/bin/env python - +# -*- coding: utf-8 -*- from __future__ import print_function import base64 import hashlib @@ -9,14 +9,77 @@ import sys import time import json -from pprint import pprint - import argparse +import ConfigParser +import os -# laddar filen -with open('/home/fredrik/otp_accounts.json') as filen: - data = json.load(filen) +def die(reason): + # Terminate with an error message + print("Ecountered an error, terminating") + print("Error message -", reason) + exit(1) + +def get_arguments(): + # Get input from the command line + parser = argparse.ArgumentParser() + parser.add_argument("site", nargs="*") + default_cfg_path = os.environ['HOME'] + '/.totprc' + parser.add_argument("-c", "--config", default=default_cfg_path, help='Path to config-file. Defaults to ~/.totprc') + + args = parser.parse_args() + return args + +def menu(data): + keynum = 1 + print(" ---------------- Available keys ----------------") + print(" |") + for p in data: + print(" | %2d - %s" % (keynum, p["label"])) + keynum = keynum + 1 + + print(" |") + print(" ------------------------------------------------") + print(" | Please select which key to generate [1 - %d] |" % keynum) + print(" ------------------------------------------------") + print() + return keynum + +def print_OTP(secret): + value = TOTP(secret).generate() + # Formatera svaret som XXX XXX + # De först 3 tecknen och sedan resten + 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 + die("Could not read %s" % args.config) + + # Verify that the config is correct + try: + config.get('totp', 'andOTPfile') + except: + 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: + with open(config.get('totp', 'andOTPfile')) as filen: + data = json.load(filen) + except: + die("Error parsing JSON, corrupt andOTP-file?") + + return data class TOTP(): @@ -58,36 +121,34 @@ class TOTP(): raise TOTP.TOTPException('Invalid secret') -def showme(args): - if len(args): - token = sys.stdin.readline().strip() if args[0] == '-' else args[0] - elif not sys.stdin.isatty(): - token = sys.stdin.readline().strip() - else: - return 'Usage: totp ' - - try: - print(TOTP(token).generate()) - except TOTP.TOTPException as e: - return 'Error: ' + e.msg - def main(): for p in data: print (TOTP(data["secret"]).generate()) if __name__ == '__main__': - parser = argparse.ArgumentParser() - parser.add_argument("site") - args = parser.parse_args() - + 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) + + + found = False for p in data: - if p["label"].strip().lower() == args.site.lower(): - print(TOTP(p["secret"]).generate()) + if p["label"].strip().lower() == args.site[0].lower(): + print_OTP(p["secret"]) + found = True + + if not found: + die("Could not find %s in the andOTP file" % args.site[0]) + -# try: -# sys.exit(main(sys.argv[1:])) -# except KeyboardInterrupt: -# sys.exit(1) - -