|
| 1 | +import datetime as dt |
| 2 | +import logging |
| 3 | +import uuid |
| 4 | +from http import HTTPStatus |
| 5 | + |
| 6 | +import jwt |
| 7 | +import requests |
| 8 | +from celery import shared_task |
| 9 | +from django.conf import settings |
| 10 | + |
| 11 | +from venueless.core.models.auth import ShortToken |
| 12 | +from venueless.core.models.world import World |
| 13 | + |
| 14 | +logger = logging.getLogger(__name__) |
| 15 | + |
| 16 | + |
| 17 | +def generate_video_token(world, days, number, traits, long=False): |
| 18 | + """ |
| 19 | + Generate video token |
| 20 | + :param world: World object |
| 21 | + :param days: A integer representing the number of days the token is valid |
| 22 | + :param number: A integer representing the number of tokens to generate |
| 23 | + :param traits: A dictionary representing the traits of the token |
| 24 | + :param long: A boolean representing if the token is long or short |
| 25 | + :return: A list of tokens |
| 26 | + """ |
| 27 | + jwt_secrets = world.config.get("JWT_secrets", []) |
| 28 | + if not jwt_secrets: |
| 29 | + logger.error("JWT_secrets is missing or empty in the configuration") |
| 30 | + return |
| 31 | + jwt_config = jwt_secrets[0] |
| 32 | + secret = jwt_config.get("secret") |
| 33 | + audience = jwt_config.get("audience") |
| 34 | + issuer = jwt_config.get("issuer") |
| 35 | + iat = dt.datetime.now(dt.timezone.utc) |
| 36 | + exp = iat + dt.timedelta(days=days) |
| 37 | + result = [] |
| 38 | + bulk_create = [] |
| 39 | + for _ in range(number): |
| 40 | + payload = { |
| 41 | + "iss": issuer, |
| 42 | + "aud": audience, |
| 43 | + "exp": exp, |
| 44 | + "iat": iat, |
| 45 | + "uid": str(uuid.uuid4()), |
| 46 | + "traits": traits, |
| 47 | + } |
| 48 | + token = jwt.encode(payload, secret, algorithm="HS256") |
| 49 | + if long: |
| 50 | + result.append(token) |
| 51 | + else: |
| 52 | + st = ShortToken(world=world, long_token=token, expires=exp) |
| 53 | + result.append(st.short_token) |
| 54 | + bulk_create.append(st) |
| 55 | + |
| 56 | + if not long: |
| 57 | + ShortToken.objects.bulk_create(bulk_create) |
| 58 | + return result |
| 59 | + |
| 60 | + |
| 61 | +def generate_talk_token(video_settings, video_tokens, event_slug): |
| 62 | + """ |
| 63 | + Generate talk token |
| 64 | + :param video_settings: A dictionary representing the video settings |
| 65 | + :param video_tokens: A list of video tokens |
| 66 | + :param event_slug: A string representing the event slug |
| 67 | + :return: A token |
| 68 | + """ |
| 69 | + iat = dt.datetime.now(dt.timezone.utc) |
| 70 | + exp = iat + dt.timedelta(days=30) |
| 71 | + payload = { |
| 72 | + "exp": exp, |
| 73 | + "iat": iat, |
| 74 | + "video_tokens": video_tokens, |
| 75 | + "slug": event_slug, |
| 76 | + } |
| 77 | + token = jwt.encode(payload, video_settings.get("secret"), algorithm="HS256") |
| 78 | + return token |
| 79 | + |
| 80 | + |
| 81 | +@shared_task(bind=True, max_retries=5, default_retry_delay=60) |
| 82 | +def configure_video_settings_for_talks( |
| 83 | + self, world_id, days, number, traits, long=False |
| 84 | +): |
| 85 | + """ |
| 86 | + Configure video settings for talks |
| 87 | + :param self: instance of the task |
| 88 | + :param world_id: A integer representing the world id |
| 89 | + :param days: A integer representing the number of days the token is valid |
| 90 | + :param number: A integer representing the number of tokens to generate |
| 91 | + :param traits: A dictionary representing the traits of the token |
| 92 | + :param long: A boolean representing if the token is long or short |
| 93 | + """ |
| 94 | + try: |
| 95 | + if not isinstance(world_id, str) or not world_id.isalnum(): |
| 96 | + raise ValueError("Invalid world_id format") |
| 97 | + world = World.objects.get(id=world_id) |
| 98 | + event_slug = world_id |
| 99 | + jwt_secrets = world.config.get("JWT_secrets", []) |
| 100 | + if not jwt_secrets: |
| 101 | + logger.error("JWT_secrets is missing or empty in the configuration") |
| 102 | + return |
| 103 | + jwt_config = jwt_secrets[0] |
| 104 | + video_tokens = generate_video_token(world, days, number, traits, long) |
| 105 | + talk_token = generate_talk_token(jwt_config, video_tokens, event_slug) |
| 106 | + header = { |
| 107 | + "Content-Type": "application/json", |
| 108 | + "Authorization": f"Bearer {talk_token}", |
| 109 | + } |
| 110 | + payload = { |
| 111 | + "video_settings": { |
| 112 | + "audience": jwt_config.get("audience"), |
| 113 | + "issuer": jwt_config.get("issuer"), |
| 114 | + "secret": jwt_config.get("secret"), |
| 115 | + } |
| 116 | + } |
| 117 | + requests.post( |
| 118 | + "{}/api/configure-video-settings/".format(settings.EVENTYAY_TALK_BASE_PATH), |
| 119 | + json=payload, |
| 120 | + headers=header, |
| 121 | + ) |
| 122 | + world.config["pretalx"] = { |
| 123 | + "event": event_slug, |
| 124 | + "domain": "{}".format(settings.EVENTYAY_TALK_BASE_PATH), |
| 125 | + "pushed": dt.datetime.now(dt.timezone.utc).isoformat(), |
| 126 | + "connected": True, |
| 127 | + } |
| 128 | + world.save() |
| 129 | + except requests.exceptions.ConnectionError as e: |
| 130 | + logger.error("Connection error: %s", e) |
| 131 | + self.retry(exc=e) |
| 132 | + except requests.exceptions.HTTPError as e: |
| 133 | + if e.response.status_code in ( |
| 134 | + HTTPStatus.UNAUTHORIZED.value, |
| 135 | + HTTPStatus.FORBIDDEN.value, |
| 136 | + HTTPStatus.NOT_FOUND.value, |
| 137 | + ): |
| 138 | + logger.error("Non-retryable error: %s", e) |
| 139 | + raise |
| 140 | + logger.error("HTTP error: %s", e) |
| 141 | + self.retry(exc=e) |
| 142 | + except ValueError as e: |
| 143 | + logger.error("Error configuring video settings: %s", e) |
0 commit comments