o
    sh2                     @  s   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
 d dlmZmZ d d	lmZ d d
lmZmZmZmZmZmZ d dlZd dlZeeZG dd dZG dd deZG dd dZG dd deZ dS )    )annotations   )cbor)	CtapError   )Ctap2Info)PinProtocol)IntEnumunique)Event)OptionalCallableMappingAnyTupleDictNc                   @  sj   e Zd ZeG dd deZeG dd deZeG dd deZedddZ	dddZ
dddZdS )BioEnrollmentc                   @  (   e Zd ZdZdZdZdZdZdZdZ	dS )	zBioEnrollment.RESULTr   r                  N)
__name__
__module____qualname__MODALITYFINGERPRINT_KINDMAX_SAMPLES_REQUIREDTEMPLATE_IDLAST_SAMPLE_STATUSREMAINING_SAMPLESTEMPLATE_INFOS r$   r$   J/var/www/html/env_mimamsha/lib/python3.10/site-packages/fido2/ctap2/bio.pyRESULT-       r&   c                   @  s   e Zd ZdZdZdS )zBioEnrollment.TEMPLATE_INFOr   r   N)r   r   r   IDNAMEr$   r$   r$   r%   TEMPLATE_INFO7   s    r*   c                   @  s   e Zd ZdZdS )zBioEnrollment.MODALITYr   N)r   r   r   FINGERPRINTr$   r$   r$   r%   r   <   s    r   infor   returnboolc                 C  s*   d| j v rdS d| jv rd| j v rdS dS )N	bioEnrollTFIDO_2_1_PREuserVerificationMgmtPreviewF)optionsversions)r,   r$   r$   r%   is_supported@   s   


zBioEnrollment.is_supportedctapr   modalityc                 C  sB   |  |js
td|| _|  | _|| jkrtd|dd S )Nz(Authenticator does not support BioEnrollzDevice does not support s)r4   r,   
ValueErrorr5   get_modalityr6   )selfr5   r6   r$   r$   r%   __init__L   s   

zBioEnrollment.__init__intc                 C  s   | j jddtjj S )zaGet bio modality.

        :return: The type of modality supported by the authenticator.
        T)r9   )r5   bio_enrollmentr   r&   r   r:   r$   r$   r%   r9   U   s   zBioEnrollment.get_modalityN)r,   r   r-   r.   )r5   r   r6   r   )r-   r<   )r   r   r   r   r
   r&   r*   r   staticmethodr4   r;   r9   r$   r$   r$   r%   r   ,   s    	
	r   c                      s   e Zd Zd fddZ  ZS )CaptureErrorcoder<   c                   s   || _ t d|  d S )NzFingerprint capture error: )rA   superr;   )r:   rA   	__class__r$   r%   r;   `   s   zCaptureError.__init__)rA   r<   )r   r   r   r;   __classcell__r$   r$   rC   r%   r@   _   s    r@   c                   @  s6   e Zd ZdZddddZ		ddddZdddZdS )FPEnrollmentContextaj  Helper object to perform fingerprint enrollment.

    :param bio: An instance of FPBioEnrollment.
    :param timeout: Optional timeout for fingerprint captures (ms).
    :ivar remaining: The number of (estimated) remaining samples needed.
    :ivar template_id: The ID of the new template (only available after the initial
        sample has been captured).
    Nbio'FPBioEnrollment'timeoutOptional[int]c                 C  s   || _ || _d | _d | _d S N)_biorI   template_id	remaining)r:   rG   rI   r$   r$   r%   r;   o   s   
zFPEnrollmentContext.__init__eventOptional[Event]on_keepaliveOptional[Callable[[int], None]]r-   Optional[bytes]c                 C  sl   | j du r| j| j||\| _ }| _n| j| j | j||\}| _|tjjkr,t	|| jdkr4| j S dS )aT  Capture a fingerprint sample.

        This call will block for up to timeout milliseconds (or indefinitely, if
        timeout not specified) waiting for the user to scan their fingerprint to
        collect one sample.

        :return: None, if more samples are needed, or the template ID if enrollment is
            completed.
        Nr   )
rM   rL   enroll_beginrI   rN   enroll_capture_nextFPBioEnrollmentFEEDBACKFP_GOODr@   )r:   rO   rQ   statusr$   r$   r%   captureu   s   


zFPEnrollmentContext.captureNonec                 C  s   | j   d| _dS )zCancels ongoing enrollment.N)rL   enroll_cancelrM   r>   r$   r$   r%   cancel   s   

zFPEnrollmentContext.cancelrK   )rG   rH   rI   rJ   )NN)rO   rP   rQ   rR   r-   rS   r-   r[   )r   r   r   __doc__r;   rZ   r]   r$   r$   r$   r%   rF   e   s    	rF   c                      s   e Zd ZdZeG dd deZeG dd deZeG dd deZd4 fddZ	d5ddZ
d6ddZ			d7d8dd Z			d7d9d#d$Zd:d&d'Zd;d<d)d*Zd=d,d-Zd>d0d1Zd?d2d3Z  ZS )@rV   a  Implementation of a draft specification of the bio enrollment API.
    WARNING: This specification is not final and this class is likely to change.

    NOTE: The get_fingerprint_sensor_info method does not require authentication, and
    can be used by setting pin_uv_protocol and pin_uv_token to None.

    :param ctap: An instance of a CTAP2 object.
    :param pin_uv_protocol: The PIN/UV protocol version used.
    :param pin_uv_token: A valid PIN/UV Auth Token for the current CTAP session.
    c                   @  r   )	zFPBioEnrollment.CMDr   r   r   r   r   r   r   N)
r   r   r   ENROLL_BEGINENROLL_CAPTURE_NEXTENROLL_CANCELENUMERATE_ENROLLMENTSSET_NAMEREMOVE_ENROLLMENTGET_SENSOR_INFOr$   r$   r$   r%   CMD   r'   rg   c                   @  s   e Zd ZdZdZdZdS )zFPBioEnrollment.PARAMr   r   r   N)r   r   r   r    TEMPLATE_NAME
TIMEOUT_MSr$   r$   r$   r%   PARAM   s    rj   c                   @  sP   e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdZdZdZdZdd ZdS )zFPBioEnrollment.FEEDBACKr   r   r   r   r   r   r   r      	   
               c                 C  s   d| j dd| j S )N0x02Xz - )valuenamer>   r$   r$   r%   __str__   s   z FPBioEnrollment.FEEDBACK.__str__N)r   r   r   rX   FP_TOO_HIGH
FP_TOO_LOWFP_TOO_LEFTFP_TOO_RIGHTFP_TOO_FASTFP_TOO_SLOWFP_POOR_QUALITYFP_TOO_SKEWEDFP_TOO_SHORTFP_MERGE_FAILURE	FP_EXISTSFP_DATABASE_FULLNO_USER_ACTIVITYNO_UP_TRANSITIONrv   r$   r$   r$   r%   rW      s"    rW   r5   r   pin_uv_protocolr	   pin_uv_tokenbytesc                   s"   t  |tjj || _|| _d S rK   )rB   r;   r   r   r+   r   r   )r:   r5   r   r   rC   r$   r%   r;      s   
zFPBioEnrollment.__init__NTc                 C  sn   | j ||||d}|r.td| j |}|d ur|t|7 }| jj|d< | j| j||d< | j	j
di |S )N)r6   sub_cmdsub_cmd_paramsrO   rQ   z>BBr   pin_uv_paramr$   )r6   structpackr   encoder   VERSIONauthenticater   r5   r=   )r:   r   paramsauthrO   rQ   kwargsmsgr$   r$   r%   _call   s   zFPBioEnrollment._callr-   Mapping[int, Any]c                 C  s   | j tjjddS )ztGet fingerprint sensor info.

        :return: A dict containing FINGERPRINT_KIND and MAX_SAMPLES_REQUIRES.
        Fr   )r   rV   rg   rf   r>   r$   r$   r%   get_fingerprint_sensor_info   s   z+FPBioEnrollment.get_fingerprint_sensor_inforI   rJ   rO   rP   rQ   rR   +Tuple[bytes, FPBioEnrollment.FEEDBACK, int]c                 C  st   t d| d | jtjj|durtjj|ind||d}t d|  |tj	j
 t|tj	j |tj	j fS )a  Start fingerprint enrollment.

        Starts the process of enrolling a new fingerprint, and will wait for the user
        to scan their fingerprint once to provide an initial sample.

        :param timeout: Optional timeout in milliseconds.
        :return: A tuple containing the new template ID, the sample status, and the
            number of samples remaining to complete the enrollment.
        z)Starting fingerprint enrollment (timeout=)NrO   rQ   Sample capture result: )loggerdebugr   rV   rg   r`   rj   ri   r   r&   r    rW   r!   r"   )r:   rI   rO   rQ   resultr$   r$   r%   rT      s   


zFPBioEnrollment.enroll_beginrM   $Tuple[FPBioEnrollment.FEEDBACK, int]c                 C  sv   t d| d tjj|i}|dur||tjj< | jtjj|||d}t d|  t	|t
jj |t
jj fS )a  Continue fingerprint enrollment.

        Continues enrolling a new fingerprint and will wait for the user to scan their
        fingerpring once to provide a new sample.
        Once the number of samples remaining is 0, the enrollment is completed.

        :param template_id: The template ID returned by a call to `enroll_begin`.
        :param timeout: Optional timeout in milliseconds.
        :return: A tuple containing the sample status, and the number of samples
            remaining to complete the enrollment.
        z$Capturing next sample with (timeout=r   Nr   r   )r   r   rV   rj   r    ri   r   rg   ra   rW   r   r&   r!   r"   )r:   rM   rI   rO   rQ   r   r   r$   r$   r%   rU     s   
z#FPBioEnrollment.enroll_capture_nextr[   c                 C  s    t d | jtjjdd dS )z*Cancel any ongoing fingerprint enrollment.z"Cancelling fingerprint enrollment.Fr   N)r   r   r   rV   rg   rb   r>   r$   r$   r%   r\   )  s   
zFPBioEnrollment.enroll_cancelrF   c                 C  s
   t | |S )zConvenience wrapper for doing fingerprint enrollment.

        See FPEnrollmentContext for details.
        :return: An initialized FPEnrollmentContext.
        )rF   )r:   rI   r$   r$   r%   enroll.  s   
zFPBioEnrollment.enrollMapping[bytes, Optional[str]]c              
   C  s\   zdd |  tjjtjj D W S  ty- } z|jtj	j
kr(i W  Y d}~S  d}~ww )zGet a dict of enrolled fingerprint templates which maps template ID's to
        their friendly names.

        :return: A dict of enrolled template_id -> name pairs.
        c                 S  s"   i | ]}|t jj |t jj qS r$   )r   r*   r(   r)   ).0tr$   r$   r%   
<dictcomp>=  s    z9FPBioEnrollment.enumerate_enrollments.<locals>.<dictcomp>N)r   rV   rg   rc   r   r&   r#   r   rA   ERRINVALID_OPTION)r:   er$   r$   r%   enumerate_enrollments6  s   z%FPBioEnrollment.enumerate_enrollmentsru   strc                 C  sH   t d|  d|  | tjjtjj	|tjj
|i t d dS )zSet/Change the friendly name of a previously enrolled fingerprint template.

        :param template_id: The ID of the template to change.
        :param name: A friendly name to give the template.
        zChanging name of template: z to zFingerprint template renamedN)r   r   hexr   rV   rg   rd   r   r*   r(   r)   r,   )r:   rM   ru   r$   r$   r%   set_nameH  s   zFPBioEnrollment.set_namec                 C  s:   t d|   | tjjtjj	|i t 
d dS )zzRemove a previously enrolled fingerprint template.

        :param template_id: The Id of the template to remove.
        zDeleting template: zFingerprint template deletedN)r   r   r   r   rV   rg   re   r   r*   r(   r,   )r:   rM   r$   r$   r%   remove_enrollmentX  s   
z!FPBioEnrollment.remove_enrollment)r5   r   r   r	   r   r   )NTNN)r-   r   )NNN)rI   rJ   rO   rP   rQ   rR   r-   r   )
rM   r   rI   rJ   rO   rP   rQ   rR   r-   r   r^   rK   )rI   rJ   r-   rF   )r-   r   )rM   r   ru   r   r-   r[   )rM   r   r-   r[   )r   r   r   r_   r   r
   rg   rj   rW   r;   r   r   rT   rU   r\   r   r   r   r   rE   r$   r$   rC   r%   rV      s0    	

	$
"

rV   )!
__future__r    r   r5   r   baser   r   pinr	   enumr
   r   	threadingr   typingr   r   r   r   r   r   r   logging	getLoggerr   r   r   	Exceptionr@   rF   rV   r$   r$   r$   r%   <module>   s    
32