from __future__ import annotations import asyncio import logging import secrets import time logger = logging.getLogger(__name__) CODE_TTL_SECONDS = 600 class GitLabPairing: def __init__(self): self._codes: dict[str, dict] = {} self._locks: dict[str, asyncio.Lock] = {} async def generate_code(self, peer_id: str, account_id: str = "default") -> str: key = f"{account_id}:{peer_id}" code = secrets.token_hex(3).upper() self._codes[key] = {"code": code, "created_at": time.monotonic()} logger.info("gitlab pairing code generated for %s: %s", peer_id, code) return code async def verify_code(self, peer_id: str, code: str, account_id: str = "default") -> bool: key = f"{account_id}:{peer_id}" entry = self._codes.get(key) if not entry: return False if time.monotonic() - entry["created_at"] > CODE_TTL_SECONDS: self._codes.pop(key, None) return False if entry["code"].upper() == code.upper().strip(): self._codes.pop(key, None) return True return False def normalize_allow_entry(self, entry: str) -> str: return str(entry).strip()