Skip to content

Commit 1bc1ccf

Browse files
committed
implement host key generation
1 parent 40d39fe commit 1bc1ccf

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

common.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from functools import lru_cache
2+
import os
3+
from time import time
4+
5+
import jwt
6+
import requests
7+
8+
ZOOM_API = "https://api.zoom.us/v2/"
9+
SPEAKERS_CORNER_USER_ID = "D0n5UNEHQiajWtgdWLlNSA"
10+
11+
@lru_cache()
12+
def zoom_headers(duration: int=10) -> dict:
13+
zoom_api_key = os.getenv("ZOOM_API_KEY")
14+
zoom_api_secret = os.getenv("ZOOM_API_SECRET")
15+
token = jwt.encode(
16+
# Create a payload of the token containing API Key & expiration time
17+
{"iss": zoom_api_key, "exp": time() + duration},
18+
zoom_api_secret,
19+
algorithm='HS256'
20+
).decode('utf-8')
21+
22+
return {'authorization': f'Bearer {token}', 'content-type': 'application/json'}
23+
24+
25+
def speakers_corner_user_id() -> str:
26+
users = requests.get(ZOOM_API + "users", headers=zoom_headers()).json()["users"]
27+
sc_user_id = next(
28+
u["id"] for u in users
29+
if u["first_name"] == "Speakers'" and u["last_name"] == "Corner"
30+
)
31+
return sc_user_id

host_key_rotation.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python
2+
3+
import os
4+
import hashlib
5+
import datetime
6+
import json
7+
8+
import requests
9+
import pytz
10+
11+
import common
12+
13+
14+
def host_key(timeslot: datetime.datetime) -> int:
15+
"""Generate a host key for a specified time."""
16+
key_salt = os.getenv("HOST_KEY_SALT").encode()
17+
timestamp = timeslot.replace(second=0, microsecond=0, minute=0).timestamp()
18+
hashed = hashlib.sha512(int(timestamp.to_bytes(5, "big")) + key_salt)
19+
return f"{int(hashed.hexdigest(), 16) % int(1e7):06}"
20+
21+
22+
def update_host_key():
23+
"""Update the host key of the speakers' corner user for the upcoming hour."""
24+
response = requests.patch(
25+
common.ZOOM_API + "users/" + common.SPEAKERS_CORNER_USER_ID,
26+
headers=common.zoom_headers(),
27+
data=json.dumps({
28+
"host_key": host_key(datetime.datetime.now() + datetime.timedelta(hours=1))
29+
})
30+
)
31+
if response.status_code != 204:
32+
raise RuntimeError(response.content.decode())

0 commit comments

Comments
 (0)