o
    Df                     @   s   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	 ddl
mZ ddlmZmZ G d	d
 d
ZG dd deeeZG dd dee	eeZdS )    )URL)RedirectResponse   )
OAuthError)BaseApp)AsyncOAuth1MixinAsyncOAuth2Mixin)AsyncOpenIDMixin)AsyncOAuth1ClientAsyncOAuth2Clientc                   @   s   e Zd Zdd ZdddZdS )StarletteAppMixinc                    sH   | dd }|r | jjrd }n|j}| j|||I d H  d S td)NstatezMissing state value)pop	frameworkcachesessionset_state_dataRuntimeError)selfrequestkwargsr   r    r   c/home/ubuntu/webapp/venv/lib/python3.10/site-packages/authlib/integrations/starlette_client/apps.pysave_authorize_data   s   z%StarletteAppMixin.save_authorize_dataNc                    s\   |rt |trt|}| j|fi |I dH }| j|fd|i|I dH  t|d ddS )a'  Create a HTTP Redirect for Authorization Endpoint.

        :param request: HTTP request instance from Starlette view.
        :param redirect_uri: Callback or redirect URI for authorization.
        :param kwargs: Extra parameters to include.
        :return: A HTTP redirect response.
        Nredirect_uriurli.  )status_code)
isinstancer   strcreate_authorization_urlr   r   )r   r   r   r   rvr   r   r   authorize_redirect   s   
z$StarletteAppMixin.authorize_redirect)N)__name__
__module____qualname__r   r!   r   r   r   r   r   
   s    r   c                   @      e Zd ZeZdd ZdS )StarletteOAuth1Appc                    s   t |j}|d}|stdd| j|j|I d H }|s$tdd|d |d< || | j|j|I d H  | j	di |I d H S )Noauth_tokenzMissing "oauth_token" parameter)descriptionz)Missing "request_token" in temporary datarequest_tokenr   )
dictquery_paramsgetr   r   get_state_datar   updateclear_state_datafetch_access_token)r   r   r   paramsr   datar   r   r   authorize_access_token*   s   




z)StarletteOAuth1App.authorize_access_tokenN)r"   r#   r$   r
   
client_clsr3   r   r   r   r   r&   '       r&   c                   @   r%   )StarletteOAuth2Appc                    s   |j d}|r|j d}t||d|j d|j dd}| jjr)d }n|j}|dd }| j||dI d H }| j||dI d H  | 	||}| j
di ||I d H }	d|	v rxd	|v rx| j|	|d	 |d
I d H }
|
|	d< |	S )Nerrorerror_description)r7   r(   coder   )r9   r   claims_optionsid_tokennonce)r<   r:   userinfor   )r+   r,   r   r   r   r   r   r-   r/   _format_state_paramsr0   parse_id_token)r   r   r   r7   r(   r1   r   r:   
state_datatokenr=   r   r   r   r3   =   s(   

z)StarletteOAuth2App.authorize_access_tokenN)r"   r#   r$   r   r4   r3   r   r   r   r   r6   :   r5   r6   N)starlette.datastructuresr   starlette.responsesr   base_clientr   r   base_client.async_appr   r   base_client.async_openidr	   httpx_clientr
   r   r   r&   r6   r   r   r   r   <module>   s    