
    'j                     d    d dl m Z  d dlmZ d dlmZmZ d dlmZ d dlm	Z	m
Z
  G d d          ZdS )	    )time)Optional)SlackApiErrorSlackTokenRotationError)	WebClient)InstallationBotc                       e Zd ZU eed<   eed<   eed<   dddededee         fdZddd	ed
e	dee         fdZ
ddded
e	dee         fdZddd	ed
e	dee         fdZdS )TokenRotatorclient	client_idclient_secretN)r   c                T    ||nt          d           | _        || _        || _        d S )N)token)r   r   r   r   )selfr   r   r   s       g/usr/local/lib/hermes-agent/venv/lib/python3.11/site-packages/slack_sdk/oauth/token_rotation/rotator.py__init__zTokenRotator.__init__   s2     & 2ff	8M8M8M"*    x   )minutes_before_expirationinstallationr   returnc                   |                      |                                |          }|                     ||          }|D|t          di |                                }|j        |_        |j        |_        |j        |_        |S )a<  Performs token rotation if the underlying tokens (bot / user) are expired / expiring.

        Args:
            installation: the current installation data
            minutes_before_expiration: the minutes before the token expiration

        Returns:
            None if no rotation is necessary for now.
        )botr   )r   r   N )perform_bot_token_rotationto_botperform_user_token_rotationr   to_dict_for_copying	bot_tokenbot_refresh_tokenbot_token_expires_at)r   r   r   rotated_botrotated_installations        r   perform_token_rotationz#TokenRotator.perform_token_rotation   s    & &*%D%D##%%&? &E &
 &
 8<7W7W%&? 8X 8
 8

 "#+'3'Y'Yl6V6V6X6X'Y'Y$-8-B *5@5R 28C8X 5##r   r   c                    |j         dS |j         t                      |dz  z   k    rdS 	 | j                            | j        | j        d|j                  }|                    d          dk    rdS t          d	i |	                                }|d         |_
        |                    d          |_        t          t                                t          |d                   z   |_         |S # t          $ r}t          |          d}~ww xY w)
a0  Performs bot token rotation if the underlying bot token is expired / expiring.

        Args:
            bot: the current bot installation data
            minutes_before_expiration: the minutes before the token expiration

        Returns:
            None if no rotation is necessary for now.
        N<   refresh_tokenr   r   
grant_typer(   
token_typer   access_token
expires_inr   )r"   r   r   oauth_v2_accessr   r   r!   getr	   r   r    intr   r   )r   r   r   refresh_responserefreshed_botes         r   r   z'TokenRotator.perform_bot_token_rotation:   s%    #+4#dff/H2/M&MMM4	-#{::."0*!3	  ;      ##L11U::t<<#"9"9";";<<M&6~&FM#.>.B.B?.S.SM+14TVVsCST`Ca?b?b1bM.   	- 	- 	-)!,,,	-s   AC/ 2A<C/ /
D9DDc                :   |j         dS |j         t                      |dz  z   k    rdS 	 | j                            | j        | j        d|j                  }|                    d          dk    rdS t          d	i |	                                }|                    d          |_
        |                    d          |_        t          t                                t          |d                   z   |_         |S # t          $ r}t          |          d}~ww xY w)
a7  Performs user token rotation if the underlying user token is expired / expiring.

        Args:
            installation: the current installation data
            minutes_before_expiration: the minutes before the token expiration

        Returns:
            None if no rotation is necessary for now.
        Nr'   r(   r)   r+   userr,   r-   r   )user_token_expires_atr   r   r.   r   r   user_refresh_tokenr/   r   r   
user_tokenr0   r   r   )r   r   r   r1   refreshed_installationr3   s         r   r   z(TokenRotator.perform_user_token_rotationa   s3    -54-9RUW9W0WWW4	-#{::."0**=	  ;      ##L11V;;t%1%W%WL4T4T4V4V%W%W"0@0D0D^0T0T"-8H8L8L_8]8]"5;>tvv;;M]^jMkIlIl;l"8)) 	- 	- 	-)!,,,	-s   AC< 2B	C< <
DDD)__name__
__module____qualname__r   __annotations__strr   r   r   r0   r%   r	   r   r   r   r   r   r   r   	   sE        NNN\` + + +S + +hyFY + + + + *-	%$ %$ %$ #%$ $'	%$
 
,	%$ %$ %$ %$V *-	%- %- %- %- $'	%-
 
#%- %- %- %-V *-	&- &- &- #&- $'	&-
 
,	&- &- &- &- &- &-r   r   N)r   typingr   slack_sdk.errorsr   r   slack_sdk.webr   "slack_sdk.oauth.installation_storer   r	   r   r   r   r   <module>rC      s                C C C C C C C C # # # # # # @ @ @ @ @ @ @ @~- ~- ~- ~- ~- ~- ~- ~- ~- ~-r   