U
    luf                  	   @   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dddg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 5 Q R X 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   2/tmp/pip-unpacked-wheel-h6ekxre8/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p}|D ]d}| }|drJq2|drj|ddd d}|d	r|ddd d}|r2|r2 qq2W 5 Q R X |dkrt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>   )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>      
# 
#$