@@ -64,11 +64,7 @@ class VideoGrants:
6464
6565@dataclasses .dataclass
6666class 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
167175def 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+ )
0 commit comments