U
    WEh	                     @   s.   d dl T d dlmZ G dd dejjeZdS )    )*)ResponseMixinc                   @   sp   e Zd ZdZejjdd Zejjdd Zejjdd Z	ejjdd	 Z
ejjd
d Zejjdd ZdS )TwoFactorViewz/two-factorc                 C   s   t  }tjtjkrtdS tjddddddg tjjjstjjjstjjj	stjj}tj
j}|d kr|dkpv|d	k}|dkp|d
k}|dkp|dk}nHtjdg tjjj}|dkp|d	k}|dkp|d
k}|dkp|dk}| dS | dS )N/two_factor.phone_verifiedtwo_factor.email_verifiedtwo_factor.application_verifiedtwo_factor.applicationtwo_factor.phonetwo_factor.emailanyZtotpemailZsmsztwo_factor.two_factor_optztwo_factor_settings.htmlztwo_factor.html)Zauth_base_hrefgcurrent_personZsystem_userZredirectload_fields
two_factorphone_verifiedemail_verifiedapplication_verifiedZglobal_settingsZtwo_factor_optZrender)selfZ	base_hrefr   Z
global_tfaZis_totp_auth_allowedZis_email_auth_allowedZis_sms_auth_allowedZ
person_tfa r   "./modules/auth/views/two_factor.pyget   s6    
  


zTwoFactorView.getc                 C   s,   t j  t jr| dt jS | dt jS )Nfailok)r   sessionZtfa_send_codeZalert_error_okauthr   r   r   r   	send_code%   s    
zTwoFactorView.send_codec              
   C   s   z@t jtjd tjdr.| dt jW S | dt jW S W n> tk
r~ } z t	  | t
|t j W Y S d }~X Y nX d S )Ncodemethodr   r   )r   r   Ztfa_check_coderequestjsonr   r   r   	ExceptionZrollback_purge_eventstr)r   er   r   r   
check_code,   s    zTwoFactorView.check_codec                 C   s   t jddddddg t jjjs6t jjjs6t jjjrB| ddS tj	
d	r`tj	
d	t jj_tj	
d
r~tj	
d
t jj_tj	
drtj	
dt jj_t jj  | dt jS )Nr   r   r   r	   r
   r   r   uK   Двухфакторная авторизация уже настроенаphoner   applicationr   )r   r   r   r   r   r   r   r   r#   r$   r   r)   r   r*   Zsaver   r   r   r   r   r   
update_2fa7   s     
  zTwoFactorView.update_2fac                 C   sD   t jjjr| ddS t jj }t|}|jdd d|j	_
|S )Nr   0   Приложение уже подключеноzContent-Typez	image/pngT)r   r   r   r   r   Ztopt_qrcode_bytesZmake_responseZheaderssetZcache_controlZno_cache)r   ZpngZresponser   r   r   qrcode_2fa_pngG   s    
zTwoFactorView.qrcode_2fa_pngc                 C   s"   t jjjr| ddS t jj S )Nr   r,   )r   r   r   r   r   Ztopt_qrcoder   r   r   r   
qrcode_2faQ   s    
zTwoFactorView.qrcode_2faN)__name__
__module____qualname__Z__url__cmfviewsactionr   r    r(   r+   r.   r/   r   r   r   r   r      s   





	r   N)Zcmf.includeZmodules.auth.views.responser   r3   r4   ZBaseViewr   r   r   r   r   <module>   s   