Skip to content

Commit

Permalink
Add attributes to AccessToken creation (livekit#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
keepingitneil authored Aug 16, 2024
1 parent d6b657e commit 173bd99
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
7 changes: 6 additions & 1 deletion livekit-api/livekit/api/access_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class Claims:
name: str = ""
video: VideoGrants = dataclasses.field(default_factory=VideoGrants)
sip: SIPGrants = dataclasses.field(default_factory=SIPGrants)
attributes: dict[str, str] = dataclasses.field(default_factory=dict)
metadata: str = ""
sha256: str = ""

Expand Down Expand Up @@ -125,6 +126,10 @@ def with_metadata(self, metadata: str) -> "AccessToken":
self.claims.metadata = metadata
return self

def with_attributes(self, attributes: dict[str, str]) -> "AccessToken":
self.claims.attributes = attributes
return self

def with_sha256(self, sha256: str) -> "AccessToken":
self.claims.sha256 = sha256
return self
Expand All @@ -148,7 +153,6 @@ def to_jwt(self) -> str:
),
}
)

return jwt.encode(claims, self.api_secret, algorithm="HS256")


Expand Down Expand Up @@ -198,6 +202,7 @@ def verify(self, token: str) -> Claims:
name=claims.get("name", ""),
video=video,
sip=sip,
attributes=claims.get("attributes", {}),
metadata=claims.get("metadata", ""),
sha256=claims.get("sha256", ""),
)
Expand Down
3 changes: 3 additions & 0 deletions livekit-api/tests/test_access_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def test_verify_token():
.with_metadata("test_metadata")
.with_grants(grants)
.with_sip_grants(sip)
.with_attributes({"key1": "value1", "key2": "value2"})
.to_jwt()
)

Expand All @@ -27,6 +28,8 @@ def test_verify_token():
assert claims.metadata == "test_metadata"
assert claims.video == grants
assert claims.sip == sip
assert claims.attributes["key1"] == "value1"
assert claims.attributes["key2"] == "value2"


def test_verify_token_invalid():
Expand Down
4 changes: 2 additions & 2 deletions livekit-rtc/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@

import os
import pathlib
from sysconfig import get_platform
from typing import Any, Dict

import setuptools # type: ignore
import setuptools.command.build_py # type: ignore
from wheel.bdist_wheel import bdist_wheel as _bdist_wheel # type: ignore
from wheel.bdist_wheel import get_platform # type: ignore

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

class bdist_wheel(_bdist_wheel):
def finalize_options(self):
self.plat_name = get_platform(None) # force a platform tag
self.plat_name = get_platform() # force a platform tag
_bdist_wheel.finalize_options(self)


Expand Down

0 comments on commit 173bd99

Please sign in to comment.