ó
    Ð]j-&  ã                  óà  • % S SK Jr  S SKrS SKrS SKrS SKrS SKJr  S SKJrJ	r	  S SK
Jr  S SKJrJr  S SKJr  S SKrS SKJr  S S	KJr  S S
KJr  S SKJr  SSKJrJr  \R:                  " 5       S-  r \S-  r \R@                  " S5      r! " S S\5      r"SSS.SSS.S.r#S\$S'     " S S\5      r% " S S\SS 9r&\ " S! S"5      5       r'\ " S# S$5      5       r( " S% S&\5      r)S)S' jr*S*S( jr+g)+é    )ÚannotationsN)Ú	dataclass)ÚdatetimeÚtimezone)ÚPath)Ú	TypedDictÚcast)Úurljoin)ÚConsole)Ú	IntPrompt)ÚSelf)ÚLogfireConfigErroré   )ÚUnexpectedResponseÚread_toml_filez.logfirezdefault.tomlzU^(?P<safe_part>pylf_v(?P<version>[0-9]+)_(?P<region>[a-z]+)_)(?P<token>[a-zA-Z0-9]+)$c                  ó*   • \ rS rSr% S\S'   S\S'   Srg)Ú_RegionDataé!   ÚstrÚbase_urlÚ
gcp_region© N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__annotations__Ú__static_attributes__r   ó    ÚS/home/mande/repo/quber/.venv/lib/python3.13/site-packages/logfire/_internal/auth.pyr   r   !   s   ‡ ØƒMØ†Or   r   zhttps://logfire-us.pydantic.devzus-east4)r   r   zhttps://logfire-eu.pydantic.devzeurope-west4)ÚusÚeuzdict[str, _RegionData]ÚREGIONSc                  ó.   • \ rS rSr% SrS\S'   S\S'   Srg)ÚUserTokenDataé3   zUser token data.r   ÚtokenÚ
expirationr   N©r   r   r   r   Ú__doc__r   r   r   r   r    r%   r%   3   s   ‡ ÙàƒJØ†Or   r%   c                  ó$   • \ rS rSr% SrS\S'   Srg)ÚUserTokensFileDataé:   z/Content of the file containing the user tokens.zdict[str, UserTokenData]Útokensr   Nr)   r   r   r    r,   r,   :   s   ‡ Ù9à$Ö$r   r,   F)Útotalc                  ój   • \ rS rSr% SrS\S'   S\S'   S\S'   \SS j5       r\SS j5       r	SS	 jr
S
rg)Ú	UserTokené@   zA user token.r   r'   r   r(   c                ó   • U " US   UUS   S9$ )Nr'   r(   )r'   r   r(   r   )Úclsr   r'   s      r    Úfrom_user_token_dataÚUserToken.from_user_token_dataH   s!   € áØ˜‘.ØØ˜\Ñ*ñ
ð 	
r   c                óÜ   • [         R                  " [        R                  S9[         R                  " U R
                  R                  S5      5      R                  [        R                  S9:¬  $ )zWhether the token is expired.)ÚtzÚZ)Útzinfo)r   Únowr   ÚutcÚfromisoformatr(   ÚrstripÚreplace)Úselfs    r    Ú
is_expiredÚUserToken.is_expiredP   sS   € ô �|Š|œxŸ|™|Ñ,´×0FÒ0FÀtÇÁ×G]ÑG]Ð^aÓGbÓ0c×0kÑ0kÜ—<‘<ð 1lð 1
ñ 
ð 	
r   c                ó\  • Sn[         R                  U R                  5      =n(       a  UR                  S5      nU[        ;  a  SnUR                  5        SU R                   S3nU(       a)  X2R                  S5      UR                  S5      S S -   -  nOX0R                  S S -  nUS-  nU$ )	Nr!   Úregionú (z) - Ú	safe_partr'   é   z****)ÚPYDANTIC_LOGFIRE_TOKEN_PATTERNÚmatchr'   Úgroupr#   Úupperr   )r@   rD   rI   Ú
token_reprs       r    Ú__str__ÚUserToken.__str__W   s¤   € ØˆÜ2×8Ñ8¸¿¹ÓDÐDˆ5ÕDØ—[‘[ Ó*ˆFØœWÓ$Ø�àŸ™›Ð' r¨$¯-©-¨¸Ð=ˆ
ÞØŸ+™+ kÓ2°U·[±[ÀÓ5IÈ"È1Ð5MÑMÑM‰JàŸ*™* R a˜.Ñ(ˆJØ�fÑˆ
ØÐr   r   N)r   r   r'   r%   Úreturnr   )rO   Úbool)rO   r   )r   r   r   r   r*   r   Úclassmethodr5   ÚpropertyrA   rM   r   r   r   r    r1   r1   @   s?   ‡ áàƒJØƒMØƒOàó
ó ð
ð ó
ó ð
÷r   r1   c                  ó~   • \ rS rSr% SrS\S'    S\S'    SSS jjrSSS	 jjrSSS
 jjrSS jr	SSS jjr
SS jrSrg)ÚUserTokenCollectionég   zåA collection of user tokens, read from a user tokens file.

Args:
    path: The path where the user tokens will be stored. If the path doesn't exist,
        an empty collection is created. Defaults to `~/.logfire/default.toml`.
zdict[str, UserToken]Úuser_tokensr   ÚpathNc           
     ó(  • Ub  UO[         U l         [        [        [	        U R                  5      5      nUR                  S0 5      R                  5        VVs0 s H  u  p4U[        SSU0UD6_M     snnU l	        g ! [
         a    0 n NUf = fs  snnf )Nr.   r   r   )
ÚDEFAULT_FILErW   r	   r,   r   ÚFileNotFoundErrorÚgetÚitemsr1   rV   )r@   rW   ÚdataÚurlr'   s        r    Ú__init__ÚUserTokenCollection.__init__v   sŽ   € ð !Ñ,‘D´,ˆŒ	ð	*ÜÔ*¬N¸4¿9¹9Ó,EÓFˆDð UY×T\ÑT\Ð]eÐgiÓTj×TpÑTpÔTrÔsÒTrÁjÀc˜C¤Ñ!A°CÐ!A¸5Ñ!AÒAÑTrÒsˆÕøô !ó 	*Ø')ŠDð	*üãss   ’#A< ÁBÁ<BÂ
Bc           	     ód  • [        U R                  R                  5       5      nUb.  U R                  R                  U5      nUc  [	        SU S35      eO¶[        U5      S:X  a  US   nO¡[        U5      S:¼  a‡  SR                  S [        USS9 5       5      n[        R                  " S	U S3[        S[        U5      S-   5       Vs/ s H  n[        U5      PM     sn[        [        R                  S
9S9nX&S-
     nO[	        S5      eUR                  (       a  [	        SU S35      eU$ s  snf )aÓ  Get a user token from the collection.

Args:
    base_url: Only look for user tokens valid for this base URL. If not provided,
        all the tokens of the collection will be considered: if only one token is
        available, it will be used, otherwise the user will be prompted to choose
        a token.

Raises:
    LogfireConfigError: If no user token is found (no token matched the base URL,
        the collection is empty, or the selected token is expired).
ú%No user token was found matching the ú8 Logfire URL. Please run `logfire auth` to authenticate.r   r   é   Ú
c              3  ób   #   • U  H%  u  pU S U SUR                   (       a  SOS S3v •  M'     g7f)z. rE   ÚexpiredÚvalidÚ)N©rA   )Ú.0Úir'   s      r    Ú	<genexpr>Ú0UserTokenCollection.get_token.<locals>.<genexpr>™   s7   é € ð $â ?‘H�Að �#�R˜�w˜b¨e×.>×.>¡ÀGÐ LÈAÕNÚ ?ùs   ‚-/)Ústartz/Multiple user tokens found. Please select one:
)Úfile)ÚchoicesÚconsoleúKYou are not logged into Logfire. Please run `logfire auth` to authenticate.zUser token z7 is expired. Please run `logfire auth` to authenticate.)ÚlistrV   Úvaluesr[   r   ÚlenÚjoinÚ	enumerater   ÚaskÚranger   r   ÚsysÚstderrrA   )r@   r   Útokens_listr'   Úchoices_strrl   Ú
int_choices          r    Ú	get_tokenÚUserTokenCollection.get_token€   s?  € ô ˜4×+Ñ+×2Ñ2Ó4Ó5ˆàÑØ×$Ñ$×(Ñ(¨Ó2ˆEØ‰}Ü(Ø;¸H¸:ð FAð Aóð ð ô
 �Ó Ó"Ø ‘N‰EÜ�Ó Ó"ØŸ)™)ñ $ä )¨+¸QÒ ?ó$ó ˆKô #ŸšØBÀ;À-ÈrÐRÜ).¨q´#°kÓ2BÀQÑ2FÔ)GÓHÒ)G Aœ˜QžÑ)GÑHÜ¤S§Z¡ZÑ0ñˆJð
  ¨Q¡Ñ/‰Eä$Ð%rÓsÐsà××Ü$ {°5°'Ð9pÐ%qÓrÐrØˆùò Is   ÃD-
c                ó¬   ^• Tb%  U4S jU R                   R                  5        5       nOU R                   R                  5       n[        S U 5       5      $ )zçCheck whether the user token collection contains at least one valid user token.

Args:
    base_url: Only check for user tokens valid for this base URL. If not provided,
        all the tokens of the collection will be considered.
c              3  óJ   >#   • U  H  oR                   T:X  d  M  Uv •  M     g 7f©N)r   )rk   Útr   s     €r    rm   Ú3UserTokenCollection.is_logged_in.<locals>.<genexpr>²   s   øé € ÐUÒ!:˜A¿j¹jÈHÑ>T—a‘aÒ!:ùs   ƒ#š	#c              3  óB   #   • U  H  oR                   (       + v •  M     g 7fr„   rj   )rk   r…   s     r    rm   r†   µ   s   é € Ð4ªV¨—|‘|×#Ñ#ªVùs   ‚)rV   ru   Úany)r@   r   r.   s    ` r    Úis_logged_inÚ UserTokenCollection.is_logged_inª   sI   ø€ ð ÑÜU ×!1Ñ!1×!8Ñ!8Ô!:ÓU‰Fà×%Ñ%×,Ñ,Ó.ˆFÜÑ4©VÓ4Ó4Ð4r   c                ón   • [         R                  X5      =U R                  U'   nU R                  5         U$ )z#Add a user token to the collection.)r1   r5   rV   Ú_dump)r@   r   r'   Ú
user_tokens       r    Ú	add_tokenÚUserTokenCollection.add_token·   s2   € ä2;×2PÑ2PÐQYÓ2aÐaˆ×Ñ˜Ñ" ZØ�
‰
ŒØÐr   c                ó  • U R                   (       d  [        S5      eUb  XR                   ;  a  [        SU S35      eUb  U/O"[        U R                   R                  5       5      nU H  nU R                   U	 M     U R	                  5         U$ )z)Remove user token(s) from the collection.rs   rb   rc   )rV   r   rt   ÚkeysrŒ   )r@   r   Úremovedr^   s       r    ÚlogoutÚUserTokenCollection.logout½   s“   € à××Ü$Ð%rÓsÐsàÑ H×4DÑ4DÓ$DÜ$Ø7¸°zð B=ð =óð ð
 !)Ñ 4�8‘*¼$¸t×?OÑ?O×?TÑ?TÓ?VÓ:WˆÛˆCØ× Ñ  Ò%ñ ð 	�
‰
ŒØˆr   c                óZ  • U R                   R                  S5       nU R                  R                  5        HX  u  p#UR	                  SU S35        UR	                  SUR
                   S35        UR	                  SUR                   S35        MZ     SSS5        g! , (       d  f       g= f)z<Dump the user token collection as TOML to the provided path.Úwz	[tokens."z"]
z	token = "z"
zexpiration = "N)rW   ÚopenrV   r\   Úwriter'   r(   )r@   Úfr   r�   s       r    rŒ   ÚUserTokenCollection._dumpÏ   sŒ   € ð �Y‰Y�^‰^˜CÔ  AØ(,×(8Ñ(8×(>Ñ(>Ö(@Ñ$�Ø—‘˜) H :¨TÐ2Ô3Ø—‘˜) J×$4Ñ$4Ð#5°SÐ9Ô:Ø—‘˜.¨×)>Ñ)>Ð(?¸sÐCÖDñ )A÷ !× Ö ús   œA7BÂ
B*)rW   rV   r„   )rW   zPath | NonerO   ÚNone)r   ú
str | NonerO   r1   )r   rœ   rO   rP   )r   r   r'   r%   rO   r1   )r   rœ   rO   z	list[str])rO   r›   )r   r   r   r   r*   r   r_   r€   r‰   rŽ   r“   rŒ   r   r   r   r    rT   rT   g   s;   ‡ ñð &Ó%Ø6à
ƒJØ4ötö(öT5ôö÷$Er   rT   c                  ó.   • \ rS rSr% SrS\S'   S\S'   Srg)ÚNewDeviceFlowéÙ   z.Matches model of the same name in the backend.r   Údevice_codeÚfrontend_auth_urlr   Nr)   r   r   r    rž   rž   Ù   s   ‡ Ù8àÓØÖr   rž   c                ó   • [         R                  " 5       S   n[        US5      n U R                  USU0S9n[        R
                  " U5        UR                  5       nUS   US   4$ ! [        R                   a  n[        S5      UeSnAff = f)	a:  Request a device code from the Logfire API.

Args:
    session: The `requests` session to use.
    base_api_url: The base URL of the Logfire instance.

Returns:
return data['device_code'], data['frontend_auth_url']
    The device code and the frontend URL to authenticate the device at, as a `NewDeviceFlow` dict.
r   z/v1/device-auth/new/Úmachine_name)Úparamsz Failed to request a device code.Nr    r¡   )
ÚplatformÚunamer
   Úpostr   Úraise_for_statusÚrequestsÚRequestExceptionr   Újson)ÚsessionÚbase_api_urlr£   Údevice_auth_endpointÚresÚer]   s          r    Úrequest_device_coder±   à   sš   € ô —>’>Ó# AÑ&€LÜ" <Ð1GÓHÐðLØ�l‰lÐ/¸ÈÐ8VˆlÐWˆÜ×+Ò+¨CÔ0ð Ÿ(™(›*€DØ�Ñ Ð%8Ñ 9Ð9Ð9øô ×$Ñ$ó LÜ Ð!CÓDÈ!ÐKûðLús   ¦(A( Á(BÁ<BÂBc                óH  • [        USU 35      nSn  U R                  USS9n[        R                  " U5        UR	                  5       nU(       a  U$  MC  ! [
        R                   a7  nUS-  nUS:¼  a  [        S5      Ue[        R                  " S5         S	nANHS	nAff = f)
a§  Poll the Logfire API for the user token.

This function will keep polling the API until it receives a user token, not that
each request should take ~10 seconds as the API endpoint will block waiting for the user to
complete authentication.

Args:
    session: The `requests` session to use.
    device_code: The device code to poll for.
    base_api_url: The base URL of the Logfire instance.

Returns:
    The user token.
z/v1/device-auth/wait/r   é   )Útimeoutr   é   zFailed to poll for token.z%Failed to poll for token. Retrying...N)
r
   r[   r   r¨   r«   r©   rª   r   ÚwarningsÚwarn)r¬   r    r­   Úauth_endpointÚerrorsr¯   Úopt_user_tokenr°   s           r    Úpoll_for_tokenr»   ö   s­   € ô ˜LÐ,AÀ+ÀÐ*OÓP€MØ€FØ
ð	&Ø—+‘+˜m°R�+Ð8ˆCÜ×/Ò/°Ô4ð 47·8±8³:ˆNÞØ%Ð%ð ñ øô ×(Ñ(ó 	CØ�a‰KˆFØ˜‹{Ü(Ð)DÓEÈ1ÐLÜ�MŠMÐA×BÑBûð		Cús   ”&A ÁB!Á*-BÂB!)r¬   úrequests.Sessionr­   r   rO   ztuple[str, str])r¬   r¼   r    r   r­   r   rO   r%   ),Ú
__future__r   r¥   Úrer{   r¶   Údataclassesr   r   r   Úpathlibr   Útypingr   r	   Úurllib.parser
   r©   Úrich.consoler   Úrich.promptr   Útyping_extensionsr   Úlogfire.exceptionsr   Úutilsr   r   ÚhomeÚHOME_LOGFIRErY   ÚcompilerH   r   r#   r   r%   r,   r1   rT   rž   r±   r»   r   r   r    Ú<module>rË      s  ðÞ "ã Û 	Û 
Û Ý !ß 'Ý ß "Ý  ã Ý  Ý !Ý "å 1ç 5à�yŠy‹{˜ZÑ'€Ø AØ˜nÑ,€Ø %ð "$§¢Ø\ó"Ð ô
�)ô ð 6Ø ñð
 6Ø$ññ	#€Ð	ó 	ð $ô�Iô ô%˜¨%ò %ð ÷#ð #ó ð#ðL ÷nEð nEó ðnEôb�Iô ô:õ,&r   