
    ph.                        S SK r S SKrS SKrS SKJrJr  S SKJr  S SKJrJ	r	J
r
Jr  S SKJrJr  S SKJr  S SKJr  \R&                  " \5      r " S S	5      r " S
 S5      r " S S5      r " S S5      rS rS\ R6                  4S jrg)    N)datetimetimezone)sleep)Any	AwaitableCallableUnion)RequestTokenErrTokenRenewalErr)IdentityProviderInterface)TokenResponsec                      \ rS rSrSrS r\S\\\	/S4   \
4   4S j5       r\R                  S\\\	/S4   \
4   SS4S j5       r\S\\\/S4   \
4   4S	 j5       r\R                  S\\\/S4   \
4   SS4S
 j5       rSrg)CredentialsListener   zn
Listeners that will be notified on events related to credentials.
Accepts callbacks and awaitable callbacks.
c                      S U l         S U l        g N)_on_next	_on_errorselfs    R/home/james-whalen/.local/lib/python3.13/site-packages/redis/auth/token_manager.py__init__CredentialsListener.__init__   s        returnNc                     U R                   $ r   r   r   s    r   on_nextCredentialsListener.on_next   s    }}r   callbackc                     Xl         g r   r   r   r    s     r   r   r      s     r   c                     U R                   $ r   r   r   s    r   on_errorCredentialsListener.on_error!   s    ~~r   c                     Xl         g r   r$   r"   s     r   r%   r&   %   s    !r   )r   r   )__name__
__module____qualname____firstlineno____doc__r   propertyr	   r   r   r   r   setter	Exceptionr%   __static_attributes__ r   r   r   r      s    
 xt4i?@   ^^!hud{&;Y&F G !D ! ! %)d): ;Y FG   __"xT0A'BI'M!N "SW " "r   r   c                   B    \ rS rSrS\S\4S jrS\4S jrS\4S jrSr	g	)
RetryPolicy*   max_attemptsdelay_in_msc                     Xl         X l        g r   )r5   r6   )r   r5   r6   s      r   r   RetryPolicy.__init__+   s    (&r   r   c                     U R                   $ )z?
Retry attempts before exception will be thrown.

:return: int
)r5   r   s    r   get_max_attemptsRetryPolicy.get_max_attempts/   s        r   c                     U R                   $ )z1
Delay between retries in seconds.

:return: int
)r6   r   s    r   get_delay_in_msRetryPolicy.get_delay_in_ms7   s     r   )r6   r5   N)
r(   r)   r*   r+   intfloatr   r:   r=   r0   r1   r   r   r3   r3   *   s-    'S 'u '!# !   r   r3   c                   f    \ rS rSrS\S\S\S\4S jrS\4S jrS\4S	 jr	S\4S
 jr
S\4S jrSrg)TokenManagerConfig@   expiration_refresh_ratiolower_refresh_bound_millis%token_request_execution_timeout_in_msretry_policyc                 6    Xl         X l        UU l        X@l        g r   )_expiration_refresh_ratio_lower_refresh_bound_millis&_token_request_execution_timeout_in_ms_retry_policy)r   rD   rE   rF   rG   s        r   r   TokenManagerConfig.__init__A   s$     *B&+E(1 	3 *r   r   c                     U R                   $ )z
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
)rI   r   s    r   get_expiration_refresh_ratio/TokenManagerConfig.get_expiration_refresh_ratioO   s     ---r   c                     U R                   $ )ad  
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
)rJ   r   s    r   get_lower_refresh_bound_millis1TokenManagerConfig.get_lower_refresh_bound_millisZ   s     ///r   c                     U R                   $ )zd
Represents the maximum time in milliseconds to wait
for a token request to complete.

:return: int
)rK   r   s    r   )get_token_request_execution_timeout_in_ms<TokenManagerConfig.get_token_request_execution_timeout_in_msg   s     :::r   c                     U R                   $ )zG
Represents the retry policy for token requests.

:return: RetryPolicy
)rL   r   s    r   get_retry_policy#TokenManagerConfig.get_retry_policyp   s     !!!r   )rI   rJ   rL   rK   N)r(   r)   r*   r+   r@   r?   r3   r   rO   rR   rU   rX   r0   r1   r   r   rB   rB   @   s]    *"'* %(* 03	*
 "*	.e 	.0 0;3 ;"+ "r   rB   c                   <   \ rS rSrS\S\4S jrS r SS\S\	S\
/ S	4   4S
 jjr   SS\S\	S\S\	S\
/ S	4   4
S jjrS rSS\4S jjrSS\4S jjrS\S\S\4S jrS\4S jrS\S\4S jr SS\	S\R,                  4S jjr SS\	S\R,                  4S jjrSrg	)TokenManagery   identity_providerconfigc                 T    Xl         X l        S U l        S U l        S U l        SU l        g )Nr   )_idp_config_next_timer	_listener_init_timer_retries)r   r]   r^   s      r   r   TokenManager.__init__z   s,     &	r   c                 N    [         R                  S5        U R                  5         g )NzToken manager are disposed)loggerinfostopr   s    r   __del__TokenManager.__del__   s    01		r   listenerskip_initialr   Nc                    Xl          [        R                  " 5       n[        R                  " 5       nUR                  SU R                  X%5      U l        [        R                  S5        [        R                  " UR!                  5       U5      R#                  5         U R$                  $ ! [         aC    [        R                  " 5       n[
        R                  " [        U4SS9nUR                  5          Nf = f)NT)targetargsdaemonr   Token manager started)rc   asyncioget_running_loopRuntimeErrornew_event_loop	threadingThread_start_event_loop_in_threadstartEvent
call_later_renew_tokenrd   rh   ri   run_coroutine_threadsafewaitresultrj   )r   rm   rn   loopthread
init_events         r   r{   TokenManager.start   s    
 "	++-D ]]_
??t  ,
 	+, 	(():DAHHJyy#  	))+D%%2$F LLN	s   B( (A
C54C5block_for_initialinitial_delay_in_msc                 N  #    Xl         [        R                  " 5       n[        R                  " 5       n[	        XPR
                  XF5      nUR                  US-  U5      U l        [        R                  S5        U(       a  UR                  5       I S h  vN   U R                  $  N7f)N  rs   )rc   rt   ru   r|   _async_to_sync_wrapper_renew_token_asyncr}   rd   rh   ri   r   rj   )r   rm   r   r   rn   r   r   wrappeds           r   start_asyncTokenManager.start_async   s      "'')]]_
 )))<
  ??+>+EwO+,//###yy $s   BB%B#B%c                     U R                   b  U R                   R                  5         U R                  b  U R                  R                  5         g g r   )rd   cancelrb   r   s    r   rj   TokenManager.stop   sC    '##%'##% (r   c                     U R                   R                  U5      nSU l        [        U5      $ ! [         a  nU R                  U R                  R                  5       R                  5       :  a_  U =R                  S-  sl        [        U R                  R                  5       R                  5       S-  5        U R                  U5      s S nA$ UeS nAff = fN   r   r   )r`   request_tokenr
   re   ra   rX   r:   r   r=   acquire_tokenr   r   force_refreshtokenes       r   r   TokenManager.acquire_token   s    	II++M:E U##  	}}t||<<>OOQQ"dll335EEG$NO))-88	s!   / 
CBCCCCc                   #     U R                   R                  U5      nSU l        [        U5      $ ! [         a  nU R                  U R                  R                  5       R                  5       :  a|  U =R                  S-  sl        [        R                  " U R                  R                  5       R                  5       S-  5      I S h  vN    U R                  U5      I S h  vN  s S nA$ UeS nAff = f7fr   )r`   r   r
   re   ra   rX   r:   rt   r   r=   acquire_token_asyncr   r   s       r   r    TokenManager.acquire_token_async   s     
	II++M:E U##  	}}t||<<>OOQQ"mmLL113CCEL   "55mDDDD	sO   C71 C7
C4BC/C	C/"C%#C/'C4(C7-C//C44C7expire_date
issue_datec                 v    U R                  U5      nU R                  X5      n[        XC5      nUS:  a  S$ US-  $ )Nr   r   )_delay_for_lower_refresh_delay_for_ratio_refreshmin)r   r   r   delay_for_lower_refreshdelay_for_ratio_refreshdelays         r   _calculate_renewal_delay%TokenManager._calculate_renewal_delay   sD    "&"?"?"L"&"?"?"X+EAIq/54</r   c                     UU R                   R                  5       -
  [        R                  " [        R
                  5      R                  5       S-  -
  $ Nr   )ra   rR   r   nowr   utc	timestamp)r   r   s     r   r   %TokenManager._delay_for_lower_refresh   sB    ll99;<||HLL)335<>	
r   c                     X-
  nUX0R                   R                  5       -  -
  nUU-
  [        R                  " [        R
                  5      R                  5       S-  -
  $ r   )ra   rO   r   r   r   r   r   )r   r   r   	token_ttlrefresh_befores        r   r   %TokenManager._delay_for_ratio_refresh   s\    ,	"AACC

 ||HLL)335<>	
r   r   c                 N    U R                  SS9nU R                  UR                  5       R                  5       UR                  5       R	                  5       5      nUR                  5       R                  5       (       a  [        S5      eU R                  R                  c/  [        R                  S5         U(       a  UR                  5         ggU(       d*   U R                  R                  UR                  5       5        US::  a   U(       a  UR                  5         gg[        R                  " 5       nUR                  X@R                   5      U l        [        R%                  SU S35        UU(       a  UR                  5         $ $ ! [         a  n[        U5      eSnAff = f! [         a>  nU R                  R&                  c  UeU R                  R'                  U5         SnAOSnAff = f U(       a  UR                  5         gg! U(       a  UR                  5         f f = f)	zY
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   rc   r   rh   warningsetr/   rt   ru   r}   r~   rb   ri   r%   )r   rn   r   	token_resr   r   r   s          r   r~   TokenManager._renew_token   s   $	!***>I11##%779##%88:E
 ""$//11%&BCC~~%%-V *   '  -NN**9+>+>+@A z    ++-D#u6G6GHDKK:5'JK   ! ! -)!,,-  	'~~&&.NN##A&&		' '  z  s[   B3F% F% )F  F%  AF% 
F"FF""F% %
G-/4G(#H
 (G--H
 
H$c                 h  #     U R                  SS9I Sh  vN nU R                  UR                  5       R                  5       UR                  5       R	                  5       5      nUR                  5       R                  5       (       a  [        S5      eU R                  R                  c/  [        R                  S5         U(       a  UR                  5         ggU(       d2   U R                  R                  UR                  5       5      I Sh  vN   US::  a   U(       a  UR                  5         gg[        R                  " 5       n[        X`R                   5      n[        R#                  SU S35        UR%                  XG5        U(       a  UR                  5         gg GN N! [         a  n[        U5      eSnAff = f! [         aG  nU R                  R&                  c  UeU R                  R'                  U5      I Sh  vN     SnANSnAff = f! U(       a  UR                  5         f f = f7f)	z`
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   rc   r   rh   r   r   r/   rt   ru   r   r   ri   r}   r%   )r   rn   r   r   r   r   r   r   s           r   r   TokenManager._renew_token_async(  s    $	!"66T6JJI11##%779##%88:E
 ""$//11%&BCC~~%%-V *   '  -..001D1D1FGGG z    ++-D,T3J3JKGKK:5'JKOOE+   E K" H  -)!,,-  	-~~&&...))!,,,		-   s   H2G FB(G  H2G !,F$ F"F$ G H22AG H2G "F$ $
F>.F99F>>G 
H7HHHH HH H//H2)ra   r`   rd   rc   rb   re   )F)Fr   F)FN)r(   r)   r*   r+   r   rB   r   rk   r   boolr   r{   r@   r   rj   r   r   r   r   r   r   rt   r|   r~   r   r0   r1   r   r   r[   r[   y   s1   !:DV #%  
"d(		> #(%&"%   #	
  
"d(	0&$M $$ $ 0E 0u 0QV 0
E 


E 

u 

 GK,! ,!6=mm,!^ GK,! ,!6=mm,! ,!r   r[   c                     ^ ^^^ UUUU 4S jnU$ )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                  <   > [         R                  " T" T 0 TD6TS9  g )N)r   )rt   ensure_future)rq   	coro_funckwargsr   s   r   r   '_async_to_sync_wrapper.<locals>.wrappedb  s    i88tDr   r1   )r   r   rq   r   r   s   ```` r   r   r   W  s    E E Nr   
event_loopc                 P    [         R                  " U 5        U R                  5         g)zv
Starts event loop in a thread.
Used to be able to schedule tasks using loop.call_later.

:param event_loop:
:return:
N)rt   set_event_looprun_forever)r   s    r   rz   rz   i  s     :&r   )rt   loggingrx   r   r   timer   typingr   r   r   r	   redis.auth.errr
   r   redis.auth.idpr   redis.auth.tokenr   	getLoggerr(   rh   r   r3   rB   r[   r   AbstractEventLooprz   r1   r   r   <module>r      sv       '  2 2 ; 4 *			8	$" "6   ,6" 6"r[! [!|$	G,E,E 	r   