o
    RDi                     @  s   d dl mZ 	 d dlZd dlZd dlmZmZ d dlmZm	Z	 d dl
mZ d dlmZmZmZ ddlmZ dd	lmZmZmZ dd
lmZ ddlmZ erSddlmZ eeZ	 dZdZ e	G dd deZ!G dd deZ"dS )    )annotationsN)ABCabstractmethod)Enumunique)getenv)TYPE_CHECKINGAnyIterator   )ER_FAILED_TO_CONNECT_TO_DB)DatabaseErrorErrorOperationalError)'SQLSTATE_CONNECTION_WAS_NOT_ESTABLISHED)TimeoutBackoffCtx)SnowflakeConnection   x   c                   @  s8   e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdS )AuthType	SNOWFLAKEEXTERNALBROWSERSNOWFLAKE_JWTOAUTHID_TOKENUSERNAME_PASSWORD_MFAOKTAPROGRAMMATIC_ACCESS_TOKENNO_AUTHWORKLOAD_IDENTITYPAT_WITH_EXTERNAL_SESSIONN)__name__
__module____qualname__DEFAULTEXTERNAL_BROWSERKEY_PAIRr   r   USR_PWD_MFAr   PATr   r   r     r)   r)   [/var/www/Datamplify/venv/lib/python3.10/site-packages/snowflake/connector/auth/by_plugin.pyr   ,   s    r   c                   @  s   e Zd ZdZ		d/d0d	d
Zed1ddZejd2ddZeed3ddZ	eed4ddZ
ed5dd Zed6d#d$Zed2d%d&Zed7d(d)Zd8d+d,Zd9d-d.ZdS ):AuthByPluginz!External Authenticator interface.Ntimeout
int | Nonebackoff_generatorIterator | NonereturnNonec              	   K  s>   d| _ t|d ur
|nt|dttdt|d| _d | _d S )NFmax_retry_attemptsMAX_CON_RETRY_ATTEMPTS)r,   r2   r.   )	consent_cache_id_tokenr   DEFAULT_AUTH_CLASS_TIMEOUTgetintr   DEFAULT_MAX_CON_RETRY_ATTEMPTS
_retry_ctx_socket_timeout)selfr,   r.   kwargsr)   r)   r*   __init__>   s   
zAuthByPlugin.__init__r7   c                 C  s   | j jS )zYThe timeout of _retry_ctx is guaranteed not to be None during AuthByPlugin initialization)r9   r,   r;   r)   r)   r*   r,   T   s   zAuthByPlugin.timeoutc                 C  s   t d d S )NzdAttempting to mutate timeout of AuthByPlugin. Create a new instance with desired parameters instead.)loggerwarningr>   r)   r)   r*   r,   Y   s   r   c                 C     t )z1Return the Snowflake friendly name of auth class.NotImplementedErrorr>   r)   r)   r*   type__   s   zAuthByPlugin.type_strc                 C  rA   )zReturn a safe version of the information used to authenticate with Snowflake.

        This is used for logging, useful for printing temporary tokens, but make sure to
        mask secrets.
        rB   r>   r)   r)   r*   assertion_contente   s   zAuthByPlugin.assertion_contentconnr   authenticatorservice_name
str | Noneaccountuserpasswordr<   r	   c                K  rA   )zPrepare for authentication.

        This function is useful for situations where we need to reach out to a 3rd-party
        service before authenticating with Snowflake.
        rB   )r;   rG   rH   rI   rK   rL   rM   r<   r)   r)   r*   prepareo   s   zAuthByPlugin.preparebodydict[Any, Any]c                 C  rA   )z.Update the body of the authentication request.rB   )r;   rO   r)   r)   r*   update_body      zAuthByPlugin.update_bodyc                 C  rA   )zReset secret members.rB   r>   r)   r)   r*   reset_secrets   rR   zAuthByPlugin.reset_secretsdict[str, Any]c                K  rA   )zRe-perform authentication.

        The difference between this and authentication is that secrets will be removed
        from memory by the time this gets called.
        rB   )r;   rG   r<   r)   r)   r*   reauthenticate   s   zAuthByPlugin.reauthenticateretc                K  s@   t |dtdj|jj|jj|d dt|ddt	d dS )a  Handles a failure when an issue happens while connecting to Snowflake.

        If the user returns from this function execution will continue. The argument
        data can be manipulated from within this function and so recovery is possible
        from here.
        Nz1Failed to connect to DB: {host}:{port}, {message}message)hostportrW   code)msgerrnosqlstate)
r   errorhandler_wrapperr   format_rest_host_portr7   r6   r   )r;   rG   rV   r<   r)   r)   r*   _handle_failure   s   zAuthByPlugin._handle_failurec                K  s   | ddr~~~~~td | jjs$td| jjd  dtd}|td| jjd  d	 t	t
| jj | j  d
S )a  Default timeout handler.

        This will trigger if the authenticator
        hasn't implemented one. By default we retry on timeouts and use
        jitter to deduce the time to sleep before retrying. The sleep
        time ranges between 1 and 16 seconds.
        delete_paramsTz1Default timeout handler invoked for authenticatorz-Could not connect to Snowflake backend after r   z attempt(s).Aborting)r\   r]   z'Hit connection timeout, attempt number z. Will retry in a bit...N)popr?   debugr9   should_retryr   current_retry_countr   timesleepfloatcurrent_sleep_time	increment)r;   rH   rI   rK   rL   rM   r<   errorr)   r)   r*   handle_timeout   s   

zAuthByPlugin.handle_timeout)NN)r,   r-   r.   r/   r0   r1   )r0   r7   )r0   r1   )r0   r   )r0   rE   )rG   r   rH   rE   rI   rJ   rK   rE   rL   rE   rM   rJ   r<   r	   r0   rJ   )rO   rP   r0   r1   )rG   r   r<   r	   r0   rT   )rG   r   rV   rP   r<   r	   r0   r1   )rH   rE   rI   rJ   rK   rE   rL   rE   rM   rE   r<   r	   r0   r1   )r!   r"   r#   __doc__r=   propertyr,   setterr   rD   rF   rN   rQ   rS   rU   rd   rp   r)   r)   r)   r*   r+   ;   s2    
r+   )#
__future__r   loggingrj   abcr   r   enumr   r   osr   typingr   r	   r
   	errorcoder   errorsr   r   r   r^   r   	time_utilr    r   	getLoggerr!   r?   r8   r5   r   r+   r)   r)   r)   r*   <module>   s*   
