
    'jJ                        d dl mZ d dlmZmZmZmZmZmZ d dl	m
Z
mZ d dlmZmZ d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ  G d d          Z G d de          Z  G d de          Z!dS )    )Logger)OptionalCallable	AwaitableDictAnySequence)SlackApiErrorSlackTokenRotationError)BotInstallation)AsyncInstallationStore)AsyncTokenRotator)AsyncWebClient)AsyncAuthorizeArgs)AuthorizeResult)AsyncBoltContext)	BoltError)get_arg_names_of_callablec                       e Zd ZdZd Zdddddedee         dee         dee         d	ee         d
ee         dee         dee         fdZ	dS )AsyncAuthorizezeThis provides authorize function that returns AuthorizeResult
    for an incoming request from Slack.c                     d S N )selfs    i/usr/local/lib/hermes-agent/venv/lib/python3.11/site-packages/slack_bolt/authorization/async_authorize.py__init__zAsyncAuthorize.__init__   s        Nactor_enterprise_idactor_team_idactor_user_idcontextenterprise_idteam_iduser_idr    r!   r"   returnc                "   K   t                      r   )NotImplementedError)r   r#   r$   r%   r&   r    r!   r"   s           r   __call__zAsyncAuthorize.__call__   s       "###r   )
__name__
__module____qualname____doc__r   r   r   strr   r*   r   r   r   r   r      s        + +   .2'+'+$ $ $ "$  }	$
 #$ #$ &c]$  }$  }$ 
/	"$ $ $ $ $ $r   r   c                       e Zd ZdZdededee         f         fdZddddde	d	e
e         d
e
e         de
e         de
e         de
e         de
e         de
e         fdZdS )AsyncCallableAuthorizezrWhen you pass the authorize argument in AsyncApp constructor,
    This authorize implementation will be used.
    loggerfunc.c                J    || _         || _        t          |          | _        d S r   )r2   r3   r   	arg_names)r   r2   r3   s      r   r   zAsyncCallableAuthorize.__init__.   s#    	2488r   Nr   r#   r$   r%   r&   r    r!   r"   r'   c                   K   	 t          ||||          |j        |j        |||||||d
}|                                D ]\  }	}
|	|vr|
||	<    fd|                                D             }|                                } j        D ](}||vr" j                            | d           d ||<   )  j        d
i | d {V }||S t          |t                    r|S t          dt          |           d          # t          $ r4} j                            d| d| d	|j         d           Y d }~d S d }~ww xY w)N)r#   r$   r%   r&   )
argsr2   clientr#   r$   r%   r&   r    r!   r"   c                 .    i | ]\  }}|j         v ||S r   )r5   ).0kvr   s      r   
<dictcomp>z3AsyncCallableAuthorize.__call__.<locals>.<dictcomp>U   s.    %i%i%itq!UVZ^ZhUhUhaUhUhUhr   z is not a valid argumentz9Unexpected returned value from authorize function (type: )(The stored bot token for enterprise_id: 
 team_id:   is no longer valid. (response: r   )r   r2   r8   itemskeysr5   warningr3   
isinstancer   
ValueErrortyper
   debugresponse)r   r#   r$   r%   r&   r    r!   r"   all_available_argsr;   r<   kwargsfound_arg_namesnameauth_resulterrs   `               r   r*   zAsyncCallableAuthorize.__call__3   s     *	*#"/##	   ".!."!.""':!.!." ""   . .1...,-&q)%i%i%i%i7I7O7O7Q7Q%i%i%iF$kkmmO ( (..K''4(I(I(IJJJ#'F4L;D49;N;Nv;N;N5N5N5N5N5N5NK"""+77 s"" !q]abm]n]n!q!q!qrrr 	 	 	KB= B BT[ B B25,B B B   44444	s$   CD D " D 
E )D;;E )r+   r,   r-   r.   r   r   r   r   r   r   r   r/   r*   r   r   r   r1   r1   )   s         9& 9i>X9X0Y 9 9 9 9 .2'+'+6 6 6 "6  }	6
 #6 #6 &c]6  }6  }6 
/	"6 6 6 6 6 6r   r1   c                      e Zd ZU dZeeef         ed<   eed<   eed<   e	e         ed<   e	e         ed<   e	e
         ed<   dZeed	<   d
d
d
ddd
dddedede	e         de	e         de	e         dedede	e         defdZd
d
d
ddede	e         de	e         de	e         de	e         de	e         de	e         de	e         fdZde	e         de	e         fd Zd!e	e         de	e         fd"Zd
S )#AsyncInstallationStoreAuthorizea  If you use the OAuth flow settings, this authorize implementation will be used.
    As long as your own InstallationStore (or the built-in ones) works as you expect,
    you can expect that the authorize layer should work for you without any customization.
    authorize_result_cachebot_onlyuser_token_resolutionfind_installation_availablefind_bot_availabletoken_rotatorzSAsyncInstallationStore with client_id/client_secret are required for token rotation_config_error_messageNFauthed_user)	client_idclient_secret!token_rotation_expiration_minutesrS   cache_enabledr8   rT   r2   installation_storerZ   r[   r\   r]   r8   c       	             || _         || _        || _        |	| _        || _        i | _        d | _        d | _        ||t          |||          | _	        nd | _	        |pd| _
        d S )N)rZ   r[   r8   x   )r2   r^   rS   rT   r]   rR   rU   rV   r   rW   r\   )
r   r2   r^   rZ   r[   r\   rS   r]   r8   rT   s
             r   r   z(AsyncInstallationStoreAuthorize.__init__{   s      "4 %:"*&(#+/("& ]%>!2#+" " "D "&D1R1YVY...r   r   r#   r$   r%   r&   r    r!   r"   r'   c          
      J	  K   | j         t          | j        d          | _         | j        t          | j        d          | _        d }d }	d }
d }d }d }| j        s| j         r 	 | j                            |||j                   d {V }||j        }|j        }	|j	        }
|j
        }|j        |k    r"d }	d }d |_        d |_        d |_        d |_
        | j        dk    r)||$| j                            |||d            d {V }n)| j                            ||||j                   d {V }||j        }	|j
        }|j        .|j        |j        k    r|j        |j        k    r|j        }|j	        }
|                     |           d {V }|C|j        }	|j
        }|j        .|j        |j        k    r|j        |j        k    r|j        }|j	        }
|                     |           d {V }||j        }|j	        }
||j        }	|j
        }nN# t$          $ r(}| j                            d|            Y d }~d S d }~wt*          $ r}d| _         Y d }~nd }~ww xY w| j        s| j         r| j        du r6|3|	0	 | j                            |||j                   d {V }||j        }|j	        }
|j        r| j        t3          | j                  | j                            || j        	           d {V }|.| j                            |           d {V  |j        }|j	        }
n}# t$          $ r(}| j                            d|            Y d }~d S d }~wt*          $ r}d| _        Y d }~n7d }~wt<          $ r'}| j                            d
|            Y d }~nd }~ww xY w|p|	}||                      ||           d S | j!        r|| j"        v r| j"        |         S 	 |j#        $                    |           d {V }d }|	'||	k    r!|j#        $                    |	           d {V }tK          j&        ||||	|
|          }| j!        r
|| j"        |<   |S # tN          $ r4}| j        (                    d| d| d|j)         d           Y d }~d S d }~ww xY w)Nasync_find_installationasync_find_bot)r$   r%   is_enterprise_installactor)r$   r%   r&   rd   zFailed to rotate tokens due to FT)botminutes_before_expirationz Failed to call find_bot method: )token)auth_test_responseuser_auth_test_response	bot_token
user_token
bot_scopesuser_scopesr?   r@   rA   r>   )*rU   hasattrr^   rV   rS   rb   rd   rk   rl   rm   rn   r&   user_refresh_tokenuser_token_expires_atrT   r$   r%   $_rotate_and_save_tokens_if_necessaryr   r2   errorr)   rc   bot_refresh_tokenrW   r   rX   perform_bot_token_rotationr\   async_save_bot	Exceptioninfo_debug_log_for_not_foundr]   rR   r8   	auth_testr   from_auth_test_responser
   rH   rI   )r   r#   r$   r%   r&   r    r!   r"   rk   rl   rm   rn   latest_bot_installationthis_user_installation	refreshedrotation_error_rf   refreshed_boterh   auth_test_api_responserj   authorize_resultrO   s                            r   r*   z(AsyncInstallationStoreAuthorize.__call__   s)      +3/6t7NPi/j/jD,"*&-d.EGW&X&XD##'	$(
.2
/3:>9=} c	9!A c	9`9 150G0_0_"/#*1*G 1` 1 1 + + + + + +' +6 7 AI!8!CJ!8!CJ"9"EK.6'AA%)
&*=A/:EI/BHL/E>B/;  5@@2>-B[?C?V?n?n2E,9,9:>	 @o @" @" :" :" :" :" :" :" 6 <@;R;j;j.;(/(/6=6S	 <k < < 6 6 6 6 6 62 2=)?)JJ*@*LK 7 A I %<$IMcMq$q$q$;$CG]Ge$e$e -C,L	-C-N
 /3.W.WXn.o.o(o(o(o(o(o(oI(4-6-A
.7.C$;$E$M )@(MQgQu(u(u(?(GKaKi(i(i 1:0CI1:1EJ '+&O&OPg&h&h h h h h h hI ,$-$7	%.%9
19 *3)=J*3*?K*    !!"TN"T"TUUUttttt& 9 9 9380000009
 M&	I 3	&	I '4//I4E*J\I+/+B+Q+Q"/#*1*G ,R , , & & & & & &
 ? #I!$J,8-5"+D,F"G"GG.2.@.[.[ #6:6\ /\ / / ) ) ) ) ) ) )4"&"9"H"H"W"WWWWWWWW(5(?I)6)AJ*    !!"TN"T"TUUUttttt& 0 0 0*/'''''' I I I  !GA!G!GHHHHHHHHI  )6J=))-AAA4  	6%4+F"F"F.u55	+2>+C+C%+C+P+P%P%P%P%P%P%P"&*#%%:*=*=070H0Hz0H0Z0Z*Z*Z*Z*Z*Z*Z'.F#9(?#%%'      ! F5E+E2## 	 	 	KB= B BT[ B B25,B B B   44444	so   "F0H 
II  III B1L2 2
N,<MN,,M88N,N''N,+A8Q$ $
R".)RR"c                 F    | j                             d| d|            d S )Nz.No installation data found for enterprise_id: r@   )r2   rH   )r   r$   r%   s      r   ry   z8AsyncInstallationStoreAuthorize._debug_log_for_not_foundf  s0    pmppgnppqqqqqr   installationc                    K   ||j         	|j        d S | j        t          | j                  | j                            || j                   d {V }| | j                            |           d {V  |S )N)r   rg   )	rp   rt   rW   r   rX   perform_token_rotationr\   r^   
async_save)r   r   r~   s      r   rr   zDAsyncInstallationStoreAuthorize._rotate_and_save_tokens_if_necessaryi  s      L$C$KP\PnPv4%D6777262D2[2[%&*&L 3\ 3
 3
 -
 -
 -
 -
 -
 -
	  )44Y?????????r   )r+   r,   r-   r.   r   r/   r   __annotations__boolr   r   rX   r   r   intr   r   r   r*   ry   r   rr   r   r   r   rQ   rQ   l   sG         
 !o!56666NNN!)$/// &&&-....!v3vvv $('+;? #+/%2 Z  Z  Z  Z 3	 Z
 C= Z  } Z ,4C= Z  Z  Z ( Z  # Z  Z  Z  ZT .2'+'+E E E "E  }	E
 #E #E &c]E  }E  }E 
/	"E E E ERrhsm rhWZm r r r rxP\G] bjkwbx      r   rQ   N)"loggingr   typingr   r   r   r   r   r	   slack_sdk.errorsr
   r   "slack_sdk.oauth.installation_storer   r   ;slack_sdk.oauth.installation_store.async_installation_storer   ,slack_sdk.oauth.token_rotation.async_rotatorr   slack_sdk.web.async_clientr   -slack_bolt.authorization.async_authorize_argsr   slack_bolt.authorizationr    slack_bolt.context.async_contextr   slack_bolt.errorr   slack_bolt.util.utilsr   r   r1   rQ   r   r   r   <module>r      s         E E E E E E E E E E E E E E E E C C C C C C C C @ @ @ @ @ @ @ @      K J J J J J 5 5 5 5 5 5 L L L L L L 4 4 4 4 4 4 = = = = = = & & & & & & ; ; ; ; ; ;$ $ $ $ $ $ $ $,@ @ @ @ @^ @ @ @FM M M M Mn M M M M Mr   