54 lines
1.5 KiB
Python
54 lines
1.5 KiB
Python
|
|
"""SIP credential provisioning for ring-group workers (§5.3).
|
||
|
|
|
||
|
|
Workers exist only as SIP endpoints — no product accounts. The dashboard shows
|
||
|
|
generated credentials + a QR code for quick softphone setup (Linphone/Zoiper/
|
||
|
|
Groundwire all accept manual entry; the QR encodes a standard sip URI plus the
|
||
|
|
fields spelled out).
|
||
|
|
"""
|
||
|
|
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import io
|
||
|
|
import re
|
||
|
|
import secrets
|
||
|
|
|
||
|
|
from gogo.config import get_settings
|
||
|
|
|
||
|
|
|
||
|
|
def make_sip_username(tenant_slug: str, worker_name: str, suffix: str | None = None) -> str:
|
||
|
|
base = re.sub(r"[^a-z0-9]+", "", worker_name.lower())[:12] or "radnik"
|
||
|
|
suffix = suffix or secrets.token_hex(2)
|
||
|
|
return f"{tenant_slug[:16]}-{base}-{suffix}"
|
||
|
|
|
||
|
|
|
||
|
|
def make_sip_password() -> str:
|
||
|
|
return secrets.token_urlsafe(12)
|
||
|
|
|
||
|
|
|
||
|
|
def sip_domain() -> str:
|
||
|
|
"""SIP registrar host — derived from base_url; overridable later if split hosts."""
|
||
|
|
from urllib.parse import urlparse
|
||
|
|
|
||
|
|
return urlparse(get_settings().base_url).hostname or "localhost"
|
||
|
|
|
||
|
|
|
||
|
|
def provisioning_text(sip_username: str, sip_password: str) -> str:
|
||
|
|
"""Payload for the QR code: sip URI + explicit fields (readable by humans too)."""
|
||
|
|
domain = sip_domain()
|
||
|
|
return (
|
||
|
|
f"sip:{sip_username}@{domain}\n"
|
||
|
|
f"Server/Domain: {domain}\n"
|
||
|
|
f"Username: {sip_username}\n"
|
||
|
|
f"Password: {sip_password}\n"
|
||
|
|
f"Transport: UDP 5060"
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
def qr_png(payload: str) -> bytes:
|
||
|
|
import qrcode
|
||
|
|
|
||
|
|
img = qrcode.make(payload)
|
||
|
|
buf = io.BytesIO()
|
||
|
|
img.save(buf, format="PNG")
|
||
|
|
return buf.getvalue()
|