Skip to content

Commit 8b48eb5

Browse files
authored
update readme & fix access token (#99)
1 parent ce81435 commit 8b48eb5

File tree

3 files changed

+31
-22
lines changed

3 files changed

+31
-22
lines changed

README.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ $ pip install livekit-api
3333
from livekit import api
3434
import os
3535

36-
token = api.AccessToken(os.getenv('LIVEKIT_API_KEY'), os.getenv('LIVEKIT_API_SECRET')) \
36+
# will automatically use the LIVEKIT_API_KEY and LIVEKIT_API_SECRET env vars
37+
token = api.AccessToken() \
3738
.with_identity("python-bot") \
3839
.with_name("Python Bot") \
3940
.with_grants(api.VideoGrants(
@@ -51,18 +52,16 @@ from livekit import api
5152
import asyncio
5253

5354
async def main():
54-
room_service = api.RoomService(
55+
lkapi = api.LiveKitAPI(
5556
'http://localhost:7880',
56-
'devkey',
57-
'secret',
5857
)
59-
room_info = await room_service.create_room(
60-
api.room.CreateRoomRequest(name="my-room"),
58+
room_info = await lkapi.room.create_room(
59+
api.CreateRoomRequest(name="my-room"),
6160
)
6261
print(room_info)
63-
results = await room_service.list_rooms(api.room.ListRoomsRequest())
62+
results = await lkapi.room.list_rooms(api.ListRoomsRequest())
6463
print(results)
65-
await room_service.aclose()
64+
await lkapi.aclose()
6665

6766
asyncio.get_event_loop().run_until_complete(main())
6867
```

livekit-api/livekit/api/access_token.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,7 @@ class VideoGrants:
6464

6565
@dataclasses.dataclass
6666
class Claims:
67-
exp: int = 0
68-
iss: str = "" # api key
69-
nbf: int = 0
70-
sub: str = "" # identity
71-
67+
identity: str = ""
7268
name: str = ""
7369
video: VideoGrants = dataclasses.field(default_factory=VideoGrants)
7470
metadata: str = ""
@@ -120,8 +116,10 @@ def to_jwt(self) -> str:
120116
if video.room_join and (not self.identity or not video.room):
121117
raise ValueError("identity and room must be set when joining a room")
122118

123-
claims = dataclasses.asdict(self.claims)
124-
claims = {camel_to_snake(k): v for k, v in claims.items()}
119+
claims = dataclasses.asdict(
120+
self.claims,
121+
dict_factory=lambda items: {snake_to_lower_camel(k): v for k, v in items},
122+
)
125123
claims.update(
126124
{
127125
"sub": self.identity,
@@ -156,17 +154,29 @@ def verify(self, token: str) -> Claims:
156154
algorithms=["HS256"],
157155
leeway=self._leeway.total_seconds(),
158156
)
159-
c = Claims(**claims)
160157

161-
video = claims["video"]
162-
video = {camel_to_snake(k): v for k, v in video.items()}
163-
c.video = VideoGrants(**video)
158+
video_dict = {camel_to_snake(k): v for k, v in claims["video"].items()}
159+
video_dict = {
160+
k: v for k, v in video_dict.items() if k in VideoGrants.__dataclass_fields__
161+
}
162+
video = VideoGrants(**video_dict)
163+
164+
c = Claims(
165+
identity=claims["sub"],
166+
name=claims["name"],
167+
video=video,
168+
metadata=claims["metadata"],
169+
sha256=claims["sha256"],
170+
)
171+
c.identity = claims["sub"]
164172
return c
165173

166174

167175
def camel_to_snake(t: str):
168176
return re.sub(r"(?<!^)(?=[A-Z])", "_", t).lower()
169177

170178

171-
def snake_to_camel(t: str):
172-
return "".join(x.title() for x in t.split("_"))
179+
def snake_to_lower_camel(t: str):
180+
return "".join(
181+
word.capitalize() if i else word for i, word in enumerate(t.split("_"))
182+
)

livekit-api/tests/test_access_token.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def test_verify_token():
2020
token_verifier = TokenVerifier(TEST_API_KEY, TEST_API_SECRET)
2121
claims = token_verifier.verify(token)
2222

23-
assert claims.sub == "test_identity"
23+
assert claims.identity == "test_identity"
2424
assert claims.metadata == "test_metadata"
2525
assert claims.video == grants
2626

0 commit comments

Comments
 (0)