1414
1515"""Firebase auth client sub module."""
1616
17+ import os
1718import time
1819
1920import firebase_admin
2425from firebase_admin import _user_identifier
2526from firebase_admin import _user_import
2627from firebase_admin import _user_mgt
28+ from firebase_admin import _utils
2729
30+ _EMULATOR_HOST_ENV_VAR = 'FIREBASE_AUTH_EMULATOR_HOST'
31+ _DEFAULT_AUTH_URL = 'https://identitytoolkit.googleapis.com'
2832
2933class Client :
3034 """Firebase Authentication client scoped to a specific tenant."""
@@ -36,17 +40,38 @@ def __init__(self, app, tenant_id=None):
3640 2. set the project ID explicitly via Firebase App options, or
3741 3. set the project ID via the GOOGLE_CLOUD_PROJECT environment variable.""" )
3842
39- credential = app . credential . get_credential ()
43+ credential = None
4044 version_header = 'Python/Admin/{0}' .format (firebase_admin .__version__ )
45+ # Non-default endpoint URLs for emulator support are set in this dict later.
46+ endpoint_urls = {}
47+
48+ # If an emulator is present, check that the given value matches the expected format and set
49+ # endpoint URLs to use the emulator. Additionally, use a fake credential.
50+ emulator_host = os .environ .get (_EMULATOR_HOST_ENV_VAR )
51+ if emulator_host :
52+ if '//' in emulator_host :
53+ raise ValueError (
54+ 'Invalid {0}: "{1}". It must follow format "host:port".' .format (
55+ _EMULATOR_HOST_ENV_VAR , emulator_host ))
56+ base_url = 'http://{0}/identitytoolkit.googleapis.com' .format (emulator_host )
57+ endpoint_urls ['v1' ] = base_url + '/v1'
58+ endpoint_urls ['v2beta1' ] = base_url + '/v2beta1'
59+ credential = _utils .EmulatorAdminCredentials ()
60+ else :
61+ # Use credentials if provided
62+ credential = app .credential .get_credential ()
63+
4164 http_client = _http_client .JsonHttpClient (
4265 credential = credential , headers = {'X-Client-Version' : version_header })
4366
4467 self ._tenant_id = tenant_id
45- self ._token_generator = _token_gen .TokenGenerator (app , http_client )
68+ self ._token_generator = _token_gen .TokenGenerator (
69+ app , http_client , url_override = endpoint_urls .get ('v1' ))
4670 self ._token_verifier = _token_gen .TokenVerifier (app )
47- self ._user_manager = _user_mgt .UserManager (http_client , app .project_id , tenant_id )
71+ self ._user_manager = _user_mgt .UserManager (
72+ http_client , app .project_id , tenant_id , url_override = endpoint_urls .get ('v1' ))
4873 self ._provider_manager = _auth_providers .ProviderConfigClient (
49- http_client , app .project_id , tenant_id )
74+ http_client , app .project_id , tenant_id , url_override = endpoint_urls . get ( 'v2beta1' ) )
5075
5176 @property
5277 def tenant_id (self ):
0 commit comments