
    ug                         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j4                  e      Zd	d
gZdZ G d d
e      Z G d d	e      Z y)    N)override)	SecretStr)UserIdentityServerAuthenticationProviderClientAuthProviderClientAuthHeaders	AuthError)System)ChromaAuthError)OpenTelemetryGranularitytrace_method)Dict!BasicAuthenticationServerProviderBasicAuthClientProviderAuthorizationc                   B     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                  j	                  d       t        t        |j                  j                              | _        y )Nchroma_client_auth_credentials)	super__init__settings	_settingsrequirer   strr   _creds)selfr   	__class__s     W/var/www/openai/venv/lib/python3.12/site-packages/chromadb/auth/basic_authn/__init__.pyr   z BasicAuthClientProvider.__init__,   sI      @AFOO$R$R ST    c                     t        j                  | j                  j                          j	                  d            j                  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   sY    ""{{++-.66w?

&/ 	 !)fWI,>"?
 	
r!   )
__name__
__module____qualname____doc__r
   r   r   r   r+   __classcell__r   s   @r    r   r   &   s:    
Uv U$ U 
/ 
 
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 | _        | j                         }|D ]  }|j                         s|j                         j                  d      D cg c]  }| }}|r|d   rt        |      dk7  st        |      st        d| d      |d   }|d   }|| j                  v rt        d      t        |      | j                  |<    y c c}w )N: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v
_raw_credsusernamepasswordr   s	           r    r   z*BasicAuthenticationServerProvider.__init__F   s     ,.--/D::<%)ZZ\%7%7%<=%<!%<J=qM
Oq(: ::, GH H  "!}H!!}H4;;& 0 
 %.h$7DKK!-  >s   0	C(z.BasicAuthenticationServerProvider.authenticateheadersc           	      z   	 t         j                         |j                         vrt        t         dz         |t         j                            }t	        j
                  dd|      }|j                         }t        j                  |      j                  d      }d|vrt        d      |j                  dd      \  }}t        |      }t        |      }|| j                  vrt        d      t        j                  |j                  d      | j                  |   j!                         j                  d            }|st        d      t#        |	      S # t        $ r+}t$        j'                  d
t)        |              Y d }~nd }~wt*        $ rw}t-        j.                  |j0                        }|d   }	|	j2                  }
|	j4                  }t$        j'                  dt7        |      j8                   d| d|
        Y d }~nd }~ww xY wt;        j<                  t?        j@                  dd             tC               )Nz header not foundz^Basic  r#   r4   z#Invalid Authorization header formatr6   zInvalid username or password)user_idz7BasicAuthenticationServerProvider.authenticate failed: zNBasicAuthenticationServerProvider.authenticate failed: Failed to authenticate z at gMbP?g{Gzt?)"r)   lowerkeysr	   resubr8   r$   	b64decoder(   r9   r   r   bcryptcheckpwr'   r&   r   loggererrorrepr	Exception	traceback
extract_tb__traceback__linenofilenametyper,   timesleeprandomuniformr   )r   rC   _auth_headerbase64_decodedrA   rB   
_pwd_checketblast_call_stackline_numberrW   s               r    authenticate_or_raisez7BasicAuthenticationServerProvider.authenticate_or_raisee   s   
$	#))+7<<>A 47J JKK"#7#=#=#?@L66*b,?L'--/L#--l;BB7KN.( EFF!/!5!5c1!=Hh8}H8}Ht{{* >??(H%668??HJ  >??11 	LLI$q'S   		%%aoo6B fO)00K&//HLL**.q'*:*:);4z;-Y 		 	

NN5%(	
 s%   EE 	H!FHA-HH)r,   r-   r.   r/   r
   r   r   r   ALLr   r   r   r   rd   r0   r1   s   @r    r   r   <   s`    8v 8$ 8> 8:R:V:V ) T#s(^ )  )  ) r!   )!r$   r[   rJ   rY   rS   rM   logging	overridesr   pydanticr   chromadb.authr   r   r   r   r	   chromadb.configr
   chromadb.errorsr    chromadb.telemetry.opentelemetryr   r   typingr   	getLoggerr,   rO   __all__r)   r   r    r!   r    <module>rq      s}      	        # +  
		8	$.0I
J& 
0 
,V (D V r!   