-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvault_manager.py
More file actions
117 lines (85 loc) · 3.38 KB
/
vault_manager.py
File metadata and controls
117 lines (85 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# vault_manager.py
import json
import os
from datetime import datetime
from rich.console import Console
from config import VAULT_DIR
from integrity import write_integrity, verify_integrity
console = Console()
def _vault_path(username: str) -> str:
os.makedirs(VAULT_DIR, exist_ok=True)
return os.path.join(VAULT_DIR, f"{username}.json")
def load_vault(username: str) -> dict:
path = _vault_path(username)
if not os.path.exists(path):
return {"version": 1, "entries": []}
verify_integrity(path)
with open(path, "r", encoding="utf-8") as f:
try:
data = json.load(f)
except json.JSONDecodeError:
console.print("[red]Vault file is corrupted. Starting with an empty vault.[/red]")
return {"version": 1, "entries": []}
if "entries" not in data:
data["entries"] = []
return data
def save_vault(username: str, vault: dict) -> None:
path = _vault_path(username)
with open(path, "w", encoding="utf-8") as f:
json.dump(vault, f, indent=2)
write_integrity(path)
def _now() -> str:
return datetime.utcnow().isoformat() + "Z"
def find_entry(vault: dict, service_name: str) -> dict | None:
target = service_name.lower()
for entry in vault.get("entries", []):
if entry.get("service", "").lower() == target:
return entry
return None
def search_entries(vault: dict, query: str) -> list[dict]:
q = query.lower()
return [e for e in vault.get("entries", []) if q in e.get("service", "").lower()]
def add_entry(vault: dict, service: str, enc_password: str, category: str | None, username_for_service: str | None):
existing = find_entry(vault, service)
if existing:
raise ValueError("Service already exists. Use edit options instead.")
entry = {
"service": service,
"username": username_for_service or "",
"category": category or "Other",
"password": enc_password,
"created_at": _now(),
"updated_at": _now(),
}
vault["entries"].append(entry)
def update_password(vault: dict, service: str, enc_password: str):
entry = find_entry(vault, service)
if not entry:
raise KeyError("Service not found")
entry["password"] = enc_password
entry["updated_at"] = _now()
def rename_service(vault: dict, old_service: str, new_service: str):
entry = find_entry(vault, old_service)
if not entry:
raise KeyError("Service not found")
entry["service"] = new_service
entry["updated_at"] = _now()
def update_category(vault: dict, service: str, category: str):
entry = find_entry(vault, service)
if not entry:
raise KeyError("Service not found")
entry["category"] = category
entry["updated_at"] = _now()
def delete_entry(vault: dict, service: str):
entries = vault.get("entries", [])
target = service.lower()
new_entries = [e for e in entries if e.get("service", "").lower() != target]
if len(new_entries) == len(entries):
raise KeyError("Service not found")
vault["entries"] = new_entries
def group_by_category(vault: dict) -> dict[str, list[dict]]:
grouped = {}
for e in vault.get("entries", []):
cat = e.get("category") or "Other"
grouped.setdefault(cat, []).append(e)
return grouped