U
    ÔÉ>i‰  ã                   @   sx   d dl 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
 d dlmZ G dd	„ d	ejƒZG d
d„ dejƒZdS )é    N)ÚHttpRequest)Úauthentication)ÚAuthenticationFailed)Úapp_settings)Úauthenticate_by_x_session_token)Úvalidate_access_tokenc                   @   s4   e Zd ZdZedœdd„Zeeje dœdd„Z	dS )	ÚXSessionTokenAuthenticationzz
    This authentication class uses the X-Session-Token that django-allauth
    is using for authentication purposes.
    )Úrequestc                 C   s   |   |¡}|rt|ƒS d S )N)Úget_session_tokenr   )Úselfr	   Útoken© r   úZ/tmp/pip-unpacked-wheel-upujnpc2/allauth/headless/contrib/rest_framework/authentication.pyÚauthenticate   s    
z(XSessionTokenAuthentication.authenticate)r	   Úreturnc                 C   s   |j  d¡S )z×
        Returns the session token for the given request, by looking up the
        ``X-Session-Token`` header. Override this if you want to extract the token
        from e.g. the ``Authorization`` header.
        zX-Session-Token)ÚheadersÚget)r   r	   r   r   r   r
      s    z-XSessionTokenAuthentication.get_session_tokenN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   ÚtypingÚOptionalÚstrr
   r   r   r   r   r      s   r   c                   @   s,   e Zd Zeedœdd„ƒZedœdd„ZdS )ÚJWTTokenAuthentication)r   c                 C   s   t jS )zM
        See: ``settings.HEADLESS_JWT_AUTHORIZATION_HEADER_SCHEME``.
        )r   ZJWT_AUTHORIZATION_HEADER_SCHEME)r   r   r   r   Úkeyword$   s    zJWTTokenAuthentication.keyword)Úkeyc                 C   s   t |ƒ}|dkrtdƒ‚|S )z3
        Validates the given access token.
        NzInvalid token.)r   r   )r   r   Zuser_payloadr   r   r   Úauthenticate_credentials+   s    z/JWTTokenAuthentication.authenticate_credentialsN)r   r   r   Úpropertyr   r   r   r   r   r   r   r   "   s   r   )r   Zdjango.httpr   Zrest_frameworkr   Zrest_framework.exceptionsr   Zallauth.headlessr   Z$allauth.headless.internal.sessionkitr   Z/allauth.headless.tokens.strategies.jwt.internalr   ZBaseAuthenticationr   ZTokenAuthenticationr   r   r   r   r   Ú<module>   s   