Ë
    áß²j  ã                  óÜ   — d Z ddlmZ ddlZddlZddlmZmZmZ  e	h d£«      Z
 e	h d£«      Z e	h d£«      ZdZ ej                  d	«      Zdd
„Zddd„Zdd„Zdd„Zdd„Z	 d	 	 	 	 	 dd„Zy)a  Credential-pool disk-boundary sanitization: strip raw secrets from *borrowed*
pool entries before they reach ``auth.json``. Deliberately free of
``hermes_cli.auth`` imports so the pool model and the auth-store write boundary
share one policy without import cycles.é    )ÚannotationsN)ÚAnyÚDictÚMapping>   ©z	xai-oauthÚdevice_code©zopenai-codexr   ©zminimax-oauthÚoauth©Únousr   ©Ú	anthropicÚhermes_pkce>   ÚscopeÚ	client_idÚ
expires_atÚ
expires_inÚ
token_typeÚlast_statusÚagent_key_idÚlast_refreshÚexpires_at_msÚsecret_sourceÚlast_status_atÚlast_error_codeÚagent_key_reusedÚlast_error_reasonÚlast_error_messageÚsecret_fingerprintÚlast_error_reset_atÚagent_key_expires_atÚagent_key_expires_inÚagent_key_obtained_at>   ÚtokenÚapikeyÚsecretÚtokensÚapi_keyÚid_tokenÚpasswordÚ	agent_keyÚ	api_tokenÚ
auth_tokenÚ
credentialÚ
secret_keyÚcredentialsÚoauth_tokenÚprivate_keyÚaccess_tokenÚbearer_tokenÚauthorizationÚclient_secretÚrefresh_tokenÚsession_token)Ú_api_keyÚ
_api_tokenÚ_access_tokenÚ_auth_tokenÚ_refresh_tokenÚ_bearer_tokenÚ_client_secretÚ	_id_tokenÚ_oauth_tokenÚ_private_keyÚ_session_tokenÚ_secret_keyÚ	_passwordÚ_secretÚ_tokenÚ_keyz(?<=[a-z0-9])(?=[A-Z])c                óÄ   — t         j                  dt        | xs d«      j                  «       «      }|j	                  «       j                  dd«      j                  dd«      S )NÚ_Ú ú-ú.)Ú_CAMEL_CASE_BOUNDARYÚsubÚstrÚstripÚlowerÚreplace)ÚkeyÚraws     úA/home/ubuntu/.hermes/hermes-agent/agent/credential_persistence.pyÚ_normalize_keyrX   3   sK   € Ü
×
"Ñ
" 3¬¨CªI°2«×(<Ñ(<Ó(>Ó
?€CØ�9‰9‹;×Ñ˜s CÓ(×0Ñ0°°cÓ:Ð:ó    c                óô   — t        | xs d«      j                  «       j                  «       }|r|dk(  s|j                  d«      ryt        |xs d«      j                  «       j                  «       }||ft        vS )zGReturn True when ``source`` points at a borrowed/reference-only secret.rL   Úmanualzmanual:F)rQ   rR   rS   Ú
startswithÚ_PERSISTABLE_PROVIDER_SOURCES)ÚsourceÚprovider_idÚnormalized_sourceÚnormalized_providers       rW   Úis_borrowed_credential_sourcerb   8   st   € ä˜FšL bÓ)×/Ñ/Ó1×7Ñ7Ó9ÐÙÐ 1°XÒ =ÐAR×A]ÑA]Ð^gÔAhØÜ˜kÒ/¨RÓ0×6Ñ6Ó8×>Ñ>Ó@ÐØÐ!2Ð3Ô;XÐXÐXrY   c                ól   — t        | «      }|r|t        v ry|t        v xs |j                  t        «      S )NF)rX   Ú_SAFE_SECRETISH_METADATA_KEYSÚ_SECRET_VALUE_KEYSÚendswithÚ_SECRET_VALUE_SUFFIXES)rU   Ú
normalizeds     rW   Ú_is_secret_payload_keyri   A   s7   € Ü Ó$€JÙ˜Ô'DÑDØØÔ+Ð+ÒZ¨z×/BÑ/BÔCYÓ/ZÐZrY   c                óž   — | €dn
t        | «      }|syt        j                  |j                  dd¬«      «      j	                  «       }d|dd › �S )zèNon-reversible ``sha256:<16 hex>`` fingerprint of one secret value.

    Callers comparing a live secret against the ``secret_fingerprint`` left on
    a sanitized (borrowed) pool row need exactly the digest this module writes.
    NrL   zutf-8Úsurrogatepass)Úerrorsúsha256:é   )rQ   ÚhashlibÚsha256ÚencodeÚ	hexdigest)ÚvalueÚtextÚdigests      rW   Úfingerprint_secret_valuerv   H   sP   € ð �‰2¤C¨£J€DÙØÜ�^‰^˜DŸK™K¨¸˜KÓHÓI×SÑSÓU€FØ�V˜C˜R�[�MÐ"Ð"rY   c                óV  — d}|D �cg c]  }| j                  |«      ‘Œ }}|| j                  «       D ��cg c]  \  }}t        |«      sŒ|‘Œ c}}z  }|D ]  }t        |«      }|sŒ|c S  | j                  d«      }t	        |t
        «      r|j                  d«      r|S y c c}w c c}}w )N)r,   r4   r8   r)   r%   r'   r    rm   )ÚgetÚitemsri   rv   Ú
isinstancerQ   r\   )ÚpayloadÚ	preferredÚkÚ
candidatesÚvrs   ÚfingerprintÚexistings           rW   Ú_credential_secret_fingerprintr‚   U   s£   € Ø\€IØ*3Ö4 Q�'—+‘+˜a•.Ð4€JÐ4Ø §¡£×N™˜˜AÔ4JÈ1Õ4M’1ÓNÑN€JØò ˆÜ.¨uÓ5ˆÚØÒðð �{‰{Ð/Ó0€HÜ�(œCÔ  X×%8Ñ%8¸Ô%CØˆØùò 5ùÛNs   ‡B µB%Á	B%c                óä   — t        | «      }t        |j                  d«      |«      s|S t        |«      }|j	                  «       D ��ci c]  \  }}t        |«      rŒ||“Œ }}}|r||d<   |S c c}}w )zòReturn a disk-safe credential-pool payload.

    Owned sources pass through unchanged.  Borrowed sources keep labels,
    source refs, status/cooldown metadata, counters and a fingerprint, but
    every raw secret value field is removed.
    r^   r    )Údictrb   rx   r‚   ry   ri   )r{   r_   Úresultr€   r}   r   Ú	sanitizeds          rW   Ú$sanitize_borrowed_credential_payloadr‡   c   sr   € ô �'‹]€FÜ(¨¯©°HÓ)=¸{ÔKØˆÜ0°Ó8€KØ"(§,¡,£.×R™$˜!˜QÔ8NÈqÕ8Q��A‘ÐR€IÑRÙØ*5ˆ	Ð&Ñ'ØÐùó Ss   ÁA,ÁA,)rU   r   ÚreturnrQ   )N)r^   r   r_   r   rˆ   Úbool)rU   r   rˆ   r‰   )rs   r   rˆ   ú
str | None)r{   úMapping[str, Any]rˆ   rŠ   )r{   r‹   r_   r   rˆ   zDict[str, Any])Ú__doc__Ú
__future__r   ro   ÚreÚtypingr   r   r   Ú	frozensetr]   rd   re   rg   ÚcompilerO   rX   rb   ri   rv   r‚   r‡   © rY   rW   ú<module>r“      s¬   ðñ+õ
 #ã Û 	ß %Ñ %ñ !*ò +ó !Ð ñ !*ò +ó !Ð ñ ò  ó Ð ðÐ ð "�r—z‘zÐ";Ó<Ð ó;ô
Yó[ó
#óð  ðØðàðð ôrY   