a
    u54`c
                     @   s   d dl 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
 d dlmZ ddlmZ dd	lmZ G d
d deZG dd deZG dd dee
ZG dd dee	ZeeZeeZdS )    N)reverse)app_settings)OAuth2Error)OAuth2AdapterOAuth2CallbackViewOAuth2LoginView)build_absolute_uri   )FeishuOAuth2Client)FeishuProviderc                   @   s6   e Zd ZejZdZdZdZdZ	e
dd Zdd Zd	S )
FeishuOAuth2Adapterz0https://open.feishu.cn/open-apis/authen/v1/indexz7https://open.feishu.cn/open-apis/authen/v1/access_tokenzChttps://open.feishu.cn/open-apis/auth/v3/app_access_token/internal/z4https://open.feishu.cn/open-apis/authen/v1/user_infoc                 C   s   |    }|d| j}|S )NZAUTHORIZE_URL)get_providerZget_settingsgetauthorization_url)selfsettingsurl r   d/var/www/html/Ranjet/env/lib/python3.9/site-packages/allauth/socialaccount/providers/feishu/views.pyauthorize_url   s    z!FeishuOAuth2Adapter.authorize_urlc                 K   s^   t j| jdd|j dd}|  | }|d dkrFtd|j |d }|  	||S )	Nzapplication/jsonzBearer )zContent-TypeZAuthorization)headerscoder   zError retrieving code: %sdata)
requestsr   user_info_urltokenZraise_for_statusjsonr   contentr   Zsociallogin_from_response)r   requestappr   kwargsZresp
extra_datar   r   r   complete_login$   s    z"FeishuOAuth2Adapter.complete_loginN)__name__
__module____qualname__r   idprovider_idr   access_token_urlZapp_access_token_urlr   propertyr   r"   r   r   r   r   r      s   
r   c                   @   s   e Zd Zdd ZdS )FeishuOAuth2ClientMixinc                 C   sd   t | jjd }| jjptj}t|||d}| j }||}t	||j
|j| jj| jj||}|S )NZ	_callback)protocol)r   Zadapterr'   Zredirect_uri_protocolr   ZDEFAULT_HTTP_PROTOCOLr   r   Z	get_scoper
   Z	client_idZsecretZaccess_token_methodr(   )r   r   r   Zcallback_urlr+   providerZscopeclientr   r   r   
get_client6   s     

	z"FeishuOAuth2ClientMixin.get_clientN)r#   r$   r%   r.   r   r   r   r   r*   5   s   r*   c                   @   s   e Zd ZdS )FeishuOAuth2LoginViewNr#   r$   r%   r   r   r   r   r/   J   s   r/   c                   @   s   e Zd ZdS )FeishuOAuth2CallbackViewNr0   r   r   r   r   r1   N   s   r1   )r   Zdjango.urlsr   Zallauth.accountr   Z-allauth.socialaccount.providers.oauth2.clientr   Z,allauth.socialaccount.providers.oauth2.viewsr   r   r   Zallauth.utilsr   r-   r
   r,   r   r   objectr*   r/   r1   Zadapter_viewZoauth2_loginZoauth2_callbackr   r   r   r   <module>   s   !
