
    ug                    J   d dl mZ d dlmZ d dlmZ d dlmZ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  ed      Z ed	      Z G d
 de      Ze
eef   Z G d de      Ze G d d             Z G d de      Z G d dee      Ze G d d             Z G d de      Zy)    )annotations)abstractmethod)Enum)AnyListOptionalDictTupleTypeVar)	dataclass)	SecretStr)	ComponentSystemTSc                      e Zd Zy)	AuthErrorN)__name__
__module____qualname__     K/var/www/openai/venv/lib/python3.12/site-packages/chromadb/auth/__init__.pyr   r      s    r   r   c                  6     e Zd ZdZd fdZedd       Z xZS )ClientAuthProviderz
    ClientAuthProvider is responsible for providing authentication headers for
    client requests. Client implementations (in our case, just the FastAPI
    client) must inject these headers into their requests.
    c                $    t         |   |       y Nsuper__init__selfsystem	__class__s     r   r    zClientAuthProvider.__init__(        r   c                     y r   r   )r"   s    r   authenticatezClientAuthProvider.authenticate+       r   r#   r   returnNone)r*   ClientAuthHeaders)r   r   r   __doc__r    r   r'   __classcell__r$   s   @r   r   r   !   s!    !  r   r   c                  F    e Zd ZU dZded<   dZded<   dZded<   dZd	ed
<   y)UserIdentitya  
    UserIdentity represents the identity of a user. In general, not all fields
    will be populated, and the fields that are populated will depend on the
    authentication provider.

    The idea is that the AuthenticationProvider is responsible for populating
    _all_ information known about the user, and the AuthorizationProvider is
    responsible for making decisions based on that information.
    struser_idNOptional[str]tenantzOptional[List[str]]	databaseszOptional[Dict[str, Any]]
attributes)r   r   r   r-   __annotations__r5   r6   r7   r   r   r   r1   r1   0   s0     L FM %)I") ,0J(/r   r1   c                  V     e Zd ZdZd fdZedd       Zd	dZd
dZ	 	 	 	 ddZ	 xZ
S )ServerAuthenticationProvidera  
    ServerAuthenticationProvider is responsible for authenticating requests. If
    a ServerAuthenticationProvider is configured, it will be called by the
    server to authenticate requests. If no ServerAuthenticationProvider is
    configured, all requests will be authenticated.

    The ServerAuthenticationProvider should return a UserIdentity object if the
    request is authenticated for use by the ServerAuthorizationProvider.
    c                    t         |   |       |j                  j                  | _        |j                  j
                  | _        y r   )r   r    settingschroma_server_auth_ignore_paths_ignore_auth_paths;chroma_overwrite_singleton_tenant_database_access_from_auth4overwrite_singleton_tenant_database_access_from_authr!   s     r   r    z%ServerAuthenticationProvider.__init__P   s>      OO;; 	 OOWW 	Ar   c                     y r   r   )r"   headerss     r   authenticate_or_raisez2ServerAuthenticationProvider.authenticate_or_raiseY   r(   r   c                |    || j                   j                         v r |j                         | j                   |   v ryy)NTF)r>   keysupper)r"   verbpaths      r   ignore_operationz-ServerAuthenticationProvider.ignore_operation]   s8    D++0022

 7 7 ==r   c                2   d }d }| j                   j                  j                  r"t        | j                   j                  d         }| j                   j                  j                  r"t        | j                   j                  d         }|s|st        d      |r|rt        d      |r"|j                  d      D cg c]  }|s|	 c}S |r&t        |d      5 }|j                         cd d d        S t        d      c c}w # 1 sw Y   t        d      xY w)N$chroma_server_authn_credentials_filechroma_server_authn_credentialszNNo credentials file or credentials found in [chroma_server_authn_credentials].zDBoth credentials file and credentials found.Please provide only one.
rShould never happen)	_systemr<   rK   r2   rL   
ValueErrorsplitopen	readlines)r"   _creds_file_credscfs        r   read_creds_or_creds_filez5ServerAuthenticationProvider.read_creds_or_creds_filee   s   <<  EE%%&LMK <<  @@../PQRF65  6+  %||D171!QA177k3'1{{} ('.//	 8'.//s   <C>C>DDc                   | j                   r|syd}d}|j                  r|j                  dk7  r|j                  }|j                  r9t        |j                        dk(  r!|j                  d   dk7  r|j                  d   }||fS )aR  
        If settings.chroma_overwrite_singleton_tenant_database_access_from_auth
        is False, this function always returns (None, None).

        If settings.chroma_overwrite_singleton_tenant_database_access_from_auth
        is True, follows the following logic:
        - If the user only has access to a single tenant, this function will
          return that tenant as its first return value.
        - If the user only has access to a single database, this function will
          return that database as its second return value. If the user has
          access to multiple tenants and/or databases, including "*", this
          function will return None for the corresponding value(s).
        - If the user has access to multiple tenants and/or databases this
          function will return None for the corresponding value(s).
        )NNN*   r   )r@   r5   r6   len)r"   userr5   databases       r   'singleton_tenant_database_if_applicablezDServerAuthenticationProvider.singleton_tenant_database_if_applicable   sy    $ HHPT;;4;;#-[[F>>c$..1Q64>>!;LPS;S~~a(Hxr   r)   )rB   zDict[str, str]r*   r1   )rG   r2   rH   r2   r*   boolr*   z	List[str])r^   zOptional[UserIdentity]r*   z#Tuple[Optional[str], Optional[str]])r   r   r   r-   r    r   rC   rI   rY   r`   r.   r/   s   @r   r:   r:   E   s@    
  06 * 	, r   r:   c                  d    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ZdZdZdZdZdZdZdZdZdZdZy)AuthzActionzR
    The set of actions that can be authorized by the authorization provider.
    zsystem:resetztenant:create_tenantztenant:get_tenantzdb:create_databasezdb:get_databasezdb:delete_databasezdb:list_databaseszdb:list_collectionszdb:count_collectionszdb:create_collectionzdb:get_or_create_collectionzcollection:get_collectionzcollection:delete_collectionzcollection:update_collectionzcollection:addzcollection:deletezcollection:getzcollection:queryzcollection:countzcollection:updatezcollection:upsertN)r   r   r   r-   RESETCREATE_TENANT
GET_TENANTCREATE_DATABASEGET_DATABASEDELETE_DATABASELIST_DATABASESLIST_COLLECTIONSCOUNT_COLLECTIONSCREATE_COLLECTIONGET_OR_CREATE_COLLECTIONGET_COLLECTIONDELETE_COLLECTIONUPDATE_COLLECTIONADDDELETEGETQUERYCOUNTUPDATEUPSERTr   r   r   rd   rd      sx     E*M$J*O$L*O(N,..<0N66
C F
CEE F Fr   rd   c                  0    e Zd ZU dZded<   ded<   ded<   y)AuthzResourcezB
    The resource being accessed in an authorization request.
    r4   r5   r_   
collectionN)r   r   r   r-   r8   r   r   r   r{   r{      s     r   r{   c                  N     e Zd ZdZd fdZe	 	 	 	 	 	 	 	 dd       ZddZ xZS )ServerAuthorizationProviderat  
    ServerAuthorizationProvider is responsible for authorizing requests. If a
    ServerAuthorizationProvider is configured, it will be called by the server
    to authorize requests. If no ServerAuthorizationProvider is configured, all
    requests will be authorized.

    ServerAuthorizationProvider should raise an exception if the request is not
    authorized.
    c                $    t         |   |       y r   r   r!   s     r   r    z$ServerAuthorizationProvider.__init__   r%   r   c                     y r   r   )r"   r^   actionresources       r   authorize_or_raisez.ServerAuthorizationProvider.authorize_or_raise   s     	r   c                    d }d }| j                   j                  j                  r| j                   j                  d   }| j                   j                  j                  r"t	        | j                   j                  d         }|s|st        d      |r|rt        d      |r"|j                  d      D cg c]  }|s|	 c}S |r&t        |d      5 }|j                         cd d d        S t        d      c c}w # 1 sw Y   t        d      xY w)Nchroma_server_authz_config_filechroma_server_authz_configz9No authz configuration file or authz configuration found.zTBoth authz configuration file and authz configuration found.Please provide only one.rM   rN   rO   )	rP   r<   r   r   r2   rQ   rR   rS   rT   )r"   _config_file_configrW   rX   s        r   read_config_or_config_filez6ServerAuthorizationProvider.read_config_or_config_file   s    <<  @@<<001RSL<<  ;;$,,//0LMNGGK  G+  &}}T282!aA288lC(A{{} )(.//	 9(.//s   3C5;C5C::Dr)   )r^   r1   r   rd   r   r{   r*   r+   rb   )	r   r   r   r-   r    r   r   r   r.   r/   s   @r   r~   r~      sF    !  *5AN	 
0r   r~   N) 
__future__r   abcr   enumr   typingr   r   r   r	   r
   r   dataclassesr   pydanticr   chromadb.configr   r   r   r   	Exceptionr   r2   r,   r   r1   r:   rd   r{   r~   r   r   r   <module>r      s    "    " 
 CLCL		 	 i(   0 0 0(U 9 U p!#t !8   )0) )0r   