o
    6d+c                     @   sl  d dl Z g dZd dlZd dlZd dlZd dlZd dlZd dlmZmZ ddl	m
Z
mZmZmZ ejr<ejjZejjZnejZejZg dZddgZd	ZG d
d deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZ G dd deZ!G dd deZ"G dd deZ#d d! Z$d"d# Z%d$d% Z&d&d' Z'dS )(    N)	BadRequestExceptionBadStateExceptionCsrfExceptionDropboxOAuth2FlowDropboxOAuth2FlowNoRedirectNotApprovedExceptionOAuth2FlowNoRedirectResultOAuth2FlowResultProviderException)datetime	timedelta   )API_HOSTWEB_HOSTpinned_sessionDEFAULT_TIMEOUT)ZofflineZonlinelegacyuserZteam   c                   @   s    e Zd ZdZdd Zdd ZdS )r   zQ
    Authorization information for an OAuth2Flow performed with no redirect.
    c                 C   sX   || _ |s	d| _nt|tr|| _nt tt|d | _|| _|| _|| _	|| _
dS )a^  
        :param str access_token: Token to be used to authenticate later requests.
        :param str account_id: The Dropbox user's account ID.
        :param str user_id: Deprecated (use :attr:`account_id` instead).
        :param str refresh_token: Token to be used to acquire new access token when existing one
            expires.
        :param expiration: Either the number of seconds from now that the token expires in or the
            datetime at which the token expires.
        :type expiration: int or datetime
        :param list scope: List of scopes to request in base oauth flow.
        N)seconds)access_token
expires_at
isinstancer   utcnowr   intrefresh_token
account_iduser_idscope)selfr   r   r   r   
expirationr    r!   dC:\Users\jesus\OneDrive\Desktop\erpjis_fastapi\backend\jisbackend\Lib\site-packages\dropbox/oauth.py__init__-   s   

z#OAuth2FlowNoRedirectResult.__init__c                 C   s    d| j | j| j| j| j| jf S )Nz2OAuth2FlowNoRedirectResult(%s, %s, %s, %s, %s, %s))r   r   r   r   r   r   r   r!   r!   r"   __repr__E   s   z#OAuth2FlowNoRedirectResult.__repr__N)__name__
__module____qualname____doc__r#   r%   r!   r!   r!   r"   r   (   s    r   c                       s4   e Zd ZdZ fddZedd Zdd Z  ZS )r	   zM
    Authorization information for an :class:`OAuth2Flow` with redirect.
    c                    s&   t t| j||||||d || _dS )z
        Same as :class:`OAuth2FlowNoRedirectResult` but with url_state.

        :param str url_state: The url state that was set by :meth:`DropboxOAuth2Flow.start`.
        )r   r   r   r   r    r   N)superr	   r#   	url_state)r   r   r   r   r+   r   
expires_inr   	__class__r!   r"   r#   U   s   

zOAuth2FlowResult.__init__c                 C   s.   t |tsJ | |j|j|j||j|j|jS N)r   r   r   r   r   r   r   r   )clsresultr+   r!   r!   r"   from_no_redirect_resulte   s   z(OAuth2FlowResult.from_no_redirect_resultc                 C   s$   d| j | j| j| j| j| j| jf S )Nz,OAuth2FlowResult(%s, %s, %s, %s, %s, %s, %s))r   r   r   r+   r   r   r   r$   r!   r!   r"   r%   k   s   zOAuth2FlowResult.__repr__)	r&   r'   r(   r)   r#   classmethodr2   r%   __classcell__r!   r!   r-   r"   r	   P   s    
r	   c                   @   sT   e Zd ZddddddedfddZ		dddZdd Zdd	d
ZdefddZ	dS )DropboxOAuth2FlowBaseNFc
           
      C   s   |d urt |dkst|tstd|d ur"|tvr"tdt|s*|s*td|r2|s2td|| _|| _|| _|| _	t
|	d| _|| _|| _|| _|r[t | _t| j| _d S d | _d | _d S )Nr   zScope list must be of type listz5Token access type must be from the following enum: {}z/Must pass in either consumer secret or use PKCEz1Must pass in scope to pass include_granted_scopes)ca_certs)lenr   listBadInputExceptionTOKEN_ACCESS_TYPESformatconsumer_keyconsumer_secretlocaletoken_access_typer   requests_sessionr   include_granted_scopes_timeout_generate_pkce_code_verifiercode_verifier_generate_pkce_code_challengecode_challenge
r   r<   r=   r>   r?   r   rA   use_pkcetimeoutr6   r!   r!   r"   r#   y   s.   
zDropboxOAuth2FlowBase.__init__c                 C   s   t d| jd}|d ur||d< |d ur||d< |d ur%|tv s!J ||d< |r/||d< d|d< |d urHd	||d
< |d urH|tv sDJ ||d< | d|tS )Ncode)Zresponse_type	client_idredirect_uristater?   rF   ZS256Zcode_challenge_method r   rA   z/oauth2/authorize)dictr<   r:   joinINCLUDE_GRANTED_SCOPES_TYPES	build_urlr   )r   rL   rM   r?   r   rA   rF   paramsr!   r!   r"   _get_authorize_url   s&   z(DropboxOAuth2FlowBase._get_authorize_urlc                 C   s   |  d}d|| jd}|r||d< n| j|d< | jd ur"| j|d< |d ur*||d< | jj||| jd}|  | }d	|v rE|d	 }n|d
 }|d }	d|v rV|d }
nd}
d|v ra|d }nd }d|v rl|d }nd }|d }t	|	|||
||S )Nz/oauth2/tokenZauthorization_code)Z
grant_typerJ   rK   rD   Zclient_secretr>   rL   )datarI   Zteam_idr   r   r    r,   r   uid)
rR   r<   r=   r>   r@   postrB   raise_for_statusjsonr   )r   rJ   rL   rD   urlrS   respdr   r   r   r,   r   rW   r!   r!   r"   _finish   sJ   








zDropboxOAuth2FlowBase._finishc                 C   s`   t jrt|t jr|d}t|}|pi }| }| jr"| j|d< |r.t|}d||f S |S )a  Build the path component for an API URL.

        This method urlencodes the parameters, adds them to the end of the target url, and puts a
        marker for the API version in front.

        :param str target: A target url (e.g. '/files') to build upon.
        :param dict params: Optional dictionary of parameters (name to value).
        :return: The path and parameters components of an API URL.
        :rtype: str
        utf8r>   z%s?%s)	sixPY2r   	text_typeencodeurl_path_quotecopyr>   _params_to_urlencoded)r   targetrS   target_pathZquery_stringr!   r!   r"   
build_path   s   

z DropboxOAuth2FlowBase.build_pathc                 C   s   d||  ||f S )a<  Build an API URL.

        This method adds scheme and hostname to the path returned from build_path.

        :param str target: A target url (e.g. '/files') to build upon.
        :param dict params: Optional dictionary of parameters (name to value).
        :return: The full API URL.
        :rtype: str
        zhttps://%s%s)ri   )r   rg   rS   hostr!   r!   r"   rR      s   
zDropboxOAuth2FlowBase.build_url)NNNNr/   )
r&   r'   r(   r   r#   rT   r^   ri   r   rR   r!   r!   r!   r"   r5   w   s    


4r5   c                       sB   e Zd ZdZddddddedf fdd	Zdd Zdd	 Z  ZS )
r   z
    OAuth 2 authorization helper for apps that can't provide a redirect URI
    (such as the command-line example apps).

    See examples under `example/oauth <https://github.com/dropbox/dropbox-sdk-python/tree/main/
    example/oauth>`_

    NFc
           
         s&   t t| j|||||||||	d	 dS )a  
        Construct an instance.

        :param str consumer_key: Your API app's "app key".
        :param str consumer_secret: Your API app's "app secret".
        :param str locale: The locale of the user of your application.  For example "en" or "en_US".
            Some API calls return localized data and error messages; this setting tells the server
            which locale to use. By default, the server uses "en_US".
        :param str token_access_type: the type of token to be requested.
            From the following enum:

            * None - creates a token with the app default (either legacy or online)
            * legacy - creates one long-lived token with no expiration
            * online - create one short-lived token with an expiration
            * offline - create one short-lived token with an expiration with a refresh token

        :param list scope: list of scopes to request in base oauth flow.
            If left blank, will default to all scopes for app.
        :param str include_granted_scopes: which scopes to include from previous grants.
            From the following enum:

            * user - include user scopes in the grant
            * team - include team scopes in the grant
            * *Note*: if this user has never linked the app, include_granted_scopes must be None

        :param bool use_pkce: Whether or not to use Sha256 based PKCE. PKCE should be only use on
            client apps which doesn't call your server. It is less secure than non-PKCE flow but
            can be used if you are unable to safely retrieve your app secret.
        :param Optional[float] timeout: Maximum duration in seconds that
            client will wait for any single packet from the server. After the timeout the client
            will give up on connection. If `None`, client will wait forever. Defaults
            to 100 seconds.
        :param str ca_cert: path to CA certificate. If left blank, default certificate location             will be used
        	r<   r=   r>   r?   r   rA   rH   rI   r6   N)r*   r   r#   rG   r-   r!   r"   r#     s   
&
z$DropboxOAuth2FlowNoRedirect.__init__c                 C   s   | j dd| j| j| j| jdS )a;  
        Starts the OAuth 2 authorization process.

        :return: The URL for a page on Dropbox's website.  This page will let the user "approve"
            your app, which gives your app permission to access the user's Dropbox account.
            Tell the user to visit this URL and approve your app.
        Nr   rA   rF   )rT   r?   r   rA   rF   r$   r!   r!   r"   startF  s
   z!DropboxOAuth2FlowNoRedirect.startc                 C   s   |  |d| jS )a  
        If the user approves your app, they will be presented with an "authorization code".
        Have the user copy/paste that authorization code into your app and then call this method to
        get an access token.

        :param str code: The authorization code shown to the user when they
            approved your app.
        :rtype: :class:`OAuth2FlowNoRedirectResult`
        :raises: The same exceptions as :meth:`DropboxOAuth2Flow.finish()`.
        N)r^   rD   )r   rJ   r!   r!   r"   finishS  s   z"DropboxOAuth2FlowNoRedirect.finish	r&   r'   r(   r)   r   r#   rm   rn   r4   r!   r!   r-   r"   r   
  s    	2r   c                       sD   e Zd ZdZddddddedf fdd	Zd
ddZdd	 Z  ZS )r   ad  
    OAuth 2 authorization helper. Use this for web apps.

    OAuth 2 has a two-step authorization process. The first step is having the user authorize your
    app. The second involves getting an OAuth 2 access token from Dropbox.

    See examples under `example/oauth <https://github.com/dropbox/dropbox-sdk-python/tree/main/
    example/oauth>`_

    NFc                    s8   t t| j||||||	|
||d	 || _|| _|| _dS )a	  
        Construct an instance.

        :param str consumer_key: Your API app's "app key".
        :param str redirect_uri: The URI that the Dropbox server will redirect the user to after the
            user finishes authorizing your app.  This URI must be HTTPS-based and pre-registered
            with the Dropbox servers, though localhost URIs are allowed without pre-registration and
            can be either HTTP or HTTPS.
        :param dict session: A dict-like object that represents the current user's web session
            (Will be used to save the CSRF token).
        :param str csrf_token_session_key: The key to use when storing the CSRF token in the session
            (For example: "dropbox-auth-csrf-token").
        :param str consumer_secret: Your API app's "app secret".
        :param str locale: The locale of the user of your application. For example "en" or "en_US".
            Some API calls return localized data and error messages; this setting tells the server
            which locale to use. By default, the server uses "en_US".
        :param str token_access_type: The type of token to be requested.
            From the following enum:

            * None - creates a token with the app default (either legacy or online)
            * legacy - creates one long-lived token with no expiration
            * online - create one short-lived token with an expiration
            * offline - create one short-lived token with an expiration with a refresh token

        :param list scope: List of scopes to request in base oauth flow.  If left blank,
            will default to all scopes for app.
        :param str include_granted_scopes: Which scopes to include from previous grants.
            From the following enum:

            * user - include user scopes in the grant
            * team - include team scopes in the grant
            * *Note*: If this user has never linked the app, :attr:`include_granted_scopes` must             be `None`

        :param bool use_pkce: Whether or not to use Sha256 based PKCE
        :param Optional[float] timeout: Maximum duration in seconds that client will wait for any
            single packet from the server. After the timeout the client will give up on connection.
            If `None`, client will wait forever. Defaults to 100 seconds.
        :param str ca_cert: path to CA certificate. If left blank, default certificate location             will be used
        rk   N)r*   r   r#   rL   sessioncsrf_token_session_key)r   r<   rL   rp   rq   r=   r>   r?   r   rA   rH   rI   r6   r-   r!   r"   r#   m  s   
/
zDropboxOAuth2Flow.__init__c                 C   sZ   t tdd}|}|dur|d| 7 }|| j| j< | j| j|| j	| j
| j| jdS )aP  
        Starts the OAuth 2 authorization process.

        This function builds an "authorization URL". You should redirect your user's browser to this
            URL, which will give them an opportunity to grant your app access to their Dropbox
            account. When the user completes this process, they will be automatically redirected to
            the :attr:`redirect_uri` you passed in to the constructor. This function will also save
            a CSRF token to :attr:`session[csrf_token_session_key]`
            (as provided to the constructor). This CSRF token will be checked on :meth:`finish()`
            to prevent request forgery.

        :param str url_state: Any data that you would like to keep in the URL through the
            authorization process. This exact value will be returned to you by :meth:`finish()`.
        :return: The URL for a page on Dropbox's website. This page will let the user "approve" your
            app, which gives your app permission to access the user's Dropbox account. Tell the user
            to visit this URL and approve your app.
           asciiN|rl   )base64urlsafe_b64encodeosurandomdecoderp   rq   rT   rL   r?   r   rA   rF   )r   r+   Z
csrf_tokenrM   r!   r!   r"   rm     s   zDropboxOAuth2Flow.startc                 C   sf  | d}|du rtd| d}| d}| d}|dur(|dur(td|du r4|du r4td| j| jvr>td	| j| j }t|d
krPtd| |d}|dk r^|}d}	n|d| }||d d }	t||syt	d||f | j| j= |dur|dkr|du rt
dt
d| |}
|dur|
d| 7 }
t|
| || j| j}t||	S )a  
        Call this after the user has visited the authorize URL (see :meth:`start()`), approved your
        app and was redirected to your redirect URI.

        :param dict query_params: The query parameters on the GET request to your redirect URI.
        :rtype: class:`OAuth2FlowResult`
        :raises: :class:`BadRequestException` If the redirect URL was missing parameters or if the
            given parameters were not valid.
        :raises: :class:`BadStateException` If there's no CSRF token in the session.
        :raises: :class:`CsrfException` If the :attr:`state` query parameter doesn't contain the
            CSRF token from the user's session.
        :raises: :class:`NotApprovedException` If the user chose not to approve your app.
        :raises: :class:`ProviderException` If Dropbox redirected to your redirect URI with some
            unexpected error identifier and error message.
        rM   Nz Missing query parameter 'state'.errorerror_descriptionrJ   zGQuery parameters 'code' and 'error' are both set; only one must be set.z1Neither query parameter 'code' or 'error' is set.zMissing CSRF token in session.   z!CSRF token unexpectedly short: %rrt   r   r   zexpected %r, got %rZaccess_deniedz&No additional description from Dropboxz'Additional description from Dropbox: %sz: )getr   rq   rp   r   r7   AssertionErrorfind_safe_equalsr   r   r
   r^   rL   rD   r	   r2   )r   Zquery_paramsrM   rz   r{   rJ   Zcsrf_token_from_sessionZ	split_posZgiven_csrf_tokenr+   Zfull_messageZno_redirect_resultr!   r!   r"   rn     sf   






zDropboxOAuth2Flow.finishr/   ro   r!   r!   r-   r"   r   a  s    
>r   c                   @      e Zd ZdZdS )r   z
    Thrown if the redirect URL was missing parameters or if the given parameters were not valid.

    The recommended action is to show an HTTP 400 error page.
    Nr&   r'   r(   r)   r!   r!   r!   r"   r     s    r   c                   @   r   )r   z
    Thrown if all the parameters are correct, but there's no CSRF token in the session. This
    probably means that the session expired.

    The recommended action is to redirect the user's browser to try the approval process again.
    Nr   r!   r!   r!   r"   r   $      r   c                   @   r   )r   z
    Thrown if the given 'state' parameter doesn't contain the CSRF token from the user's session.
    This is blocked to prevent CSRF attacks.

    The recommended action is to respond with an HTTP 403 error page.
    Nr   r!   r!   r!   r"   r   .  r   r   c                   @   r   )r   z1
    The user chose not to approve your app.
    Nr   r!   r!   r!   r"   r   8  s    r   c                   @   r   )r
   z
    Dropbox redirected to your redirect URI with some unexpected error identifier and error message.

    The recommended action is to log the error, tell the user something went wrong, and let them try
    again.
    Nr   r!   r!   r!   r"   r
   ?  r   r
   c                   @   r   )r9   z
    Thrown if incorrect types/values are used

    This should only ever be thrown during testing, app should have validation of input prior to
    reaching this point
    Nr   r!   r!   r!   r"   r9   I  r   r9   c                 C   sH   t | t |kr
dS d}t| |D ]\}}|t|t|A O }q|dkS )NFr   )r7   zipord)abrescacbr!   r!   r"   r   S  s   r   c                    s(   dd   fddt | D }t|S )a
  
    Returns a application/x-www-form-urlencoded :class:`str` representing the key/value pairs in
    :attr:`params`.

    Keys are values are ``str()``'d before calling :meth:`urllib.urlencode`, with the exception of
    unicode objects which are utf8-encoded.
    c                 S   s4   t | tjr| S t | tjr| dS t| dS )Nutf-8)r   r`   binary_typerb   rc   str)or!   r!   r"   rc   d  s
   
z%_params_to_urlencoded.<locals>.encodec                    s   i | ]\}} | |qS r!   r!   ).0kvrc   r!   r"   
<dictcomp>m  s    z)_params_to_urlencoded.<locals>.<dictcomp>)r`   	iteritems
url_encode)rS   Zutf8_paramsr!   r   r"   rf   \  s   	rf   c                  C   s@   t ttd} tdd| } t| tkr| d d } | S )Nr   z[^a-zA-Z0-9]+rV   r   )	ru   rv   rw   rx   PKCE_VERIFIER_LENGTHry   resubr7   )rD   r!   r!   r"   rC   p  s
   rC   c                 C   s4   t | d }t|d}|dd}|S )Nr   =rV   )hashlibsha256rc   digestru   rv   ry   replace)rD   rF   r!   r!   r"   rE   w  s   rE   )(r   __all__ru   rw   r`   urllibr   r   r   rp   r   r   r   r   PY3parsequoterd   	urlencoder   r:   rQ   r   objectr   r	   r5   r   r   	Exceptionr   r   r   r   r
   r9   r   rf   rC   rE   r!   r!   r!   r"   <module>   sD    
(' W ;	



	