
    ~h                     ~    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e      Z	 G d	 d
      Z
 G d d      Zy)    )default_json_headers   )InvalidRequestError)Hookable)hooked)OAuth2Requestc                   f     e Zd ZdgZdZeZdef fdZe	d        Z
	 	 	 	 	 d
dZd Zd Zd	 Z xZS )	BaseGrantclient_secret_basicNrequestc                 Z    t         |           d | _        d | _        || _        || _        y N)super__init__promptredirect_urir   server)selfr   r   	__class__s      ^/opt/mcp/mcp-sentiment/venv/lib/python3.12/site-packages/authlib/oauth2/rfc6749/grants/base.pyr   zBaseGrant.__init__   s+         c                 .    | j                   j                  S r   )r   clientr   s    r   r   zBaseGrant.client   s    ||"""r   c                     || j                   }| j                  j                  | j                  j                  |||||      S )N)r   
grant_typeuserscope
expires_ininclude_refresh_token)
GRANT_TYPEr   generate_tokenr   r   )r   r   r   r   r   r    s         r   r"   zBaseGrant.generate_token!   sK     J{{))<<&&!!"7 * 
 	
r   c                     | j                   j                  | j                  | j                        }| j                   j	                  d||        |S )a  Authenticate client with the given methods for token endpoint.

        For example, the client makes the following HTTP request using TLS:

        .. code-block:: http

            POST /token HTTP/1.1
            Host: server.example.com
            Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW
            Content-Type: application/x-www-form-urlencoded

            grant_type=authorization_code&code=SplxlOBeZQQYbYS6WxSbIA
            &redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb

        Default available methods are: "none", "client_secret_basic" and
        "client_secret_post".

        :return: client
        after_authenticate_client)r   grant)r   authenticate_clientr   TOKEN_ENDPOINT_AUTH_METHODSsend_signal)r   r   s     r   "authenticate_token_endpoint_clientz,BaseGrant.authenticate_token_endpoint_client4   sI    ( 00LL$::
 	 ;FRVWr   c                 N    | j                   j                  || j                        S )z%A method to save token into database.)r   
save_tokenr   )r   tokens     r   r+   zBaseGrant.save_tokenN   s    {{%%eT\\::r   c                 x    | j                   j                  j                  }| j                  j	                  |      S )zAValidate if requested scope is supported by Authorization Server.)r   payloadr   r   validate_requested_scope)r   r   s     r   r/   z"BaseGrant.validate_requested_scopeR   s,    $$**{{33E::r   )NNNNT)__name__
__module____qualname__r'   r!   r   TOKEN_RESPONSE_HEADERr   r   propertyr   r"   r)   r+   r/   __classcell__)r   s   @r   r
   r
   	   s`    #8"9 J 1  # #
 "
&4;;r   r
   c                   8    e Zd ZdgZdZedefd       Zd Zd Z	y)TokenEndpointMixinPOSTNr   c                 |    |j                   j                  | j                  k(  xr |j                  | j                  v S r   )r.   r   r!   methodTOKEN_ENDPOINT_HTTP_METHODSclsr   s     r   check_token_endpointz'TokenEndpointMixin.check_token_endpoint_   s7     OO&&#..8 B#"A"AA	
r   c                     t               r   NotImplementedErrorr   s    r   validate_token_requestz)TokenEndpointMixin.validate_token_requestf       !##r   c                     t               r   r@   r   s    r   create_token_responsez(TokenEndpointMixin.create_token_responsei   rC   r   )
r0   r1   r2   r;   r!   classmethodr   r>   rB   rE    r   r   r7   r7   X   s4    #)( J
= 
 
$$r   r7   c                       e Zd Z e       ZdZedefd       Ze	defd       Z
e	defd       Zed        Zd Zdefd	Zy
)AuthorizationEndpointMixinFr   c                 F    |j                   j                  | j                  v S r   )r.   response_typeRESPONSE_TYPESr<   s     r   check_authorization_endpointz7AuthorizationEndpointMixin.check_authorization_endpointq   s    ,,0B0BBBr   c                 T   | j                   j                  r^|j                  | j                   j                        s#t        d| j                   j                   d      | j                   j                  S |j	                         }|s!t        d| j                   j
                        |S )NzRedirect URI z is not supported by client.z"Missing 'redirect_uri' in request.state)r.   r   check_redirect_urir   get_default_redirect_urirP   )r   r   r   s      r   #validate_authorization_redirect_uriz>AuthorizationEndpointMixin.validate_authorization_redirect_uriu   s    ??'',,W__-I-IJ)#GOO$@$@#AA]^  ??///!::<L)8@U@U   r   c                     | j                   j                  }g d}|D ]E  }t        |j                  |g             dkD  s"t	        d| d| j                   j
                         y)zFor the Authorization Endpoint, request and response parameters MUST NOT be included
        more than once. Per `Section 3.1`_.

        .. _`Section 3.1`: https://tools.ietf.org/html/rfc6749#section-3.1
        )rK   	client_idr   r   rP      z
Multiple 'z' in request.rO   N)r.   datalistlengetr   rP   )r   rW   
parametersparams       r   &validate_no_multiple_request_parameterzAAuthorizationEndpointMixin.validate_no_multiple_request_parameter   sd     ??++U
 	E8<<r*+a/) }5W__=R=R 	r   c                 4    | j                         }|| _        |S r   )validate_authorization_requestr   )r   r   s     r   validate_consent_requestz3AuthorizationEndpointMixin.validate_consent_request   s    ::<(r   c                     t               r   r@   r   s    r   r^   z9AuthorizationEndpointMixin.validate_authorization_request   rC   r   r   c                     t               r   r@   )r   r   
grant_users      r   create_authorization_responsez8AuthorizationEndpointMixin.create_authorization_response   rC   r   N)r0   r1   r2   setrL   ERROR_RESPONSE_FRAGMENTrF   r   rM   staticmethodrS   r\   r   r_   r^   strrc   rG   r   r   rI   rI   m   s    UN#C= C C  ]         
$$# $r   rI   N)authlib.constsr   errorsr   hooksr   r   requestsr   r
   r7   rI   rG   r   r   <module>rl      s7    / (   $L; L;^$ $*1$ 1$r   