diff --git a/yubi_lock/api/user.py b/yubi_lock/api/user.py new file mode 100644 index 0000000..ade507e --- /dev/null +++ b/yubi_lock/api/user.py @@ -0,0 +1,16 @@ +import os, pwd, json +from yubi_lock.api import BASE_DIR + + +def get_username(): + return pwd.getpwuid(os.getuid())[0] + + +def store_key(ident): + with open(os.path.join(BASE_DIR, "data/keys.json")) as file: + existing_data = json.load(file) + + existing_data.append(ident) + + with open(os.path.join(BASE_DIR, "data/keys.json"), 'w') as file: + json.dump(existing_data, file) diff --git a/yubi_lock/api/yubikey.py b/yubi_lock/api/yubikey.py index 4d0cf65..0f558be 100644 --- a/yubi_lock/api/yubikey.py +++ b/yubi_lock/api/yubikey.py @@ -1,4 +1,5 @@ import yubico +from yubi_lock.api.user import get_username def get_all_yubikeys(debug): @@ -12,3 +13,7 @@ def get_all_yubikeys(debug): except yubico.yubikey.YubiKeyError: pass return keys + + +def generate_ident(key): + return "{}:{}".format(get_username(), key.serial()) diff --git a/yubi_lock/data/keys.json b/yubi_lock/data/keys.json new file mode 100644 index 0000000..5c9128a --- /dev/null +++ b/yubi_lock/data/keys.json @@ -0,0 +1 @@ +["jake:4547568"] \ No newline at end of file diff --git a/yubi_lock/register/__init__.py b/yubi_lock/register/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/yubi_lock/register/cli.py b/yubi_lock/register/cli.py new file mode 100644 index 0000000..6249513 --- /dev/null +++ b/yubi_lock/register/cli.py @@ -0,0 +1,22 @@ +import click +from yubi_lock.api.yubikey import get_all_yubikeys, generate_ident +from yubi_lock.api.user import store_key + + +@click.command('register', short_help='Add a device') +def cli(): + print("Scanning for keys...") + keys = get_all_yubikeys(False) + + if not keys: + print("No YubiKeys detected!") + return 1 + elif len(keys) > 1: + print("Please only insert the YubiKey you wish to register. Found {}.".format(len(keys))) + return 1 + + key = keys[0] + print("Found {} with Serial {}.".format(key.description, key.serial())) + ident = generate_ident(key) + store_key(ident) + print("YubiKey has been stored, you can now use it to login")