
    _j                     
   d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlmZ d dl	m
Z
 d dlmZmZmZmZmZ d dlmZ d dlmZ d dlmZmZ d dlmZ  ej        e          Zd	d
gZdZ G d d
e          Z G d d	e          Z dS )    N)override)	SecretStr)UserIdentityServerAuthenticationProviderClientAuthProviderClientAuthHeaders	AuthError)System)ChromaAuthError)OpenTelemetryGranularitytrace_method)Dict!BasicAuthenticationServerProviderBasicAuthClientProviderAuthorizationc                   H     e Zd ZdZdeddf fdZedefd            Z xZ	S )r   z
    Client auth provider for basic auth. The credentials are passed as a
    base64-encoded string in the Authorization header prepended with "Basic ".
    systemreturnNc                     t                                          |           |j        | _        |j                            d           t          t          |j        j                            | _        d S )Nchroma_client_auth_credentials)	super__init__settings	_settingsrequirer   strr   _creds)selfr   	__class__s     _/var/www/FlaskApp/flask-venv/lib/python3.11/site-packages/chromadb/auth/basic_authn/__init__.pyr   z BasicAuthClientProvider.__init__,   s[        @AAAFO$R S STT    c                     t          j        | j                                                             d                                        d          }t          t          d|           iS )Nutf-8zBasic )base64	b64encoder   get_secret_valueencodedecodeAUTHORIZATION_HEADERr   )r   encodeds     r    authenticatez$BasicAuthClientProvider.authenticate2   sf    "{++--/66w??
 

&// 	 !),>W,>,>"?"?
 	
r!   )
__name__
__module____qualname____doc__r
   r   r   r   r+   __classcell__r   s   @r    r   r   &   s         
Uv U$ U U U U U U 
/ 
 
 
 X
 
 
 
 
r!   c                        e Zd ZdZdeddf fdZ edej                  e	de
eef         defd                        Z xZS )	r   a/  
    Server auth provider for basic auth. The credentials are read from
    `chroma_server_authn_credentials_file` and each line must be in the format
    <username>:<bcrypt passwd>.

    Expects tokens to be passed as a base64-encoded string in the Authorization
    header prepended with "Basic".
    r   r   Nc                 *   t                                          |           |j        | _        i | _        |                                 }|D ]}|                                sd |                                                    d          D             }|r|d         rt          |          dk    st          |          st          d| d          |d         }|d         }|| j        v rt          d          t          |          | j        |<   d S )	Nc                     g | ]}|S  r5   ).0vs     r    
<listcomp>z>BasicAuthenticationServerProvider.__init__.<locals>.<listcomp>P   s    ===!===r!   :r      z$Invalid htpasswd credentials found: z3. Lines must be exactly <username>:<bcrypt passwd>.   zXDuplicate username found in [chroma_server_authn_credentials]. Usernames must be unique.)r   r   r   r   r   read_creds_or_creds_filestripsplitlenall
ValueErrorr   )r   r   credsline
_raw_credsusernamepasswordr   s          r    r   z*BasicAuthenticationServerProvider.__init__F   sE      ,.--// 	8 	8D::<< ==TZZ\\%7%7%<%<===J	qM	 
OOq((: ) !H: H H H   "!}H!!}H4;&& 0  
 %.h$7$7DK!!-	8 	8r!   z.BasicAuthenticationServerProvider.authenticateheadersc           	         	 t                                           |                                vrt          t           dz             |t                                                    }t	          j        dd|          }|                                }t          j        |          	                    d          }d|vrt          d          |
                    dd          \  }}t          |          }t          |          }|| j        vrt          d          t          j        |                    d          | j        |                                                             d                    }|st          d          t#          |	          S # t          $ r4}t$                              d
t)          |                      Y d }~n~d }~wt*          $ rn}t-          j        |j                  }|d         }	|	j        }
|	j        }t$                              dt7          |          j         d| d|
            Y d }~nd }~ww xY wt;          j        t?          j         dd                     tC                      )Nz header not foundz^Basic  r#   r9   z#Invalid Authorization header formatr;   zInvalid username or password)user_idz7BasicAuthenticationServerProvider.authenticate failed: zNBasicAuthenticationServerProvider.authenticate failed: Failed to authenticate z at gMbP?g{Gzt?)"r)   lowerkeysr	   resubr=   r$   	b64decoder(   r>   r   r   bcryptcheckpwr'   r&   r   loggererrorrepr	Exception	traceback
extract_tb__traceback__linenofilenametyper,   timesleeprandomuniformr   )r   rG   _auth_headerbase64_decodedrE   rF   
_pwd_checketblast_call_stackline_numberr[   s               r    authenticate_or_raisez7BasicAuthenticationServerProvider.authenticate_or_raisee   s   
$	#))++7<<>>AA 47J JKKK"#7#=#=#?#?@L6*b,??L'--//L#-l;;BB7KKN.(( EFFF!/!5!5c1!=!=Hh8}}H8}}Ht{** >???((H%6688??HH J  @ >???1111 	 	 	LLS$q''SS         		 		 		%ao66B fO)0K&/HLLY*.q''*:Y Y@HY YKVY Y       		 	
N5%((	
 	
 	
 s%   FF 
I*G

IA$I  I)r,   r-   r.   r/   r
   r   r   r   ALLr   r   r   r   rh   r0   r1   s   @r    r   r   <   s         8v 8$ 8 8 8 8 8 8> \8:R:V  ) T#s(^ )  )  )  )  X )  )  )  )  ) r!   )!r$   r_   rN   r]   rW   rQ   logging	overridesr   pydanticr   chromadb.authr   r   r   r   r	   chromadb.configr
   chromadb.errorsr    chromadb.telemetry.opentelemetryr   r   typingr   	getLoggerr,   rS   __all__r)   r   r   r5   r!   r    <module>rt      s     				                                 # " " " " " + + + + + +              
	8	$	$.0I
J& 
 
 
 
 
0 
 
 
,V  V  V  V  V (D V  V  V  V  V r!   