
    0j"                         d 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mZmZmZmZmZ ddlmZ ddlmZmZmZ dd	lmZ  ej        e          Z G d
 d          ZdS )z[
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the MIT License.
    N)OptionalUnion)HTTPStatusError)	ExchangeUserTokenParamsGetUserTokenParamsInvokeResponseSignInFailureInvokeActivity!SignInTokenExchangeInvokeActivitySignInVerifyStateInvokeActivityTokenExchangeInvokeResponseTokenExchangeInvokeResponseTypeTokenExchangeRequest)EventEmitter   )
ErrorEvent	EventTypeSignInEvent)ActivityContextc                       e Zd Zdedee         ddfdZdee         de	e
ee
         f         fdZdee         deed                  fdZdee         deed                  fd	ZdS )
OauthHandlersdefault_connection_nameevent_emitterreturnNc                 "    || _         || _        d S )N)r   r   )selfr   r   s      f/home/rurouni/.hermes/hermes-agent/venv/lib/python3.11/site-packages/microsoft_teams/apps/app_oauth.py__init__zOauthHandlers.__init__   s    '>$*    ctxc                   K   |j         }|j        }|j        }	 |j        j        | j        k    r0t                              d|j        j         d| j         d           	 |j        j	        
                    t          |j        j        |j        j        |j        t          |j        j	                                       d{V }| j                            dt%          ||                     	  |             d{V  dS # t&          $ r}t)          |t*                    r|j        j        }|d	vrt                              d
|j        j         d|j        j         d|            | j                            dt5          |d|i                     t7          |pd          cY d}~ |             d{V  S t                              d|j        j         d|j        j         d|            nct                              d|j        j         d|j        j         d|            | j                            dt5          |d|i                     t7          dt;          |j        j        |j        j        t=          |          pd                    cY d}~ |             d{V  S d}~ww xY w#  |             d{V  w xY w)[
        Decorator to register a function that handles the sign-in token exchange.
        z5Sign-in token exchange invoked with connection name 'z#', but default connection name is 'z''. Token verification will likely fail.)token)connection_nameuser_id
channel_idexchange_requestNsign_inactivity_ctxtoken_response  i    z Error exchanging token for user  in conversation : erroractivityr0   context  statusz"Unable to exchange token for user r-   zunable to exchange token...)idr#   failure_detail)r6   body)r1   apinextvaluer#   r   loggerwarningusersr"   exchanger   from_r7   r%   r   r   emitr   	Exception
isinstancer   responsestatus_coder0   conversationr   r   infor   strr   r   r1   r:   next_handlerr"   er6   s           r   sign_in_token_exchangez$OauthHandlers.sign_in_token_exchange"   s      <gx2	!~-1MMM<HNLj < <7;7S< < <  (!io66+(0(F ( 1#+#6)="*."6* * *	  	 	 	 	 	 	 	 	 "''	;C`e3f3f3fggg< ,..         ;    a11 jZ3F_44Lx~?P L L,4,A,DL LHIL L   *//!V`bjUk9l9l9lmmm-V]sCCCCCCCC( ,..        ' KKHX^=N H H(0(=(@H HDEH H   
 LLHX^=N H H(0(=(@H HDEH H   &++GZaR\^fQg5h5h5hiii%4#>,(0(F'*1vv'N1N          ,..        ;: ,..        sK   AJ9 BD J6BJ1'J6(J9 =CJ1J6J9 1J66J9 9Kc                   K   |j         }|j        }	 |j        }|j                            d|j        j         d|j        j         d|j         d|j	         d	           | j
                            dt          t          d|j         d|j	                   d|i	                     	  |             d
{V  d
S #  |             d
{V  w xY w)a  
        Default handler for signin/failure invoke activities.

        Teams sends a signin/failure invoke when SSO token exchange fails
        (e.g., due to a misconfigured Entra app registration). This handler
        logs the failure details and emits an error event so developers are
        notified rather than having the failure silently swallowed.

        Known failure codes (sent by the Teams client):
            - ``installappfailed``: Failed to install the app in the user's personal
              scope (non-silent).
            - ``authrequestfailed``: The SSO auth request failed after app installation
              (non-silent).
            - ``installedappnotfound``: The bot app is not installed for the user or group chat.
            - ``invokeerror``: A generic error occurred during the SSO invoke flow.
            - ``resourcematchfailed``: The token exchange resource URI on the OAuthCard does
              not match the Application ID URI in the Entra app registration's
              "Expose an API" section.
            - ``oauthcardnotvalid``: The bot's OAuthCard could not be parsed.
            - ``tokenmissing``: AAD token acquisition failed.
            - ``userconsentrequired``: The user needs to consent (handled via OAuth card
              fallback, does not typically reach the bot).
            - ``interactionrequired``: User interaction is required (handled via OAuth card
              fallback, does not typically reach the bot).
        zSign-in failed for user r.   r/   u    — z. If the code is 'resourcematchfailed', verify that your Entra app registration has 'Expose an API' configured with the correct Application ID URI matching your OAuth connection's Token Exchange URL.r0   zSign-in failure: r1   r2   N)r1   r;   r<   r=   r>   rA   r7   rG   codemessager   rB   r   rC   )r   r   r1   rK   failures        r   sign_in_failurezOauthHandlers.sign_in_failure_   s9     8 <x	!nGJ[8>+< [ [ ( 5 8[ [<[ [&-o[ [ [   ###$\$\$\7?$\$\]]'2     ,..         ,,..        s   BB: :Cc           	        K   |j         }|j        }|j        }	 |j        j        sUt
                              d|j        j         d|j	        j         d           t          d           |             d{V  S t
                              d|j	        j         d|j        j         d	|j        j                    	 |j        j                            t          | j        |j	        j        |j        |j        j        
                     d{V }| j                            dt)          ||                     t
                              d|j	        j         d|j        j                    	  |             d{V  dS # t*          $ r}t
                              d|j	        j         d|j        j         d|            t/          |t0                    rc|j        j        }|dvrS| j                            dt7          |d|i                     t          |pd          cY d}~ |             d{V  S t          d          cY d}~ |             d{V  S d}~ww xY w#  |             d{V  w xY w)r!   z,Auth state not present for conversation id 'z' and user id 'z'. r,   r5   Nz!Verifying sign-in state for user z in conversationz with state )r#   r$   r%   rO   r'   r(   z Sign-in state verified for user r.   z'Error verifying sign-in state for user r/   r+   r0   r1   r2   r4   r-   )r1   r:   r;   r<   stater=   r>   rG   r7   rA   r   debugr?   r"   getr   r   r%   r   rB   r   rC   r0   rD   r   rE   rF   r   rJ   s           r   sign_in_verify_statez"OauthHandlers.sign_in_verify_state   sw      <gx)	!>' 2;8CXC[ ; ;$,N$5; ; ;   &S111F ,..        C LLPHN4E P P(+P P9A9MP P  
!io11&(,(D ( 1#+#6%^1	          "''	;C`e3f3f3fgggux~7Huu[c[p[suu    ,..             7hn>O 7 7,/7 7347 7   a11 DZ3F_44*//!V`bjUk9l9l9lmmm-V]sCCCCCCCC
 ,..        	 &        ,..         ,..        sQ   AI' :AI' <B2F 
I$BI$I$%I' :I	I$
I' I$$I' 'I9)__name__
__module____qualname__rI   r   r   r   r   r
   r   r   r   rM   r	   r   rR   r   rW    r   r   r   r      s        + +LQZD[ +`d + + + +;!"#DE;!	.?^0__	`;! ;! ;! ;!z1!"#>?1!	.&	'1! 1! 1! 1!f2!"#BC2!	.&	'2! 2! 2! 2! 2! 2!r   r   )__doc__loggingtypingr   r   httpxr   microsoft_teams.apir   r   r   r	   r
   r   r   r   r   microsoft_teams.commonr   eventsr   r   r   routingr   	getLoggerrX   r=   r   r[   r   r   <module>re      sD   
  " " " " " " " " ! ! ! ! ! !
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 0 / / / / / 6 6 6 6 6 6 6 6 6 6 $ $ $ $ $ $		8	$	$g! g! g! g! g! g! g! g! g! g!r   