
    mBNj8                    2   d dl mZ d dlZd dlmZ ddlmZ ddlm	Z	m
Z
 ddlmZmZmZmZmZ ddlmZ ej&                  rdd	lmZ  ed
      Z G d de      Z G d de      Z G d d      Z G d de      Z G d de      Z G d de      Z G d d      Zy)    )annotationsN)	getLogger   )_typing)AsyncCooperativeLock	AsyncLock)AsyncAuthManagerAsyncClientCertificateProviderClientCertificateexpiring_auth_has_expiredExpiringAuth)_TAuth)
Neo4jErrorzneo4j.auth_managementc                  <    e Zd ZU ded<   ddZddZ	 	 	 	 	 	 d	dZy)
AsyncStaticAuthManagerr   _authc                    || _         y Nr   selfauths     m/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/neo4j/_async/auth_management.py__init__zAsyncStaticAuthManager.__init__1   	    
    c                "   K   | j                   S wr   r   )r   s    r   get_authzAsyncStaticAuthManager.get_auth4   s     zzs   c                   K   yw)NF )r   r   errors      r   handle_security_exceptionz0AsyncStaticAuthManager.handle_security_exception7   s         N)r   r   returnNoner$   r   r   r   r!   r   r$   bool)__name__
__module____qualname____annotations__r   r   r"   r    r   r   r   r   .   s.    M#-	r   r   c                  l    e Zd ZU ded<   ded<   ded<   ded<   	 	 	 	 	 	 dd	Zd
 ZddZ	 	 	 	 	 	 ddZy)AsyncNeo4jAuthTokenManagerzExpiringAuth | None_current_auth)t.Callable[[], t.Awaitable[ExpiringAuth]]	_providerfrozenset[str]_handled_codesr   _lockc                L    || _         || _        d | _        t               | _        y r   )r1   r3   r/   r   r4   )r   providerhandled_codess      r   r   z#AsyncNeo4jAuthTokenManager.__init__C   s$    
 "+![
r   c                   K   	 | j                          d {   | _        | j                  t        d      y 7 !# t        $ r}t        j	                  d|        d }~ww xY ww)Nz.[     ]  _: <AUTH MANAGER> provider failed: %rzbAuth provider function passed to expiration_based AuthManager returned None, expected ExpiringAuth)r1   r/   BaseExceptionlogr!   	TypeError)r   es     r   _refresh_authz(AsyncNeo4jAuthTokenManager._refresh_authM   si     	'+~~'7!7D %C  &	 "8 	IIFJ	s1   A#; 9	; A#; 	A AA  A#c                Z  K   | j                   4 d {    | j                  }|t        |      rBt        j	                  d|dnd       | j                          d {    | j                  }|J |j                  cd d d       d {    S 7 |7 37 	# 1 d {  7  sw Y   y xY ww)Nz*[     ]  _: <AUTH MANAGER> refreshing (%s)initztime out)r4   r/   r   r:   debugr=   r   r   s     r   r   z#AsyncNeo4jAuthTokenManager.get_authY   s     :::%%D|8>		@"lF
 ((***))'''99 :: + :::sW   B+BB+ABBB>B+
BB+BB+B(BB($B+c                j  K   |j                   | j                  vry| j                  4 d {    | j                  }|G|j                  |k(  r8t
        j                  d|j                          | j                          d {    	 d d d       d {    y7 k7 7 	# 1 d {  7  sw Y   y xY ww)NFz0[     ]  _: <AUTH MANAGER> refreshing (error %s)T)coder3   r4   r/   r   r:   r@   r=   )r   r   r!   cur_auths       r   r"   z4AsyncNeo4jAuthTokenManager.handle_security_exceptionf   s      ::T000:::))H#(=		FJJ ((*** :: + :::sW   *B3BB3ABBBB3BB3BB3B0$B'%B0,B3N)r6   r0   r7   r2   r$   r%   r&   r'   )r)   r*   r+   r,   r   r=   r   r"   r    r   r   r.   r.   =   sb    &&88""!;! &! 
	!
#-	r   r.   c                  V    e Zd ZdZedd       Ze	 	 	 	 dd       Ze	 	 	 	 dd       Zy)	AsyncAuthManagersa  
    A collection of :class:`.AsyncAuthManager` factories.

    .. versionadded:: 5.8

    .. versionchanged:: 5.12

        * Method ``expiration_based()`` was renamed to :meth:`bearer`.
        * Added :meth:`basic`.

    .. versionchanged:: 5.14 Stabilized from preview.
    c                    t        |       S )a  
        Create a static auth manager.

        The manager will always return the auth info provided at its creation.

        Example::

            # NOTE: this example is for illustration purposes only.
            #       The driver will automatically wrap static auth info in a
            #       static auth manager.

            import neo4j
            from neo4j.auth_management import AsyncAuthManagers


            auth = neo4j.basic_auth("neo4j", "password")

            with neo4j.GraphDatabase.driver(
                "neo4j://example.com:7687",
                auth=AsyncAuthManagers.static(auth)
                # auth=auth  # this is equivalent
            ) as driver:
                ...  # do stuff

        :param auth: The auth to return.

        :returns:
            An instance of an implementation of :class:`.AsyncAuthManager` that
            always returns the same auth.

        .. versionadded:: 5.8

        .. versionchanged:: 5.14 Stabilized from preview.
        )r   )r   s    r   staticzAsyncAuthManagers.static   s    H &d++r   c                >     t        d      }d fd}t        ||      S )a,  
        Create an auth manager handling basic auth password rotation.

        This factory wraps the provider function in an auth manager
        implementation that caches the provided auth info until the server
        notifies the driver that the auth info has expired (by returning
        an error that indicates that the password is invalid).

        Note that this implies that the provider function will be called again
        if it provides wrong auth info, potentially deferring failure due to a
        wrong password or username.

        .. warning::

            The provider function **must not** interact with the driver in any
            way as this can cause deadlocks and undefined behaviour.

            The provider function must only ever return auth information
            belonging to the same identity.
            Switching identities is undefined behavior.
            You may use :ref:`session-level authentication<session-auth-ref>`
            for such use-cases.

        Example::

            import neo4j
            from neo4j.auth_management import (
                AsyncAuthManagers,
                ExpiringAuth,
            )


            async def auth_provider():
                # some way of getting a token
                user, password = await get_current_auth()
                return (user, password)


            with neo4j.GraphDatabase.driver(
                "neo4j://example.com:7687",
                auth=AsyncAuthManagers.basic(auth_provider)
            ) as driver:
                ...  # do stuff

        :param provider:
            A callable that provides new auth info whenever the server notifies
            the driver that the previous auth info is invalid.

        :returns:
            An instance of an implementation of :class:`.AsyncAuthManager` that
            returns auth info from the given provider and refreshes it, calling
            the provider again, when the auth info was rejected by the server.

        .. versionadded:: 5.12

        .. versionchanged:: 5.14 Stabilized from preview.
        )%Neo.ClientError.Security.Unauthorizedc                 @   K   t                  d {         S 7 wr   )r   )r6   s   r   wrapped_providerz1AsyncAuthManagers.basic.<locals>.wrapped_provider   s     hj 011 0s   	)r$   r   	frozensetr.   )r6   r7   rK   s   `  r   basiczAsyncAuthManagers.basic   s&    z ""LM	2 **:MJJr   c                0    t        d      }t        | |      S )a	  
        Create an auth manager for potentially expiring bearer auth tokens.

        This factory wraps the provider function in an auth manager
        implementation that caches the provided auth info until either the
        :attr:`.ExpiringAuth.expires_at` exceeded or the server notified the
        driver that the auth info has expired (by returning an error that
        indicates that the bearer auth token has expired).

        .. warning::

            The provider function **must not** interact with the driver in any
            way as this can cause deadlocks and undefined behaviour.

            The provider function must only ever return auth information
            belonging to the same identity.
            Switching identities is undefined behavior.
            You may use :ref:`session-level authentication<session-auth-ref>`
            for such use-cases.

        Example::

            import neo4j
            from neo4j.auth_management import (
                AsyncAuthManagers,
                ExpiringAuth,
            )


            async def auth_provider():
                # some way of getting a token
                sso_token = await get_sso_token()
                # assume we know our tokens expire every 60 seconds
                expires_in = 60

                # Include a little buffer so that we fetch a new token
                # *before* the old one expires
                expires_in -= 10

                auth = neo4j.bearer_auth(sso_token)
                return ExpiringAuth(auth=auth).expires_in(expires_in)


            with neo4j.GraphDatabase.driver(
                "neo4j://example.com:7687",
                auth=AsyncAuthManagers.bearer(auth_provider)
            ) as driver:
                ...  # do stuff

        :param provider:
            A callable that provides a :class:`.ExpiringAuth` instance.

        :returns:
            An instance of an implementation of :class:`.AsyncAuthManager` that
            returns auth info from the given provider and refreshes it, calling
            the provider again, when the auth info expires (either because it's
            reached its expiry time or because the server flagged it as
            expired).

        .. versionadded:: 5.12

        .. versionchanged:: 5.14 Stabilized from preview.
        )z%Neo.ClientError.Security.TokenExpiredrI   rL   )r6   r7   s     r   bearerzAsyncAuthManagers.bearer   s#    F "
 *(MBBr   N)r   r   r$   r	   )r6   z#t.Callable[[], t.Awaitable[_TAuth]]r$   r	   )r6   r0   r$   r	   )r)   r*   r+   __doc__staticmethodrG   rN   rP   r    r   r   rE   rE   v   su     #, #,J AK5AK	AK AKF HC;HC	HC HCr   rE   c                  (    e Zd ZU ded<   ddZddZy)%_AsyncStaticClientCertificateProviderClientCertificate | None_certc                    || _         y r   rV   r   certs     r   r   z._AsyncStaticClientCertificateProvider.__init__=  r   r   c                6   K   | j                   d c}| _         |S wr   rX   rY   s     r   get_certificatez5_AsyncStaticClientCertificateProvider.get_certificate@  s     ::tdjs   NrZ   r   r$   r%   r$   rU   )r)   r*   r+   r,   r   r\   r    r   r   rT   rT   :  s    ##r   rT   c                  6    e Zd ZdZej
                  dd       Zy)&AsyncRotatingClientCertificateProvidera[  
    Abstract base class for certificate providers that can rotate certificates.

    The provider will make the driver use the initial certificate for all
    connections until the certificate is updated using the
    :meth:`update_certificate` method.
    From that point on, the new certificate will be used for all new
    connections until :meth:`update_certificate` is called again and so on.

    Example::

        from neo4j import AsyncGraphDatabase
        from neo4j.auth_management import (
            ClientCertificate,
            AsyncClientCertificateProviders,
        )


        provider = AsyncClientCertificateProviders.rotating(
            ClientCertificate(
                certfile="path/to/certfile.pem",
                keyfile="path/to/keyfile.pem",
                password=lambda: "super_secret_password"
            )
        )
        driver = AsyncGraphDatabase.driver(
           # secure driver must be configured for client certificate
           # to be used: (...+s[sc] scheme or encrypted=True)
           "neo4j+s://example.com:7687",
           # auth still required as before, unless server is configured to not
           # use authentication
           auth=("neo4j", "password"),
           client_certificate=provider
        )

        # do work with the driver, until the certificate needs to be rotated
        ...

        await provider.update_certificate(
            ClientCertificate(
                certfile="path/to/new/certfile.pem",
                keyfile="path/to/new/keyfile.pem",
                password=lambda: "new_super_secret_password"
            )
        )

        # do more work with the driver, until the certificate needs to be
        # rotated again
        ...

    .. versionadded:: 5.19

    .. versionchanged:: 5.24

        Turned this class into an abstract class to make the actual
        implementation internal. This entails removing the possibility to
        directly instantiate this class. Please use the factory method
        :meth:`.AsyncClientCertificateProviders.rotating` instead.

    .. versionchanged:: 5.27 Stabilized from preview.
    c                   K   yw)z2Update the certificate to use for new connections.Nr    rY   s     r   update_certificatez9AsyncRotatingClientCertificateProvider.update_certificate  s     r#   Nr]   )r)   r*   r+   rQ   abcabstractmethodrb   r    r   r   r`   r`   E  s$    <| 	A Ar   r`   c                  $    e Zd ZddZddZddZy),_AsyncNeo4jRotatingClientCertificateProviderc                0    || _         t               | _        y r   )rV   r   r4   )r   initial_certs     r   r   z5_AsyncNeo4jRotatingClientCertificateProvider.__init__  s    /;
)+
r   c                   K   | j                   4 d {    | j                  d c}| _        |cd d d       d {    S 7 +7 # 1 d {  7  sw Y   y xY wwr   r4   rV   rY   s     r   r\   z<_AsyncNeo4jRotatingClientCertificateProvider.get_certificate  s7     :::#zz4D$* ::::::s@   A?AAAAAAA	A
AAc                   K   | j                   4 d {    || _        d d d       d {    y 7 7 # 1 d {  7  sw Y   y xY wwr   rj   rY   s     r   rb   z?_AsyncNeo4jRotatingClientCertificateProvider.update_certificate  s)     :::DJ ::::::s9   A	0A	4A	2A	A	A=AA	N)rh   r   r$   r%   r^   r]   )r)   r*   r+   r   r\   rb   r    r   r   rf   rf     s    ,
r   rf   c                  <    e Zd ZdZedd       Ze	 	 	 	 dd       Zy)AsyncClientCertificateProvidersz
    A collection of :class:`.AsyncClientCertificateProvider` factories.

    .. versionadded:: 5.19

    .. versionchanged:: 5.27 Stabilized from preview.
    c                    t        |       S )z
        Create a static client certificate provider.

        The provider simply makes the driver use the given certificate for all
        connections.
        )rT   )rZ   s    r   rG   z&AsyncClientCertificateProviders.static  s     5T::r   c                    t        |       S )z
        Create certificate provider that allows for rotating certificates.

        .. seealso:: :class:`.AsyncRotatingClientCertificateProvider`
        )rf   )rh   s    r   rotatingz(AsyncClientCertificateProviders.rotating  s     <LIIr   N)rZ   r   r$   r
   )rh   r   r$   r`   )r)   r*   r+   rQ   rR   rG   rp   r    r   r   rm   rm     sC     ; ; J'J	/J Jr   rm   )
__future__r   rc   loggingr    r   t_async_compat.concurrencyr   r   _auth_managementr	   r
   r   r   r   apir   TYPE_CHECKING
exceptionsr   r:   r   r.   rE   rT   r`   rf   rm   r    r   r   <module>rz      s   " # 
     ??' '(- 6!1 6rAC ACH,J AA-K AAH*"J Jr   