Skip to content

Commit

Permalink
Merge pull request #9 from neil-vqa/dev
Browse files Browse the repository at this point in the history
Chore: type hints
  • Loading branch information
neil-vqa authored Aug 23, 2021
2 parents 32a5a46 + 97e86f1 commit b6fd876
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
49 changes: 29 additions & 20 deletions lihim/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import nacl.utils
import errno
import os
from typing import Optional
from typing import overload, Tuple, List
from peewee import ModelSelect
from .config import ConfigPath
from .models import User, Group, Pair
from pathlib import Path
Expand All @@ -16,7 +17,13 @@
CLI commands associated with users use these functions.
"""

def use_key(key: str, encrypt_text: Optional[str] = None, decrypt_text: Optional[bytes] = None):
@overload
def use_key(key: str, encrypt_text: str) -> nacl.utils.EncryptedMessage: ...

@overload
def use_key(key: str, decrypt_text: bytes) -> str: ...

def use_key(key, encrypt_text = None, decrypt_text = None):
with open(key, "rb") as f:
key_bin = f.read()
box = nacl.secret.SecretBox(key_bin)
Expand All @@ -30,7 +37,7 @@ def use_key(key: str, encrypt_text: Optional[str] = None, decrypt_text: Optional
decoded_text = plaintext.decode('UTF-8')
return decoded_text

def load_key():
def load_key() -> str:
try:
with open(conf.session_path, "r") as f:
current_user = json.load(f)
Expand All @@ -40,7 +47,7 @@ def load_key():
except Exception as e:
raise e

def check_key_exists(key):
def check_key_exists(key: str) -> None:
try:
key_file = Path(key)
if not key_file.exists():
Expand All @@ -62,15 +69,15 @@ def create_user(username: str, password: str, key: str) -> None:
except Exception as e:
raise e

def check_users():
def check_users() -> ModelSelect:
users = User.select()
return users

"""
Functions associated with managing the session.
"""

def enter_user(username: str, password: str, key_path: str, key_name: str):
def enter_user(username: str, password: str, key_path: str, key_name: str) -> None:
"""
Writes the entered username and password to session.json when logging in.
"""
Expand All @@ -92,7 +99,7 @@ def enter_user(username: str, password: str, key_path: str, key_name: str):
except Exception as e:
raise e

def load_session_json():
def load_session_json() -> Tuple[str, str]:
try:
with open(conf.session_path, "r") as f:
current_user = json.load(f)
Expand All @@ -104,7 +111,7 @@ def load_session_json():
except Exception as e:
raise e

def get_user(username):
def get_user(username) -> User:
"""
Gets the User instance for current_user variable.
"""
Expand All @@ -114,7 +121,7 @@ def get_user(username):
except:
raise ValueError("User does not exist. Unauthenticated.")

def check_password(current_user, password):
def check_password(current_user, password) -> None:
"""
Verifies the password in session.json against
the hashed password in the database.
Expand All @@ -127,7 +134,7 @@ def check_password(current_user, password):
except Exception as e:
raise e

def allow_user():
def allow_user() -> Tuple[bool, User]:
"""
Check if authenticated.
"""
Expand All @@ -139,7 +146,7 @@ def allow_user():
except Exception as e:
raise e

def clear_user():
def clear_user() -> None:
"""
Clears username and password in session.json.
"""
Expand All @@ -159,7 +166,7 @@ def clear_user():
use these functions.
"""

def create_group(name: str, current_user: User):
def create_group(name: str, current_user: User) -> None:
try:
new_group = Group(
name=name,
Expand All @@ -169,18 +176,18 @@ def create_group(name: str, current_user: User):
except Exception as e:
raise e

def check_groups(current_user: User):
def check_groups(current_user: User) -> ModelSelect:
groups = current_user.groups
return groups

def check_group_pairs(name: str, current_user: User):
def check_group_pairs(name: str, current_user: User) -> ModelSelect:
try:
group = Group.get(Group.user==current_user, Group.name==name)
return group.pairs
except Group.DoesNotExist:
raise ValueError("Group does not exist.")

def create_pair(key: str, value: str, group: str, current_user: User):
def create_pair(key: str, value: str, group: str, current_user: User) -> None:
try:
group_to_add = Group.get(Group.user==current_user, Group.name==group)
key_file = load_key()
Expand All @@ -192,20 +199,22 @@ def create_pair(key: str, value: str, group: str, current_user: User):
user=current_user
)
new_pair.save()
except Group.DoesNotExist:
raise ValueError("Group does not exist.")
except Exception as e:
raise e

def check_pairs(current_user: User):
def check_pairs(current_user: User) -> ModelSelect:
pairs = current_user.pairs
return pairs

def check_key_value(key: str, current_user: User):
def check_key_value(key: str, current_user: User) -> List[Tuple[str, str, str]]:
key_file = load_key()
pairs = current_user.pairs
key_val = [(pair.key_string, use_key(key_file, decrypt_text=pair.value_string), pair.group.name) for pair in pairs if pair.key_string == key]
return key_val

def load_pair_in_group(group_name: str, key: str, current_user: User):
def load_pair_in_group(group_name: str, key: str, current_user: User) -> Pair:
try:
pair_in_group = Group.get(Group.user==current_user, Group.name==group_name)
pair = Pair.get(Pair.user==current_user, Pair.group==pair_in_group, Pair.key_string==key)
Expand All @@ -215,7 +224,7 @@ def load_pair_in_group(group_name: str, key: str, current_user: User):
except Pair.DoesNotExist:
raise ValueError("Pair does not exist.")

def delete_group(name: str, current_user: User):
def delete_group(name: str, current_user: User) -> None:
try:
group = Group.get(Group.user==current_user, Group.name==name)
group.delete_instance(recursive=True)
Expand All @@ -224,7 +233,7 @@ def delete_group(name: str, current_user: User):
except Exception as e:
raise e

def delete_pair(pair: Pair, current_user: User):
def delete_pair(pair: Pair, current_user: User) -> None:
try:
pair.delete_instance()
except Exception as e:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "lihim"
version = "1.0.1"
version = "1.0.2"
description = "CLI for managing secret keys, tokens, sensitive and/or public key-value pairs."
authors = ["Neil Van <nvq.alino@gmail.com>"]
license = "MIT"
Expand Down

0 comments on commit b6fd876

Please sign in to comment.