35 lines
1013 B
Python
35 lines
1013 B
Python
import aiohttp
|
|
from nacl.exceptions import BadSignatureError
|
|
|
|
from v6d0auth import certs
|
|
from v6d0auth.certs import averify
|
|
from v6d0auth.config import myroot, caurl
|
|
|
|
__all__ = ('request_signature', 'mycert')
|
|
|
|
|
|
async def request_signature() -> bytes:
|
|
async with aiohttp.ClientSession() as session:
|
|
async with session.post(f'{caurl}/push', data=certs.vkey.encode()) as response:
|
|
if response.status not in [200, 429]:
|
|
raise RuntimeError(response.status)
|
|
async with session.ws_connect(f'{caurl}/pullws') as ws:
|
|
await ws.send_bytes(certs.vkey.encode())
|
|
try:
|
|
return await ws.receive_bytes()
|
|
except TypeError:
|
|
raise TimeoutError
|
|
|
|
|
|
_certfile = myroot / 'cert'
|
|
|
|
|
|
async def mycert() -> bytes:
|
|
try:
|
|
cert = _certfile.read_bytes()
|
|
averify(cert)
|
|
except (FileNotFoundError, BadSignatureError):
|
|
cert = await request_signature()
|
|
_certfile.write_bytes(cert)
|
|
return cert
|