a
    ò,cÓ	  ã                   @   sô   d dl mZm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 d dlmZmZ d d	lmZ d d
lmZ G dd„ deƒZe
dgƒeeegƒeegƒddd„ƒƒƒZG dd„ deƒZG dd„ dejƒZdS )é    )ÚSessionAuthenticationÚBasicAuthentication)ÚIsAuthenticated)ÚResponse)ÚAPIView)Úapi_viewÚauthentication_classesÚpermission_classes)ÚToken)ÚObtainAuthToken)ÚUserÚGroup)Úauthentication)Ú
exceptionsc                   @   s$   e Zd ZeegZegZddd„ZdS )ÚTestAuthView0Nc                 C   s   t |jƒt |jƒdœ}t|ƒS ©N)ÚuserÚauth©Ústrr   r   r   )ÚselfÚrequestÚformatÚcontent© r   ú-/var/www/html/lartica/authentication/views.pyÚget   s    þzTestAuthView0.get)N)	Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r	   r   r   r   r   r   r      s   r   ÚGETNc                 C   s   t | jƒt | jƒdœ}t|ƒS r   r   )r   r   r   r   r   r   ÚTestAuthView'   s    þr!   c                   @   s   e Zd Zdd„ ZdS )ÚCustomAuthTokenc                 O   sR   | j |jd|id�}|jdd� |jd }tjj|d�\}}t|j|j	|j
dœƒS )Nr   )ÚdataÚcontextT)Úraise_exceptionr   )r   )ÚtokenÚuser_idÚemail)Zserializer_classr#   Úis_validZvalidated_datar
   ÚobjectsÚget_or_creater   ÚkeyÚpkr(   )r   r   ÚargsÚkwargsÚ
serializerr   r&   Úcreatedr   r   r   Úpost5   s    ÿ
ýzCustomAuthToken.postN)r   r   r   r2   r   r   r   r   r"   3   s   r"   c                   @   s   e Zd Zdd„ ZdS )ÚExampleAuthenticationc                 C   sN   |j  d¡}|sd S ztjj|d�}W n tjyD   t d¡‚Y n0 |d fS )NZHTTP_X_USERNAME)ÚusernamezNo such user)ÚMETAr   r   r*   ÚDoesNotExistr   ZAuthenticationFailed)r   r   r4   r   r   r   r   ÚauthenticateC   s    z"ExampleAuthentication.authenticateN)r   r   r   r7   r   r   r   r   r3   B   s   r3   )N)Zrest_framework.authenticationr   r   Zrest_framework.permissionsr   Zrest_framework.responser   Zrest_framework.viewsr   Zrest_framework.decoratorsr   r   r	   Úrest_framework.authtoken.modelsr
   Zrest_framework.authtoken.viewsr   Údjango.contrib.auth.modelsr   r   Úrest_frameworkr   r   r   r!   r"   ZBaseAuthenticationr3   r   r   r   r   Ú<module>   s"   
	