Skip to content

Commit 173bd99

Browse files
Add attributes to AccessToken creation (#233)
1 parent d6b657e commit 173bd99

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

livekit-api/livekit/api/access_token.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class Claims:
7777
name: str = ""
7878
video: VideoGrants = dataclasses.field(default_factory=VideoGrants)
7979
sip: SIPGrants = dataclasses.field(default_factory=SIPGrants)
80+
attributes: dict[str, str] = dataclasses.field(default_factory=dict)
8081
metadata: str = ""
8182
sha256: str = ""
8283

@@ -125,6 +126,10 @@ def with_metadata(self, metadata: str) -> "AccessToken":
125126
self.claims.metadata = metadata
126127
return self
127128

129+
def with_attributes(self, attributes: dict[str, str]) -> "AccessToken":
130+
self.claims.attributes = attributes
131+
return self
132+
128133
def with_sha256(self, sha256: str) -> "AccessToken":
129134
self.claims.sha256 = sha256
130135
return self
@@ -148,7 +153,6 @@ def to_jwt(self) -> str:
148153
),
149154
}
150155
)
151-
152156
return jwt.encode(claims, self.api_secret, algorithm="HS256")
153157

154158

@@ -198,6 +202,7 @@ def verify(self, token: str) -> Claims:
198202
name=claims.get("name", ""),
199203
video=video,
200204
sip=sip,
205+
attributes=claims.get("attributes", {}),
201206
metadata=claims.get("metadata", ""),
202207
sha256=claims.get("sha256", ""),
203208
)

livekit-api/tests/test_access_token.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def test_verify_token():
1717
.with_metadata("test_metadata")
1818
.with_grants(grants)
1919
.with_sip_grants(sip)
20+
.with_attributes({"key1": "value1", "key2": "value2"})
2021
.to_jwt()
2122
)
2223

@@ -27,6 +28,8 @@ def test_verify_token():
2728
assert claims.metadata == "test_metadata"
2829
assert claims.video == grants
2930
assert claims.sip == sip
31+
assert claims.attributes["key1"] == "value1"
32+
assert claims.attributes["key2"] == "value2"
3033

3134

3235
def test_verify_token_invalid():

livekit-rtc/setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414

1515
import os
1616
import pathlib
17+
from sysconfig import get_platform
1718
from typing import Any, Dict
1819

1920
import setuptools # type: ignore
2021
import setuptools.command.build_py # type: ignore
2122
from wheel.bdist_wheel import bdist_wheel as _bdist_wheel # type: ignore
22-
from wheel.bdist_wheel import get_platform # type: ignore
2323

2424
here = pathlib.Path(__file__).parent.resolve()
2525
about: Dict[Any, Any] = {}
@@ -29,7 +29,7 @@
2929

3030
class bdist_wheel(_bdist_wheel):
3131
def finalize_options(self):
32-
self.plat_name = get_platform(None) # force a platform tag
32+
self.plat_name = get_platform() # force a platform tag
3333
_bdist_wheel.finalize_options(self)
3434

3535

0 commit comments

Comments
 (0)