66import time # generate_token
77import hmac # _sign_string
88import hashlib
9- from typing import List # use for type hinting
9+ from typing import List
1010import requests # create_session, archiving
1111import json # archiving
1212import platform # user-agent
@@ -174,6 +174,7 @@ def generate_token(
174174 expire_time = None ,
175175 data = None ,
176176 initial_layout_class_list = [],
177+ use_jwt = True ,
177178 ):
178179 """
179180 Generates a token for a given session.
@@ -212,6 +213,9 @@ def generate_token(
212213 `live streaming broadcasts <https://tokbox.com/developer/guides/broadcast/#live-streaming>`_ and
213214 `composed archives <https://tokbox.com/developer/guides/archiving/layout-control.html>`_
214215
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+
215219 :rtype:
216220 The token string.
217221 """
@@ -287,7 +291,7 @@ def generate_token(
287291 try :
288292 decoded_session_id = base64 .b64decode (sub_session_id_bytes_padded , b ("-_" ))
289293 parts = decoded_session_id .decode ("utf-8" ).split (u ("~" ))
290- except Exception as e :
294+ except Exception :
291295 raise OpenTokException (
292296 u ("Cannot generate token, the session_id {0} was not valid" ).format (
293297 session_id
@@ -300,6 +304,29 @@ def generate_token(
300304 ).format (session_id , self .api_key )
301305 )
302306
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+
303330 data_params = dict (
304331 session_id = session_id ,
305332 create_time = now ,
@@ -322,6 +349,7 @@ def generate_token(
322349 sentinal = self .TOKEN_SENTINEL ,
323350 base64_data = base64 .b64encode (decoded_base64_bytes ).decode (),
324351 )
352+
325353 return token
326354
327355 def create_session (
@@ -470,7 +498,7 @@ def create_session(
470498 try :
471499 logger .debug (
472500 "POST to %r with params %r, headers %r, proxies %r" ,
473- self .endpoints .session_url (),
501+ self .endpoints .get_session_url (),
474502 options ,
475503 self .get_headers (),
476504 self .proxies ,
@@ -654,7 +682,7 @@ def start_archive(
654682
655683 logger .debug (
656684 "POST to %r with params %r, headers %r, proxies %r" ,
657- self .endpoints .archive_url (),
685+ self .endpoints .get_archive_url (),
658686 json .dumps (payload ),
659687 self .get_json_headers (),
660688 self .proxies ,
@@ -701,7 +729,7 @@ def stop_archive(self, archive_id):
701729 """
702730 logger .debug (
703731 "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" ,
705733 self .get_json_headers (),
706734 self .proxies ,
707735 )
@@ -736,7 +764,7 @@ def delete_archive(self, archive_id):
736764 """
737765 logger .debug (
738766 "DELETE to %r with headers %r, proxies %r" ,
739- self .endpoints .archive_url (archive_id ),
767+ self .endpoints .get_archive_url (archive_id ),
740768 self .get_json_headers (),
741769 self .proxies ,
742770 )
@@ -766,7 +794,7 @@ def get_archive(self, archive_id):
766794 """
767795 logger .debug (
768796 "GET to %r with headers %r, proxies %r" ,
769- self .endpoints .archive_url (archive_id ),
797+ self .endpoints .get_archive_url (archive_id ),
770798 self .get_json_headers (),
771799 self .proxies ,
772800 )
@@ -959,7 +987,7 @@ def send_signal(self, session_id, payload, connection_id=None):
959987 """
960988 logger .debug (
961989 "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 ),
963991 json .dumps (payload ),
964992 self .get_json_headers (),
965993 self .proxies ,
@@ -1456,7 +1484,7 @@ def start_broadcast(self, session_id, options, stream_mode=BroadcastStreamModes.
14561484
14571485 payload .update (options )
14581486
1459- endpoint = self .endpoints .broadcast_url ()
1487+ endpoint = self .endpoints .get_broadcast_url ()
14601488
14611489 logger .debug (
14621490 "POST to %r with params %r, headers %r, proxies %r" ,
@@ -1500,7 +1528,7 @@ def stop_broadcast(self, broadcast_id):
15001528 projectId, createdAt, updatedAt and resolution
15011529 """
15021530
1503- endpoint = self .endpoints .broadcast_url (broadcast_id , stop = True )
1531+ endpoint = self .endpoints .get_broadcast_url (broadcast_id , stop = True )
15041532
15051533 logger .debug (
15061534 "POST to %r with headers %r, proxies %r" ,
@@ -1639,7 +1667,7 @@ def get_broadcast(self, broadcast_id):
16391667 projectId, createdAt, updatedAt, resolution, broadcastUrls and status
16401668 """
16411669
1642- endpoint = self .endpoints .broadcast_url (broadcast_id )
1670+ endpoint = self .endpoints .get_broadcast_url (broadcast_id )
16431671
16441672 logger .debug (
16451673 "GET to %r with headers %r, proxies %r" ,
@@ -1697,7 +1725,7 @@ def set_broadcast_layout(
16971725 if stylesheet is not None :
16981726 payload ["stylesheet" ] = stylesheet
16991727
1700- endpoint = self .endpoints .broadcast_url (broadcast_id , layout = True )
1728+ endpoint = self .endpoints .get_broadcast_url (broadcast_id , layout = True )
17011729
17021730 logger .debug (
17031731 "PUT to %r with params %r, headers %r, proxies %r" ,
0 commit comments