Städar upp i filen, tar bort en del redundant

This commit is contained in:
2018-07-14 11:47:15 +02:00
parent 6bf08e69c4
commit 658a221900

55
alla.py
View File

@@ -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])