def get_user_twilio_token(self, user_id, context): try: user_id = user_id.value # print("get_user_twilio_token", user_id) TWILIO_ACCOUNT_SID = 'AC0cb509ad66d21f69f2fd6e4b566e2449' TWILIO_API_KEY_SID = 'SK6ea1006b0adabde2957b9077e0a25679' TWILIO_API_KEY_SECRET = 'AP6uPE2wJoVACZlWG2CSnDBI40MRaYfM' # Create an Access Token token = AccessToken(TWILIO_ACCOUNT_SID, TWILIO_API_KEY_SID, TWILIO_API_KEY_SECRET) # Set the Identity of this token token.identity = user_id # Grant access to Video grant = VideoGrant(room='AwesomeRoom') token.add_grant(grant) # Serialize the token as a JWT jwt = token.to_jwt().decode('utf-8') print(jwt) print(user_id) print(token.payload["exp"]) return main_pb2.JWTToken(id=main_pb2.UUID(value=str(user_id)), expiry_time=token.payload["exp"], jwt=jwt) except Exception as e: print("Error in get_user", e) traceback.print_exc(file=sys.stdout)
def on_room_entry_request(data): user_id = (models.DB.session.query( models.CurrentConnections.user).filter_by( sid=flask.request.sid).first()) models.DB.session.add(models.EnteredRooms(user_id, data["roomId"])) models.DB.session.commit() socketio.emit("room entry accepted", room=flask.request.sid) flask_socketio.join_room(str(data["roomId"])) print("room entry accepted") emit_room_history(flask.request.sid) username = models.DB.session.query( models.AuthUser.username).filter_by(id=user_id).first()[0] if data['roomId'] not in roomTokens.keys(): token = AccessToken(twilio_account_sid, twilio_api_key_sid, twilio_api_key_secret, identity=username) roomTokens[data['roomId']] = token token.add_grant(VideoGrant(room=data['roomId'])) socketio.emit( "token", { 'tokens': token.to_jwt().decode(), 'room': str(data['roomId']), 'username': username }) else: token = roomTokens[data['roomId']] token.identity = username socketio.emit( "token", { 'tokens': token.to_jwt().decode(), 'room': str(data['roomId']), 'username': username })
def create_token(identity): token = AccessToken(account_sid, api_key, api_secret) token.identity = identity # Create a Video grant and add to token video_grant = VideoGrant(room='441') token.add_grant(video_grant) # Return token info as JSON # return {"identity":identity, "token":token.to_jwt()} return token.to_jwt()
def test_sync_grant(self): scat = AccessToken(ACCOUNT_SID, SIGNING_KEY_SID, 'secret') scat.identity = "bender" scat.add_grant(SyncGrant(service_sid='IS123', endpoint_id='blahblahendpoint')) token = scat.to_jwt() assert_is_not_none(token) decoded_token = AccessToken.from_jwt(token, 'secret') self._validate_claims(decoded_token.payload) assert_equal(2, len(decoded_token.payload['grants'])) assert_equal("bender", decoded_token.payload['grants']['identity']) assert_equal({ 'service_sid': 'IS123', 'endpoint_id': 'blahblahendpoint' }, decoded_token.payload['grants']['data_sync'])
def getToken(request, room): # Create an Access Token token = AccessToken(ACCOUNT_SID, API_KEY_SID, API_KEY_SECRET) username = '******'.join(random.choice(NAME_WORDS[i]) for i in range(2)) # Set the Identity of this token token.identity = username # Grant access to Video grant = VideoGrant(room=room) # this must be here token.add_grant(grant) # Serialize the token as a JWT jwt = token.to_jwt() return HttpResponse(jwt)
def test_sync_grant(self): scat = AccessToken(ACCOUNT_SID, SIGNING_KEY_SID, 'secret') scat.identity = "bender" scat.add_grant( SyncGrant(service_sid='IS123', endpoint_id='blahblahendpoint')) token = scat.to_jwt() assert_is_not_none(token) decoded_token = AccessToken.from_jwt(token, 'secret') self._validate_claims(decoded_token.payload) assert_equal(2, len(decoded_token.payload['grants'])) assert_equal("bender", decoded_token.payload['grants']['identity']) assert_equal( { 'service_sid': 'IS123', 'endpoint_id': 'blahblahendpoint' }, decoded_token.payload['grants']['data_sync'])
def get_token(identity, room_name): try: # Create an Access Token token = AccessToken(ACCOUNT_SID, API_KEY_SID, API_KEY_SECRET) # token = AccessToken(account_sid, api_key, api_secret, identity=identity) # Set the Identity of this token token.identity = identity # Grant access to Video grant = VideoGrant(room=room_name) token.add_grant(grant) # Serialize the token as a JWT jwt = token.to_jwt() return jwt.decode() except Exception as e: print(e)
def home_video_token(): account_sid = os.environ['TWILIO_ACCOUNT_SID'] video_sid = os.environ['TWILIO_VIDEO_API_SID'] video_secret = os.environ['TWILIO_VIDEO_API_SECRET'] # Generate a random user name identity = alphanumeric_only.sub('', home_name) #Create an Access Token access = AccessToken(account_sid, video_sid, video_secret) access.identity = identity video_grant = VideoGrant() access.add_grant(video_grant) access_token = access.to_jwt() return jsonify( identity=identity, token=access_token.decode('utf-8'))
def token(): # get credentials for environment variables account_sid = os.environ['TWILIO_ACCOUNT_SID'] api_key = os.environ['TWILIO_API_KEY'] api_secret = os.environ['TWILIO_API_SECRET'] # Create an Access Token token = AccessToken(account_sid, api_key, api_secret) # Set the Identity of this token token.identity = fake.user_name() # Grant access to Twilio Video grant = VideoGrant() grant.configuration_profile_sid = os.environ['TWILIO_CONFIGURATION_SID'] token.add_grant(grant) # Return token info as JSON return jsonify(identity=token.identity, token=token.to_jwt())
def token(): # Get credentials for environment variables account_sid = 'ACbce0865b277c75152da6fc469b650463' api_key = 'SK2e4b45c9cb8835c6ad1a614c3346b209' api_secret = 'l6GKan4KkOKCmiktZDTmhahRY7q2aTPz' # Create an Access Token token = AccessToken(account_sid, api_key, api_secret) # Set the Identity of this token token.identity = request.values.get('identity') or 'identity' # Grant access to Video grant = VideoGrant() grant.room = request.values.get('room') token.add_grant(grant) # Return token return token.to_jwt()
def token(): # Get credentials for environment variables account_sid = os.environ['TWILIO_ACCOUNT_SID'] api_key = os.environ['TWILIO_API_KEY'] api_secret = os.environ['TWILIO_API_SECRET'] # Create an Access Token token = AccessToken(account_sid, api_key, api_secret) # Set the Identity of this token token.identity = request.values.get('identity') or 'identity' # Grant access to Video grant = VideoGrant() grant.room = request.values.get('room') token.add_grant(grant) # Return token return token.to_jwt()
def token(): # Get credentials for environment variables account_sid = "ACa9489e89bea2fd6a143b64def1cf01f7" api_key = "SKe3a14561d59837544e4079f95e9b6616" api_secret = "4mxMGKNUjD80RhIZ1xGxtaP6ABCnIlUD" # Create an Access Token token = AccessToken(account_sid, api_key, api_secret) # Set the Identity of this token token.identity = request.values.get('identity') or 'identity' # Grant access to Video grant = VideoGrant() grant.room = request.values.get('room') token.add_grant(grant) # Return token return token.to_jwt()
def token(): # get credentials for environment variables account_sid = os.environ['TWILIO_ACCOUNT_SID'] api_key = os.environ['TWILIO_API_KEY'] api_secret = os.environ['TWILIO_API_SECRET'] # Create an Access Token token = AccessToken(account_sid, api_key, api_secret) # Set the Identity of this token token.identity = fake.user_name() # Grant access to Conversations grant = ConversationsGrant() grant.configuration_profile_sid = os.environ['TWILIO_CONFIGURATION_SID'] token.add_grant(grant) # Return token info as JSON return jsonify(identity=token.identity, token=token.to_jwt())
def create(self, validated_data): """ Create access token instance for Twilio Video. :param dict validated_data: Validated data. :returns: Twilio access token instance :rtype: twilio.jwt.access_token.AccessToken """ twilio_token = AccessToken( account_sid=settings.TWILIO_ACCOUNT_SID, signing_key_sid=settings.TWILIO_VIDEO_API_KEY_SID, secret=settings.TWILIO_VIDEO_API_KEY_SECRET, valid_until=validated_data['valid_until']) twilio_token.identity = validated_data['identity'] # grant access to the room video_grant = VideoGrant(room=validated_data['room_name']) twilio_token.add_grant(video_grant) return twilio_token
def generate_user_token(request): # Substitute your Twilio AccountSid and ApiKey details ACCOUNT_SID = 'SK53fff1b19287ba8960109d5bbdc0c56c' API_KEY_SID = '77d9e514a0c2fe8337dc219dc5e1a00e' API_KEY_SECRET = 'CQKVnuDRufzbyhbjhANkCFTIbHg5jc8f' # Create an Access Token token = AccessToken(ACCOUNT_SID, API_KEY_SID, API_KEY_SECRET) # Set the Identity of this token token.identity = 'example-user' # Grant access to Video grant = VideoGrant(room='cool room') token.add_grant(grant) # Serialize the token as a JWT jwt = token.to_jwt() print(jwt)
def gen_token(): # Substitute your Twilio AccountSid and ApiKey details ACCOUNT_SID = 'AC26b05c62b2a3faac5bdd3519341fe265' API_KEY_SID = 'SK733ec5148e4224b85c71c9dbba61cf28' API_KEY_SECRET = 'SaHY5Pa52vzWv3iJsi09iylN8URjzgWk' # Create an Access Token token = AccessToken(ACCOUNT_SID, API_KEY_SID, API_KEY_SECRET) # Set the Identity of this token token.identity = fake.user_name() print(token) # Grant access to Video grant = VideoGrant() token.add_grant(grant) # Serialize the token as a JWT data = jsonify(identity=token.identity, token=token.to_jwt().decode('utf-8')) return data
def token_room(): identity = request.get_json()['identity'] room_name = request.get_json()['room'] token = AccessToken(account_sid, api_key, api_secret) client = Client(account_sid, auth_token) try: room = client.video.rooms.create(enable_turn=True, type='peer-to-peer', unique_name=room_name) except TwilioRestException as e: room = client.video.rooms.list(unique_name=room_name, limit=1)[0] if not room: raise AttributeError("Room Could not be created!") token.identity = identity grant = VideoGrant(room=room.unique_name) token.add_grant(grant) token_s = str(token.to_jwt().decode("utf-8")) return jsonify({'identity': identity, 'token': token_s})
def token(room_name): # get credentials for environment variables account_sid = os.getenv("TWILIO_ACCOUNT_SID") api_key = os.getenv("TWILIO_API_KEY") api_secret = os.getenv("TWILIO_API_SECRET") # Create an Access Token token = AccessToken(account_sid, api_key, api_secret) # Set the Identity of this token print(_request_ctx_stack.top.current_user['sub']) token.identity = _request_ctx_stack.top.current_user['sub'] # Grant access to Video grant = VideoGrant() grant.room = room_name token.add_grant(grant) # Return token info as JSON return jsonify(identity=token.identity, token=token.to_jwt().decode('UTF-8'))
def get_token(identity, room): # Get credentials for environment variables account_sid = os.environ['TWILIO_ACCOUNT_SID'] api_key = os.environ['TWILIO_API_KEY'] api_secret = os.environ['TWILIO_API_SECRET'] # Create an Access Token token = AccessToken(account_sid, api_key, api_secret) # Set the Identity of this token time_stamp = time.time() token.identity = identity + str(time_stamp) # Grant access to Video grant = VideoGrant() grant.room = room token.add_grant(grant) # Return token return token.to_jwt().decode('utf-8')
def token(request): # Create an Access Token token = AccessToken(ACCOUNT_SID, API_KEY, API_SECRET) # Set the Identity of this token # token.identity = request.values.get('identity') or 'identity' token.identity = fake.name() # Grant access to Video grant = VideoGrant() # grant.room = requests.get('room') grant.room = 'room' token.add_grant(grant) # Return token # return token.to_jwt() data = { 'identity': token.identity, 'token': token.to_jwt().decode('utf-8') } return Response(data, status=HTTP_200_OK)