o
    i$                     @   s  d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ d dlmZm	Z	m
Z
 d dlmZmZ d dlmZ ejddZejdd	ZeG d
d dZeG dd dZeG dd dZeG dd dZeG dd dZG dd dZG dd dZdefddZdefddZdS )    N)	dataclass)OptionalListLiteral)MessageToDict	ParseDict)RoomConfiguration   )hours   )minutesc                   @   s  e Zd ZU dZee ed< dZee ed< dZee ed< dZ	ee ed< dZ
ee ed< dZeed< dZee ed	< d
Zeed< d
Zeed< d
Zeed< dZeee  ed< dZee ed< dZee ed< dZee ed< dZee ed< dZee ed< dS )VideoGrantsNroom_create	room_listroom_record
room_admin	room_join roomdestination_roomTcan_publishcan_subscribecan_publish_datacan_publish_sourcescan_update_own_metadataingress_adminhiddenrecorderagent)__name__
__module____qualname__r   r   bool__annotations__r   r   r   r   r   strr   r   r   r   r   r   r   r   r   r   r    r%   r%   Z/var/www/html/livekit_bhavya/venv/lib/python3.10/site-packages/livekit/api/access_token.pyr      s"   
 r   c                   @   s&   e Zd ZU dZeed< dZeed< dS )	SIPGrantsFadmincallN)r   r    r!   r(   r"   r#   r)   r%   r%   r%   r&   r'   H   s   
 r'   c                   @      e Zd ZU dZeed< dS )InferenceGrantsFperformN)r   r    r!   r,   r"   r#   r%   r%   r%   r&   r+   P      
 r+   c                   @   r*   )ObservabilityGrantsFwriteN)r   r    r!   r/   r"   r#   r%   r%   r%   r&   r.   V   r-   r.   c                   @   s   e Zd ZU dZeed< dZeed< dZeed< dZeed< dZ	e
e ed< dZe
e ed< dZe
e ed	< dZe
e ed
< dZe
eeef  ed< dZe
e ed< dZe
e ed< dZe
e ed< defddZdS )Claimsr   identitynamekindmetadataNvideosip	inferenceobservability
attributessha256room_presetroom_configreturnc                 C   s*   t j| dd d}| jrt| j|d< |S )Nc                 S   s   dd | D S )Nc                 S   s*   i | ]\}}|d ur|dkrt ||qS )Nr   )snake_to_lower_camel.0kvr%   r%   r&   
<dictcomp>o   s    $z3Claims.asdict.<locals>.<lambda>.<locals>.<dictcomp>r%   )itemsr%   r%   r&   <lambda>o   s    zClaims.asdict.<locals>.<lambda>)dict_factory
roomConfig)dataclassesasdictr<   r   )selfclaimsr%   r%   r&   rI   k   s   zClaims.asdict)r   r    r!   r1   r$   r#   r2   r3   r4   r5   r   r   r6   r'   r7   r+   r8   r.   r9   dictr:   r;   r<   r   rI   r%   r%   r%   r&   r0   \   s   
 r0   c                   @   s:  e Zd Zed Z		d.dee dee ddfddZdej	dd fd	d
Z
dedd fddZdedd fddZdedd fddZdedd fddZdedd fddZdedd fddZdedd fddZdedd fddZd eeef dd fd!d"Zd#edd fd$d%Zd&edd fd'd(Zd)edd fd*d+Zdefd,d-ZdS )/AccessToken)standardegressingressr6   r   Napi_key
api_secretr=   c                 C   sP   |pt d}|pt d}|r|std|| _|| _t | _d| _t| _	d S )NLIVEKIT_API_KEYLIVEKIT_API_SECRET"api_key and api_secret must be setr   )
osgetenv
ValueErrorrQ   rR   r0   rK   r1   DEFAULT_TTLttl)rJ   rQ   rR   r%   r%   r&   __init__{   s   
zAccessToken.__init__rZ   c                 C   
   || _ | S N)rZ   )rJ   rZ   r%   r%   r&   with_ttl      zAccessToken.with_ttlgrantsc                 C      || j _| S r]   )rK   r5   rJ   r`   r%   r%   r&   with_grants      zAccessToken.with_grantsc                 C   ra   r]   )rK   r6   rb   r%   r%   r&   with_sip_grants   rd   zAccessToken.with_sip_grantsc                 C   ra   r]   )rK   r8   rb   r%   r%   r&   with_observability_grants   rd   z%AccessToken.with_observability_grantsc                 C   ra   r]   )rK   r7   rb   r%   r%   r&   with_inference_grants   rd   z!AccessToken.with_inference_grantsr1   c                 C   r\   r]   )r1   )rJ   r1   r%   r%   r&   with_identity   r_   zAccessToken.with_identityr3   c                 C   ra   r]   )rK   r3   )rJ   r3   r%   r%   r&   	with_kind   rd   zAccessToken.with_kindr2   c                 C   ra   r]   )rK   r2   )rJ   r2   r%   r%   r&   	with_name   rd   zAccessToken.with_namer4   c                 C   ra   r]   )rK   r4   )rJ   r4   r%   r%   r&   with_metadata   rd   zAccessToken.with_metadatar9   c                 C   ra   r]   )rK   r9   )rJ   r9   r%   r%   r&   with_attributes   rd   zAccessToken.with_attributesr:   c                 C   ra   r]   )rK   r:   )rJ   r:   r%   r%   r&   with_sha256   rd   zAccessToken.with_sha256presetc                 C   ra   r]   )rK   r;   )rJ   rn   r%   r%   r&   with_room_preset   rd   zAccessToken.with_room_presetconfigc                 C   ra   r]   )rK   r<   )rJ   rp   r%   r%   r&   with_room_config   rd   zAccessToken.with_room_configc              
   C   s   | j j}|r|jr| jr|jstd| j  }|| j| jt	
tjtjj t	
tjtjj| j  d tj|| jddS )Nz1identity and room must be set when joining a room)subissnbfexpHS256)	algorithm)rK   r5   r   r1   r   rX   rI   updaterQ   calendartimegmdatetimenowtimezoneutcutctimetuplerZ   jwtencoderR   )rJ   r5   
jwt_claimsr%   r%   r&   to_jwt   s   

zAccessToken.to_jwtNN)r   r    r!   r   ParticipantKindr   r$   r[   r{   	timedeltar^   r   rc   r'   re   r.   rf   r+   rg   rh   ri   rj   rk   rL   rl   rm   ro   r   rq   r   r%   r%   r%   r&   rM   x   s2    
rM   c                
   @   sX   e Zd Z		deddee dee dejddfddZd	d
dede	de
fddZdS )TokenVerifierN)leewayrQ   rR   r   r=   c                C   s2   |pt d}|pt d}|| _|| _|| _d S )NrS   rT   )rV   rW   rQ   rR   _leeway)rJ   rQ   rR   r   r%   r%   r&   r[      s
   
zTokenVerifier.__init__T)verify_signaturetokenr   c                C   s  |r| j r| jstdtj|| jpd| j pddg| j d|id}|dt }dd |	 D }d	d |	 D }t
di |}|d
t }dd |	 D }dd |	 D }tdi |}|dt }dd |	 D }dd |	 D }tdi |}	t|dd|dd|||	|di |dd|ddd}
|dr|d|
_|drt|dt dd|
_|
S )NrU   r   rv   r   )keyissuer
algorithmsr   optionsr5   c                 S      i | ]	\}}t ||qS r%   camel_to_snaker?   r%   r%   r&   rC          z(TokenVerifier.verify.<locals>.<dictcomp>c                 S       i | ]\}}|t jv r||qS r%   )r   __dataclass_fields__r?   r%   r%   r&   rC           r6   c                 S   r   r%   r   r?   r%   r%   r&   rC      r   c                 S   r   r%   )r'   r   r?   r%   r%   r&   rC      r   r7   c                 S   r   r%   r   r?   r%   r%   r&   rC      r   c                 S   r   r%   )r+   r   r?   r%   r%   r&   rC      s    rr   r2   r9   r4   r:   )r1   r2   r5   r6   r7   r9   r4   r:   
roomPresetrG   T)ignore_unknown_fieldsr%   )rQ   rR   rX   r   decoder   total_secondsgetrL   rD   r   r'   r+   r0   r;   r   r   r<   )rJ   r   r   rK   
video_dictr5   sip_dictr6   inference_dictr7   grant_claimsr%   r%   r&   verify   sV   






zTokenVerifier.verifyr   )r   r    r!   DEFAULT_LEEWAYr   r$   r{   r   r[   r"   r0   r   r%   r%   r%   r&   r      s    
 r   tc                 C   s   t dd|  S )Nz(?<!^)(?=[A-Z])_)rerr   lowerr   r%   r%   r&   r     s   r   c                 C   s   d dd t| dD S )Nr   c                 s   s$    | ]\}}|r|  n|V  qd S r]   )
capitalize)r@   iwordr%   r%   r&   	<genexpr>  s   " z'snake_to_lower_camel.<locals>.<genexpr>r   )join	enumeratesplitr   r%   r%   r&   r>     s   r>   )ry   r   r{   rV   r   rH   r   typingr   r   r   google.protobuf.json_formatr   r   livekit.protocol.roomr   r   rY   r   r   r'   r+   r.   r0   rM   r   r$   r   r>   r%   r%   r%   r&   <module>   s4   (^C