mirror of
https://github.com/PR0M3TH3AN/SeedPass.git
synced 2025-09-09 15:58:48 +00:00
Add password policy options to API
This commit is contained in:
@@ -16,6 +16,7 @@ from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
from seedpass.core.manager import PasswordManager
|
||||
from seedpass.core.entry_types import EntryType
|
||||
from seedpass.core.api import UtilityService
|
||||
|
||||
|
||||
app = FastAPI()
|
||||
@@ -117,11 +118,23 @@ def create_entry(
|
||||
etype = (entry.get("type") or entry.get("kind") or "password").lower()
|
||||
|
||||
if etype == "password":
|
||||
policy_keys = [
|
||||
"include_special_chars",
|
||||
"allowed_special_chars",
|
||||
"special_mode",
|
||||
"exclude_ambiguous",
|
||||
"min_uppercase",
|
||||
"min_lowercase",
|
||||
"min_digits",
|
||||
"min_special",
|
||||
]
|
||||
kwargs = {k: entry.get(k) for k in policy_keys if entry.get(k) is not None}
|
||||
index = _pm.entry_manager.add_entry(
|
||||
entry.get("label"),
|
||||
int(entry.get("length", 12)),
|
||||
entry.get("username"),
|
||||
entry.get("url"),
|
||||
**kwargs,
|
||||
)
|
||||
return {"id": index}
|
||||
|
||||
@@ -566,6 +579,30 @@ def change_password(
|
||||
return {"status": "ok"}
|
||||
|
||||
|
||||
@app.post("/api/v1/password")
|
||||
def generate_password(
|
||||
data: dict, authorization: str | None = Header(None)
|
||||
) -> dict[str, str]:
|
||||
"""Generate a password using optional policy overrides."""
|
||||
_check_token(authorization)
|
||||
assert _pm is not None
|
||||
length = int(data.get("length", 12))
|
||||
policy_keys = [
|
||||
"include_special_chars",
|
||||
"allowed_special_chars",
|
||||
"special_mode",
|
||||
"exclude_ambiguous",
|
||||
"min_uppercase",
|
||||
"min_lowercase",
|
||||
"min_digits",
|
||||
"min_special",
|
||||
]
|
||||
kwargs = {k: data.get(k) for k in policy_keys if data.get(k) is not None}
|
||||
util = UtilityService(_pm)
|
||||
password = util.generate_password(length, **kwargs)
|
||||
return {"password": password}
|
||||
|
||||
|
||||
@app.post("/api/v1/vault/lock")
|
||||
def lock_vault(authorization: str | None = Header(None)) -> dict[str, str]:
|
||||
"""Lock the vault and clear sensitive data from memory."""
|
||||
|
@@ -84,6 +84,34 @@ class SyncResponse(BaseModel):
|
||||
delta_ids: List[str] = []
|
||||
|
||||
|
||||
class PasswordPolicyOptions(BaseModel):
|
||||
"""Optional password policy overrides."""
|
||||
|
||||
include_special_chars: bool | None = None
|
||||
allowed_special_chars: str | None = None
|
||||
special_mode: str | None = None
|
||||
exclude_ambiguous: bool | None = None
|
||||
min_uppercase: int | None = None
|
||||
min_lowercase: int | None = None
|
||||
min_digits: int | None = None
|
||||
min_special: int | None = None
|
||||
|
||||
|
||||
class AddPasswordEntryRequest(PasswordPolicyOptions):
|
||||
label: str
|
||||
length: int
|
||||
username: str | None = None
|
||||
url: str | None = None
|
||||
|
||||
|
||||
class GeneratePasswordRequest(PasswordPolicyOptions):
|
||||
length: int
|
||||
|
||||
|
||||
class GeneratePasswordResponse(BaseModel):
|
||||
password: str
|
||||
|
||||
|
||||
class VaultService:
|
||||
"""Thread-safe wrapper around vault operations."""
|
||||
|
||||
|
Reference in New Issue
Block a user