revert: don't cipher password in config
This commit is contained in:
@@ -1,48 +1,17 @@
|
|||||||
"""Core configuration handling class and related logic."""
|
"""Core configuration handling class and related logic."""
|
||||||
|
|
||||||
import os
|
|
||||||
import getpass
|
import getpass
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import yaml
|
import yaml
|
||||||
from cryptography.fernet import Fernet
|
|
||||||
|
|
||||||
|
|
||||||
class Configuration:
|
class Configuration:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.config_file = Path.home() / ".config" / "skywipe" / "config.yml"
|
self.config_file = Path.home() / ".config" / "skywipe" / "config.yml"
|
||||||
self.key_file = Path.home() / ".config" / "skywipe" / ".key"
|
|
||||||
self._fernet = None
|
|
||||||
|
|
||||||
def exists(self) -> bool:
|
def exists(self) -> bool:
|
||||||
return self.config_file.exists()
|
return self.config_file.exists()
|
||||||
|
|
||||||
def _get_or_create_key(self) -> bytes:
|
|
||||||
if self.key_file.exists():
|
|
||||||
return self.key_file.read_bytes()
|
|
||||||
|
|
||||||
key = Fernet.generate_key()
|
|
||||||
config_dir = self.key_file.parent
|
|
||||||
config_dir.mkdir(parents=True, exist_ok=True)
|
|
||||||
self.key_file.write_bytes(key)
|
|
||||||
os.chmod(self.key_file, 0o600)
|
|
||||||
return key
|
|
||||||
|
|
||||||
def _get_fernet(self) -> Fernet:
|
|
||||||
if self._fernet is None:
|
|
||||||
key = self._get_or_create_key()
|
|
||||||
self._fernet = Fernet(key)
|
|
||||||
return self._fernet
|
|
||||||
|
|
||||||
def encrypt_password(self, password: str) -> str:
|
|
||||||
fernet = self._get_fernet()
|
|
||||||
encrypted = fernet.encrypt(password.encode())
|
|
||||||
return encrypted.decode()
|
|
||||||
|
|
||||||
def decrypt_password(self, encrypted_password: str) -> str:
|
|
||||||
fernet = self._get_fernet()
|
|
||||||
decrypted = fernet.decrypt(encrypted_password.encode())
|
|
||||||
return decrypted.decode()
|
|
||||||
|
|
||||||
def create(self):
|
def create(self):
|
||||||
if self.exists():
|
if self.exists():
|
||||||
overwrite = input(
|
overwrite = input(
|
||||||
@@ -71,11 +40,9 @@ class Configuration:
|
|||||||
print("Error: batch_size and delay must be integers")
|
print("Error: batch_size and delay must be integers")
|
||||||
return
|
return
|
||||||
|
|
||||||
encrypted_password = self.encrypt_password(password)
|
|
||||||
|
|
||||||
config_data = {
|
config_data = {
|
||||||
"handle": handle,
|
"handle": handle,
|
||||||
"password": encrypted_password,
|
"password": password,
|
||||||
"batch_size": batch_size,
|
"batch_size": batch_size,
|
||||||
"delay": delay,
|
"delay": delay,
|
||||||
"verbose": verbose
|
"verbose": verbose
|
||||||
|
|||||||
Reference in New Issue
Block a user