
    D6i                         d dl mZ d dlmZmZ 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	e	      Z G d
 de      Z
y)    wraps)redirecturl_for)
to_unicode)OAuth1Session)OAuth2Session)	URLObject)cached_propertyc                   v     e Zd ZdZd	 fd	Zed        Zd Ze fd       Z	ed        Z
 fdZ	 d
 fd	Z xZS )r   a  
    A :class:`requests.Session` subclass that can do some special things:

    * lazy-loads OAuth1 tokens from the storage via the blueprint
    * handles OAuth1 authentication
      (from :class:`requests_oauthlib.OAuth1Session` superclass)
    * has a ``base_url`` property used for relative URL resolution

    Note that this is a session between the consumer (your website) and the
    provider (e.g. Google), and *not* a session between a user of your website
    and your website.
    c                 R    t        |   |i | || _        t        |      | _        y N)super__init__	blueprintr
   base_urlselfr   r   argskwargs	__class__s        `/home/azureuser/techstart-app/venv/lib/python3.12/site-packages/flask_dance/consumer/requests.pyr   zOAuth1Session.__init__   s(    $)&)"!(+    c                 .    | j                   j                  S zX
        Get and set the values in the OAuth token, structured as a dictionary.
        r   tokenr   s    r   r   zOAuth1Session.token       
 ~~###r   c                     | j                   }|rWd|v rSd|v rOt        |d         | j                  j                  _        t        |d         | j                  j                  _        yy)Noauth_tokenoauth_token_secretTF)r   r   authclientresource_owner_keyresource_owner_secret)r   ts     r   
load_tokenzOAuth1Session.load_token%   s^    JJ!#(<(A 3=Q}=M2NDII/5?BV@W5XDII2r   c                 8    | j                          t        |   S a  This is the property used when you have a statement in your code
        that reads "if <provider>.authorized:", e.g. "if google.authorized:".

        The way it works is kind of complicated: this function just tries
        to load the token, and then the 'super()' statement basically just
        tests if the token exists (see BaseOAuth1Session.authorized).

        To load the token, it calls the load_token() function within this class,
        which in turn checks the 'token' property of this class (another
        function), which in turn checks the 'token' property of the blueprint
        (see base.py), which calls 'storage.get()' to actually try to load
        the token from the cache/db (see the 'get()' function in
        storage/sqla.py).
        r(   r   
authorizedr   r   s    r   r,   zOAuth1Session.authorized/         	w!!r   c                       fd}|S )a   
        .. versionadded:: 1.3.0

        This is a decorator for a view function. If the current user does not
        have an OAuth token, then they will be redirected to the
        :meth:`~flask_dance.consumer.oauth1.OAuth1ConsumerBlueprint.login`
        view to obtain one.
        c                 2     t                fd       }|S )Nc                      j                   s-j                  j                   d}t        t	        |            S  | i |S Nz.loginr,   r   namer   r   r   r   endpointfuncr   s      r   check_authorizationzROAuth1Session.authorization_required.<locals>.wrapper.<locals>.check_authorizationN   A    "&.."5"5!6f=H#GH$566T,V,,r   r   r7   r8   r   s   ` r   wrapperz5OAuth1Session.authorization_required.<locals>.wrapperM   !    4[- - '&r    r   r;   s   ` r   authorization_requiredz$OAuth1Session.authorization_requiredB       	' r   c                     | j                   r*| j                   j                  |j                        |_        t        |   |      S r   )r   relativeurlr   prepare_request)r   requestr   s     r   rD   zOAuth1Session.prepare_requestY   s4    ==--00=GKw&w//r   c                 P    |r| j                          t        |   d||||d|S )N)methodrC   dataheadersr=   )r(   r   rE   )r   rG   rC   rH   rI   should_load_tokenr   r   s          r   rE   zOAuth1Session.request^   s9     OOw 
sw
BH
 	
r   NN)NNT)__name__
__module____qualname____doc__r   r   r   r(   propertyr,   r?   rD   rE   __classcell__r   s   @r   r   r      sd    ,
 $ $ " "$  ,0 GK
 
r   r   c                   z     e Zd ZdZd	 fd	Zed        Zd Zed        Z	e fd       Z
ed        Zd	 fd	Z xZS )
r	   a  
    A :class:`requests.Session` subclass that can do some special things:

    * lazy-loads OAuth2 tokens from the storage via the blueprint
    * handles OAuth2 authentication
      (from :class:`requests_oauthlib.OAuth2Session` superclass)
    * has a ``base_url`` property used for relative URL resolution

    Note that this is a session between the consumer (your website) and the
    provider (e.g. Google), and *not* a session between a user of your website
    and your website.
    c                 V    t        |   |i | || _        t        |      | _        | `y r   )r   r   r   r
   r   r   r   s        r   r   zOAuth2Session.__init__v   s-    $)&)"!(+Jr   c                 .    | j                   j                  S r   r   r   s    r   r   zOAuth2Session.token|   r   r   c                     | j                   | j                  _         | j                   r&| j                  j                  | j                          yy)NTF)r   _clientpopulate_token_attributesr   s    r   r(   zOAuth2Session.load_token   s5    !ZZ::LL224::>r   c                 T    | j                   xr | j                   j                  d      S )zD
        Returns the ``access_token`` from the OAuth token.
        access_token)r   getr   s    r   rZ   zOAuth2Session.access_token   s     
 zz<djjnn^<<r   c                 8    | j                          t        |   S r*   r+   r-   s    r   r,   zOAuth2Session.authorized   r.   r   c                       fd}|S )a   
        .. versionadded:: 1.3.0

        This is a decorator for a view function. If the current user does not
        have an OAuth token, then they will be redirected to the
        :meth:`~flask_dance.consumer.oauth2.OAuth2ConsumerBlueprint.login`
        view to obtain one.
        c                 2     t                fd       }|S )Nc                      j                   s-j                  j                   d}t        t	        |            S  | i |S r2   r3   r5   s      r   r8   zROAuth2Session.authorization_required.<locals>.wrapper.<locals>.check_authorization   r9   r   r   r:   s   ` r   r;   z5OAuth2Session.authorization_required.<locals>.wrapper   r<   r   r=   r>   s   ` r   r?   z$OAuth2Session.authorization_required   r@   r   c           
          | j                   r| j                   j                  |      }| j                          t        |   d||||| j
                  j                  | j
                  j                  d|S )N)rG   rC   rH   rI   	client_idclient_secretr=   )r   rB   r(   r   rE   r   ra   rb   )r   rG   rC   rH   rI   r   r   s         r   rE   zOAuth2Session.request   sn    ==--((-Cw 
nn....66
 
 	
r   rK   )rL   rM   rN   rO   r   r   r   r(   rP   rZ   r,   r?   rE   rQ   rR   s   @r   r	   r	   h   sl     $ $ = = " "$  ,
 
r   r	   N)	functoolsr   flaskr   r   oauthlib.commonr   requests_oauthlibr   BaseOAuth1Sessionr	   BaseOAuth2Session	urlobjectr
   werkzeug.utilsr   r=   r   r   <module>rk      s9     # & @ @  *Z
% Z
z`
% `
r   