
    Mh?                         d Z ddlZddlZddlmZmZmZmZmZm	Z	m
Z
mZ ddlZddlmZ ddlmZ ddlmZ dZd	Z G d
 d          ZddgZdS )z*Base implementation of 0MQ authentication.    N)Any	AwaitableDictListOptionalSetTupleUnion)_check_version)z85   )load_certificates*s   1.0c                      e Zd ZU dZded<   eed<   eed<   eeef         ed<   ded<   e	e         ed	<   e	e         ed
<   eeeeef         f         ed<   eeee
ef         f         ed<   eed<   	 	 	 d1ded         dedefdZd2dZd2dZdeddfdZdeddfdZ	 d3dedeeeef                  ddfdZ	 d4dedeeej        f         ddfdZ	 d3dededdfdZde
defd Z	 d3dedee         ddfd!Zd"ee
         fd#Zded$ed%edeee
f         fd&Zded'e
deee
f         fd(Zded)e
deee
f         fd*Z	 d5d,e
d-e
d.e
d/eddf
d0ZdS )6Authenticatora  Implementation of ZAP authentication for zmq connections.

    This authenticator class does not register with an event loop. As a result,
    you will need to manually call `handle_zap_message`::

        auth = zmq.Authenticator()
        auth.allow("127.0.0.1")
        auth.start()
        while True:
            await auth.handle_zap_msg(auth.zap_socket.recv_multipart())

    Alternatively, you can register `auth.zap_socket` with a poller.

    Since many users will want to run ZAP in a way that does not block the
    main thread, other authentication classes (such as :mod:`zmq.auth.thread`)
    are provided.

    Note:

    - libzmq provides four levels of security: default NULL (which the Authenticator does
      not see), and authenticated NULL, PLAIN, CURVE, and GSSAPI, which the Authenticator can see.
    - until you add policies, all incoming NULL connections are allowed.
      (classic ZeroMQ behavior), and all PLAIN and CURVE connections are denied.
    - GSSAPI requires no configuration.
    zzmq.Contextcontextencoding	allow_anycredentials_providersz
zmq.Socket
zap_socket_allowed_denied	passwordscertslogNutf-8c                 F   t          dd           |pt          j                                        | _        || _        d| _        i | _        d | _        t                      | _
        t                      | _        i | _        i | _        |pt          j        d          | _        d S )N)   r   securityFzzmq.auth)r   zmqContextinstancer   r   r   r   r   setr   r   r   r   logging	getLoggerr   )selfr   r   r   s       M/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/zmq/auth/base.py__init__zAuthenticator.__init__:   s     	vz***8#+"6"6"8"8 %'"uu  
7'+J77    returnc                     | j                             t          j        t          j                  | _        d| j        _        | j                            d           | j        	                    d           dS )zCreate and bind the ZAP socket)socket_classr   zinproc://zeromq.zap.01StartingN)
r   socketr    REPSocketr   lingerbindr   debugr&   s    r'   startzAuthenticator.startP   s]    ,--cgCJ-OO!"5666z"""""r)   c                 T    | j         r| j                                          d| _         dS )zClose the ZAP socketN)r   closer4   s    r'   stopzAuthenticator.stopW   s+    ? 	$O!!###r)   	addressesc                     | j         rt          d          | j                            dd                    |                     | j                            |           dS )a6  Allow IP address(es).

        Connections from addresses not explicitly allowed will be rejected.

        - For NULL, all clients from this address will be accepted.
        - For real auth setups, they will be allowed to continue with authentication.

        allow is mutually exclusive with deny.
        z Only use allow or deny, not bothzAllowing %s,N)r   
ValueErrorr   r3   joinr   updater&   r9   s     r'   allowzAuthenticator.allow]   s\     < 	A?@@@}chhy&9&9:::Y'''''r)   c                     | j         rt          d          | j                            dd                    |                     | j                            |           dS )zDeny IP address(es).

        Addresses not explicitly denied will be allowed to continue with authentication.

        deny is mutually exclusive with allow.
        z"Only use a allow or deny, not bothz
Denying %sr;   N)r   r<   r   r3   r=   r   r>   r?   s     r'   denyzAuthenticator.denyl   s\     = 	CABBB|SXXi%8%8999I&&&&&r)   r   domainc                 T    |r
|| j         |<   | j                            d|           dS )zConfigure PLAIN authentication for a given domain.

        PLAIN authentication uses a plain-text password file.
        To cover all domains, use "*".
        You can modify the password file at any time; it is reloaded automatically.
        zConfigure plain: %sNr   r   r3   )r&   rC   r   s      r'   configure_plainzAuthenticator.configure_plainx   s5      	/%.DN6",f55555r)   .locationc                    | j                             d||           |t          k    r	d| _        dS d| _        	 t	          |          | j        |<   dS # t          $ r'}| j                             d||           Y d}~dS d}~ww xY w)a	  Configure CURVE authentication for a given domain.

        CURVE authentication uses a directory that holds all public client certificates,
        i.e. their public keys.

        To cover all domains, use "*".

        You can add and remove certificates in that directory at any time. configure_curve must be called
        every time certificates are added or removed, in order to update the Authenticator's state

        To allow all client keys without checking, specify CURVE_ALLOW_ANY for the location.
        zConfigure curve: %s[%s]TFz&Failed to load CURVE certs from %s: %sN)r   r3   CURVE_ALLOW_ANYr   r   r   	Exceptionerror)r&   rC   rH   es       r'   configure_curvezAuthenticator.configure_curve   s    " 	0&(CCC&&!DNNN"DNV%6x%@%@
6""" V V VGSTUUUUUUUUUVs   A 
BA>>Bcredentials_providerc                 f    d| _         ||| j        |<   dS | j                            d|           dS )a  Configure CURVE authentication for a given domain.

        CURVE authentication using a callback function validating
        the client public key according to a custom mechanism, e.g. checking the
        key against records in a db. credentials_provider is an object of a class which
        implements a callback method accepting two parameters (domain and key), e.g.::

            class CredentialsProvider(object):

                def __init__(self):
                    ...e.g. db connection

                def callback(self, domain, key):
                    valid = ...lookup key and/or domain in db
                    if valid:
                        logging.info('Authorizing: {0}, {1}'.format(domain, key))
                        return True
                    else:
                        logging.warning('NOT Authorizing: {0}, {1}'.format(domain, key))
                        return False

        To cover all domains, use "*".
        FNz0None credentials_provider provided for domain:%s)r   r   r   rL   )r&   rC   rO   s      r'   configure_curve_callbackz&Authenticator.configure_curve_callback   sA    6 +1ED&v...HNNMvVVVVVr)   client_public_keyc                 P    t          j        |                              d          S )a  Return the User-Id corresponding to a CURVE client's public key

        Default implementation uses the z85-encoding of the public key.

        Override to define a custom mapping of public key : user-id

        This is only called on successful authentication.

        Parameters
        ----------
        client_public_key: bytes
            The client public key used for the given message

        Returns
        -------
        user_id: unicode
            The user ID as text
        ascii)r   encodedecode)r&   rR   s     r'   curve_user_idzAuthenticator.curve_user_id   s#    & z+,,33G<<<r)   c                     dS )z~Configure GSSAPI authentication

        Currently this is a no-op because there is nothing to configure with GSSAPI.
        N )r&   rC   rH   s      r'   configure_gssapizAuthenticator.configure_gssapi   s      r)   msgc           	      r   K   t          |          dk     rh j                            d|           t          |          dk     r j                            d           n                     |d         dd           dS |dd         \  }}}}}}|dd         }|                     j        d	          }|                     j        d	          }|t          k    r4 j                            d
|                                |dd           dS  j                            d||||||           d}	d}
d} j        rG| j        v rd}	 j                            d|           nmd}
d} j                            d|           nM j	        rF| j	        v r d}
d} j                            d|           nd}	 j                            d|           d}|
s|dk    r |	s j                            d           d}	n|dk    rtt          |          dk    r4 j                            d|                                |dd           dS  fd|D             \  }} 
                    |||          \  }	}n|dk    rt          |          dk    r4 j                            d|                                |dd           dS |d         }                     ||           d{V \  }	}|	r                     |          }n|d k    r}t          |          dk    r4 j                            d!|                                |dd           dS |d         }|                    d"          }                     ||          \  }	}|	r                     |d#d$|           dS                      |d|           dS )%zPerform ZAP authentication   z*Invalid ZAP message, not enough frames: %r   zNot enough information to replyr   s   400s   Not enough framesNreplacezInvalid ZAP version: %rs   Invalid versionzQversion: %r, request_id: %r, domain: %r, address: %r, identity: %r, mechanism: %rFs	   NO ACCESSTzPASSED (allowed) address=%ss   Address not allowedzDENIED (not allowed) address=%ss   Address deniedzDENIED (denied) address=%szPASSED (not denied) address=%s	anonymouss   NULLzALLOWED (NULL)s   PLAINzInvalid PLAIN credentials: %rs   Invalid credentialsc              3   N   K   | ]}|                     j        d           V   dS )r_   N)rV   r   ).0cr&   s     r'   	<genexpr>z3Authenticator.handle_zap_message.<locals>.<genexpr>$  sD       & &;<AHHT]I66& & & & & &r)   s   CURVEzInvalid CURVE credentials: %rr   s   GSSAPIzInvalid GSSAPI credentials: %rutf8   200   OK)lenr   rL   _send_zap_replyrV   r   VERSIONr3   r   r   _authenticate_plain_authenticate_curverW   _authenticate_gssapi)r&   r[   version
request_idrC   addressidentity	mechanismcredentialsalloweddeniedreasonusernamepasswordkey	principals   `               r'   handle_zap_messagez Authenticator.handle_zap_message   sF     s88a<<HNNGMMM3xx!||@AAAA$$SVV5IJJJFDGGAVWh	!""gt}i88..	::gHNN4c:::  V5GHHHF8		
 		
 		
 = 	J$-''<gFFFF/@'JJJJ\ 	J$,&&*;WEEEE?III  $	OG##G#/000h&&{##q((HNN#BKPPP((V=STTTF& & & &@K& & &"( #'":":68X"V"Vh&&{##q((HNN#BKPPP((V=STTTF!!n(,(@(@(M(M"M"M"M"M"M"M 7#11#66Hi''{##q((HNN#C[QQQ((V=STTTF'N	$++F33"&";";FI"N"N 	=  VUHEEEEE  VV<<<<<r)   rw   rx   c                 N   d}d}| j         rz|sd}|| j         v r/|| j         |         v r|| j         |         |         k    rd}nd}nd}nd}|r| j                            d|||           n9| j                            d	|           nd
}| j                            d|           ||fS )zPLAIN ZAP authenticationFr)   r   Ts   Invalid passwords   Invalid usernames   Invalid domainz1ALLOWED (PLAIN) domain=%s username=%s password=%sz	DENIED %ss   No passwords definedzDENIED (PLAIN) %srE   )r&   rC   rw   rx   rt   rv   s         r'   rk   z!Authenticator._authenticate_plainC  s     > 	8 ''t~f5554>&#9(#CCC"&!40FF* 4G	    {F3333 -FHNN.777r)   
client_keyc                 t  K   d}d}| j         r d}d}| j                            d           n| j        i k    r|sd}|| j        v rt	          j        |          }| j        |                             ||          }t          |t                    r| d{V }|rd}d}nd}|rd	nd
}| j                            d|||           nqd}nn|sd}|| j	        v r_t	          j        |          }| j	        |         
                    |          rd}d}nd}|rd	nd
}| j                            d|||           nd}||fS )zCURVE ZAP authenticationFr)   Trg   z ALLOWED (CURVE allow any client)r   Ns   Unknown keyALLOWEDDENIEDz0%s (CURVE auth_callback) domain=%s client_key=%ss   Unknown domainz"%s (CURVE) domain=%s client_key=%s)r   r   r3   r   r   rU   callback
isinstancer   r   get)r&   rC   r}   rt   rv   z85_client_keyrstatuss           r'   rl   z!Authenticator._authenticate_curvei  s      > 4	+GFHNN=>>>>'2-- 333!$J!7!7.v6??WWa++  A ,"G"FF+F&-;8F"	    +  ##!$J!7!7:f%)).99 ,"G"FF+F&-;88"	    +r)   rz   c                 >    | j                             d||           dS )zPNothing to do for GSSAPI, which has already been handled by an external service.z'ALLOWED (GSSAPI) domain=%s principal=%s)Trg   )r   r3   )r&   rC   rz   s      r'   rm   z"Authenticator._authenticate_gssapi  s     @&)TTT{r)   r`   ro   status_codestatus_textuser_idc                    |dk    r|nd}t          |t                    r|                    | j        d          }d}| j                            d||           t          |||||g}| j                            |           dS )z.Send a ZAP reply to finish the authentication.rf   r)   r_   zZAP reply code=%s text=%sN)	r   strrU   r   r   r3   rj   r   send_multipart)r&   ro   r   r   r   metadatareplys          r'   ri   zAuthenticator._send_zap_reply  s     )F22''gs## 	?nnT]I>>G2KMMM*k;R&&u-----r)   )Nr   N)r*   N)r   N)r   rG   )r`   ) __name__
__module____qualname____doc____annotations__r   boolr   r   r   bytesr   r(   r5   r8   r@   rB   rF   r
   osPathLikerN   rQ   rW   rZ   r   r{   r	   rk   rl   rm   ri   rY   r)   r'   r   r      s         4 MMMOOOS>)))#hXCc3h'((((T%*%%&&&&	HHH ,0	8 8-(8 8 	8 8 8 8,# # # #   ( ( ( ( ( (
's 
't 
' 
' 
' 
' HL6 66,4T#s(^,D6	6 6 6 6 FIV VV+0bk1A+BV	V V V V8 >B W  W W7: W	 W  W  W  WD=u = = = = =, <@ +3C=	   b=DK b= b= b= b=H$$%($47$	tU{	$ $ $ $L<<',<	tU{	< < < <|3 5 U4QV;EW     #. .. . 	.
 . 
. . . . . .r)   r   rJ   )r   r$   r   typingr   r   r   r   r   r   r	   r
   r    	zmq.errorr   	zmq.utilsr   r   r   rJ   rj   r   __all__rY   r)   r'   <module>r      s    0 0
  				 J J J J J J J J J J J J J J J J J J J J 



 $ $ $ $ $ $       $ $ $ $ $ $
f. f. f. f. f. f. f. f.R -
.r)   