| 1 | from __future__ import annotations |
| 2 | |
| 3 | import hashlib |
| 4 | import re |
| 5 | import secrets |
| 6 | from uuid import uuid4 |
| 7 | |
| 8 | from open_agent_registry.config import settings |
| 9 | |
| 10 | _NAME_RE = re.compile(r"^[a-zA-Z][a-zA-Z0-9_-]{2,63}$") |
| 11 | |
| 12 | |
| 13 | def normalize_name(name: str) -> str: |
| 14 | cleaned = name.strip() |
| 15 | if not _NAME_RE.match(cleaned): |
| 16 | raise ValueError( |
| 17 | "name must be 3–64 chars, start with a letter, use letters/digits/_/- only" |
| 18 | ) |
| 19 | return cleaned |
| 20 | |
| 21 | |
| 22 | def new_agent_id() -> str: |
| 23 | return f"agt_{uuid4().hex[:16]}" |
| 24 | |
| 25 | |
| 26 | def new_api_key() -> str: |
| 27 | return f"{settings.api_key_prefix}{secrets.token_urlsafe(32)}" |
| 28 | |
| 29 | |
| 30 | def hash_secret(value: str) -> str: |
| 31 | return hashlib.sha256(value.encode("utf-8")).hexdigest() |
| 32 | |
| 33 | |
| 34 | def new_claim_token() -> str: |
| 35 | return secrets.token_urlsafe(24) |
| 36 | |
| 37 | |
| 38 | def new_claim_code() -> str: |
| 39 | return f"{secrets.randbelow(1_000_000):06d}" |
| 40 | |
View only · write via MCP/CIDE