o
    ^[2h                     @   sl   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 deZ	G d	d
 d
Z
G dd dZdS )    )default_json_headers   )InvalidRequestError)Hookable)hooked)OAuth2Requestc                       sh   e Zd ZdgZdZeZdef fddZe	dd Z
					dd	d
Zdd Zdd Zdd Z  ZS )	BaseGrantclient_secret_basicNrequestc                    s&   t    d | _d | _|| _|| _d S N)super__init__promptredirect_urir
   server)selfr
   r   	__class__ s/home/skpark/git/infrasmart_work/infrasmart/venv/lib/python3.10/site-packages/authlib/oauth2/rfc6749/grants/base.pyr      s
   

zBaseGrant.__init__c                 C   s   | j jS r   )r
   clientr   r   r   r   r      s   zBaseGrant.clientTc                 C   s*   |d u r| j }| jj| jj|||||dS )N)r   
grant_typeuserscope
expires_ininclude_refresh_token)
GRANT_TYPEr   generate_tokenr
   r   )r   r   r   r   r   r   r   r   r   r   !   s   zBaseGrant.generate_tokenc                 C   s(   | j | j| j}| j jd|| 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   r   r   r   "authenticate_token_endpoint_client4   s
   z,BaseGrant.authenticate_token_endpoint_clientc                 C   s   | j || jS )z%A method to save token into database.)r   
save_tokenr
   )r   tokenr   r   r   r%   N   s   zBaseGrant.save_tokenc                 C   s   | j jj}| j|S )zAValidate if requested scope is supported by Authorization Server.)r
   payloadr   r   validate_requested_scope)r   r   r   r   r   r(   R   s   
z"BaseGrant.validate_requested_scope)NNNNT)__name__
__module____qualname__r"   r   r   TOKEN_RESPONSE_HEADERr   r   propertyr   r   r$   r%   r(   __classcell__r   r   r   r   r   	   s     

r   c                   @   s8   e Zd ZdgZdZedefddZdd Zdd	 Z	dS )
TokenEndpointMixinPOSTNr
   c                 C   s   |j j| jko|j| jv S r   )r'   r   r   methodTOKEN_ENDPOINT_HTTP_METHODSclsr
   r   r   r   check_token_endpoint_   s   
z'TokenEndpointMixin.check_token_endpointc                 C      t  r   NotImplementedErrorr   r   r   r   validate_token_requestf      z)TokenEndpointMixin.validate_token_requestc                 C   r6   r   r7   r   r   r   r   create_token_responsei   r:   z(TokenEndpointMixin.create_token_response)
r)   r*   r+   r2   r   classmethodr   r5   r9   r;   r   r   r   r   r/   X   s    r/   c                   @   sn   e Zd Ze ZdZedefddZe	defddZ
e	defddZed	d
 Zdd ZdefddZdS )AuthorizationEndpointMixinFr
   c                 C   s   |j j| jv S r   )r'   response_typeRESPONSE_TYPESr3   r   r   r   check_authorization_endpointq   s   z7AuthorizationEndpointMixin.check_authorization_endpointc                 C   sR   | j jr|| j jstd| j j d| j jS | }|s'td| j jd|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_urirB   )r
   r   r   r   r   r   #validate_authorization_redirect_uriu   s   z>AuthorizationEndpointMixin.validate_authorization_redirect_uric                 C   sJ   | j j}g d}|D ]}t||g dkr"td| d| j jdq
dS )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
        )r>   	client_idr   r   rB      z
Multiple 'z' in request.rA   N)r'   datalistlengetr   rB   )r
   rH   
parametersparamr   r   r   &validate_no_multiple_request_parameter   s   zAAuthorizationEndpointMixin.validate_no_multiple_request_parameterc                 C   s   |   }|| _|S r   )validate_authorization_requestr   )r   r   r   r   r   validate_consent_request   s   z3AuthorizationEndpointMixin.validate_consent_requestc                 C   r6   r   r7   r   r   r   r   rN      r:   z9AuthorizationEndpointMixin.validate_authorization_requestr   c                 C   r6   r   r7   )r   r   
grant_userr   r   r   create_authorization_response   r:   z8AuthorizationEndpointMixin.create_authorization_responseN)r)   r*   r+   setr?   ERROR_RESPONSE_FRAGMENTr<   r   r@   staticmethodrE   rM   r   rO   rN   strrQ   r   r   r   r   r=   m   s    
r=   N)authlib.constsr   errorsr   hooksr   r   requestsr   r   r/   r=   r   r   r   r   <module>   s    O