U
    OEh                     @   s*   d dl T d dlmZ G dd dejZdS )    )*)cmf_auth_two_factorc                       sd   e Zd ZdZejjddg Zdd 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 }| j sF| }|| _|   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 |	 W  5 Q R  S Q R X 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)format)pyotpqrcode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BytesIOgetvalue)
selfr   r   r   r   ZsecreturlZqrZimgbuffer r   #./cmf/models/cmf_auth_two_factor.pytopt_qrcode_bytes
   s(    


$

z"CmfAuthTwoFactor.topt_qrcode_bytesc                 C   s    dd l }||  d}|S )Nr   zutf-8)base64Z	b64encoder   decode)r   r    Zbase64_imager   r   r   r   '   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_fields6   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)r2   r.   r"   r-   r/   r#   r0   r$   r   r1   r%   r   )r   argskwargsr(   r   r   r   9   s    zCmfAuthTwoFactor.savec                 C   s2   |    d| _d| _d| _d | _d | _|   d S )NF)r2   r/   r0   r1   r   r,   r   r'   r   r   r   r   G   s    zCmfAuthTwoFactor.reset)__name__
__module____qualname__Z	api_allowr   r   Zapi_methodsr   r   r&   r.   r2   r   r   __classcell__r   r   r(   r   r      s    r   N)Zcmf.includeZ
cmf.fieldsr   r   r   r   r   r   <module>   s   