B
    b\
                 @   s   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 d dlmZ d dlmZ d d	lmZ ejZG d
d deZG dd deZG dd de	ZdS )    )unicode_literals)gettext_lazy)settings)TokenAuthenticationSessionAuthentication)JSONWebTokenAuthentication)
exceptions)api_settings)
StaffToken)Employeec               @   s   e Zd ZdZdZeZdS )StaffTokenAuthenticationa  
    Simple token based authentication.

    Clients should authenticate by passing the token key in the "Authorization"
    HTTP header, prepended with the string "Token ".  For example:

        Authorization: Token 401f7ac837da42b97f613d789819ff93537bee6a
    TokenN)__name__
__module____qualname____doc__keywordr
   Zmodel r   r   >G:\easytimepro\master/mysite/staff\api\staff_authentication.pyr      s   r   c               @   s   e Zd Zdd ZdS )StaffJSONWebTokenAuthenticationc             C   st   t |}|std}t|ytjj|d}W n& tk
rV   td}t|Y nX |jsptd}t||S )zV
        Returns an active user that matches the payload's user id and email.
        zInvalid payload.)Zemp_codezInvalid signature.zUser account is disabled.)	jwt_get_username_from_payload_r   ZAuthenticationFailedr   Zobjectsget	Exception	is_active)selfZpayloadZusernamemsguserr   r   r   authenticate_credentials!   s    

z8StaffJSONWebTokenAuthentication.authenticate_credentialsN)r   r   r   r   r   r   r   r   r      s   r   c               @   s   e Zd Zdd ZdS )StaffSessionAuthenticationc             C   sh   t |jdd}|r|jsdS |jdkrVy|jdd|jtj< W n t	k
rT   Y nX | 
| |dfS )z{
        Returns a `User` if the request session currently has a logged in user.
        Otherwise returns `None`.
        r   N)ZPUTZPATCHZDELETEZcsrfmiddlewaretoken )getattrZ_requestr   methodZPOSTr   ZMETAr   ZCSRF_HEADER_NAMEr   Zenforce_csrf)r   Zrequestr   r   r   r   authenticate:   s    


z'StaffSessionAuthentication.authenticateN)r   r   r   r#   r   r   r   r   r   8   s   r   N)Z
__future__r   Zdjango.utils.translationr   r   Zdjango.confr   Zrest_framework.authenticationr   r   Z!rest_framework_jwt.authenticationr   Zrest_frameworkr   Zrest_framework_jwt.settingsr	   Zmysite.staff.models.staff_tokenr
   Z&mysite.personnel.models.model_employeer   Z JWT_PAYLOAD_GET_USERNAME_HANDLERr   r   r   r   r   r   r   r   <module>   s   