o
    h]                     @   sz   d 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
 G dd dZdd	 ZG d
d deZe
eZe	eZdS )aT  
OAuth2 Adapter for Battle.net

Resources:

* Battle.net OAuth2 documentation:
    https://dev.battle.net/docs/read/oauth
* Battle.net API documentation:
    https://dev.battle.net/io-docs
* Original announcement:
    https://us.battle.net/en/forum/topic/13979297799
* The Battle.net API forum:
    https://us.battle.net/en/forum/15051532/
    )settings)get_adapter)OAuth2Error)OAuth2AdapterOAuth2CallbackViewOAuth2LoginViewc                   @   s(   e Zd ZdZdZdZdZdZdZdZ	dS )	RegionapaccneukrseatwusN)
__name__
__module____qualname__APACCNEUKRSEATWUS r   r   f/var/www/html/pos/venv/lib/python3.10/site-packages/allauth/socialaccount/providers/battlenet/views.pyr      s    r   c                 C   s   z|   }W n ty   td| j w | jdksd|v r>|ddp)|dd}|ddp5|dd}td||f d	|vrHtd
| |S )Nz$Invalid JSON from Battle.net API: %ri  error typeerror_descriptiondetailzBattle.net error: %s (%s)idz$Invalid data from Battle.net API: %r)json
ValueErrorr   textstatus_codeget)responsedatar   descr   r   r   _check_errors%   s   
r*   c                       s   e Zd ZdZdZejejejej	ej
ejejfZedd Zedd Zedd Zed	d
 Zedd Zdd Z fddZ  ZS )BattleNetOAuth2Adapterz
    OAuth2 adapter for Battle.net
    https://dev.battle.net/docs/read/oauth

    Region is set to us by default, but can be overridden with the
    `region` GET parameter when performing a login.
    Can be any of eu, us, kr, sea, tw or cn
    	battlenetc                 C   sb   | j jdd }|tjkrtjS || jv r|S tt	di di dd}|| jv r.|S tjS )Nregionr   SOCIALACCOUNT_PROVIDERSr,   REGIONr   )
requestGETr&   lowerr   r   r   valid_regionsgetattrr   selfr-   r   r   r   battlenet_regionZ   s   



z'BattleNetOAuth2Adapter.battlenet_regionc                 C   s   | j }|tjkr
dS dS )Nzhttps://oauth.battlenet.com.cnzhttps://oauth.battle.net)r7   r   r   r5   r   r   r   battlenet_base_urlp   s   
z)BattleNetOAuth2Adapter.battlenet_base_urlc                 C   
   | j d S )Nz/tokenr8   r6   r   r   r   access_token_urlw      
z'BattleNetOAuth2Adapter.access_token_urlc                 C   r9   )Nz
/authorizer:   r;   r   r   r   authorize_url{   r=   z$BattleNetOAuth2Adapter.authorize_urlc                 C   r9   )Nz	/userinfor:   r;   r   r   r   profile_url   r=   z"BattleNetOAuth2Adapter.profile_urlc                 K   sB   t   j| jdd|j id}t|}| j|d< |  ||S )Nauthorizationz	Bearer %s)headersr-   )	r   get_requests_sessionr&   r?   tokenr*   r7   get_providersociallogin_from_response)r6   r0   apprC   kwargsr'   r(   r   r   r   complete_login   s   
z%BattleNetOAuth2Adapter.complete_loginc                    sB   t t| ||}|jdd }|r|| jv r|d| 7 }|S )Nr-   r   z
?region=%s)superr+   get_callback_urlr1   r&   r2   r3   )r6   r0   rF   rr-   	__class__r   r   rJ      s
   z'BattleNetOAuth2Adapter.get_callback_url)r   r   r   __doc__provider_idr   r   r   r   r   r   r   r   r3   propertyr7   r8   r<   r>   r?   rH   rJ   __classcell__r   r   rL   r   r+   D   s.    	





r+   N)rN   django.confr   allauth.socialaccount.adapterr   -allauth.socialaccount.providers.oauth2.clientr   ,allauth.socialaccount.providers.oauth2.viewsr   r   r   r   r*   r+   adapter_viewoauth2_loginoauth2_callbackr   r   r   r   <module>   s    

X