Coverage for webapp/solutions/auth.py: 50%
16 statements
« prev ^ index » next coverage.py v7.16.1, created at 2026-09-18 22:11 +0000
« prev ^ index » next coverage.py v7.16.1, created at 2026-09-18 22:11 +0000
1import requests
2import hashlib
3import hmac
4import time
5import os
7HMAC_SECRET = os.getenv("FLASK_HMAC_SECRET_KEY")
8BASE_URL = os.getenv(
9 "FLASK_SOLUTIONS_API_BASE", "https://solutions.staging.charmhub.io/api"
10)
13def login(username: str) -> str:
14 if not HMAC_SECRET:
15 raise ValueError("FLASK_HMAC_SECRET_KEY env variable not set")
17 timestamp = str(int(time.time()))
18 msg = f"{username}|{timestamp}".encode()
19 sig = hmac.new(HMAC_SECRET.encode(), msg, hashlib.blake2b).hexdigest()
21 r = requests.post(
22 f"{BASE_URL}/login",
23 json={"username": username, "timestamp": timestamp, "signature": sig},
24 )
26 r.raise_for_status()
27 return r.json()["token"]