14
14
15
15
"""Firebase auth client sub module."""
16
16
17
+ import os
17
18
import time
18
19
19
20
import firebase_admin
24
25
from firebase_admin import _user_identifier
25
26
from firebase_admin import _user_import
26
27
from firebase_admin import _user_mgt
28
+ from firebase_admin import _utils
27
29
30
+ _EMULATOR_HOST_ENV_VAR = 'FIREBASE_AUTH_EMULATOR_HOST'
31
+ _DEFAULT_AUTH_URL = 'https://identitytoolkit.googleapis.com'
28
32
29
33
class Client :
30
34
"""Firebase Authentication client scoped to a specific tenant."""
@@ -36,17 +40,38 @@ def __init__(self, app, tenant_id=None):
36
40
2. set the project ID explicitly via Firebase App options, or
37
41
3. set the project ID via the GOOGLE_CLOUD_PROJECT environment variable.""" )
38
42
39
- credential = app . credential . get_credential ()
43
+ credential = None
40
44
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
+
41
64
http_client = _http_client .JsonHttpClient (
42
65
credential = credential , headers = {'X-Client-Version' : version_header })
43
66
44
67
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' ))
46
70
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' ))
48
73
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' ) )
50
75
51
76
@property
52
77
def tenant_id (self ):
0 commit comments