En hyfsat fungerande version på plats nu
This commit is contained in:
123
alla.py
123
alla.py
@@ -1,5 +1,5 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
import base64
|
import base64
|
||||||
import hashlib
|
import hashlib
|
||||||
@@ -9,14 +9,77 @@ import sys
|
|||||||
import time
|
import time
|
||||||
|
|
||||||
import json
|
import json
|
||||||
from pprint import pprint
|
|
||||||
|
|
||||||
import argparse
|
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():
|
class TOTP():
|
||||||
@@ -58,36 +121,34 @@ class TOTP():
|
|||||||
raise TOTP.TOTPException('Invalid secret')
|
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 <secret>'
|
|
||||||
|
|
||||||
try:
|
|
||||||
print(TOTP(token).generate())
|
|
||||||
except TOTP.TOTPException as e:
|
|
||||||
return 'Error: ' + e.msg
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
for p in data:
|
for p in data:
|
||||||
print (TOTP(data["secret"]).generate())
|
print (TOTP(data["secret"]).generate())
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
parser = argparse.ArgumentParser()
|
args = get_arguments() # Get args from cmdline
|
||||||
parser.add_argument("site")
|
cfg = read_config(args) # Read from the cfg file
|
||||||
args = parser.parse_args()
|
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:
|
for p in data:
|
||||||
if p["label"].strip().lower() == args.site.lower():
|
if p["label"].strip().lower() == args.site[0].lower():
|
||||||
print(TOTP(p["secret"]).generate())
|
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)
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user