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 f'Bearer { token } '
329+
303330 data_params = dict (
304331 session_id = session_id ,
305332 create_time = now ,
@@ -470,7 +497,7 @@ def create_session(
470497 try :
471498 logger .debug (
472499 "POST to %r with params %r, headers %r, proxies %r" ,
473- self .endpoints .session_url (),
500+ self .endpoints .get_session_url (),
474501 options ,
475502 self .get_headers (),
476503 self .proxies ,
@@ -654,7 +681,7 @@ def start_archive(
654681
655682 logger .debug (
656683 "POST to %r with params %r, headers %r, proxies %r" ,
657- self .endpoints .archive_url (),
684+ self .endpoints .get_archive_url (),
658685 json .dumps (payload ),
659686 self .get_json_headers (),
660687 self .proxies ,
@@ -701,7 +728,7 @@ def stop_archive(self, archive_id):
701728 """
702729 logger .debug (
703730 "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" ,
705732 self .get_json_headers (),
706733 self .proxies ,
707734 )
@@ -736,7 +763,7 @@ def delete_archive(self, archive_id):
736763 """
737764 logger .debug (
738765 "DELETE to %r with headers %r, proxies %r" ,
739- self .endpoints .archive_url (archive_id ),
766+ self .endpoints .get_archive_url (archive_id ),
740767 self .get_json_headers (),
741768 self .proxies ,
742769 )
@@ -766,7 +793,7 @@ def get_archive(self, archive_id):
766793 """
767794 logger .debug (
768795 "GET to %r with headers %r, proxies %r" ,
769- self .endpoints .archive_url (archive_id ),
796+ self .endpoints .get_archive_url (archive_id ),
770797 self .get_json_headers (),
771798 self .proxies ,
772799 )
@@ -959,7 +986,7 @@ def send_signal(self, session_id, payload, connection_id=None):
959986 """
960987 logger .debug (
961988 "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 ),
963990 json .dumps (payload ),
964991 self .get_json_headers (),
965992 self .proxies ,
@@ -1456,7 +1483,7 @@ def start_broadcast(self, session_id, options, stream_mode=BroadcastStreamModes.
14561483
14571484 payload .update (options )
14581485
1459- endpoint = self .endpoints .broadcast_url ()
1486+ endpoint = self .endpoints .get_broadcast_url ()
14601487
14611488 logger .debug (
14621489 "POST to %r with params %r, headers %r, proxies %r" ,
@@ -1500,7 +1527,7 @@ def stop_broadcast(self, broadcast_id):
15001527 projectId, createdAt, updatedAt and resolution
15011528 """
15021529
1503- endpoint = self .endpoints .broadcast_url (broadcast_id , stop = True )
1530+ endpoint = self .endpoints .get_broadcast_url (broadcast_id , stop = True )
15041531
15051532 logger .debug (
15061533 "POST to %r with headers %r, proxies %r" ,
@@ -1639,7 +1666,7 @@ def get_broadcast(self, broadcast_id):
16391666 projectId, createdAt, updatedAt, resolution, broadcastUrls and status
16401667 """
16411668
1642- endpoint = self .endpoints .broadcast_url (broadcast_id )
1669+ endpoint = self .endpoints .get_broadcast_url (broadcast_id )
16431670
16441671 logger .debug (
16451672 "GET to %r with headers %r, proxies %r" ,
@@ -1697,7 +1724,7 @@ def set_broadcast_layout(
16971724 if stylesheet is not None :
16981725 payload ["stylesheet" ] = stylesheet
16991726
1700- endpoint = self .endpoints .broadcast_url (broadcast_id , layout = True )
1727+ endpoint = self .endpoints .get_broadcast_url (broadcast_id , layout = True )
17011728
17021729 logger .debug (
17031730 "PUT to %r with params %r, headers %r, proxies %r" ,
0 commit comments