U
    ImhD                     @   s*   d dl T d dlmZ G dd dejZdS )    )*)cmf_auth_two_factorc                       s\   e Zd ZdZejjddg Zdd Z fddZdd	 Z	d
d Z
 fddZdd Z  ZS )CmfAuthTwoFactorTtopt_qrcoderesetc              	   C   s   dd l }dd l}dd l}dd l}dd l}d }| j sN| }|| _|   nt	
  | j }W 5 Q R X | dg d|j| jd jj d| d}|jdddd	}|| |jd
d |jddd}	| }
|	j|
dd ||
 d}|S )Nr   zpersons.loginzotpauth://totp/EvaTeam:z?secret=z&issuer=EvaTeam   
      )versionZbox_sizeZborderT)ZfitZblackZwhite)Z
fill_colorZ
back_colorZPNG)formatzutf-8)pyotpqrcodebase64iourllibtopt_secretloadZrandom_base32saveZcmfutilZdisable_aclZdecryptload_fieldsparsequotepersonsZloginvalueZQRCodeZadd_dataZmakeZ
make_imageBytesIOZ	b64encodegetvaluedecode)selfr   r   r   r   r   ZsecreturlZqrZimgbufferZbase64_image r   #./cmf/models/cmf_auth_two_factor.pyr   
   s,    


$
zCmfAuthTwoFactor.topt_qrcodec                    s   t   ddddg S )Nphoneemailapplicationr   )supersave_preload_fieldsr   	__class__r   r    r%   )   s    z$CmfAuthTwoFactor.save_preload_fieldsc                 C   sF   t jrt jjs| jr| jjs d S t j| j krBdt j_t j  d S )NT)	gZsessioncheckedtwo_factor_opt
is_changedZcurrent_personr   r   r   r&   r   r   r    _calc_session,   s
    zCmfAuthTwoFactor._calc_sessionc                 C   s   |  dddddg d S )Nphone_verifiedemail_verifiedapplication_verifiedr   r+   )r   r&   r   r   r    _load_2fa_fields3   s    z!CmfAuthTwoFactor._load_2fa_fieldsc                    sb   |    |   | jjrd| _| jjr,d| _| jjs<| jjrBd| _	| j	rT| jsTd| _t
 j||S )NFZtopt)r1   r-   r!   r,   r.   r"   r/   r#   r   r0   r$   r   )r   argskwargsr'   r   r    r   6   s    zCmfAuthTwoFactor.savec                 C   s2   |    d| _d| _d| _d | _d | _|   d S )NF)r1   r.   r/   r0   r   r+   r   r&   r   r   r    r   D   s    zCmfAuthTwoFactor.reset)__name__
__module____qualname__Z	api_allowr   r   Zapi_methodsr   r%   r-   r1   r   r   __classcell__r   r   r'   r    r      s    r   N)Zcmf.includeZ
cmf.fieldsr   r   r   r   r   r    <module>   s   