Add registration of device

This commit is contained in:
Jake Howard 2016-05-19 21:32:01 +01:00
parent adf7ee12b8
commit bf63979796
5 changed files with 44 additions and 0 deletions

16
yubi_lock/api/user.py Normal file
View file

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

View file

@ -1,4 +1,5 @@
import yubico import yubico
from yubi_lock.api.user import get_username
def get_all_yubikeys(debug): def get_all_yubikeys(debug):
@ -12,3 +13,7 @@ def get_all_yubikeys(debug):
except yubico.yubikey.YubiKeyError: except yubico.yubikey.YubiKeyError:
pass pass
return keys return keys
def generate_ident(key):
return "{}:{}".format(get_username(), key.serial())

1
yubi_lock/data/keys.json Normal file
View file

@ -0,0 +1 @@
["jake:4547568"]

View file

22
yubi_lock/register/cli.py Normal file
View file

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