a
    õ£Beë  ã                	   @   s  d Z ddlZddlZddlZddlmZmZmZmZ ddl	Z	dZ
dZdeeejf eeeef eeeef  eeeef  eddœdd	„Zdeeejf eeeeef  eeef d
œdd„Zeeejf eeee f dœdd„Zdeeejf eeef dœdd„Zg d¢ZdS )z10MQ authentication related functions and classes.é    N)ÚDictÚOptionalÚTupleÚUnionz›#   ****  Generated on {0} by pyzmq  ****
#   ZeroMQ CURVE **Secret** Certificate
#   DO NOT PROVIDE THIS FILE TO OTHER USERS nor change its permissions.

a
  #   ****  Generated on {0} by pyzmq  ****
#   ZeroMQ CURVE Public Certificate
#   Exchange securely, or use a secure mechanism to verify the contents
#   of this file after exchange. Store public certificates in your home
#   directory, in the .curve subdirectory.

úutf-8)Úkey_filenameÚbannerÚ
public_keyÚ
secret_keyÚmetadataÚencodingÚreturnc           	   	   C   s  t |tƒr| |¡}t |tƒr(| |¡}t| ddd��¸}| | tj ¡ ¡¡ | d¡ |r®| ¡ D ]H\}}t |tƒr€| |¡}t |tƒr”| |¡}| d|› d|› d�¡ qd| d¡ | d	|› d
�¡ |rà| d|› d
�¡ W d  ƒ n1 sô0    Y  dS )zCreate a certificate fileÚwÚutf8)r   z	metadata
z    z = Ú
zcurve
z    public-key = "z"
z    secret-key = "N)	Ú
isinstanceÚbytesÚdecodeÚopenÚwriteÚformatÚdatetimeÚnowÚitems)	r   r   r	   r
   r   r   ÚfÚkÚv© r   úQ/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/zmq/auth/certs.pyÚ_write_key_file   s$    	









r   )Úkey_dirÚnamer   r   c           	      C   sj   t  ¡ \}}tj | |¡}|› d�}|› d�}tj ¡ }t|t 	|¡|ƒ t|t
 	|¡|||d� ||fS )zeCreate zmq certificates.

    Returns the file paths to the public and secret certificate files.
    z.key_secretz.key)r
   r   )ÚzmqZcurve_keypairÚosÚpathÚjoinr   r   r   Ú_cert_public_bannerr   Ú_cert_secret_banner)	r    r!   r   r	   r
   Úbase_filenameZsecret_key_fileZpublic_key_filer   r   r   r   Úcreate_certificates>   s    	


ûr)   )Úfilenamer   c                 C   sÒ   d}d}t j | ¡s"td| › �ƒ‚t| dƒ�z}|D ]d}| ¡ }| d¡rJq2| d¡rj| dd¡d  d¡}| d	¡rŠ| dd¡d  d¡}|r2|r2 q˜q2W d  ƒ n1 s¬0    Y  |du rÊtd
|  ƒ‚||fS )a  Load public and secret key from a zmq certificate.

    Returns (public_key, secret_key)

    If the certificate file only contains the public key,
    secret_key will be None.

    If there is no public key found in the file, ValueError will be raised.
    NzInvalid certificate file: Úrbó   #s
   public-keyó   =é   s    	'"s
   secret-keyzNo public key found in %s)	r#   r$   ÚexistsÚOSErrorr   ÚstripÚ
startswithÚsplitÚ
ValueError)r*   r	   r
   r   Úliner   r   r   Úload_certificateZ   s$    


$r6   Ú.)Ú	directoryr   c                 C   s\   i }t j | ¡std| › �ƒ‚t j | d¡}t |¡}|D ]}t|ƒ\}}|r:d||< q:|S )z5Load public keys from all certificates in a directoryzInvalid certificate directory: z*.keyT)r#   r$   Úisdirr0   r%   Úglobr6   )r8   ÚcertsZglob_stringZ
cert_filesÚ	cert_filer	   Ú_r   r   r   Úload_certificates}   s    

r>   )r)   r6   r>   )NNr   )N)r7   )Ú__doc__r   r:   r#   Útypingr   r   r   r   r"   r'   r&   ÚstrÚPathLiker   r   r)   r6   Úboolr>   Ú__all__r   r   r   r   Ú<module>   s>      ú
ù# ý
üþ#$