o
    Df                     @   sR   d dl mZ ddlmZ ddlmZ G dd dZG dd dZG d	d
 d
ZdS )    )default_json_headers   )OAuth2Request)InvalidRequestErrorc                   @   sj   e Zd ZdgZdZeZdefddZe	dd Z
		dd	d
Zdd Zdd Zdd Zdd Zdd ZdS )	BaseGrantclient_secret_basicNrequestc                 C   s4   d | _ d | _|| _|| _t t t t d| _d S )N)$after_validate_authorization_requestafter_validate_consent_requestafter_validate_token_requestprocess_token)promptredirect_urir   serverset_hooks)selfr   r    r   [/home/ubuntu/webapp/venv/lib/python3.10/site-packages/authlib/oauth2/rfc6749/grants/base.py__init__   s   zBaseGrant.__init__c                 C   s   | j jS N)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_client0   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&   K   s   zBaseGrant.save_tokenc                 C   s   | j j}| j j}| j||S )zAValidate if requested scope is supported by Authorization Server.)r   r   stater   validate_requested_scope)r   r   r(   r   r   r   r)   O   s   z"BaseGrant.validate_requested_scopec                 C   s,   || j vrtd|| j | j | | d S )NzHook type %s is not in %s.)r   
ValueErroradd)r   	hook_typehookr   r   r   register_hookU   s
   
zBaseGrant.register_hookc                 O   s*   | j | D ]}|| g|R i | qd S r   )r   )r   r,   argskwargsr-   r   r   r   execute_hook[   s   zBaseGrant.execute_hook)NNNNT)__name__
__module____qualname__r#   r   r   TOKEN_RESPONSE_HEADERr   r   propertyr   r   r%   r&   r)   r.   r1   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ko|j| jv S r   )r   r   methodTOKEN_ENDPOINT_HTTP_METHODSclsr   r   r   r   check_token_endpointg   s   
z'TokenEndpointMixin.check_token_endpointc                 C      t  r   NotImplementedErrorr   r   r   r   validate_token_requestl      z)TokenEndpointMixin.validate_token_requestc                 C   r>   r   r?   r   r   r   r   create_token_responseo   rB   z(TokenEndpointMixin.create_token_response)
r2   r3   r4   r:   r   classmethodr   r=   rA   rC   r   r   r   r   r7   `   s    r7   c                   @   sX   e Zd Ze ZdZedefddZe	defddZ
dd Zd	d
 ZdefddZdS )AuthorizationEndpointMixinFr   c                 C   s   |j | jv S r   )response_typeRESPONSE_TYPESr;   r   r   r   check_authorization_endpointw   s   z7AuthorizationEndpointMixin.check_authorization_endpointc                 C   sN   | j r|| j std| j  d| jd| j S | }|s%td| jd|S )NzRedirect URI z is not supported by client.)r(   z"Missing "redirect_uri" in request.)r   check_redirect_urir   r(   get_default_redirect_uri)r   r   r   r   r   r   #validate_authorization_redirect_uri{   s   z>AuthorizationEndpointMixin.validate_authorization_redirect_uric                 C   s   |   }| d| || _d S )Nr
   )validate_authorization_requestr1   r   )r   r   r   r   r   validate_consent_request   s   
z3AuthorizationEndpointMixin.validate_consent_requestc                 C   r>   r   r?   r   r   r   r   rL      rB   z9AuthorizationEndpointMixin.validate_authorization_requestr   c                 C   r>   r   r?   )r   r   
grant_userr   r   r   create_authorization_response   rB   z8AuthorizationEndpointMixin.create_authorization_responseN)r2   r3   r4   r   rG   ERROR_RESPONSE_FRAGMENTrD   r   rH   staticmethodrK   rM   rL   strrO   r   r   r   r   rE   s   s    rE   N)	authlib.constsr   requestsr   errorsr   r   r7   rE   r   r   r   r   <module>   s    Z