o
    Vg]                     @   s   d dl Z d dlZd dl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mZ d dlmZmZ G d	d
 d
eZdS )    N)settings)	AuthTokenloginTbl)HttpResponse)	dbsession)SQLAlchemyError)status
exceptions)get_authorization_headerBaseAuthenticationc                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 ZdS )
TokenAuthenticationNc                 C   s   t S )N)r   )self r   X/home/ubuntu/Idpwoa/Idpwoa_Backend/Idpwoa_Backend/userrestframeworkTokenAuthenticaion.py	get_model      zTokenAuthentication.get_modelc                 C   s   t  }t| }|r|d  dkrd S t|dkr#d}t|t|dkr0d}t|z|d }|dkr@d}t|W n tyO   d	}t|w |	  | 
|S )
Nr   s   bearer   z.Invalid token header. No credentials provided.   zInvalid token headernullzNull token not allowedzIInvalid token header. Token string should not contain invalid characters.)r   Sessionr
   splitlowerlenr	   AuthenticationFailedUnicodeErrorcloseauthenticate_credentials)r   requestsessionauthmsgtokenr   r   r   authenticate   s,   




z TokenAuthentication.authenticatec              
   C   s  t  }ztj|tjdgd}W n tjy/ } zt| |  ddd}t	
|d }~ww |r:|d }|d }dddd	}zz|ttj|ktj|k }W n$ tyx } ztd
| |  |  dddd	}t	
|d }~ww z|tj|d }W n ty } ztd| t	
dddd }~ww z|tj||d }	W n ty } zt| |  d }	W Y d }~nd }~ww d|	jd }
|
|kst	
|tj|d tj k rt	
dddW nE tjy   tddddd Y S  tjtjfy   tddddd Y S  ty8 } ztdddddW  Y d }~S d }~ww |  ||fS )NHS256)
algorithmsErrorzToken mismatch)responsemessageuser_idexpiry401)r&   r'   r   zToken Not Exitz!Token mismatch or Token not found)useridzUser not ExitzToken is invalid)r(   key    zutf-8z%Y-%m-%dzToken Expired.zToken is expired403)r   zInternal server error500)r   r   jwtdecoder   
SECRET_KEYDecodeErrorprintrollbackr	   r   queryr   filterr(   r,   oner   r   r   	filter_byencodedatetimestrptimedatetodayExpiredSignatureErrorr   InvalidTokenError)r   r!   r   payloader    r(   r)   user
auth_tokenstored_tokenr   r   r   r   *   sp   

&



z,TokenAuthentication.authenticate_credentialsc                 C   s   dS )NBearerr   )r   r   r   r   r   authenticate_headera   r   z'TokenAuthentication.authenticate_header)__name__
__module____qualname__modelr   r"   r   rG   r   r   r   r   r      s    7r   )r0   jsonr;   django.confr   idpwoa.modelsr   r   django.httpr   Idpwoa_Backendr   sqlalchemy.excr   rest_frameworkr   r	   rest_framework.authenticationr
   r   r   r   r   r   r   <module>   s    