Städar upp i filen, tar bort en del redundant
This commit is contained in:
57
alla.py
57
alla.py
@@ -1,17 +1,16 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
|
import argparse
|
||||||
import base64
|
import base64
|
||||||
|
import ConfigParser
|
||||||
import hashlib
|
import hashlib
|
||||||
import hmac
|
import hmac
|
||||||
|
import json
|
||||||
|
import os
|
||||||
import struct
|
import struct
|
||||||
import sys
|
|
||||||
import time
|
import time
|
||||||
|
|
||||||
import json
|
|
||||||
import argparse
|
|
||||||
import ConfigParser
|
|
||||||
import os
|
|
||||||
|
|
||||||
|
|
||||||
def die(reason):
|
def die(reason):
|
||||||
@@ -20,6 +19,7 @@ def die(reason):
|
|||||||
print("Error message -", reason)
|
print("Error message -", reason)
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
|
|
||||||
def get_arguments():
|
def get_arguments():
|
||||||
# Get input from the command line
|
# Get input from the command line
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
@@ -30,7 +30,9 @@ def get_arguments():
|
|||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
return args
|
return args
|
||||||
|
|
||||||
|
|
||||||
def menu(data):
|
def menu(data):
|
||||||
|
# Print a pretty menu to choose from
|
||||||
keynum = 1
|
keynum = 1
|
||||||
print(" ---------------- Available keys ----------------")
|
print(" ---------------- Available keys ----------------")
|
||||||
print(" |")
|
print(" |")
|
||||||
@@ -45,18 +47,22 @@ def menu(data):
|
|||||||
print()
|
print()
|
||||||
return keynum
|
return keynum
|
||||||
|
|
||||||
|
|
||||||
def print_OTP(secret):
|
def print_OTP(secret):
|
||||||
|
# Generate the key and pretty print it
|
||||||
value = TOTP(secret).generate()
|
value = TOTP(secret).generate()
|
||||||
# Formatera svaret som XXX XXX
|
# Format response like XXX XXX
|
||||||
# De först 3 tecknen och sedan resten
|
|
||||||
print(value[:3], value[3:])
|
print(value[:3], value[3:])
|
||||||
|
|
||||||
|
|
||||||
def read_config(args):
|
def read_config(args):
|
||||||
# Read from the config file
|
# Read from the config file
|
||||||
config = ConfigParser.ConfigParser()
|
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)
|
die("Could not read %s" % args.config)
|
||||||
|
|
||||||
# Verify that the config is correct
|
# 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)
|
die("Could not find path to 'andOTPfile' in %s" % args.config)
|
||||||
|
|
||||||
if not os.path.isfile(config.get('totp', 'andOTPfile')):
|
if not os.path.isfile(config.get('totp', 'andOTPfile')):
|
||||||
# Hittar inte
|
|
||||||
die("The file %s does not exist" % config.get('totp', 'andOTPfile'))
|
die("The file %s does not exist" % config.get('totp', 'andOTPfile'))
|
||||||
|
|
||||||
return config
|
return config
|
||||||
|
|
||||||
|
|
||||||
def read_file(config):
|
def read_file(config):
|
||||||
# Open and parse the data file
|
# Open and parse the data file
|
||||||
try:
|
try:
|
||||||
@@ -121,34 +127,25 @@ class TOTP():
|
|||||||
raise TOTP.TOTPException('Invalid secret')
|
raise TOTP.TOTPException('Invalid secret')
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
for p in data:
|
|
||||||
print (TOTP(data["secret"]).generate())
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
args = get_arguments() # Get args from cmdline
|
args = get_arguments() # Get args from cmdline
|
||||||
cfg = read_config(args) # Read from the cfg file
|
cfg = read_config(args) # Read from the cfg file
|
||||||
data = read_file(cfg) # Open the data file
|
data = read_file(cfg) # Open the data file
|
||||||
|
|
||||||
if not args.site:
|
if not args.site: # Show a menu if no input
|
||||||
waiting = True
|
menu(data)
|
||||||
while waiting:
|
site = input("Key #: ")
|
||||||
menu(data)
|
if site:
|
||||||
site = input("Key #: ")
|
print_OTP(data[site-1]["secret"]) # -1 since index start with 0 and the menu with 1
|
||||||
if site:
|
exit(0)
|
||||||
print_OTP(data[site-1]["secret"]) # -1 since index start with 0 and the menu with 1
|
|
||||||
waiting = False
|
|
||||||
exit(0)
|
|
||||||
|
|
||||||
|
for p in data: # Try to find a matching site
|
||||||
found = False
|
|
||||||
for p in data:
|
|
||||||
if p["label"].strip().lower() == args.site[0].lower():
|
if p["label"].strip().lower() == args.site[0].lower():
|
||||||
print_OTP(p["secret"])
|
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])
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user