/
mikopbx
/
ModuleSpeechRecognize
Обзор
Документация
Войти
/
mikopbx
/
ModuleSpeechRecognize
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
master
Lib/python/auth.py
42 строки
1 KB
boffart
init
02 окт 2025, 12:39
02 окт 2025, 12:39
107ee1d
Код
Авторство
О чём код?
import base64 import copy import hmac import json from time import time TEN_MINUTES = 600 # seconds def generate_jwt(api_key, secret_key, payload, expiration_time=TEN_MINUTES): header = { "alg": "HS256", "typ": "JWT", "kid": api_key } payload_copy = copy.deepcopy(payload) current_timestamp = int(time()) payload_copy["exp"] = current_timestamp + expiration_time # payload_copy["iat"] = current_timestamp # payload_copy["nbf"] = current_timestamp payload_bytes = json.dumps(payload_copy, separators=(',', ':')).encode("utf-8") header_bytes = json.dumps(header, separators=(',', ':')).encode("utf-8") data = (base64.urlsafe_b64encode(header_bytes).strip(b'=') + b"." + base64.urlsafe_b64encode(payload_bytes).strip(b'=')) signature = hmac.new(base64.urlsafe_b64decode(secret_key), msg=data, digestmod="sha256") jwt = data + b"." + base64.urlsafe_b64encode(signature.digest()).strip(b'=') return jwt.decode("utf-8") def authorization_metadata(api_key, secret_key, scope, type=list): auth_payload = { "iss": "test_issuer", "sub": "test_user", "aud": scope } metadata = [ ("authorization", "Bearer " + generate_jwt(api_key, secret_key, auth_payload)) ] return type(metadata)