
    Fi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dS )#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loopback_pkcezopenai-codexdevice_code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])keyr   returnstrc                    t          | pd                                          }t                              d|          }|                                                    dd                              dd          S )N _-.)rM   strip_CAMEL_CASE_BOUNDARYsublowerreplace)rK   raws     A/home/ubuntu/.hermes/hermes-agent/agent/credential_persistence.py_normalize_keyrZ   a   s`    
ciR..


 
 C

"
"3
,
,C99;;sC((00c:::    sourceprovider_idboolc                .   t          | pd                                                                          }|sdS |dk    s|                    d          rdS t          |pd                                                                          }||ft          vS )zGReturn True when ``source`` points at a borrowed/reference-only secret.rO   Fmanualzmanual:)rM   rS   rV   
startswith_PERSISTABLE_PROVIDER_SOURCES)r\   r]   normalized_sourcenormalized_providers       rY   is_borrowed_credential_sourcere   g   s    FLb))//117799 uH$$(9(D(DY(O(O$uk/R006688>>@@!23;XXXr[   c                    t          |           }|r	|t          v rdS |t          v rdS |                    t                    S )NFT)rZ   _SAFE_SECRETISH_METADATA_KEYS_SECRET_VALUE_KEYSendswith_SECRET_VALUE_SUFFIXES)rK   
normalizeds     rY   _is_secret_payload_keyrl   r   sM    $$J 'DDDu'''t5666r[   value
str | Nonec                    | d S t          |           }|sd S t          j        |                    dd                                                    }d|d d          S )Nzutf-8surrogatepass)errorssha256:   )rM   hashlibsha256encode	hexdigest)rm   textdigests      rY   _fingerprint_valuerz   {   se    }tu::D t^DKKKHHIISSUUF"VCRC["""r[   payloadMapping[str, Any]c                ^   dD ]*}t          |                     |                    }|r|c S +|                                 D ])\  }}t          |          rt          |          }|r|c S *|                     d          }t	          |t
                    r|                    d          r|S d S )N)r-   r5   r9   r*   r&   r(   r!   rr   )rz   getitemsrl   
isinstancerM   ra   )r{   rK   fingerprintrm   existings        rY   _credential_secret_fingerprintr      s    [  (S)9)9:: 		 mmoo # #
U!#&& 	#,U33K #""""{{/00H(C   X%8%8%C%C 4r[   Dict[str, Any]c                    t          |           }t          |                    d          |          s|S t          |          }d |                                D             }|r||d<   |S )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\   c                8    i | ]\  }}t          |          ||S  )rl   ).0rK   rm   s      rY   
<dictcomp>z8sanitize_borrowed_credential_payload.<locals>.<dictcomp>   s<       C%c**U  r[   r!   )dictre   r~   r   r   )r{   r]   resultr   	sanitizeds        rY   $sanitize_borrowed_credential_payloadr      s     ']]F(H)=)={KK 088K  ,,..  I
  6*5	&'r[   )rK   r   rL   rM   )N)r\   r   r]   r   rL   r^   )rK   r   rL   r^   )rm   r   rL   rn   )r{   r|   rL   rn   )r{   r|   r]   r   rL   r   )__doc__
__future__r   rt   retypingr   r   r   	frozensetrb   rg   rh   rj   compilerT   rZ   re   rl   rz   r   r   r   r[   rY   <module>r      so    # " " " " "  				 % % % % % % % % % % !*	 + + + ! !  !*	 + + + ! ! . Y         0 & "rz";<< ; ; ; ;Y Y Y Y Y7 7 7 7# # # #   (       r[   