
    D6i3                         d dl Z d dlZd dlZd dlmZmZ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mZmZmZ dd	lmZ  ej,                  e      Z G d
 de      Zy)    N)current_appredirectrequesturl_for)generate_token)MissingCodeError)cached_property)Response   )BaseOAuthConsumerBlueprintoauth_authorizedoauth_before_loginoauth_error)OAuth2Sessionc                       e Zd ZdZ	 	 dddddddddddddddddddddddddddddZed        Zej                  d        Zed	        Z	d
 Z
ddZd Zd Zy)OAuth2ConsumerBlueprintzU
    A subclass of :class:`flask.Blueprint` that sets up OAuth 2 authentication.
    NFS256)clientauto_refresh_urlauto_refresh_kwargsscopestatestatic_folderstatic_url_pathtemplate_folder
url_prefix	subdomainurl_defaults	root_path	login_urlauthorized_urlbase_urlauthorization_urlauthorization_url_params	token_urltoken_url_paramsredirect_urlredirect_tosession_classstoragerule_kwargsuse_pkcecode_challenge_methodc                   t        j                  | |||
||||||||||       || _        |xs t        | _        || _        || _        || _        || _        || _	        |	| _
        || _        || _        || _        |xs i | _        || _        |xs i | _        || _        || _        || _        || _        | j+                  | j,                         y)a  
        Most of the constructor arguments are forwarded either to the
        :class:`flask.Blueprint` constructor or the
        :class:`requests_oauthlib.OAuth2Session` constructor, including
        ``**kwargs`` (which is forwarded to
        :class:`~requests_oauthlib.OAuth2Session`).
        Only the arguments that are relevant to Flask-Dance are documented here.

        Args:
            base_url: The base URL of the OAuth provider.
                If specified, all URLs passed to this instance will be
                resolved relative to this URL.
            authorization_url: The URL specified by the OAuth provider for
                obtaining an
                `authorization grant <https://datatracker.ietf.org/doc/html/rfc6749#section-1.3>`__.
                This can be an fully-qualified URL, or a path that is
                resolved relative to the ``base_url``.
            authorization_url_params (dict): A dict of extra
                key-value pairs to include in the query string of the
                ``authorization_url``, beyond those necessary for a standard
                OAuth 2 authorization grant request.
            token_url: The URL specified by the OAuth provider for
                obtaining an
                `access token <https://datatracker.ietf.org/doc/html/rfc6749#section-1.4>`__.
                This can be an fully-qualified URL, or a path that is
                resolved relative to the ``base_url``.
            token_url_params (dict): A dict of extra
                key-value pairs to include in the query string of the
                ``token_url``, beyond those necessary for a standard
                OAuth 2 access token request.
            login_url: The URL route for the ``login`` view that kicks off
                the OAuth dance. This string will be
                :ref:`formatted <python:formatstrings>`
                with the instance so that attributes can be interpolated.
                Defaults to ``/{bp.name}``, so that the URL is based on the name
                of the blueprint.
            authorized_url: The URL route for the ``authorized`` view that
                completes the OAuth dance. This string will be
                :ref:`formatted <python:formatstrings>`
                with the instance so that attributes can be interpolated.
                Defaults to ``/{bp.name}/authorized``, so that the URL is
                based on the name of the blueprint.
            redirect_url: When the OAuth dance is complete,
                redirect the user to this URL.
            redirect_to: When the OAuth dance is complete,
                redirect the user to the URL obtained by calling
                :func:`~flask.url_for` with this argument. If you do not specify
                either ``redirect_url`` or ``redirect_to``, the user will be
                redirected to the root path (``/``).
            session_class: The class to use for creating a Requests session
                between the consumer (your website) and the provider (e.g.
                Google). Defaults to
                :class:`~flask_dance.consumer.requests.OAuth2Session`.
            storage: A token storage class, or an instance of a token storage
                class, to use for this blueprint. Defaults to
                :class:`~flask_dance.consumer.storage.session.SessionStorage`.
            rule_kwargs (dict, optional): Additional arguments that should be passed when adding
                the login and authorized routes. Defaults to ``None``.
            use_pkce: If true then the authorization flow will follow the PKCE (Proof Key for Code Exchange).
                For more details please refer to `RFC7636 <https://www.rfc-editor.org/rfc/rfc7636#section-4.1>`__
            code_challenge_method: Code challenge method to be used in authorization code flow with PKCE
                instead of client secret. It will be used only if ``use_pkce`` is set to True.
                Defaults to ``S256``.
        )r   r   r   r   r   r   r   r    r!   r*   r+   N)r   __init__r"   r   r)   
_client_idr   r   r   r   r   kwargsclient_secretr#   r$   r%   r&   r'   r(   r-   r,   teardown_app_requestteardown_session) selfnameimport_name	client_idr2   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r1   s                                    ^/home/azureuser/techstart-app/venv/lib/python3.12/site-packages/flask_dance/consumer/oauth2.pyr/   z OAuth2ConsumerBlueprint.__init__   s    F 	#++'++!%)#	
" !*;m $ 0#6 

* "3(@(FB%" 0 6B(&%:" !!$"7"78    c                 .    | j                   j                  S N)sessionr8   )r5   s    r9   r8   z!OAuth2ConsumerBlueprint.client_id   s    ||%%%r:   c                 \    || j                   _        || j                   j                  _        y r<   )r=   r8   _client)r5   values     r9   r8   z!OAuth2ConsumerBlueprint.client_id   s     !&).&r:   c                       j                   d j                   j                   j                   j                   j
                   j                    j                  d j                  } fd}||_	         j                  |      S )z
        This is a session between the consumer (your website) and the provider
        (e.g. Google). It is *not* a session between a user of your website
        and your website.
        :return:
        )r8   r   r   r   r   r   	blueprintr"   c                     | _         y r<   token)rE   r5   s    r9   token_updaterz6OAuth2ConsumerBlueprint.session.<locals>.token_updater   s
    DJr:    )r)   r0   r   r   r   r   r   r"   r1   rF   session_created)r5   retrF   s   `  r9   r=   zOAuth2ConsumerBlueprint.session   s     !d   

oo;;!22 $ 8 8****]]

 kk

	 *##C((r:   c                     |S r<   rG   )r5   r=   s     r9   rH   z'OAuth2ConsumerBlueprint.session_created   s    r:   c                 (    	 | ` y # t        $ r Y y w xY wr<   )r=   KeyError)r5   	exceptions     r9   r4   z(OAuth2ConsumerBlueprint.teardown_session   s    	 		s    	c                 ,   t         j                  d| j                         t        dd      | j                  _        | j                  rt        d      }| j                  j                  j                  || j                        }| j                  j                  | j                  |d       | j                   d	}|t        j                  |<   t         j                  d
|        | j                  j                  | j                  fd| j                   i| j                  \  }}| j                   d}|t        j                  |<   t         j                  d|       t         j                  d|       t#        j$                  | |       t'        |      S )Nclient_id = %s.authorizedT	_external0   )length)code_verifierr-   )r-   code_challenge_oauth_code_verifiercode_verifier = %sr   _oauth_state
state = %szredirect URL = %s)url)logdebugr8   r   r=   redirect_urir,   r   r?   create_code_challenger-   r$   updater6   flaskr#   r   r   sendr   )r5   rU   rV   code_verifier_keyr[   r   	state_keys          r9   loginzOAuth2ConsumerBlueprint.login   sR   		"DNN3$+MT$J!==*"5M!\\11GG+&*&@&@ H N ))00-1-G-G&4 $(99+-A B/<EMM+,II*M:3T\\33""
*.**
8<8U8U

U yyk.	#(i 		,&		%s+#.}r:   c                 :   | j                   r| j                   }n$| j                  rt        | j                        }nd}t        j	                  d|       t
        j                  j                  d      }|rt
        j                  j                  d      }t
        j                  j                  d      }t        j                  d|||       t        j                  | |||      }|r.|D ])  \  }}t        |t        t        j                  f      s'|c S  t        |      S | j                    d}|t"        j$                  vr)t        j'                  d	       t        t        d
            S t"        j$                  |   }	t        j	                  d|	       |	| j$                  _        t"        j$                  |= | j*                  r| j                    d}
|
t"        j$                  vr)t        j'                  d       t        t        d
            S t"        j$                  |
   }t        j	                  d|       t"        j$                  |
= || j,                  d<   t        dd      | j$                  _        t        j	                  d| j0                         t        j	                  d| j2                         	  | j$                  j4                  | j6                  ft
        j8                  | j2                  d| j,                  }tC        j                  | |      xs g }d}|D ]1  \  }}t        |t        t        j                  f      r|c S |dk(  s0d}3 |r	 || _"        t        |      S t        |      S # t:        $ rL}|j                  d   dj=                  t?        j@                  t
        j                              f|_         d}~ww xY w# tF        $ rJ}t        j                  dtI        |             t        j                  | |       Y d}~t        |      S d}~ww xY w)z
        This is the route/function that the user will be redirected to by
        the provider (e.g. Google) after the user has logged into the
        provider's website and authorized your app to access their account.
        /znext_url = %serrorerror_description	error_uriz7OAuth 2 authorization error: %s description: %s uri: %s)rh   ri   rj   rY   z*state not found, redirecting user to loginz.loginrZ   rW   z2code_verifier not found, redirecting user to loginrX   rU   rP   TrQ   rO   zclient_secret = %s)authorization_responser2   r   zOThe redirect request did not contain the expected parameters. Instead I got: {}NrD   FzOAuth 2 authorization error: %s)rh   )%r'   r(   r   r\   r]   r   argsgetwarningr   rb   
isinstancer
   r   response_classr   r6   ra   r=   info_stater,   r&   r^   r8   r2   fetch_tokenr%   r[   r   formatjsondumpsr   rE   
ValueErrorstr)r5   next_urlrh   
error_descrj   results_rI   rd   r   rc   rU   rE   e	set_tokenfuncs                   r9   
authorizedz"OAuth2ConsumerBlueprint.authorized   sn    ((Ht//0HH		/8,   ) ))*=>J((5IKKI	 "&&EZ9G % #FAs!#+2L2L'MN"
# H%%yyk.	EMM)HHABGH-..i(		,&#MM)$==#'99+-A B 5MN 122!MM*;<MII*M:/05BD!!/2$+MT$J!		"DNN3		&(:(:;	,DLL,,'.{{"00 ''	E #''E:@b	  	"ID##+*D*DEF
e|!			" 4"
 !!x!!/   	q	ahhJJw||,AF 	(  4=s5zJ  U33!!4s2   0AM/ O /	O8AN??O	P6PP)NNr<   )__name__
__module____qualname____doc__r/   propertyr8   setterr	   r=   rH   r4   re   r   rG   r:   r9   r   r      s     K9  !%$AK9Z & & / /
 ) )2:["r:   r   )ru   loggingra   r   r   r   r   oauthlib.commonr   oauthlib.oauth2r   werkzeug.utilsr	   werkzeug.wrappersr
   baser   r   r   r   requestsr   	getLoggerr   r\   r   rG   r:   r9   <module>r      sP       9 9 * , * &  $g!w"8 w"r:   