6
6
import time # generate_token
7
7
import hmac # _sign_string
8
8
import hashlib
9
- from typing import List # use for type hinting
9
+ from typing import List
10
10
import requests # create_session, archiving
11
11
import json # archiving
12
12
import platform # user-agent
@@ -174,6 +174,7 @@ def generate_token(
174
174
expire_time = None ,
175
175
data = None ,
176
176
initial_layout_class_list = [],
177
+ use_jwt = True ,
177
178
):
178
179
"""
179
180
Generates a token for a given session.
@@ -212,6 +213,9 @@ def generate_token(
212
213
`live streaming broadcasts <https://tokbox.com/developer/guides/broadcast/#live-streaming>`_ and
213
214
`composed archives <https://tokbox.com/developer/guides/archiving/layout-control.html>`_
214
215
216
+ :param bool use_jwt: Whether to use JWT tokens or not. If set to False, the token will be a
217
+ plain text token. If set to True (the default), the token will be a JWT.
218
+
215
219
:rtype:
216
220
The token string.
217
221
"""
@@ -287,7 +291,7 @@ def generate_token(
287
291
try :
288
292
decoded_session_id = base64 .b64decode (sub_session_id_bytes_padded , b ("-_" ))
289
293
parts = decoded_session_id .decode ("utf-8" ).split (u ("~" ))
290
- except Exception as e :
294
+ except Exception :
291
295
raise OpenTokException (
292
296
u ("Cannot generate token, the session_id {0} was not valid" ).format (
293
297
session_id
@@ -300,6 +304,29 @@ def generate_token(
300
304
).format (session_id , self .api_key )
301
305
)
302
306
307
+ if use_jwt :
308
+ payload = {}
309
+ payload ['iss' ] = self .api_key
310
+ payload ['ist' ] = 'project'
311
+ payload ['iat' ] = now
312
+ payload ["exp" ] = expire_time
313
+ payload ['nonce' ] = random .randint (0 , 999999 )
314
+ payload ['role' ] = role .value
315
+ payload ['scope' ] = 'session.connect'
316
+ payload ['session_id' ] = session_id
317
+ if initial_layout_class_list :
318
+ payload ['initial_layout_class_list' ] = (
319
+ initial_layout_class_list_serialized
320
+ )
321
+ if data :
322
+ payload ['connection_data' ] = data
323
+
324
+ headers = {'alg' : 'HS256' , 'typ' : 'JWT' }
325
+
326
+ token = encode (payload , self .api_secret , algorithm = "HS256" , headers = headers )
327
+
328
+ return f'Bearer { token } '
329
+
303
330
data_params = dict (
304
331
session_id = session_id ,
305
332
create_time = now ,
@@ -470,7 +497,7 @@ def create_session(
470
497
try :
471
498
logger .debug (
472
499
"POST to %r with params %r, headers %r, proxies %r" ,
473
- self .endpoints .session_url (),
500
+ self .endpoints .get_session_url (),
474
501
options ,
475
502
self .get_headers (),
476
503
self .proxies ,
@@ -654,7 +681,7 @@ def start_archive(
654
681
655
682
logger .debug (
656
683
"POST to %r with params %r, headers %r, proxies %r" ,
657
- self .endpoints .archive_url (),
684
+ self .endpoints .get_archive_url (),
658
685
json .dumps (payload ),
659
686
self .get_json_headers (),
660
687
self .proxies ,
@@ -701,7 +728,7 @@ def stop_archive(self, archive_id):
701
728
"""
702
729
logger .debug (
703
730
"POST to %r with headers %r, proxies %r" ,
704
- self .endpoints .archive_url (archive_id ) + "/stop" ,
731
+ self .endpoints .get_archive_url (archive_id ) + "/stop" ,
705
732
self .get_json_headers (),
706
733
self .proxies ,
707
734
)
@@ -736,7 +763,7 @@ def delete_archive(self, archive_id):
736
763
"""
737
764
logger .debug (
738
765
"DELETE to %r with headers %r, proxies %r" ,
739
- self .endpoints .archive_url (archive_id ),
766
+ self .endpoints .get_archive_url (archive_id ),
740
767
self .get_json_headers (),
741
768
self .proxies ,
742
769
)
@@ -766,7 +793,7 @@ def get_archive(self, archive_id):
766
793
"""
767
794
logger .debug (
768
795
"GET to %r with headers %r, proxies %r" ,
769
- self .endpoints .archive_url (archive_id ),
796
+ self .endpoints .get_archive_url (archive_id ),
770
797
self .get_json_headers (),
771
798
self .proxies ,
772
799
)
@@ -959,7 +986,7 @@ def send_signal(self, session_id, payload, connection_id=None):
959
986
"""
960
987
logger .debug (
961
988
"POST to %r with params %r, headers %r, proxies %r" ,
962
- self .endpoints .signaling_url (session_id , connection_id ),
989
+ self .endpoints .get_signaling_url (session_id , connection_id ),
963
990
json .dumps (payload ),
964
991
self .get_json_headers (),
965
992
self .proxies ,
@@ -1456,7 +1483,7 @@ def start_broadcast(self, session_id, options, stream_mode=BroadcastStreamModes.
1456
1483
1457
1484
payload .update (options )
1458
1485
1459
- endpoint = self .endpoints .broadcast_url ()
1486
+ endpoint = self .endpoints .get_broadcast_url ()
1460
1487
1461
1488
logger .debug (
1462
1489
"POST to %r with params %r, headers %r, proxies %r" ,
@@ -1500,7 +1527,7 @@ def stop_broadcast(self, broadcast_id):
1500
1527
projectId, createdAt, updatedAt and resolution
1501
1528
"""
1502
1529
1503
- endpoint = self .endpoints .broadcast_url (broadcast_id , stop = True )
1530
+ endpoint = self .endpoints .get_broadcast_url (broadcast_id , stop = True )
1504
1531
1505
1532
logger .debug (
1506
1533
"POST to %r with headers %r, proxies %r" ,
@@ -1639,7 +1666,7 @@ def get_broadcast(self, broadcast_id):
1639
1666
projectId, createdAt, updatedAt, resolution, broadcastUrls and status
1640
1667
"""
1641
1668
1642
- endpoint = self .endpoints .broadcast_url (broadcast_id )
1669
+ endpoint = self .endpoints .get_broadcast_url (broadcast_id )
1643
1670
1644
1671
logger .debug (
1645
1672
"GET to %r with headers %r, proxies %r" ,
@@ -1697,7 +1724,7 @@ def set_broadcast_layout(
1697
1724
if stylesheet is not None :
1698
1725
payload ["stylesheet" ] = stylesheet
1699
1726
1700
- endpoint = self .endpoints .broadcast_url (broadcast_id , layout = True )
1727
+ endpoint = self .endpoints .get_broadcast_url (broadcast_id , layout = True )
1701
1728
1702
1729
logger .debug (
1703
1730
"PUT to %r with params %r, headers %r, proxies %r" ,
0 commit comments