o
    sh\W                     @  sx  d dl mZ ddlmZ ddlmZmZ ddlmZm	Z	m
Z
mZmZ ddlmZ d dlmZmZmZmZ d dlmZmZ d d	lmZmZmZmZmZmZmZ d d
lZd d
l Z 	 G dd de!Z"e"de"_#edddG dd de!Z$edddG dd de!Z%edddG dd de!Z&edddG dd de!Z'G dd deZ(G dd de)ee(dZ*eG dd de*Z+eG d d! d!e*Z,eG d"d# d#e*Z-eG d$d% d%e*Z.eG d&d' d'e*Z/eG d(d) d)e*Z0G d*d+ d+eZ1dNd/d0Z2eddd1G d2d3 d3e1Z3eddd1G d4d5 d5e1Z4eddd1G d6d7 d7e1Z5eddd1G d8d9 d9e1Z6eddd1G d:d; d;e1Z7eddd1G d<d= d=e1Z8eddd1G d>d? d?e1Z9eddd1G d@dA dAe1Z:eddd1G dBdC dCe1Z;eddd1G dDdE dEe1Z<eddd1G dFdG dGe1Z=eddd1G dHdI dIe1Z>eddd1G dJdK dKe1Z?G dLdM dMee)ef Z@d
S )O    )annotations   )cbor)CoseKeyES256)sha256websafe_decodewebsafe_encode
ByteBuffer_JsonDataObject)webauthn_json_mapping)EnumEnumMetauniqueIntFlag)	dataclassfield)AnyMappingOptionalSequenceTupleUnioncastNc                   @  sF   e Zd ZU dddZdd Zdd Zd	d
 ZedddZd e	d< dS )Aaguiddatabytesc                 C  s   t | dkr
tdd S )N   zAAGUID must be 16 bytes)len
ValueError)selfr    r!   I/var/www/html/env_mimamsha/lib/python3.10/site-packages/fido2/webauthn.py__init__8   s   zAaguid.__init__c                 C  s
   | t jkS N)r   NONEr    r!   r!   r"   __bool__<      
zAaguid.__bool__c                 C  sP   |   }|d d  d|dd  d|dd  d|dd  d|dd   	S )N   -   r      )hex)r    hr!   r!   r"   __str__?   s   HzAaguid.__str__c                 C  s   dt |  dS )NzAAGUID())strr&   r!   r!   r"   __repr__C   s   zAaguid.__repr__valuer1   returnc                 C  s   |  |ddS )Nr*    )fromhexreplaceclsr3   r!   r!   r"   parseF      zAaguid.parser%   N)r   r   )r3   r1   r4   r   )
__name__
__module____qualname__r#   r'   r/   r2   classmethodr:   __annotations__r!   r!   r!   r"   r   7   s   
 
r   s                   FT)initfrozenc                      sx   e Zd ZU ded< ded< ded< d fdd	Zd
d ZedddZedddZ	edddZ
edddZ  ZS )AttestedCredentialDatar   aaguidr   credential_idr   
public_key_c                   s^   t    t| }t| d|d  t| d|d  t| d|d  |d r-tdd S )	NrD   r   rE   r   rF         Wrong length)superr#   rC   _parseobject__setattr__r   )r    rG   parsed	__class__r!   r"   r#   W   s   

zAttestedCredentialData.__init__c                 C     t | S r$   reprr&   r!   r!   r"   r/   a      zAttestedCredentialData.__str__r   r4   #Tuple[bytes, bytes, CoseKey, bytes]c                 C  sJ   t | }t|d}||d}t| \}}||t||fS )zParse the components of an AttestedCredentialData from a binary
        string, and return them.

        :param data: A binary string containing an attested credential data.
        :return: AAGUID, credential ID, public key, and remaining data.
        r   >H)r
   r   readunpackr   decode_fromr   r:   )r   readerrD   cred_idpub_keyrestr!   r!   r"   rL   d   s
   zAttestedCredentialData._parsec                 C  s&   | |t dt| | t| S )a%  Create an AttestedCredentialData by providing its components.

        :param aaguid: The AAGUID of the authenticator.
        :param credential_id: The binary ID of the credential.
        :param public_key: A COSE formatted public key.
        :return: The attested credential data.
        rW   )structpackr   r   encode)r9   rD   rE   rF   r!   r!   r"   creater   s   zAttestedCredentialData.create$Tuple[AttestedCredentialData, bytes]c                 C  s$   |  |\}}}}| ||||fS )a  Unpack an AttestedCredentialData from a byte string, returning it and
        any remaining data.

        :param data: A binary string containing an attested credential data.
        :return: The parsed AttestedCredentialData, and any remaining data from
            the input.
        )rL   rb   )r9   r   rD   r\   r]   r^   r!   r!   r"   unpack_from   s   	z"AttestedCredentialData.unpack_from
key_handlec                 C  s   |  tj|t|S )ap  Create an AttestatedCredentialData from a CTAP1 RegistrationData instance.

        :param key_handle: The CTAP1 credential key_handle.
        :type key_handle: bytes
        :param public_key: The CTAP1 65 byte public key.
        :type public_key: bytes
        :return: The credential data, using an all-zero AAGUID.
        :rtype: AttestedCredentialData
        )rb   r   r%   r   
from_ctap1)r9   re   rF   r!   r!   r"   rf      s   z!AttestedCredentialData.from_ctap1rG   r   )r   r   r4   rV   )rD   r   rE   r   rF   r   r4   rC   )r   r   r4   rc   )re   r   rF   r   r4   rC   )r<   r=   r>   r@   r#   r/   staticmethodrL   r?   rb   rd   rf   __classcell__r!   r!   rP   r"   rC   Q   s   
 
rC   c                      s   e Zd ZU dZG dd deZded< ded< ded	< d
ed< ded< d% fddZdd Ze			d&d'ddZ
d(ddZd(ddZd(ddZd(dd Zd(d!d"Zd(d#d$Z  ZS ))AuthenticatorDataa  Binary encoding of the authenticator data.

    :param _: The binary representation of the authenticator data.
    :ivar rp_id_hash: SHA256 hash of the RP ID.
    :ivar flags: The flags of the authenticator data, see
        AuthenticatorData.FLAG.
    :ivar counter: The signature counter of the authenticator.
    :ivar credential_data: Attested credential data, if available.
    :ivar extensions: Authenticator extensions, if available.
    c                   @  s@   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S )	AuthenticatorData.FLAGzqAuthenticator data flags

        See https://www.w3.org/TR/webauthn/#sec-authenticator-data for details
        r      r)   r   @      N)r<   r=   r>   __doc__UPUVBEBSATEDUSER_PRESENTUSER_VERIFIEDBACKUP_ELIGIBILITYBACKUP_STATEATTESTEDEXTENSION_DATAr!   r!   r!   r"   FLAG   s    r|   r   
rp_id_hashrk   flagsintcounterz Optional[AttestedCredentialData]credential_dataOptional[Mapping]
extensionsrG   c                   s   t    t| }t| d|d t| d|d t| d|d | }| jtj	j
@ r:t|\}}nd }t| d| | jtj	j@ rRt|\}}nd }t| d| |ratd	d S )
Nr}       r~   Br   z>Ir   r   rJ   )rK   r#   r
   rM   rN   rX   rY   r~   rj   r|   rt   rC   rd   ru   r   rZ   r   )r    rG   r[   r^   r   r   rP   r!   r"   r#      s"   
zAuthenticatorData.__init__c                 C  rR   r$   rS   r&   r!   r!   r"   r/      rU   zAuthenticatorData.__str__    Nc                 C  s4   | |t d|| | |durt| S d S )a  Create an AuthenticatorData instance.

        :param rp_id_hash: SHA256 hash of the RP ID.
        :param flags: Flags of the AuthenticatorData.
        :param counter: Signature counter of the authenticator data.
        :param credential_data: Authenticated credential data (only if attested
            credential data flag is set).
        :param extensions: Authenticator extensions (only if ED flag is set).
        :return: The authenticator data.
        z>BINr   )r_   r`   r   ra   )r9   r}   r~   r   r   r   r!   r!   r"   rb      s   zAuthenticatorData.creater4   boolc                 C     t | jtjj@ S )z,Return true if the User Present flag is set.)r   r~   rj   r|   rp   r&   r!   r!   r"   is_user_present   r;   z!AuthenticatorData.is_user_presentc                 C  r   )z-Return true if the User Verified flag is set.)r   r~   rj   r|   rq   r&   r!   r!   r"   is_user_verified   r;   z"AuthenticatorData.is_user_verifiedc                 C  r   )z2Return true if the Backup Eligibility flag is set.)r   r~   rj   r|   rr   r&   r!   r!   r"   is_backup_eligible  r;   z$AuthenticatorData.is_backup_eligiblec                 C  r   )z,Return true if the Backup State flag is set.)r   r~   rj   r|   rs   r&   r!   r!   r"   is_backed_up  r;   zAuthenticatorData.is_backed_upc                 C  r   )z8Return true if the Attested credential data flag is set.)r   r~   rj   r|   rt   r&   r!   r!   r"   is_attested  r;   zAuthenticatorData.is_attestedc                 C  r   )z/Return true if the Extenstion data flag is set.)r   r~   rj   r|   ru   r&   r!   r!   r"   has_extension_data  r;   z$AuthenticatorData.has_extension_datarg   )r   N)
r}   r   r~   rk   r   r   r   r   r   r   )r4   r   )r<   r=   r>   ro   r   r|   r@   r#   r/   r?   rb   r   r   r   r   r   r   ri   r!   r!   rP   r"   rj      s(   
 




rj   c                      s`   e Zd ZU dZded< ded< ded< d fd
dZdd ZedddZedddZ	  Z
S )AttestationObjecta  Binary CBOR encoded attestation object.

    :param _: The binary representation of the attestation object.
    :ivar fmt: The type of attestation used.
    :ivar auth_data: The attested authenticator data.
    :ivar att_statement: The attestation statement.
    r1   fmtrj   	auth_dataMapping[str, Any]att_stmtrG   r   c                   sd   t    ttttf tt| }t	
| d|d  t	
| dt|d  t	
| d|d  d S )Nr   r   authDatar   attStmt)rK   r#   r   r   r1   r   r   decoder   rM   rN   rj   r    rG   r   rP   r!   r"   r#   #  s
   
zAttestationObject.__init__c                 C  rR   r$   rS   r&   r!   r!   r"   r/   +  rU   zAttestationObject.__str__r4   c                 C  s   | t |||dS )N)r   r   r   )r   ra   )r9   r   r   r   r!   r!   r"   rb   .  s   zAttestationObject.create	app_paramc                 C  s>   |  dt |tjjtjjB dt|j|j|j	g|j
dS )a  Create an AttestationObject from a CTAP1 RegistrationData instance.

        :param app_param: SHA256 hash of the RP ID used for the CTAP1 request.
        :type app_param: bytes
        :param registration: The CTAP1 registration data.
        :type registration: RegistrationData
        :return: The attestation object, using the "fido-u2f" format.
        :rtype: AttestationObject
        zfido-u2fr   )x5csig)rb   rj   r|   rt   rp   rC   rf   re   rF   certificate	signature)r9   r   registrationr!   r!   r"   rf   6  s   zAttestationObject.from_ctap1rg   )r   r1   r   rj   r   r   r4   r   )r   r   r4   r   )r<   r=   r>   ro   r@   r#   r/   r?   rb   rf   ri   r!   r!   rP   r"   r     s   
 r   c                      s   e Zd ZU eG dd deeZded< ded< ded< dZd	ed
< d fddZ	e
	ddddZdd ZedddZedddZ  ZS )CollectedClientDatac                   @     e Zd ZdZdZdS )zCollectedClientData.TYPEzwebauthn.createzwebauthn.getN)r<   r=   r>   CREATEGETr!   r!   r!   r"   TYPEQ      r   r1   typer   	challengeoriginFr   cross_originrG   c                   sl   t    t|  }t| d|d  t| dt|d  t| d|d  t| d|dd d S )Nr   r   r   r   crossOriginF)	rK   r#   jsonloadsr   rM   rN   r   getr   rP   r!   r"   r#   [  s   
zCollectedClientData.__init__Union[bytes, str]r4   c                 K  s<   t |tr
t|}n|}| tj||||d|dd S )N)r   r   r   r   ),:)
separators)
isinstancer   r	   r   dumpsra   )r9   r   r   r   r   kwargsencoded_challenger!   r!   r"   rb   d  s    
	
	zCollectedClientData.createc                 C  rR   r$   rS   r&   r!   r!   r"   r/   ~  rU   zCollectedClientData.__str__c                 C  rR   r$   )r	   r&   r!   r!   r"   b64     zCollectedClientData.b64c                 C  rR   r$   )r   r&   r!   r!   r"   hash  r   zCollectedClientData.hashrg   )F)
r   r1   r   r   r   r1   r   r   r4   r   )r4   r1   )r4   r   )r<   r=   r>   r   r1   r   r   r@   r   r#   r?   rb   r/   propertyr   r   ri   r!   r!   rP   r"   r   O  s    
 	r   c                      s$   e Zd Zdd Z fddZ  ZS )_StringEnumMetac                 C  s   d S r$   r!   r8   r!   r!   r"   
_get_value  s   z_StringEnumMeta._get_valuec                   s:   zt  j|g|R i |W S  ty   | | Y S w r$   )rK   __call__r   r   )r9   r3   argsr   rP   r!   r"   r     s
   z_StringEnumMeta.__call__)r<   r=   r>   r   r   ri   r!   r!   rP   r"   r     s    r   c                   @     e Zd ZdZdS )_StringEnumzYEnum of strings for WebAuthn types.

    Unrecognized values are treated as missing.
    N)r<   r=   r>   ro   r!   r!   r!   r"   r     s    r   )	metaclassc                   @  s   e Zd ZdZdZdZdZdS )AttestationConveyancePreferencenoneindirectdirect
enterpriseN)r<   r=   r>   r%   INDIRECTDIRECT
ENTERPRISEr!   r!   r!   r"   r     s
    r   c                   @     e Zd ZdZdZdZdS )UserVerificationRequirementrequired	preferreddiscouragedNr<   r=   r>   REQUIRED	PREFERREDDISCOURAGEDr!   r!   r!   r"   r         r   c                   @  r   )ResidentKeyRequirementr   r   r   Nr   r!   r!   r!   r"   r     r   r   c                   @  r   )AuthenticatorAttachmentplatformzcross-platformN)r<   r=   r>   PLATFORMCROSS_PLATFORMr!   r!   r!   r"   r     r   r   c                   @  s    e Zd ZdZdZdZdZdZdS )AuthenticatorTransportusbnfcblehybridinternalN)r<   r=   r>   USBNFCBLEHYBRIDINTERNALr!   r!   r!   r"   r     s    r   c                   @  r   )PublicKeyCredentialTypez
public-keyN)r<   r=   r>   
PUBLIC_KEYr!   r!   r!   r"   r     s    r   c                      s<   e Zd Z fddZe fddZe fddZ  ZS )_WebAuthnDataObjectc                   s"   t jr	t |S tt| |S r$   )r   enabledrK   __getitem__r   r    keyrP   r!   r"   r     s   z_WebAuthnDataObject.__getitem__c                   s&   t jr
t ||S tt| ||S r$   )r   r   rK   _parse_valuer   r9   tr3   rP   r!   r"   r     s   z _WebAuthnDataObject._parse_valuec                   s   t   t |S r$   )r   warnrK   	from_dict)r9   r   rP   r!   r"   r     s   z_WebAuthnDataObject.from_dict)r<   r=   r>   r   r?   r   r   ri   r!   r!   rP   r"   r     s    r   r   r4   r   c                   s    fdd D S )Nc                   s   i | ]}|t t |qS r!   )rK   r   r   ).0kr   r!   r"   
<dictcomp>      z_as_cbor.<locals>.<dictcomp>r!   r   r!   r   r"   _as_cbor  s   r   )eqrB   c                   @  s0   e Zd ZU ded< dZded< ed
dd	ZdS )PublicKeyCredentialRpEntityr1   nameNOptional[str]idr4   Optional[bytes]c                 C  s   | j rt| j dS dS )z%Return SHA256 hash of the identifier.utf8N)r   r   ra   r&   r!   r!   r"   id_hash  s   z#PublicKeyCredentialRpEntity.id_hash)r4   r   )r<   r=   r>   r@   r   r   r   r!   r!   r!   r"   r     s
   
 r   c                   @  *   e Zd ZU ded< ded< dZded< dS )PublicKeyCredentialUserEntityr1   r   r   r   Nr   display_name)r<   r=   r>   r@   r   r!   r!   r!   r"   r        
 r   c                   @  s   e Zd ZU ded< ded< dS )PublicKeyCredentialParametersr   r   r   algNr<   r=   r>   r@   r!   r!   r!   r"   r     s   
 r   c                   @  r   )PublicKeyCredentialDescriptorr   r   r   r   Nz*Optional[Sequence[AuthenticatorTransport]]
transports)r<   r=   r>   r@   r  r!   r!   r!   r"   r    r   r  c                      sN   e Zd ZU dZded< dZded< dZded< dZd	ed
<  fddZ  Z	S )AuthenticatorSelectionCriteriaN!Optional[AuthenticatorAttachment]authenticator_attachmentz Optional[ResidentKeyRequirement]resident_key%Optional[UserVerificationRequirement]user_verificationFzOptional[bool]require_resident_keyc                   sJ   t    | jd u rt| d| jrtjntj t| d| jtjk d S )Nr  r
  )	rK   __post_init__r  rM   rN   r
  r   r   r   r&   rP   r!   r"   r    s   

	
z,AuthenticatorSelectionCriteria.__post_init__)
r<   r=   r>   r  r@   r  r	  r
  r  ri   r!   r!   rP   r"   r    s   
 r  c                   @  sj   e Zd ZU ded< ded< ded< ded< d	Zd
ed< d	Zded< d	Zded< d	Zded< d	Zded< d	S )"PublicKeyCredentialCreationOptionsr   rpr   userr   r   z'Sequence[PublicKeyCredentialParameters]pub_key_cred_paramsNOptional[int]timeout1Optional[Sequence[PublicKeyCredentialDescriptor]]exclude_credentialsz(Optional[AuthenticatorSelectionCriteria]authenticator_selectionz)Optional[AttestationConveyancePreference]attestationOptional[Mapping[str, Any]]r   )	r<   r=   r>   r@   r  r  r  r  r   r!   r!   r!   r"   r    s   
 r  c                   @  sR   e Zd ZU ded< dZded< dZded< dZded	< dZd
ed< dZded< dS )!PublicKeyCredentialRequestOptionsr   r   Nr  r  r   rp_idr  allow_credentialsr  r	  r  r   )	r<   r=   r>   r@   r  r  r  r	  r   r!   r!   r!   r"   r  #  s   
 r  c                      sj   e Zd ZU eedddZded< ded< dZd	ed
<  fddZe	 fddZ
e	 fddZ  ZS ) AuthenticatorAttestationResponseclientDataJSONr   metadatar   client_datar   attestation_objectNr  extension_resultsc                       |dkr
t js
| jS t |S N
clientDatar   r   r  rK   r   r   rP   r!   r"   r   4     z,AuthenticatorAttestationResponse.__getitem__c                   6   |d urt jst|}|dd |d< |}t |S Nr$  r  r   r   dictpoprK   r   r9   r   r3   rP   r!   r"   r   9  
   z*AuthenticatorAttestationResponse.from_dictc                   &   |t tttf  kr|S t ||S r$   r   r   r1   r   rK   r   r   rP   r!   r"   r   A     z-AuthenticatorAttestationResponse._parse_value)r<   r=   r>   r   r*  r  r@   r!  r   r?   r   r   ri   r!   r!   rP   r"   r  .  s   
 r  c                      s   e Zd ZU eedddZded< ded< ded	< d
Zded< d
Zded< d
Z	ded<  fddZ
e fddZe fddZ  ZS )AuthenticatorAssertionResponser  r  r  r   r  rj   authenticator_datar   r   Nr   user_handlerE   r  r!  c                   r"  r#  r%  r   rP   r!   r"   r   S  r&  z*AuthenticatorAssertionResponse.__getitem__c                   r'  r(  r)  r,  rP   r!   r"   r   X  r-  z(AuthenticatorAssertionResponse.from_dictc                   r.  r$   r/  r   rP   r!   r"   r   `  r0  z+AuthenticatorAssertionResponse._parse_value)r<   r=   r>   r   r*  r  r@   r3  rE   r!  r   r?   r   r   ri   r!   r!   rP   r"   r1  J  s   
 r1  c                      R   e Zd ZU ded< ded< dZded< dZded	< dZd
ed<  fddZ  ZS )RegistrationResponser   r   r  responseNr  r  /Optional[AuthenticationExtensionsClientOutputs]client_extension_results!Optional[PublicKeyCredentialType]r   c                      t   t   d S r$   r   requirerK   r  r&   rP   r!   r"   r  q     z"RegistrationResponse.__post_init__	r<   r=   r>   r@   r  r8  r   r  ri   r!   r!   rP   r"   r5  i     
 r5  c                      r4  )AuthenticationResponser   r   r1  r6  Nr  r  r7  r8  r9  r   c                   r:  r$   r;  r&   rP   r!   r"   r    r=  z$AuthenticationResponse.__post_init__r>  r!   r!   rP   r"   r@  w  r?  r@  c                   @     e Zd ZU ded< dS )CredentialCreationOptionsr  rF   Nr  r!   r!   r!   r"   rB       
 rB  c                   @  rA  )CredentialRequestOptionsr  rF   Nr  r!   r!   r!   r"   rD    rC  rD  c                   @  sB   e Zd ZdZdddZdd Zdd	 Zd
d Zdd Zdd Z	dS )%AuthenticationExtensionsClientOutputsa  Holds extension output from a call to MakeCredential or GetAssertion.

    When accessed as a dict, all bytes values will be serialized to base64url encoding,
    capable of being serialized to JSON.

    When accessed using attributes, richer types will instead be returned.
    outputsr   c                 C  s   dd |  D | _d S )Nc                 S  s   i | ]\}}|d ur||qS r$   r!   )r   r   vr!   r!   r"   r     r   zBAuthenticationExtensionsClientOutputs.__init__.<locals>.<dictcomp>)items_members)r    rF  r!   r!   r"   r#     s   z.AuthenticationExtensionsClientOutputs.__init__c                 C  
   t | jS r$   )iterrI  r&   r!   r!   r"   __iter__  r(   z.AuthenticationExtensionsClientOutputs.__iter__c                 C  rJ  r$   )r   rI  r&   r!   r!   r"   __len__  r(   z-AuthenticationExtensionsClientOutputs.__len__c                 C  s<   | j | }t|trt|S t|trt|tst|S |S r$   )rI  r   r   r	   r   r*  )r    r   r3   r!   r!   r"   r     s   

z1AuthenticationExtensionsClientOutputs.__getitem__c                 C  s:   | d}|d ddd |dd  D  }| j|S )NrG   r   r5   c                 s  s    | ]}|  V  qd S r$   )title)r   pr!   r!   r"   	<genexpr>  s    zDAuthenticationExtensionsClientOutputs.__getattr__.<locals>.<genexpr>r   )splitjoinrI  r   )r    r   partsr   r!   r!   r"   __getattr__  s   
$z1AuthenticationExtensionsClientOutputs.__getattr__c                 C  s   t t| S r$   )rT   r*  r&   r!   r!   r"   r2     s   z.AuthenticationExtensionsClientOutputs.__repr__N)rF  r   )
r<   r=   r>   ro   r#   rL  rM  r   rT  r2   r!   r!   r!   r"   rE    s    
rE  )r   r   r4   r   )A
__future__r   r5   r   coser   r   utilsr   r   r	   r
   r   featuresr   enumr   r   r   r   dataclassesr   r   typingr   r   r   r   r   r   r   r_   r   r   r   r%   rC   rj   r   r   r   r1   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r1  r5  r@  rB  rD  rE  r!   r!   r!   r"   <module>   s   $	


L
v
9:















