o
    sh$                     @  s   d dl mZ ddlmZ ddlmZ ddlmZmZm	Z	 ddl
mZmZ ddlmZmZ d d	lmZmZ d d
lmZmZmZ d dlZd dlZeeZG dd dZdS )    )annotations   )cbor)	CtapError)PublicKeyCredentialDescriptorPublicKeyCredentialUserEntity_as_cbor   )Ctap2Info)PinProtocol_PinUv)IntEnumunique)MappingSequenceAnyNc                   @  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e	d6ddZ
e	d6ddZd7ddZd8ddZd9ddZd9dd Zd9d!d"Zd:d$d%Zd;d'd(Zd9d)d*Zd:d+d,Zd<d0d1Zd=d4d5ZdS )>CredentialManagementaa  Implementation of a draft specification of the Credential Management API.
    WARNING: This specification is not final and this class is likely to change.

    :param ctap: An instance of a CTAP2 object.
    :param pin_uv_protocol: An instance of a PinUvAuthProtocol.
    :param pin_uv_token: A valid PIN/UV Auth Token for the current CTAP session.
    c                   @  s(   e Zd ZdZdZdZdZdZdZdZ	dS )	zCredentialManagement.CMDr	   r                  N)
__name__
__module____qualname__GET_CREDS_METADATAENUMERATE_RPS_BEGINENUMERATE_RPS_NEXTENUMERATE_CREDS_BEGINENUMERATE_CREDS_NEXTDELETE_CREDENTIALUPDATE_USER_INFO r#   r#   N/var/www/html/env_mimamsha/lib/python3.10/site-packages/fido2/ctap2/credman.pyCMD:   s    r%   c                   @  s   e Zd ZdZdZdZdS )zCredentialManagement.PARAMr	   r   r   N)r   r   r   
RP_ID_HASHCREDENTIAL_IDUSERr#   r#   r#   r$   PARAMD   s    r)   c                   @  s8   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S )zCredentialManagement.RESULTr	   r   r   r   r   r   r      	   
      N)r   r   r   EXISTING_CRED_COUNTMAX_REMAINING_COUNTRPr&   	TOTAL_RPSr(   r'   
PUBLIC_KEYTOTAL_CREDENTIALSCRED_PROTECTLARGE_BLOB_KEYr#   r#   r#   r$   RESULTJ   s    r6   infor   returnboolc                 C  s,   | j drdS d| jv rd| j v rdS dS )NcredMgmtTFIDO_2_1_PREcredentialMgmtPreviewF)optionsgetversionsr7   r#   r#   r$   is_supportedX   s
   z!CredentialManagement.is_supportedc                 C  s   t | jdS )Nr:   )r9   r=   r>   r@   r#   r#   r$   is_update_supporteda   s   z(CredentialManagement.is_update_supportedctapr
   pin_uv_protocolr   pin_uv_tokenbytesc                 C  s*   |  |js
td|| _t||| _d S )Nz4Authenticator does not support Credential Management)rA   r7   
ValueErrorrC   r   pin_uv)selfrC   rD   rE   r#   r#   r$   __init__f   s   zCredentialManagement.__init__NTc                 C  sh   ||d}|r+t d|}|d ur|t|7 }| jjj|d< | jj| jj||d< | j	j
di |S )N)sub_cmdsub_cmd_paramsz>BrD   pin_uv_paramr#   )structpackr   encoderH   protocolVERSIONauthenticatetokenrC   credential_mgmt)rI   rK   paramsauthkwargsmsgr#   r#   r$   _callr   s   
zCredentialManagement._callMapping[int, Any]c                 C     |  tjjS )aS  Get credentials metadata.

        This returns the existing resident credentials count, and the max
        possible number of remaining resident credentials (the actual number of
        remaining credentials may depend on algorithm choice, etc).

        :return: A dict containing EXISTING_CRED_COUNT, and MAX_REMAINING_COUNT.
        )rZ   r   r%   r   rI   r#   r#   r$   get_metadata~   s   	z!CredentialManagement.get_metadatac                 C  r\   )a   Start enumeration of RP entities of resident credentials.

        This will begin enumeration of stored RP entities, returning the first
        entity, as well as a count of the total number of entities stored.

        :return: A dict containing RP, RP_ID_HASH, and TOTAL_RPS.
        )rZ   r   r%   r   r]   r#   r#   r$   enumerate_rps_begin   s   z(CredentialManagement.enumerate_rps_beginc                 C     | j tjjddS )zGet the next RP entity stored.

        This continues enumeration of stored RP entities, returning the next
        entity.

        :return: A dict containing RP, and RP_ID_HASH.
        FrW   )rZ   r   r%   r   r]   r#   r#   r$   enumerate_rps_next      z'CredentialManagement.enumerate_rps_nextSequence[Mapping[int, Any]]c              
     s   z   }W n ty" } z|jtjjkrg W  Y d}~S  d}~ww |tjj }|dkr/g S  fddtd|D }|g| S )zvConvenience method to enumerate all RPs.

        See enumerate_rps_begin and enumerate_rps_next for details.
        Nr   c                      g | ]}   qS r#   )rb   .0_r]   r#   r$   
<listcomp>   s    z6CredentialManagement.enumerate_rps.<locals>.<listcomp>r	   )	r_   r   codeERRNO_CREDENTIALSr   r6   r1   range)rI   firsten_rpsrestr#   r]   r$   enumerate_rps   s   
z"CredentialManagement.enumerate_rps
rp_id_hashc                 C  s   |  tjjtjj|iS )a  Start enumeration of resident credentials.

        This will begin enumeration of resident credentials for a given RP,
        returning the first credential, as well as a count of the total number
        of resident credentials stored for the given RP.

        :param rp_id_hash: SHA256 hash of the RP ID.
        :return: A dict containing USER, CREDENTIAL_ID, PUBLIC_KEY, and
            TOTAL_CREDENTIALS.
        )rZ   r   r%   r   r)   r&   )rI   rs   r#   r#   r$   enumerate_creds_begin   s   
z*CredentialManagement.enumerate_creds_beginc                 C  r`   )zGet the next resident credential stored.

        This continues enumeration of resident credentials, returning the next
        credential.

        :return: A dict containing USER, CREDENTIAL_ID, and PUBLIC_KEY.
        Fra   )rZ   r   r%   r    r]   r#   r#   r$   enumerate_creds_next   rc   z)CredentialManagement.enumerate_creds_nextc              
     s|   z
 j |i |}W n ty& } z|jtjjkr!g W  Y d}~S  d}~ww  fddtd|tjj	dD }|g| S )zConvenience method to enumerate all resident credentials for an RP.

        See enumerate_creds_begin and enumerate_creds_next for details.
        Nc                   re   r#   )ru   rf   r]   r#   r$   ri      s    z8CredentialManagement.enumerate_creds.<locals>.<listcomp>r	   )
rt   r   rj   rk   rl   rm   r>   r   r6   r3   )rI   argsrX   rn   ro   rq   r#   r]   r$   enumerate_creds   s   

z$CredentialManagement.enumerate_credscred_idr   Nonec                 C  sD   t |}td|  | tjjtjj	t
|i td dS )z~Delete a resident credential.

        :param cred_id: The PublicKeyCredentialDescriptor of the credential to delete.
        zDeleting credential with ID: zCredential deletedN)r   	from_dictloggerdebugrZ   r   r%   r!   r)   r'   r   r7   )rI   rx   r#   r#   r$   delete_cred   s   
z CredentialManagement.delete_cred	user_infor   c                 C  sv   t | jjstdt|}t|}t	d| d|  | 
t jjt jjt|t jjt|i td dS )zUpdate the user entity of a resident key.

        :param cred_id: The PublicKeyCredentialDescriptor of the credential to update.
        :param user_info: The user info update.
        z/Authenticator does not support update_user_infozUpdating credential: z with user info: zCredential user info updatedN)r   rB   rC   r7   rG   r   rz   r   r{   r|   rZ   r%   r"   r)   r'   r   r(   )rI   rx   r~   r#   r#   r$   update_user_info   s   


z%CredentialManagement.update_user_info)r7   r   r8   r9   )rC   r
   rD   r   rE   rF   )NT)r8   r[   )r8   rd   )rs   rF   r8   r[   )rx   r   r8   ry   )rx   r   r~   r   r8   ry   )r   r   r   __doc__r   r   r%   r)   r6   staticmethodrA   rB   rJ   rZ   r^   r_   rb   rr   rt   ru   rw   r}   r   r#   r#   r#   r$   r   1   s.    	












r   )
__future__r    r   rC   r   webauthnr   r   r   baser
   r   pinr   r   enumr   r   typingr   r   r   rN   logging	getLoggerr   r{   r   r#   r#   r#   r$   <module>   s   
