U
    yd                     @   sn   d dl mZ d dlT ddlmZ d dlmZmZmZmZmZ d dl	m
Z
 d dlZd dlZG dd	 d	ejZdS )
    )fields)*   )cmf_session)datedatetime	timedeltatimezonetime)AESNc                   @   s   e Zd Zi Zeeje	j
ddgZeed< ejf eZedddZeedddZd	d
 Zdd ZeedddZdS )
CmfSessioncmfZ	templatesloader)returnc                 C   s   | j jdd }t|S )N:   )idvaluesplitcmfutilZcrypt)selfmessage r   ./cmf/models/cmf_session.py	get_token   s    zCmfSession.get_tokenc                 C   s   zt |}W n   td Y dS X tjjd| ddgd}|rtt	j
}||jt|jjd kr|dg |j|}|rtjd	d
}|jj|_|jj|_|jj|_d|_d	|_td|j d|j d|j  ||fS dS )NuD   Не удалось расшифровать токен сессии)NNzCmfSession:Fr   )r   Zexpiredr   )Zsecondszplugin.plugin.*T)emptyzfrom_session_token: jwt is ok, z, z, is_local=)r   decryptgdebugmodelsr   getr   nowr	   ZutcZreauth_dater   Zaccess_token_expires_inr   Zload_fieldsZpluginZrefresh_tokenZCmfAuthZ
user_loginZloginZ
user_emailemailZ
user_scopeZscopeZjwt_is_supportZjwt_is_match_org)clsZsession_tokenZdecrypted_tokensessionr!   objr   r   r   
from_token   s*    



"zCmfSession.from_tokenc                    s0  dd l  tjj| jddgd}| }|r,| jrN| j | j  dk rNd S d	 fddt
d	D }|d
ksz|dkr|jjr|| _| j  |   tj }||jjj| d nn|d
ks|dkr,|jjr,|| _| j  |   | jd}|j||d}tj }|j|jjj|dd d S )Nr   two_factor.*r   r   r       c                 3   s   | ]}t  d dV  qdS )r   	   N)strZrandint).0_randomr   r   	<genexpr>5   s     z+CmfSession.tfa_send_code.<locals>.<genexpr>   anyZsmsu&    ваш проверочный код.r"   ztfa_mail.html)tfa_codepersonuJ   Код безопасности для учетной записи EvaTeam)Zsubject)r0   r   	CmfPersonr    user_idget_two_factor_optZsms_send_timer!   Ztotal_secondsjoinrange
two_factorZphonesms_codeset_nowsaveZCmfPluginSMSGateZget_local_smsgateZsend_messageZnumberr"   
_jinja_envZget_templateZrenderZCmfPluginMailBoxZget_local_mailboxr   )r   r5   Ztwo_factor_optr4   Zsms_gatetemplateZ
email_bodyZmail_boxr   r/   r   tfa_send_code-   s*    



zCmfSession.tfa_send_codec                 C   s   ddl m} | jrd S tjj| jd}| rdtj	i}||}t
tj d| }tjdd|jd< d	|jd
< d|jd< |S d S )Nr   )	urlencode)r   Znext_urlz/auth/two-factor?ZOriginr   zAccess-Control-Allow-Origintruez Access-Control-Allow-CredentialszContent-Type, x-ijtzAccess-Control-Allow-Headers)Zurllib.parserB   checkedr   r6   r    r7   r8   ZrequestZurlZredirectconfigZAUTH_SERVER_URLZheaders)r   rB   r5   paramsZqsrr   r   r   tfa_check_two_factorE   s    


zCmfSession.tfa_check_two_factor)codec              	   C   s   dd l }tj jd7  _tjj  tjj| kr:dtj_nXtj	j
tjjddgd}|jjrt  ||jj }W 5 Q R X || rdtj_tj  tjjjS )Nr   r   Tr'   r   r(   )pyotpr   r$   Zsms_try_countZsms_try_timer=   r<   rD   r   r6   r    r7   r;   Ztopt_secretr   Zdisable_aclZTOTPr   Zverifyr>   r   )rI   rJ   r5   Ztotpr   r   r   tfa_check_codeS   s    



zCmfSession.tfa_check_codeN)__name__
__module____qualname__Z_optionsZjinja2ZFileSystemLoaderospathr9   rE   ZPROJECT_DIRZ_loaderZEnvironmentr?   r,   r   classmethodr&   rA   rH   staticmethodrK   r   r   r   r   r      s   r   )r   r   Zcmf.includer   r   r   r   r	   r
   ZCrypto.Cipherr   Zbinasciibase64r   r   r   r   r   <module>   s   