
    `gj                        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 helpers.

These helpers define which credential-pool entries are references to borrowed
runtime secrets and strip raw values before those entries are written to
``auth.json``.  They intentionally have no dependency on ``hermes_cli.auth`` so
both the pool model and the final auth-store write boundary can share the same
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        | xs d      j                         }t        j                  d|      }|j	                         j                  dd      j                  dd      S )N _-.)strstrip_CAMEL_CASE_BOUNDARYsublowerreplace)keyraws     O/root/.hermes/venv/lib/python3.12/site-packages/agent/credential_persistence.py_normalize_keyrX   a   sP    
ciR.


 C

"
"3
,C99;sC(00c::    c                    t        | xs d      j                         j                         }|sy|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.rK   Fmanualzmanual:)rO   rP   rS   
startswith_PERSISTABLE_PROVIDER_SOURCES)sourceprovider_idnormalized_sourcenormalized_providers       rW   is_borrowed_credential_sourcerb   g   sv    FLb)//1779H$(9(D(DY(Ok/R0668>>@!23;XXXrY   c                j    t        |       }|r|t        v ry|t        v ry|j                  t              S )NFT)rX   _SAFE_SECRETISH_METADATA_KEYS_SECRET_VALUE_KEYSendswith_SECRET_VALUE_SUFFIXES)rU   
normalizeds     rW   _is_secret_payload_keyri   r   s8    $J'DD''566rY   c                    | y t        |       }|sy t        j                  |j                  dd            j	                         }d|d d  S )Nzutf-8surrogatepass)errorssha256:   )rO   hashlibsha256encode	hexdigest)valuetextdigests      rW   _fingerprint_valuerv   {   sP    }u:D^^DKKKHISSUFVCR[M""rY   c                $   dD ]"  }t        | j                  |            }|s |c S  | j                         D ]"  \  }}t        |      st        |      }|s |c S  | j                  d      }t	        |t
              r|j                  d      r|S y )N)r,   r4   r8   r)   r%   r'   r    rm   )rv   getitemsri   
isinstancerO   r\   )payloadrU   fingerprintrs   existings        rW   _credential_secret_fingerprintr~      s    [ (S)9:
 mmo #
U!#&,U3K""	# {{/0H(C X%8%8%CrY   c                    t        |       }t        |j                  d      |      s|S t        |      }|j	                         D ci c]  \  }}t        |      s|| }}}|r||d<   |S c c}}w )aK  Return a disk-safe credential-pool payload.

    Owned sources (manual entries and Hermes-owned OAuth/device-code state)
    pass through unchanged.  Borrowed/reference-only sources keep labels,
    source refs, status/cooldown metadata, counters, and a non-reversible
    fingerprint, but raw secret value fields are removed.
    r^   r    )dictrb   rx   r~   ry   ri   )r{   r_   resultr|   rU   rs   	sanitizeds          rW   $sanitize_borrowed_credential_payloadr      s     ']F(H)={K08K !,,.C%c* 	U
I 
 *5	&'s   A+)rU   r   returnrO   )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   compilerQ   rX   rb   ri   rv   r~   r    rY   rW   <module>r      s    #  	 % % !* + !  !* + ! .     0 & "rzz";< ;Y7#(  rY   