o
    RDi@&                     @   s   d dl Z d dlmZmZ d dlmZ d dlmZ d dlm	Z	m
Z
mZ d dlmZ ddlmZmZ dd	lmZ G d
d dZG dd deZdd ZdS )    N)urlparse
urlunparse)oauth2)Request)quote	urlencode
urlencoded)OAuth2Error   )FatalClientErrorOAuthToolkitError)oauth2_settingsc                   @   sz   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd ZdS )OAuthLibCorez
    Wrapper for oauth Server providing django-specific interfaces.

    Meant for things like extracting request data and converting
    everything to formats more palatable for oauthlib's Server.
    Nc                 C   s.   t j}| }t j}|pt j|fi || _dS )zM
        :params server: An instance of oauthlib.oauth2.Server class
        N)r   OAUTH2_VALIDATOR_CLASSserver_kwargsOAUTH2_SERVER_CLASSserver)selfr   validator_class	validatorr    r   X/var/www/Datamplify/venv/lib/python3.10/site-packages/oauth2_provider/oauth2_backends.py__init__   s   zOAuthLibCore.__init__c                 C   sZ   t t| }tdd |d D t}|D ]}|d |t|dd|d< qt|S )z
        Django considers "safe" some characters that aren't so for oauthlib.
        We have to search for them and properly escape.
        c                 s   s    | ]}|V  qd S Nr   ).0cr   r   r   	<genexpr>$   s    z6OAuthLibCore._get_escaped_full_path.<locals>.<genexpr>       )safe)	listr   get_full_pathset
differencer   replacer   r   )r   requestparsedunsafer   r   r   r   _get_escaped_full_path   s
   z#OAuthLibCore._get_escaped_full_pathc                 C   s   dS )aP  
        Produce extra credentials for token response. This dictionary will be
        merged with the response.
        See also: `oauthlib.oauth2.rfc6749.TokenEndpoint.create_token_response`

        :param request: The current django.http.HttpRequest object
        :return: dictionary of extra credentials or None (default)
        Nr   r   r%   r   r   r   _get_extra_credentials*   s   	z#OAuthLibCore._get_extra_credentialsc                 C   s4   |  |}|j}| |}t| |}||||fS )z
        Extract parameters from the Django request object.
        Such parameters will then be passed to OAuthLib to build its own
        Request object. The body should be encoded using OAuthLib urlencoded.
        )r(   methodextract_headersr   extract_body)r   r%   urihttp_methodheadersbodyr   r   r   _extract_params5   s
   

zOAuthLibCore._extract_paramsc                 C   st   |j  }d|v r|d= d|v r|d= d|v r|d |d< d|v r'|d |d< | r1d|d< |S d|v r8|d= |S )	z
        Extracts headers from the Django request object
        :param request: The current django.http.HttpRequest object
        :return: a dictionary with OAuthLib needed headers
        z
wsgi.inputzwsgi.errorsHTTP_AUTHORIZATIONAuthorizationHTTP_ORIGINOrigin1X_DJANGO_OAUTH_TOOLKIT_SECURE)METAcopy	is_secure)r   r%   r0   r   r   r   r,   A   s   
zOAuthLibCore.extract_headersc                 C   s
   |j  S )z
        Extracts the POST body from the Django request object
        :param request: The current django.http.HttpRequest object
        :return: provided POST parameters
        )POSTitemsr)   r   r   r   r-   Z   s   
zOAuthLibCore.extract_bodyc           	   
   C   s|   z|  |\}}}}| jj||||d\}}||fW S  tjy, } zt|dd}~w tjy= } zt|dd}~ww )z
        A wrapper method that calls validate_authorization_request on `server_class` instance.

        :param request: The current django.http.HttpRequest object
        )r/   r1   r0   )errorN)r2   r   validate_authorization_requestr   r   r	   r   )	r   r%   r.   r/   r1   r0   scopescredentialsr>   r   r   r   r?   b   s   



z+OAuthLibCore.validate_authorization_requestc              
   C   s   z5|st j|ddd|j|d< | |\}}}}| jj|||||d\}	}
}|	dd}||	|
|fW S  t jyJ } zt||d dd}~w t jy^ } zt	||d dd}~ww )	a  
        A wrapper method that calls create_authorization_response on `server_class`
        instance.

        :param request: The current django.http.HttpRequest object
        :param scopes: A list of provided scopes
        :param credentials: Authorization credentials dictionary containing
                           `client_id`, `state`, `redirect_uri`, `response_type`
        :param allow: True if the user authorize the client, otherwise False
        stateN)rB   user)r.   r/   r0   r@   rA   Locationredirect_uri)r>   rE   )
r   AccessDeniedErrorgetrC   r2   r   create_authorization_responser   r	   r   )r   r%   r@   rA   allowrequest_urir/   _request_headersr0   r1   statusr.   r>   r   r   r   rH   t   s*   
z*OAuthLibCore.create_authorization_responsec           	   
   C   s   |  |\}}}}| |}z| j|||||\}}}|dd}||||fW S  tyC } zd|j|j|jfW  Y d}~S d}~ww )z
        A wrapper method that calls create_token_response on `server_class` instance.

        :param request: The current django.http.HttpRequest object
        rD   N)	r2   r*   r   create_token_responserG   r	   r0   jsonstatus_code)	r   r%   r.   r/   r1   r0   extra_credentialsrM   excr   r   r   rN      s   


z"OAuthLibCore.create_token_responsec                 C   sB   |  |\}}}}| j||||\}}}|dd}||||fS )z
        A wrapper method that calls create_revocation_response on a
        `server_class` instance.

        :param request: The current django.http.HttpRequest object
        rD   N)r2   r   create_revocation_responserG   )r   r%   r.   r/   r1   r0   rM   r   r   r   rS      s   z'OAuthLibCore.create_revocation_responsec              
   C   s|   |  |\}}}}z| j||||\}}}|dd}||||fW S  ty= } zd|j|j|jfW  Y d}~S d}~ww )z
        A wrapper method that calls create_userinfo_response on a
        `server_class` instance.

        :param request: The current django.http.HttpRequest object
        rD   N)r2   r   create_userinfo_responserG   r	   r0   rO   rP   )r   r%   r.   r/   r1   r0   rM   rR   r   r   r   rT      s   z%OAuthLibCore.create_userinfo_responsec           	      C   s4   |  |\}}}}| jj|||||d\}}||fS )z
        A wrapper method that calls verify_request on `server_class` instance.

        :param request: The current django.http.HttpRequest object
        :param scopes: A list of scopes required to verify so that request is verified
        )r@   )r2   r   verify_request)	r   r%   r@   r.   r/   r1   r0   validrr   r   r   rU      s   zOAuthLibCore.verify_requestc                 C   s.   |  |\}}}}t||||}| jj|S )zWrapper to call  `authenticate_client` on `server_class` instance.

        :param request: The current django.http.HttpRequest object
        )r2   OauthlibRequestr   request_validatorauthenticate_client)r   r%   r.   r/   r1   r0   oauth_requestr   r   r   rZ      s   z OAuthLibCore.authenticate_clientr   )__name__
__module____qualname____doc__r   r(   r*   r2   r,   r-   r?   rH   rN   rS   rT   rU   rZ   r   r   r   r   r      s    
	#r   c                   @   s   e Zd ZdZdd ZdS )JSONOAuthLibCorezW
    Extends the default OAuthLibCore to parse correctly application/json requests
    c                 C   sL   zt |jd }W |S  ty   d}Y |S  ty%   d}Y |S w )z
        Extracts the JSON body from the Django request object
        :param request: The current django.http.HttpRequest object
        :return: provided POST parameters "urlencodable"
        zutf-8 )rO   loadsr1   decoder=   AttributeError
ValueError)r   r%   r1   r   r   r   r-      s   zJSONOAuthLibCore.extract_bodyN)r\   r]   r^   r_   r-   r   r   r   r   r`      s    r`   c                  C   s.   t j} |  }t j}t j|fi |}t |S )zb
    Utility function that returns an instance of
    `oauth2_provider.backends.OAuthLibCore`
    )r   r   r   r   OAUTH2_BACKEND_CLASS)r   r   r   r   r   r   r   get_oauthlib_core   s
   
rg   )rO   urllib.parser   r   oauthlibr   oauthlib.commonr   rX   r   r   r   oauthlib.oauth2r	   
exceptionsr   r   settingsr   r   r`   rg   r   r   r   r   <module>   s     P