Created
May 28, 2023 01:22
-
-
Save guilhermebene/9087ab78ee1e998cdf1cea9a7a001cb5 to your computer and use it in GitHub Desktop.
A Python function for creating credentials for using TURN services provided by eturnal STUN/TURN Server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import base64 | |
import hashlib | |
import hmac | |
import json | |
import time | |
from datetime import datetime, timedelta | |
def create_credentials(secret, suffix): | |
"""Creates credentials for Eturnal access available for 24 hours. | |
Eturnal project: https://github.com/processone/eturnal | |
Credentials based on the "REST API For Access To TURN Services": https://datatracker.ietf.org/doc/html/draft-uberti-behave-turn-rest-00 | |
""" | |
current_time = datetime.utcnow() | |
future_time = current_time + timedelta(hours=24) | |
expiry = future_time.strftime('%Y-%m-%dT%H:%M:%SZ') | |
unix_time = int(time.mktime(time.strptime(expiry, '%Y-%m-%dT%H:%M:%SZ'))) | |
username = create_username(unix_time, suffix) | |
hmac_obj = hmac.new(secret.encode('utf-8'), username.encode('utf-8'), hashlib.sha1) | |
password = base64.b64encode(hmac_obj.digest()).decode('utf-8') | |
return username, password |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment