o
    b                     @   sL   d dl mZ ddlmZ G dd deZG dd deZG dd	 d	eZd
S )    )default_json_headers   )InvalidRequestErrorc                   @   sd   e Zd ZdgZdZeZ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 )	BaseGrantZclient_secret_basicNc                 C   s(   || _ || _t t t t d| _d S )N)Z$after_validate_authorization_requestafter_validate_consent_requestZafter_validate_token_requestZprocess_token)requestserverset_hooks)selfr   r    r   a/var/www/secure340b-portal/env/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}|d ur||}| jj||||||dS )N)r   
grant_typeuserscope
expires_ininclude_refresh_token)
GRANT_TYPEr   r   Zget_allowed_scoper   generate_token)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
        Zafter_authenticate_client)r   Zgrant)r   Zauthenticate_clientr   TOKEN_ENDPOINT_AUTH_METHODSsend_signal)r   r   r   r   r   "authenticate_token_endpoint_client3   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   O   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   S   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_hookY   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   ZTOKEN_RESPONSE_HEADERr   propertyr   r   r   r   r   r$   r'   r   r   r   r   r      s    


r   c                   @   s2   e Zd ZdgZdZedd Zdd Zdd ZdS )	TokenEndpointMixinPOSTNc                 C   s   |j | jkS r   )r   r   clsr   r   r   r   check_token_endpointk      z'TokenEndpointMixin.check_token_endpointc                 C      t  r   NotImplementedErrorr   r   r   r   validate_token_requesto      z)TokenEndpointMixin.validate_token_requestc                 C   r2   r   r3   r   r   r   r   create_token_responser   r6   z(TokenEndpointMixin.create_token_response)	r(   r)   r*   ZTOKEN_ENDPOINT_HTTP_METHODSr   classmethodr0   r5   r7   r   r   r   r   r,   d   s    
r,   c                   @   sF   e Zd Ze ZdZedd Zedd Z	dd Z
dd	 Zd
d ZdS )AuthorizationEndpointMixinFc                 C   s   |j | jv S r   )Zresponse_typeRESPONSE_TYPESr.   r   r   r   check_authorization_endpointz   r1   z7AuthorizationEndpointMixin.check_authorization_endpointc                 C   sF   | j r|| j std| j | jd| j S | }|s!td|S )Nz-Redirect URI {!r} is not supported by client.)r   z"Missing "redirect_uri" in request.)redirect_uriZcheck_redirect_urir   formatr   Z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_requestr'   )r   r<   r   r   r   validate_consent_request   s   z3AuthorizationEndpointMixin.validate_consent_requestc                 C   r2   r   r3   r   r   r   r   r?      r6   z9AuthorizationEndpointMixin.validate_authorization_requestc                 C   r2   r   r3   )r   r<   Z
grant_userr   r   r   create_authorization_response   r6   z8AuthorizationEndpointMixin.create_authorization_responseN)r(   r)   r*   r	   r:   ZERROR_RESPONSE_FRAGMENTr8   r;   staticmethodr>   r@   r?   rA   r   r   r   r   r9   v   s    

r9   N)Zauthlib.constsr   errorsr   objectr   r,   r9   r   r   r   r   <module>   s
    _