
    (phKB                     `   S r SSKrSSKrSSKrSSKrSSKJr  SSKJr  SSKJr  SSKJr  Sr	Sr
\R                  " S	5      r/ S
Qr\R                  r\R                  rSS jrSS jr SS jr  SS jrSS jrS\
S4S jrS r " S S5      r " S S5      r " S S5      r " S S5      rS rg)zFirebase tenant management module.

This module contains functions for creating and configuring authentication tenants within a
Google Cloud Identity Platform (GCIP) instance.
    N)auth)_auth_utils)_http_client)_utils_tenant_mgtd   z^[a-zA-Z][a-zA-Z0-9-]{3,19}$)
ListTenantsPageTenantTenantIdMismatchErrorTenantNotFoundErrorauth_for_tenantcreate_tenantdelete_tenant
get_tenantlist_tenantsupdate_tenantc                 :    [        U5      nUR                  U 5      $ )a  Gets an Auth Client instance scoped to the given tenant ID.

Args:
    tenant_id: A tenant ID string.
    app: An App instance (optional).

Returns:
    auth.Client: An ``auth.Client`` object.

Raises:
    ValueError: If the tenant ID is None, empty or not a string.
)_get_tenant_mgt_servicer   	tenant_idapptenant_mgt_services      L/var/www/html/venv/lib/python3.13/site-packages/firebase_admin/tenant_mgt.pyr   r   9   s      15--i88    c                 :    [        U5      nUR                  U 5      $ )a{  Gets the tenant corresponding to the given ``tenant_id``.

Args:
    tenant_id: A tenant ID string.
    app: An App instance (optional).

Returns:
    Tenant: A tenant object.

Raises:
    ValueError: If the tenant ID is None, empty or not a string.
    TenantNotFoundError: If no tenant exists by the given ID.
    FirebaseError: If an error occurs while retrieving the tenant.
)r   r   r   s      r   r   r   J   s      15((33r   c                 8    [        U5      nUR                  XUS9$ )a  Creates a new tenant from the given options.

Args:
    display_name: Display name string for the new tenant. Must begin with a letter and contain
        only letters, digits and hyphens. Length must be between 4 and 20.
    allow_password_sign_up: A boolean indicating whether to enable or disable the email sign-in
        provider (optional).
    enable_email_link_sign_in: A boolean indicating whether to enable or disable email link
        sign-in (optional). Disabling this makes the password required for email sign-in.
    app: An App instance (optional).

Returns:
    Tenant: A tenant object.

Raises:
    ValueError: If any of the given arguments are invalid.
    FirebaseError: If an error occurs while creating the tenant.
display_nameallow_password_sign_upenable_email_link_sign_in)r   r   )r   r   r    r   r   s        r   r   r   ]   s-    ( 15++!"; , = =r   c                 :    [        U5      nUR                  XUUS9$ )a  Updates an existing tenant with the given options.

Args:
    tenant_id: ID of the tenant to update.
    display_name: Updated display name string for the tenant (optional).
    allow_password_sign_up: A boolean indicating whether to enable or disable the email sign-in
        provider.
    enable_email_link_sign_in: A boolean indicating whether to enable or disable email link
        sign-in. Disabling this makes the password required for email sign-in.
    app: An App instance (optional).

Returns:
    Tenant: The updated tenant object.

Raises:
    ValueError: If any of the given arguments are invalid.
    TenantNotFoundError: If no tenant exists by the given ID.
    FirebaseError: If an error occurs while creating the tenant.
r   )r   r   )r   r   r   r    r   r   s         r   r   r   w   s0    , 15++E["; , = =r   c                 <    [        U5      nUR                  U 5        g)aW  Deletes the tenant corresponding to the given ``tenant_id``.

Args:
    tenant_id: A tenant ID string.
    app: An App instance (optional).

Raises:
    ValueError: If the tenant ID is None, empty or not a string.
    TenantNotFoundError: If no tenant exists by the given ID.
    FirebaseError: If an error occurs while retrieving the tenant.
N)r   r   r   s      r   r   r      s     15$$Y/r   c                 >   ^ [        U5      mU4S jn[        X0U5      $ )a  Retrieves a page of tenants from a Firebase project.

The ``page_token`` argument governs the starting point of the page. The ``max_results``
argument governs the maximum number of tenants that may be included in the returned page.
This function never returns None. If there are no user accounts in the Firebase project, this
returns an empty page.

Args:
    page_token: A non-empty page token string, which indicates the starting point of the page
        (optional). Defaults to ``None``, which will retrieve the first page of users.
    max_results: A positive integer indicating the maximum number of users to include in the
        returned page (optional). Defaults to 100, which is also the maximum number allowed.
    app: An App instance (optional).

Returns:
    ListTenantsPage: A page of tenants.

Raises:
    ValueError: If ``max_results`` or ``page_token`` are invalid.
    FirebaseError: If an error occurs while retrieving the user accounts.
c                 &   > TR                  X5      $ N)r   )
page_tokenmax_resultsr   s     r   downloadlist_tenants.<locals>.download   s    !..zGGr   )r   r	   )r&   r'   r   r(   r   s       @r   r   r      s#    , 15H8==r   c                 B    [         R                  " U [        [        5      $ r%   )r   get_app_service_TENANT_MGT_ATTRIBUTE_TenantManagementService)r   s    r   r   r      s    !!#'<>VWWr   c                   ^    \ rS rSrSrS r\S 5       r\S 5       r\S 5       r	\S 5       r
Srg	)
r
      a  Represents a tenant in a multi-tenant application.

Multi-tenancy support requires Google Cloud Identity Platform (GCIP). To learn more about
GCIP including pricing and features, see https://cloud.google.com/identity-platform.

Before multi-tenancy can be used in a Google Cloud Identity Platform project, tenants must be
enabled in that project via the Cloud Console UI. A Tenant instance provides information
such as the display name, tenant identifier and email authentication configuration.
c                     [        U[        5      (       d  [        SR                  U5      5      eSU;  a  [        S5      eXl        g )Nz0Invalid data argument in Tenant constructor: {0}namez&Tenant response missing required keys.)
isinstancedict
ValueErrorformat_dataselfdatas     r   __init__Tenant.__init__   s?    $%%OVVW[\]]~EFF
r   c                 H    U R                   S   nUR                  S5      S   $ )Nr1   /)r6   split)r8   r1   s     r   r   Tenant.tenant_id   s#    zz&!zz#r""r   c                 8    U R                   R                  S5      $ )NdisplayNamer6   getr8   s    r   r   Tenant.display_name   s    zz~~m,,r   c                 :    U R                   R                  SS5      $ )NallowPasswordSignupFrC   rE   s    r   r   Tenant.allow_password_sign_up   s    zz~~3U;;r   c                 :    U R                   R                  SS5      $ )NenableEmailLinkSigninFrC   rE   s    r   r     Tenant.enable_email_link_sign_in   s    zz~~5u==r   )r6   N)__name__
__module____qualname____firstlineno____doc__r:   propertyr   r   r   r    __static_attributes__ r   r   r
   r
      s\     # # - - < < > >r   r
   c                   \    \ rS rSrSrSrS rS rS r SS jr	  SS	 jr
S
 rS\4S jrSrg)r-      z#Firebase tenant management service.z)https://identitytoolkit.googleapis.com/v2c                 H   UR                   R                  5       nSR                  [        R                  5      nSR                  U R
                  UR                  5      nXl        [        R                  " X$SU0S9U l
        0 U l        [        R                  " 5       U l        g )NzPython/Admin/{0}z{0}/projects/{1}zX-Client-Version)
credentialbase_urlheaders)rX   get_credentialr5   firebase_admin__version__TENANT_MGT_URL
project_idr   r   JsonHttpClientclienttenant_clients	threadingRLocklock)r8   r   rX   version_headerrY   s        r   r:   !_TenantManagementService.__init__   s    ^^224
+22>3M3MN%,,T-@-@#..Q"11!?QSa>bd OO%	r   c                 h   [        U[        5      (       a  U(       d  [        SR                  U5      5      eU R                     XR
                  ;   a  U R
                  U   sSSS5        $ [        R                  " U R                  US9nX R
                  U'   UsSSS5        $ ! , (       d  f       g= f)z;Gets an Auth Client instance scoped to the given tenant ID.=Invalid tenant ID: {0}. Tenant ID must be a non-empty string.N)r   )	r2   strr4   r5   re   rb   r   Clientr   )r8   r   ra   s      r   r   (_TenantManagementService.auth_for_tenant   s    )S))OVVW`ac c YY///**95 Y [[Y?F-3	* YYs   B#+.B##
B1c                 T   [        U[        5      (       a  U(       d  [        SR                  U5      5      e U R                  R                  SSR                  U5      5      n[        U5      $ ! [        R                  R                   a  n[        R                  " U5      eSnAff = f)z9Gets the tenant corresponding to the given ``tenant_id``.ri   rD   /tenants/{0}N)r2   rj   r4   r5   ra   bodyr
   requests
exceptionsRequestExceptionr   handle_auth_backend_error)r8   r   ro   errors       r   r   #_TenantManagementService.get_tenant  s    )S))OVVW`ac c	 ;;##E>+@+@+KLD $< ""33 	?77>>	?s   +A. .B'B""B'Nc                 V   S[        U5      0nUb  [        R                  " US5      US'   Ub  [        R                  " US5      US'    U R                  R	                  SSUS9n[        U5      $ ! [        R                  R                   a  n[        R                  " U5      eSnAff = f)z/Creates a new tenant from the given parameters.rB   NrH   rK   post/tenants)json)
_validate_display_namer   validate_booleanra   ro   r
   rp   rq   rr   rs   )r8   r   r   r    payloadro   rt   s          r   r   &_TenantManagementService.create_tenant  s     !"8"FG!--8-I-I&(=.?G)*$0/:/K/K)+B0DG+,	 ;;##FJW#ED $< ""33 	?77>>	?s   	A/ /B(B##B(c                 b   [        U[        5      (       a  U(       d  [        S5      e0 nUb  [        U5      US'   Ub  [        R
                  " US5      US'   Ub  [        R
                  " US5      US'   U(       d  [        S5      eSR                  U5      nSR                  [        R                  " U5      5      nS	R                  U5      n U R                  R                  S
XeUS9n	[        U	5      $ ! [        R                  R                   a  n
[        R                  " U
5      eSn
A
ff = f)z7Updates the specified tenant with the given parameters.z%Tenant ID must be a non-empty string.NrB   rH   rK   z4At least one parameter must be specified for update.rn   ,zupdateMask={0}patch)ry   params)r2   rj   r4   rz   r   r{   r5   joinbuild_update_maskra   ro   r
   rp   rq   rr   rs   )r8   r   r   r   r    r|   urlupdate_maskr   ro   rt   s              r   r   &_TenantManagementService.update_tenant%  s#    )S))DEE#%;L%IGM"!--8-I-I&(=.?G)*$0/:/K/K)+B0DG+, STT##I.hh{<<WEF!((5	 ;;##GSv#ND $< ""33 	?77>>	?s   C5 5D.D))D.c                 @   [        U[        5      (       a  U(       d  [        SR                  U5      5      e U R                  R                  SSR                  U5      5        g! [        R                  R                   a  n[        R                  " U5      eSnAff = f)z<Deletes the tenant corresponding to the given ``tenant_id``.ri   deletern   N)r2   rj   r4   r5   ra   requestrp   rq   rr   r   rs   )r8   r   rt   s      r   r   &_TenantManagementService.delete_tenantC  s    )S))OVVW`ac c	?KK.*?*?	*JK""33 	?77>>	?s   +A$ $BBBc                    Ub'  [        U[        5      (       a  U(       d  [        S5      e[        U[        5      (       d  [        S5      eUS:  d
  U[        :  a  [        SR                  [        5      5      eSU0nU(       a  XS'    U R                  R                  SS	US
9$ ! [        R                  R                   a  n[        R                  " U5      eSnAff = f)zRetrieves a batch of tenants.Nz&Page token must be a non-empty string.zMax results must be an integer.   zAMax results must be a positive integer less than or equal to {0}.pageSize	pageTokenrD   rx   )r   )r2   rj   r4   int_MAX_LIST_TENANTS_RESULTSr5   ra   ro   rp   rq   rr   r   rs   )r8   r&   r'   r|   rt   s        r   r   %_TenantManagementService.list_tenantsN  s    !j#..j !IJJ+s++>???k,EEv78: : {+#-K 	?;;##E:g#FF""33 	?77>>	?s   	B$ $CCC)r   ra   re   rb   )NNNNN)rM   rN   rO   rP   rQ   r^   r:   r   r   r   r   r   r   r   rS   rT   r   r   r-   r-      sF    -@N&  X\ ( HL&* <	? '+8Q ?r   r-   c                   Z    \ rS rSrSrS r\S 5       r\S 5       r\S 5       r	S r
S rS	rg
)r	   ic  a8  Represents a page of tenants fetched from a Firebase project.

Provides methods for traversing tenants included in this page, as well as retrieving
subsequent pages of tenants. The iterator returned by ``iterate_all()`` can be used to iterate
through all tenants in the Firebase project starting from this page.
c                 6    Xl         X0l        U" X#5      U l        g r%   )	_download_max_results_current)r8   r(   r&   r'   s       r   r:   ListTenantsPage.__init__k  s    !' 9r   c                 x    U R                   R                  S/ 5       Vs/ s H  n[        U5      PM     sn$ s  snf )zBA list of ``ExportedUserRecord`` instances available in this page.tenants)r   rD   r
   r7   s     r   r   ListTenantsPage.tenantsp  s3     *.):):9b)IJ)It)IJJJs   7c                 :    U R                   R                  SS5      $ )zKPage token string for the next page (empty string indicates no more pages).nextPageToken )r   rD   rE   s    r   next_page_tokenListTenantsPage.next_page_tokenu  s     }}  "55r   c                 ,    [        U R                  5      $ )z6A boolean indicating whether more pages are available.)boolr   rE   s    r   has_next_pageListTenantsPage.has_next_pagez  s     D(())r   c                 |    U R                   (       a+  [        U R                  U R                  U R                  5      $ g)zRetrieves the next page of tenants, if available.

Returns:
    ListTenantsPage: Next page of tenants, or None if this is the last page.
N)r   r	   r   r   r   rE   s    r   get_next_pageListTenantsPage.get_next_page  s/     "4>>43G3GIZIZ[[r   c                     [        U 5      $ )a  Retrieves an iterator for tenants.

Returned iterator will iterate through all the tenants in the Firebase project
starting from this page. The iterator will never buffer more than one page of tenants
in memory at a time.

Returns:
    iterator: An iterator of Tenant instances.
)_TenantIteratorrE   s    r   iterate_allListTenantsPage.iterate_all  s     t$$r   )r   r   r   N)rM   rN   rO   rP   rQ   r:   rR   r   r   r   r   r   rS   rT   r   r   r	   r	   c  sT    :
 K K 6 6 * *
%r   r	   c                   0    \ rS rSrSrS rS rS rS rSr	g)	r   i  zAn iterator that allows iterating over tenants.

This implementation loads a page of tenants into memory, and iterates on them. When the whole
page has been traversed, it loads another page. This class never keeps more than one page
of entries in memory.
c                 B    U(       d  [        S5      eXl        SU l        g )NzCurrent page must not be None.r   )r4   _current_page_index)r8   current_pages     r   r:   _TenantIterator.__init__  s    =>>)r   c                    U R                   [        U R                  R                  5      :X  aA  U R                  R                  (       a&  U R                  R                  5       U l        SU l         U R                   [        U R                  R                  5      :  a:  U R                  R                  U R                      nU =R                   S-  sl         U$ [        e)Nr   r   )r   lenr   r   r   r   StopIteration)r8   results     r   next_TenantIterator.next  s    ;;#d008899!!//%)%7%7%E%E%G";;T//7788''//<FKK1KMr   c                 "    U R                  5       $ r%   )r   rE   s    r   __next___TenantIterator.__next__  s    yy{r   c                     U $ r%   rT   rE   s    r   __iter___TenantIterator.__iter__  s    r   )r   r   N)
rM   rN   rO   rP   rQ   r:   r   r   r   rS   rT   r   r   r   r     s    	r   r   c                     [        U [        5      (       d  [        S5      e[        R	                  U 5      (       d  [        S5      eU $ )NzInvalid type for displayNamezjdisplayName must start with a letter and only consist of letters, digits and hyphens with 4-20 characters.)r2   rj   r4   _DISPLAY_NAME_PATTERNsearch)r   s    r   rz   rz     sF    lC((788 ''55,- 	- r   r%   r   )NNNN)rQ   rerc   rp   r\   r   r   r   r   r,   r   compiler   __all__r   r   r   r   r   r   r   r   r   r
   r-   r	   r   rz   rT   r   r   <module>r      s    
     & ' ! &  

#AB  $99 !55 9"4( X\=6 ^b=80  !.GT >8X"> ">Jx? x?v0% 0%f @r   