瀏覽代碼

POO refactoring

Guénaël Muller 7 年之前
父節點
當前提交
9060535a9c
共有 1 個文件被更改,包括 89 次插入53 次删除
  1. 89 53
      tracim/tracim/lib/jitsi_meet/jitsi_meet.py

+ 89 - 53
tracim/tracim/lib/jitsi_meet/jitsi_meet.py 查看文件

@@ -1,54 +1,90 @@
1
-import jwt
2
-import json
3 1
 import datetime
4
-import time
5
-
6
-JITSI_URL="https://prosody"
7
-JWT_APP_ID="test"
8
-JWT_SECRET="secret"
9
-JWT_ALG='HS256'
10
-JWT_DURATION=60*1 # duration in second
11
-JITSI_USE_TOKEN=True
12
-
13
-
14
-
15
-def _generate_token(room:str)->str:
16
-    '''
17
-    Create jwt token according to room name and config
18
-    :param room: room name
19
-    :return: jwt encoded token as string
20
-    '''
21
-    now = datetime.datetime.utcnow()
22
-    exp = now+datetime.timedelta(seconds=JWT_DURATION)
23
-    data = {
24
-        "iss":JWT_APP_ID, #Issuer
25
-        "room": room, # Custom-param for jitsi_meet
26
-        "aud": "*", # TODO: Understood this param
27
-        "exp": exp, # Expiration date
28
-        "nbf": now, # NotBefore
29
-        "iat": now  # IssuedAt
30
-    }
31
-    jwt
32
-    jwttoken= jwt.encode(data,
33
-                        JWT_SECRET,
34
-                        algorithm=JWT_ALG)
35
-    return jwttoken.decode("utf-8")
36
-
37
-def _generate_url(room:str)->str:
38
-    '''
39
-    Generate url with or without token
40
-    :param room: room name
41
-    :return: url as string
42
-    '''
43
-    if JITSI_USE_TOKEN:
44
-        token=_generate_token(room)
45
-        url="{}/{}?jwt={}".format(JITSI_URL,
46
-                              room
47
-                              ,token)
48
-    else:
49
-        url="{}/{}".format(JITSI_URL,
50
-                           room)
51
-    return url
52
-
53
-
54
-print(_generate_url("test"))
2
+import typing
3
+
4
+import jwt
5
+
6
+JITSI_URL = "https://prosody"
7
+JWT_APP_ID = "test"
8
+JWT_SECRET = "secret"
9
+JWT_ALG = 'HS256'
10
+JWT_DURATION = 60*1  # duration in second
11
+JITSI_USE_TOKEN = True
12
+
13
+
14
+class JitsiTokenConfig:
15
+
16
+    def __init__(self,
17
+                 app_id: str,
18
+                 secret: str,
19
+                 alg: str,
20
+                 duration: int,
21
+                 )-> None:
22
+        self.app_id = app_id
23
+        self.secret = secret
24
+        self.alg = alg
25
+        self.duration = duration
26
+
27
+
28
+class JitsiMeetRoomHandler:
29
+
30
+    def __init__(self,
31
+                 base_url: str,
32
+                 token_config: typing.Optional[JitsiTokenConfig],
33
+                 ) -> None:
34
+        self.base_url = base_url
35
+        self.token_config = token_config
36
+
37
+    def generate_token(self, room: str)->str:
38
+        """
39
+        Create jwt token according to room name and config
40
+        :param room: room name
41
+        :return: jwt encoded token as string
42
+        """
43
+        assert self.token_config
44
+        now = datetime.datetime.utcnow()
45
+        exp = now+datetime.timedelta(seconds=self.token_config.duration)
46
+        data = {
47
+            "iss": self.token_config.app_id,  # Issuer
48
+            "room": room,  # Custom-param for jitsi_meet
49
+            "aud": "*",  # TODO: Understood this param
50
+            "exp": exp,  # Expiration date
51
+            "nbf": now,  # NotBefore
52
+            "iat": now,   # IssuedAt
53
+        }
54
+        jwt_token = jwt.encode(data,
55
+                               self.token_config.secret,
56
+                               algorithm=self.token_config.alg)
57
+        return jwt_token.decode("utf-8")
58
+
59
+    def generate_url(self, room: str)->str:
60
+        """
61
+        Generate url with or without token
62
+        :param room: room name
63
+        :return: url as string
64
+        """
65
+        if self.token_config:
66
+            token = self.generate_token(room)
67
+            url = "{}/{}?jwt={}".format(JITSI_URL,
68
+                                        room,
69
+                                        token,)
70
+        else:
71
+            url = "{}/{}".format(JITSI_URL,
72
+                                 room,)
73
+        return url
74
+
75
+if JITSI_USE_TOKEN:
76
+    defaultTokenConfig = JitsiTokenConfig(
77
+        app_id=JWT_APP_ID,
78
+        secret=JWT_SECRET,
79
+        alg=JWT_ALG,
80
+        duration=JWT_DURATION,
81
+    )
82
+else:
83
+    defaultTokenConfig = None
84
+
85
+JitsiMeetRoom = JitsiMeetRoomHandler(
86
+    base_url=JITSI_URL,
87
+    token_config= defaultTokenConfig
88
+)
89
+
90
+print(JitsiMeetRoom.generate_url('test'))