
    (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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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])keyr   returnstrc                    t          | pd                                          }t                              d|          }|                                                    dd                              dd          S )N _-.)rL   strip_CAMEL_CASE_BOUNDARYsublowerreplace)rJ   raws     B/home/rurouni/.hermes/hermes-agent/agent/credential_persistence.py_normalize_keyrY   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.rN   Fmanualzmanual:)rL   rR   rU   
startswith_PERSISTABLE_PROVIDER_SOURCES)r[   r\   normalized_sourcenormalized_providers       rX   is_borrowed_credential_sourcerd   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XrZ   c                    t          |           }|r	|t          v rdS |t          v rdS |                    t                    S )NFT)rY   _SAFE_SECRETISH_METADATA_KEYS_SECRET_VALUE_KEYSendswith_SECRET_VALUE_SUFFIXES)rJ   
normalizeds     rX   _is_secret_payload_keyrk   r   sM    $$J 'DDDu'''t5666rZ   value
str | Nonec                    | d S t          |           }|sd S t          j        |                    dd                                                    }d|d d          S )Nzutf-8surrogatepass)errorssha256:   )rL   hashlibsha256encode	hexdigest)rl   textdigests      rX   _fingerprint_valuery   {   se    }tu::D t^DKKKHHIISSUUF"VCRC["""rZ   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,   r4   r8   r)   r%   r'   r    rq   )ry   getitemsrk   
isinstancerL   r`   )rz   rJ   fingerprintrl   existings        rX   _credential_secret_fingerprintr      s    [  (S)9)9:: 		 mmoo # #
U!#&& 	#,U33K #""""{{/00H(C   X%8%8%C%C 4rZ   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  )rk   ).0rJ   rl   s      rX   
<dictcomp>z8sanitize_borrowed_credential_payload.<locals>.<dictcomp>   s<       C%c**U  rZ   r    )dictrd   r}   r   r~   )rz   r\   resultr   	sanitizeds        rX   $sanitize_borrowed_credential_payloadr      s     ']]F(H)=)={KK 088K  ,,..  I
  6*5	&'rZ   )rJ   r   rK   rL   )N)r[   r   r\   r   rK   r]   )rJ   r   rK   r]   )rl   r   rK   rm   )rz   r{   rK   rm   )rz   r{   r\   r   rK   r   )__doc__
__future__r   rs   retypingr   r   r   	frozensetra   rf   rg   ri   compilerS   rY   rd   rk   ry   r   r   r   rZ   rX   <module>r      so    # " " " " "  				 % % % % % % % % % % !*	 + + + ! !  !*	 + + + ! ! . Y         0 & "rz";<< ; ; ; ;Y Y Y Y Y7 7 7 7# # # #   (       rZ   