o
    RDi                     @   sD   d dl mZ d dlmZ d dlmZ ddlmZ G dd deZdS )	    )OrderedDict)SuspiciousOperation)BaseAuthentication   )get_oauthlib_corec                   @   s,   e Zd ZdZdZdd Zdd Zdd Zd	S )
OAuth2AuthenticationzE
    OAuth 2 authentication backend using `django-oauth-toolkit`
    apic                 C   s   d dd | D S )zZ
        Return a string of comma-separated key-value pairs (e.g. k="v",k2="v2").
        ,c                 S   s   g | ]\}}d j ||dqS )z	{k}="{v}")kv)format).0r
   r    r   n/var/www/Datamplify/venv/lib/python3.10/site-packages/oauth2_provider/contrib/rest_framework/authentication.py
<listcomp>   s    z8OAuth2Authentication._dict_to_string.<locals>.<listcomp>)joinitems)selfmy_dictr   r   r   _dict_to_string   s   z$OAuth2Authentication._dict_to_stringc              
   C   sz   |du rdS t  }z|j|g d\}}W n ty+ } zt|dkr&t| d}~ww |r4|j|jfS t|di |_dS )zk
        Returns two-tuple of (user, token) if authentication succeeds,
        or None otherwise.
        N)scopesz%Invalid hex encoding in query string.oauth2_error)	r   verify_request
ValueErrorstrr   useraccess_tokengetattrr   )r   requestoauthlib_corevalidrerrorr   r   r   authenticate   s   z!OAuth2Authentication.authenticatec                 C   s8   t d| jfg}t|di }|| dj| |dS )z=
        Bearer is the only finalized type currently
        realmr   zBearer {attributes})
attributes)r   www_authenticate_realmr   updater   r   )r   r   www_authenticate_attributesr   r   r   r   authenticate_header*   s   
z(OAuth2Authentication.authenticate_headerN)__name__
__module____qualname____doc__r&   r   r#   r)   r   r   r   r   r   	   s    r   N)	collectionsr   django.core.exceptionsr   rest_framework.authenticationr   oauth2_backendsr   r   r   r   r   r   <module>   s
    