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 token
329
+
303
330
data_params = dict (
304
331
session_id = session_id ,
305
332
create_time = now ,
@@ -322,6 +349,7 @@ def generate_token(
322
349
sentinal = self .TOKEN_SENTINEL ,
323
350
base64_data = base64 .b64encode (decoded_base64_bytes ).decode (),
324
351
)
352
+
325
353
return token
326
354
327
355
def create_session (
@@ -470,7 +498,7 @@ def create_session(
470
498
try :
471
499
logger .debug (
472
500
"POST to %r with params %r, headers %r, proxies %r" ,
473
- self .endpoints .session_url (),
501
+ self .endpoints .get_session_url (),
474
502
options ,
475
503
self .get_headers (),
476
504
self .proxies ,
@@ -654,7 +682,7 @@ def start_archive(
654
682
655
683
logger .debug (
656
684
"POST to %r with params %r, headers %r, proxies %r" ,
657
- self .endpoints .archive_url (),
685
+ self .endpoints .get_archive_url (),
658
686
json .dumps (payload ),
659
687
self .get_json_headers (),
660
688
self .proxies ,
@@ -701,7 +729,7 @@ def stop_archive(self, archive_id):
701
729
"""
702
730
logger .debug (
703
731
"POST to %r with headers %r, proxies %r" ,
704
- self .endpoints .archive_url (archive_id ) + "/stop" ,
732
+ self .endpoints .get_archive_url (archive_id ) + "/stop" ,
705
733
self .get_json_headers (),
706
734
self .proxies ,
707
735
)
@@ -736,7 +764,7 @@ def delete_archive(self, archive_id):
736
764
"""
737
765
logger .debug (
738
766
"DELETE to %r with headers %r, proxies %r" ,
739
- self .endpoints .archive_url (archive_id ),
767
+ self .endpoints .get_archive_url (archive_id ),
740
768
self .get_json_headers (),
741
769
self .proxies ,
742
770
)
@@ -766,7 +794,7 @@ def get_archive(self, archive_id):
766
794
"""
767
795
logger .debug (
768
796
"GET to %r with headers %r, proxies %r" ,
769
- self .endpoints .archive_url (archive_id ),
797
+ self .endpoints .get_archive_url (archive_id ),
770
798
self .get_json_headers (),
771
799
self .proxies ,
772
800
)
@@ -959,7 +987,7 @@ def send_signal(self, session_id, payload, connection_id=None):
959
987
"""
960
988
logger .debug (
961
989
"POST to %r with params %r, headers %r, proxies %r" ,
962
- self .endpoints .signaling_url (session_id , connection_id ),
990
+ self .endpoints .get_signaling_url (session_id , connection_id ),
963
991
json .dumps (payload ),
964
992
self .get_json_headers (),
965
993
self .proxies ,
@@ -1456,7 +1484,7 @@ def start_broadcast(self, session_id, options, stream_mode=BroadcastStreamModes.
1456
1484
1457
1485
payload .update (options )
1458
1486
1459
- endpoint = self .endpoints .broadcast_url ()
1487
+ endpoint = self .endpoints .get_broadcast_url ()
1460
1488
1461
1489
logger .debug (
1462
1490
"POST to %r with params %r, headers %r, proxies %r" ,
@@ -1500,7 +1528,7 @@ def stop_broadcast(self, broadcast_id):
1500
1528
projectId, createdAt, updatedAt and resolution
1501
1529
"""
1502
1530
1503
- endpoint = self .endpoints .broadcast_url (broadcast_id , stop = True )
1531
+ endpoint = self .endpoints .get_broadcast_url (broadcast_id , stop = True )
1504
1532
1505
1533
logger .debug (
1506
1534
"POST to %r with headers %r, proxies %r" ,
@@ -1639,7 +1667,7 @@ def get_broadcast(self, broadcast_id):
1639
1667
projectId, createdAt, updatedAt, resolution, broadcastUrls and status
1640
1668
"""
1641
1669
1642
- endpoint = self .endpoints .broadcast_url (broadcast_id )
1670
+ endpoint = self .endpoints .get_broadcast_url (broadcast_id )
1643
1671
1644
1672
logger .debug (
1645
1673
"GET to %r with headers %r, proxies %r" ,
@@ -1697,7 +1725,7 @@ def set_broadcast_layout(
1697
1725
if stylesheet is not None :
1698
1726
payload ["stylesheet" ] = stylesheet
1699
1727
1700
- endpoint = self .endpoints .broadcast_url (broadcast_id , layout = True )
1728
+ endpoint = self .endpoints .get_broadcast_url (broadcast_id , layout = True )
1701
1729
1702
1730
logger .debug (
1703
1731
"PUT to %r with params %r, headers %r, proxies %r" ,
0 commit comments