Ë
    §Œjd0  ã                   óò   — d dl Z d dlZd dlZd dlmZmZ d dlmZ d dlmZm	Z	m
Z
mZ d dlmZmZ d dlmZ d dlmZ  ej&                  e«      Z G d„ d	«      Z G d
„ d«      Z G d„ d«      Z G d„ d«      Zd„ Zy)é    N)ÚdatetimeÚtimezone)Úsleep)ÚAnyÚ	AwaitableÚCallableÚUnion)ÚRequestTokenErrÚTokenRenewalErr)ÚIdentityProviderInterface)ÚTokenResponsec                   óö   — e Zd ZdZd„ Zedeeegdf   e	f   fd„«       Z
e
j                  deeegdf   e	f   ddfd„«       Z
edeeegdf   e	f   fd„«       Zej                  deeegdf   e	f   ddfd	„«       Zy)
ÚCredentialsListenerzz
    Listeners that will be notified on events related to credentials.
    Accepts callbacks and awaitable callbacks.
    c                 ó    — d | _         d | _        y ©N)Ú_on_nextÚ	_on_error©Úselfs    úb/var/www/html/Fitness-lenito-AI-main/venv/lib/python3.12/site-packages/redis/auth/token_manager.pyÚ__init__zCredentialsListener.__init__   s   € ØˆŒØˆ�ó    ÚreturnNc                 ó   — | j                   S r   ©r   r   s    r   Úon_nextzCredentialsListener.on_next   s   € à�}‰}Ðr   Úcallbackc                 ó   — || _         y r   r   ©r   r   s     r   r   zCredentialsListener.on_next   s	   € à ˆ�r   c                 ó   — | j                   S r   ©r   r   s    r   Úon_errorzCredentialsListener.on_error!   s   € à�~‰~Ðr   c                 ó   — || _         y r   r!   r   s     r   r"   zCredentialsListener.on_error%   s	   € à!ˆ�r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr	   r   r   r   r   ÚsetterÚ	Exceptionr"   © r   r   r   r      så   „ ñò
ð ð˜˜x¨¨¨t¨Ñ4°iÐ?Ñ@ò ó ðð ‡^�^ð!  h°¨u°d¨{Ñ&;¸YÐ&FÑ Gð !ÈDò !ó ð!ð ð˜% ¨)¨°dÐ):Ñ ;¸YÐ FÑGò ó ðð ‡_�_ð"  x°°¸TÐ0AÑ'BÀIÐ'MÑ!Nð "ÐSWò "ó ñ"r   r   c                   ó4   — e Zd Zdedefd„Zdefd„Zdefd„Zy)ÚRetryPolicyÚmax_attemptsÚdelay_in_msc                 ó    — || _         || _        y r   )r.   r/   )r   r.   r/   s      r   r   zRetryPolicy.__init__+   s   € Ø(ˆÔØ&ˆÕr   r   c                 ó   — | j                   S )zW
        Retry attempts before exception will be thrown.

        :return: int
        )r.   r   s    r   Úget_max_attemptszRetryPolicy.get_max_attempts/   s   € ð × Ñ Ð r   c                 ó   — | j                   S )zI
        Delay between retries in seconds.

        :return: int
        )r/   r   s    r   Úget_delay_in_mszRetryPolicy.get_delay_in_ms7   s   € ð ×ÑÐr   N)r$   r%   r&   ÚintÚfloatr   r2   r4   r+   r   r   r-   r-   *   s-   „ ð' Sð '°uó 'ð! #ó !ð  ô  r   r-   c                   óT   — e Zd Zdedededefd„Zdefd„Zdefd„Zdefd	„Z	defd
„Z
y)ÚTokenManagerConfigÚexpiration_refresh_ratioÚlower_refresh_bound_millisÚ%token_request_execution_timeout_in_msÚretry_policyc                 ó<   — || _         || _        || _        || _        y r   )Ú_expiration_refresh_ratioÚ_lower_refresh_bound_millisÚ&_token_request_execution_timeout_in_msÚ_retry_policy)r   r9   r:   r;   r<   s        r   r   zTokenManagerConfig.__init__A   s*   € ð *BˆÔ&Ø+EˆÔ(à1ð 	Ô3ð *ˆÕr   r   c                 ó   — | j                   S )a&  
        Represents the ratio of a token's lifetime at which a refresh should be triggered. # noqa: E501
        For example, a value of 0.75 means the token should be refreshed
        when 75% of its lifetime has elapsed (or when 25% of its lifetime remains).

        :return: float
        )r>   r   s    r   Úget_expiration_refresh_ratioz/TokenManagerConfig.get_expiration_refresh_ratioO   s   € ð ×-Ñ-Ð-r   c                 ó   — | j                   S )a¤  
        Represents the minimum time in milliseconds before token expiration
        to trigger a refresh, in milliseconds.
        This value sets a fixed lower bound for when a token refresh should occur,
        regardless of the token's total lifetime.
        If set to 0 there will be no lower bound and the refresh will be triggered
        based on the expirationRefreshRatio only.

        :return: int
        )r?   r   s    r   Úget_lower_refresh_bound_millisz1TokenManagerConfig.get_lower_refresh_bound_millisZ   s   € ð ×/Ñ/Ð/r   c                 ó   — | j                   S )z„
        Represents the maximum time in milliseconds to wait
        for a token request to complete.

        :return: int
        )r@   r   s    r   Ú)get_token_request_execution_timeout_in_msz<TokenManagerConfig.get_token_request_execution_timeout_in_msg   s   € ð ×:Ñ:Ð:r   c                 ó   — | j                   S )z_
        Represents the retry policy for token requests.

        :return: RetryPolicy
        )rA   r   s    r   Úget_retry_policyz#TokenManagerConfig.get_retry_policyp   s   € ð ×!Ñ!Ð!r   N)r$   r%   r&   r6   r5   r-   r   rC   rE   rG   rI   r+   r   r   r8   r8   @   s]   „ ð*à"'ð*ð %(ð*ð 03ð	*ð
 "ó*ð	.¨eó 	.ð0°ó 0ð;¸3ó ;ð" +ô "r   r8   c                   óú   — e Zd Zdedefd„Zd„ Z	 ddedede	g df   fd	„Z
	 	 	 dded
ededede	g df   f
d„Zd„ Zddefd„Zddefd„Zdededefd„Zdefd„Zdedefd„Zddefd„Z	 ddedej,                  fd„Zy)ÚTokenManagerÚidentity_providerÚconfigc                 óX   — || _         || _        d | _        d | _        d | _        d| _        y )Nr   )Ú_idpÚ_configÚ_next_timerÚ	_listenerÚ_init_timerÚ_retries)r   rL   rM   s      r   r   zTokenManager.__init__z   s0   € ð &ˆŒ	ØˆŒØˆÔØˆŒØˆÔØˆ�r   c                 óN   — t         j                  d«       | j                  «        y )NzToken manager are disposed)ÚloggerÚinfoÚstopr   s    r   Ú__del__zTokenManager.__del__„   s   € Ü�‰Ð0Ô1Ø�	‰	�r   ÚlistenerÚskip_initialr   Nc                 óð  ‡ ‡‡‡‡— |‰ _         	 t        j                  «       Št        j                  «       Šˆˆ ˆfd„}‰j                  |«      ‰ _        t        j                  d«       ‰j                  «        ‰ j                  S # t        $ rh t        j                  «       Št        j                  «       Šˆˆfd„}t        j                  |d¬«      }|j                  «        ‰j                  «        Y ŒÒw xY w)Nc                  óp   •— t        j                  ‰ «       ‰j                  «        ‰ j                  «        y r   )ÚasyncioÚset_event_loopÚsetÚrun_forever)ÚloopÚ
loop_readys   €€r   Ú
start_loopz&TokenManager.start.<locals>.start_loop˜   s)   ø€ ô
 ×&Ñ& tÔ,Ø—‘Ô Ø× Ñ Õ"r   T)ÚtargetÚdaemonc                  ót   •— 	 ‰j                  ‰«       ‰ j                  «        y # ‰ j                  «        w xY wr   )Ú_renew_tokenr`   )Ú	init_doner   r[   s   €€€r   Úrenew_with_callbackz/TokenManager.start.<locals>.renew_with_callbackª   s)   ø€ ð Ø×!Ñ! ,Ô/à—‘•ø�	—‘•ús   ƒ% ¥7úToken manager started)rR   r^   Úget_running_loopÚRuntimeErrorÚnew_event_loopÚ	threadingÚEventÚThreadÚstartÚwaitÚcall_soon_threadsaferS   rV   rW   rX   )	r   rZ   r[   rd   Úthreadrj   ri   rb   rc   s	   ` `   @@@r   rr   zTokenManager.startˆ   sÇ   ü€ ð
 "ˆŒð	Ü×+Ñ+Ó-ˆDô0 —O‘OÓ%ˆ	ö	 ð  ×4Ñ4Ð5HÓIˆÔÜ�‰Ð+Ô,ð 	�‰ÔØ�y‰yÐøôK ò 	ä×)Ñ)Ó+ˆDô #Ÿ™Ó*ˆJõ#ô ×%Ñ%¨ZÀÔEˆFØ�L‰LŒNð �O‰OÖð)	ús   ŽB ÂA.C5Ã4C5Úblock_for_initialÚinitial_delay_in_msc              ƒ   óF  K  — || _         t        j                  «       }t        j                  «       }t	        || j
                  ||«      }|j                  |dz  |«      | _        t        j                  d«       |r|j                  «       ƒ d {  –—†  | j                  S 7 Œ­w)Néè  rk   )rR   r^   rl   rp   Ú_async_to_sync_wrapperÚ_renew_token_asyncÚ
call_laterrS   rV   rW   rs   rX   )r   rZ   rv   rw   r[   rb   Ú
init_eventÚwrappeds           r   Ústart_asynczTokenManager.start_async¸   s�   è ø€ ð "ˆŒä×'Ñ'Ó)ˆÜ—]‘]“_ˆ
ô )Ø�$×)Ñ)¨<¸ó
ˆð  Ÿ?™?Ð+>ÀÑ+EÀwÓOˆÔÜ�‰Ð+Ô,áØ—/‘/Ó#×#Ð#à�y‰yÐð $ús   ‚BB!ÂBÂB!c                 óž   — | j                   �| j                   j                  «        | j                  �| j                  j                  «        y y r   )rS   ÚcancelrQ   r   s    r   rX   zTokenManager.stopÐ   sC   € Ø×ÑÐ'Ø×Ñ×#Ñ#Ô%Ø×ÑÐ'Ø×Ñ×#Ñ#Õ%ð (r   c                 ó¬  — 	 | j                   j                  |«      }d| _        t        |«      S # t        $ r›}| j                  | j                  j                  «       j                  «       k  r_| xj                  dz  c_        t        | j                  j                  «       j                  «       dz  «       | j                  |«      cY d }~S |‚d }~ww xY w©Né   ry   r   )rO   Úrequest_tokenr
   rT   rP   rI   r2   r   r4   Úacquire_tokenr   ©r   Úforce_refreshÚtokenÚes       r   r†   zTokenManager.acquire_tokenÖ   sª   € ð	Ø—I‘I×+Ñ+¨MÓ:ˆEð ˆŒÜ˜UÓ#Ð#øô ò 	Ø�}‰}˜tŸ|™|×<Ñ<Ó>×OÑOÓQÒQØ—’ Ñ"•Ü�d—l‘l×3Ñ3Ó5×EÑEÓGÈ$ÑNÔOØ×)Ñ)¨-Ó8Õ8à�ûð	ús!   ‚/ ¯	C¸BCÃCÃCÃCc              ƒ   óì  K  — 	 | j                   j                  |«      }d| _        t        |«      S # t        $ r·}| j                  | j                  j                  «       j                  «       k  r{| xj                  dz  c_        t        j                  | j                  j                  «       j                  «       dz  «      ƒ d {  –—†7   | j                  |«      ƒ d {  –—†7  cY d }~S |‚d }~ww xY w­wrƒ   )rO   r…   r
   rT   rP   rI   r2   r^   r   r4   Úacquire_token_asyncr   r‡   s       r   rŒ   z TokenManager.acquire_token_asyncä   sÆ   è ø€ ð
	Ø—I‘I×+Ñ+¨MÓ:ˆEð ˆŒÜ˜UÓ#Ð#øô ò 	Ø�}‰}˜tŸ|™|×<Ñ<Ó>×OÑOÓQÒQØ—’ Ñ"•Ü—m‘mØ—L‘L×1Ñ1Ó3×CÑCÓEÈÑLó÷ ñ ð "×5Ñ5°mÓD×DÐDÕDà�ûð	üsO   ‚C4„1 ŸC4±	C1ºBC,ÃCÃC,ÃC"Ã C,Ã$C1Ã%C4Ã*C,Ã,C1Ã1C4Úexpire_dateÚ
issue_datec                 óx   — | j                  |«      }| j                  ||«      }t        ||«      }|dk  rdS |dz  S )Nr   ry   )Ú_delay_for_lower_refreshÚ_delay_for_ratio_refreshÚmin)r   r�   rŽ   Údelay_for_lower_refreshÚdelay_for_ratio_refreshÚdelays         r   Ú_calculate_renewal_delayz%TokenManager._calculate_renewal_delayô   sI   € Ø"&×"?Ñ"?ÀÓ"LÐØ"&×"?Ñ"?ÀÈZÓ"XÐÜÐ+Ð-DÓEˆà˜A’IˆqÐ/ 5¨4¡<Ð/r   c                 ó¦   — || j                   j                  «       z
  t        j                  t        j
                  «      j                  «       dz  z
  S ©Nry   )rP   rE   r   Únowr   ÚutcÚ	timestamp)r   r�   s     r   r�   z%TokenManager._delay_for_lower_refreshû   sB   € àØ�l‰l×9Ñ9Ó;ñ<ä�|‰|œHŸL™LÓ)×3Ñ3Ó5¸Ñ<ñ>ð	
r   c                 óÀ   — ||z
  }||| j                   j                  «       z  z
  }||z
  t        j                  t        j
                  «      j                  «       dz  z
  S r˜   )rP   rC   r   r™   r   rš   r›   )r   r�   rŽ   Ú	token_ttlÚrefresh_befores        r   r‘   z%TokenManager._delay_for_ratio_refresh  s`   € Ø *Ñ,ˆ	Ø"Ø˜Ÿ™×AÑAÓCÑCñ
ˆð
 Øñä�|‰|œHŸL™LÓ)×3Ñ3Ó5¸Ñ<ñ>ð	
r   c                 ó4  — 	 | j                  d¬«      }| j                  |j                  «       j                  «       |j                  «       j	                  «       «      }|j                  «       j                  «       rt        d«      ‚| j                  j                  €t        j                  d«       y|s*	 | j                  j                  |j                  «       «       |dk  ryt        j                  «       }|j                  || j                  «      | _        t        j#                  d|› d�«       |S # t        $ r}t        |«      ‚d}~ww xY w# t        $ r=}| j                  j$                  €|‚| j                  j%                  |«       Y d}~yd}~ww xY w)	zq
        Task to renew token from identity provider.
        Schedules renewal tasks based on token TTL.
        T©rˆ   úRequested token is expiredNú@No registered callback for token renewal task. Renewal cancelledr   ú Next token renewal scheduled in ú seconds)r†   r–   Ú	get_tokenÚget_expires_at_msÚget_received_at_msÚ
is_expiredr   rR   r   rV   Úwarningr*   r^   rl   r|   rh   rQ   rW   r"   )r   r[   Ú	token_resr•   rŠ   rb   s         r   rh   zTokenManager._renew_token  se  € ð!	'Ø×*Ñ*¸Ð*Ó>ˆIØ×1Ñ1Ø×#Ñ#Ó%×7Ñ7Ó9Ø×#Ñ#Ó%×8Ñ8Ó:óˆEð
 ×"Ñ"Ó$×/Ñ/Ô1Ü%Ð&BÓCÐCà�~‰~×%Ñ%Ð-Ü—‘ØVôð áð-Ø—N‘N×*Ñ*¨9×+>Ñ+>Ó+@ÔAð ˜ŠzØä×+Ñ+Ó-ˆDØ#Ÿ™¨u°d×6GÑ6GÓHˆDÔÜ�K‰KÐ:¸5¸'ÀÐJÔKØÐøô !ò -Ü)¨!Ó,Ð,ûð-ûô ò 	'Ø�~‰~×&Ñ&Ð.Ø�à�N‰N×#Ñ# A×&Ñ&ûð		'úsI   ‚B0E Â3E Â6)D5 ÃE Ã%AE Ä5	EÄ>E	Å	EÅE Å	FÅ3FÆFr}   c              ƒ   ó*  K  — 	 | j                  d¬«      ƒ d{  –—† }| j                  |j                  «       j                  «       |j                  «       j	                  «       «      }|j                  «       j                  «       rt        d«      ‚| j                  j                  €*t        j                  d«       	 |r|j                  «        yy|s2	 | j                  j                  |j                  «       «      ƒ d{  –—†  |dk  r	 |r|j                  «        yyt        j                  «       }t        || j                   «      }t        j#                  d|› d�«       |j%                  ||«       |r|j                  «        yy7 �Œo7 ŒŠ# t        $ r}t        |«      ‚d}~ww xY w# t        $ rF}| j                  j&                  €|‚| j                  j'                  |«      ƒ d{  –—†7   Y d}~Œd}~ww xY w# |r|j                  «        w w xY w­w)	zx
        Async task to renew tokens from identity provider.
        Schedules renewal tasks based on token TTL.
        Tr    Nr¡   r¢   r   r£   r¤   )rŒ   r–   r¥   r¦   r§   r¨   r   rR   r   rV   r©   r`   r*   r^   rl   rz   r{   rW   r|   r"   )r   r[   r}   rª   r•   rŠ   rb   r~   s           r   r{   zTokenManager._renew_token_async7  sÖ  è ø€ ð$	!Ø"×6Ñ6ÀTÐ6ÓJ×JˆIØ×1Ñ1Ø×#Ñ#Ó%×7Ñ7Ó9Ø×#Ñ#Ó%×8Ñ8Ó:óˆEð
 ×"Ñ"Ó$×/Ñ/Ô1Ü%Ð&BÓCÐCà�~‰~×%Ñ%Ð-Ü—‘ØVôð ñ* Ø—‘Õ ð ñ'  ð-ØŸ.™.×0Ñ0°×1DÑ1DÓ1FÓG×GÐGð ˜ŠzØñ Ø—‘Õ ð ô ×+Ñ+Ó-ˆDÜ,¨T°4×3JÑ3JÓKˆGÜ�K‰KÐ:¸5¸'ÀÐJÔKØ�O‰O˜E 7Ô+ñ Ø—‘Õ ð ðE Kùð" HùÜ ò -Ü)¨!Ó,Ð,ûð-ûô ò 	-Ø�~‰~×&Ñ&Ð.Ø�à—.‘.×)Ñ)¨!Ó,×,Ö,ûð		-ûñ Ø—‘Õ ð üs¦   ‚H„F) ™FšB"F) Â=HÃF) Ã,F Ä FÄF ÄF) ÄHÄAF) Å4HÆF) ÆF Æ	F&ÆF!Æ!F&Æ&F) Æ)	G8Æ26G3Ç(G+Ç)G3Ç.G; Ç3G8Ç8G; Ç;HÈH)F)Fr   F)FN)r$   r%   r&   r   r8   r   rY   r   Úboolr   rr   r6   r   rX   r   r†   rŒ   r–   r�   r‘   rh   r^   rp   r{   r+   r   r   rK   rK   y   s  „ ðØ!:ðØDVóòð #ñ.à%ð.ð ð.ð 
�"�d�(Ñ	ó	.ðf #(Ø%&Ø"ñà%ðð  ðð #ð	ð
 ðð 
�"�d�(Ñ	óò0&ñ$°Mó $ñ$Àó $ð 0°Eð 0Àuð 0ÐQVó 0ð
°Eó 
ð

°Eð 

Àuó 

ñ''¨ó ''ðT GKñ,!Ø ð,!Ø6=·m±mô,!r   rK   c                 ó   ‡ ‡‡‡— ˆˆˆˆ fd„}|S )aŸ  
    Wraps an asynchronous function so it can be used with loop.call_later.

    :param loop: The event loop in which the coroutine will be executed.
    :param coro_func: The coroutine function to wrap.
    :param args: Positional arguments to pass to the coroutine function.
    :param kwargs: Keyword arguments to pass to the coroutine function.
    :return: A regular function suitable for loop.call_later.
    c                  ó@   •— t        j                   ‰‰ i ‰¤Ž‰¬«       y )N)rb   )r^   Úensure_future)ÚargsÚ	coro_funcÚkwargsrb   s   €€€€r   r~   z'_async_to_sync_wrapper.<locals>.wrappedq  s   ø€ ä×Ñ™i¨Ð8°Ñ8¸tÖDr   r+   )rb   r±   r°   r²   r~   s   ```` r   rz   rz   f  s   û€ ÷Eð €Nr   )r^   Úloggingro   r   r   Útimer   Útypingr   r   r   r	   Úredis.auth.errr
   r   Úredis.auth.idpr   Úredis.auth.tokenr   Ú	getLoggerr$   rV   r   r-   r8   rK   rz   r+   r   r   Ú<module>rº      sf   ðÛ Û Û ß 'Ý ß 2Ó 2ç ;Ý 4Ý *à	ˆ×	Ñ	˜8Ó	$€÷"ñ "÷6 ñ  ÷,6"ñ 6"÷rj!ñ j!óZr   