Skip to content

Commit 6b7acc8

Browse files
authored
Support requesting a temporary token (JWT) with region and client ref (#133)
1 parent 1830e6e commit 6b7acc8

File tree

6 files changed

+52
-7
lines changed

6 files changed

+52
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [4.0.7] - 2025-06-19
9+
10+
- Support requesting a temporary token (JWT) with region and client ref
11+
812
## [4.0.6] - 2025-06-19
913

1014
- Moved channel_diarization_labels field from realtime transcription config to common class.

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4.0.6
1+
4.0.7

speechmatics/client.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ async def run(
587587
self.connection_settings.generate_temp_token
588588
and self.connection_settings.auth_token is not None
589589
):
590-
temp_token = await _get_temp_token(self.connection_settings.auth_token)
590+
temp_token = await _get_temp_token(self.connection_settings)
591591
token = f"Bearer {temp_token}"
592592
extra_headers["Authorization"] = token
593593

@@ -670,18 +670,30 @@ async def send_message(self, message_type: str, data: Optional[Any] = None):
670670
raise exc
671671

672672

673-
async def _get_temp_token(api_key):
673+
async def _get_temp_token(connection_settings: ConnectionSettings):
674674
"""
675675
Used to get a temporary token from management platform api for SaaS users
676676
"""
677677
version = get_version()
678-
mp_api_url = os.getenv("SM_MANAGEMENT_PLATFORM_URL", "https://mp.speechmatics.com")
678+
mp_api_url = os.getenv("SM_MANAGEMENT_PLATFORM_URL", connection_settings.mp_url)
679+
680+
assert mp_api_url, "Management platform URL not set"
681+
679682
endpoint = mp_api_url + "/v1/api_keys"
680683
params = {"type": "rt", "sm-sdk": f"python-{version}"}
681-
body = {"ttl": 60}
682-
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
684+
payload: dict[str, Union[str, int]] = {"ttl": 60}
685+
686+
if connection_settings.region:
687+
payload["region"] = connection_settings.region
688+
if connection_settings.client_ref:
689+
payload["client_ref"] = connection_settings.client_ref
690+
691+
headers = {
692+
"Authorization": f"Bearer {connection_settings.auth_token}",
693+
"Content-Type": "application/json",
694+
}
683695
# pylint: disable=no-member
684-
response = httpx.post(endpoint, json=body, params=params, headers=headers)
696+
response = httpx.post(endpoint, json=payload, params=params, headers=headers)
685697
response.raise_for_status()
686698
response.read()
687699
key_object = response.json()

speechmatics/models.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,15 @@ class ConnectionSettings:
469469
"""Automatically generate a temporary token for authentication.
470470
Enterprise customers should set this to False."""
471471

472+
mp_url: Optional[str] = "https://mp.speechmatics.com"
473+
"""Management platform URL for generating temporary tokens."""
474+
475+
region: Optional[str] = "eu"
476+
"""Region for generating temporary tokens."""
477+
478+
client_ref: Optional[str] = None
479+
"""Client reference for generating temporary tokens."""
480+
472481
def set_missing_values_from_config(self, mode: UsageMode):
473482
stored_config = read_config_from_home()
474483
if self.url is None or self.url == "":

tests/test_cli.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,7 @@ def test_rt_main_with_multichannel_option(mock_server):
782782
]
783783

784784
cli.main(vars(cli.parse_args(args)))
785+
mock_server.wait_for_clean_disconnects()
785786

786787
assert mock_server.clients_connected_count == 1
787788
assert mock_server.clients_disconnected_count == 1

tests/test_models.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,25 @@ def test_connection_settings_url(url, want):
9090
assert got == want
9191

9292

93+
def test_default_jwt_connection_settings():
94+
connection_settings = models.ConnectionSettings(url="examples.com")
95+
assert connection_settings.generate_temp_token is False
96+
assert connection_settings.region == "eu"
97+
assert connection_settings.client_ref is None
98+
99+
100+
def test_custom_jwt_connection_settings():
101+
connection_settings = models.ConnectionSettings(
102+
url="examples.com",
103+
generate_temp_token=True,
104+
region="usa",
105+
client_ref="test",
106+
)
107+
assert connection_settings.generate_temp_token is True
108+
assert connection_settings.region == "usa"
109+
assert connection_settings.client_ref == "test"
110+
111+
93112
@mark.parametrize(
94113
"params, want",
95114
[

0 commit comments

Comments
 (0)