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
25
26
from firebase_admin import _user_import
26
27
from firebase_admin import _user_mgt
27
28
29
+ _EMULATOR_HOST_ENV_VAR = 'FIREBASE_AUTH_EMULATOR_HOST'
30
+ _DEFAULT_AUTH_URL = 'https://identitytoolkit.googleapis.com'
28
31
29
32
class Client :
30
33
"""Firebase Authentication client scoped to a specific tenant."""
@@ -36,17 +39,39 @@ def __init__(self, app, tenant_id=None):
36
39
2. set the project ID explicitly via Firebase App options, or
37
40
3. set the project ID via the GOOGLE_CLOUD_PROJECT environment variable.""" )
38
41
39
- credential = app . credential . get_credential ()
42
+ credential = None
40
43
version_header = 'Python/Admin/{0}' .format (firebase_admin .__version__ )
44
+ http_headers = {'X-Client-Version' : version_header }
45
+ # Fallback to default endpoint URLs if an emulator isn't present.
46
+ id_toolkit_endpoints = {
47
+ "v1" : None ,
48
+ "v2beta1" : None ,
49
+ }
50
+
51
+ # If an emulator is present, check that the given value matches the expected format and set endpoint URLs to use
52
+ # the emulator. Also set a fake authorization token.
53
+ emulator_host = os .environ .get (_EMULATOR_HOST_ENV_VAR )
54
+ if emulator_host :
55
+ if '//' in emulator_host :
56
+ raise ValueError (
57
+ 'Invalid {0}: "{1}". It must follow format "host:port".' .format (
58
+ _EMULATOR_HOST_ENV_VAR , emulator_host ))
59
+ http_headers ["Authorization" ] = "Bearer owner"
60
+ base_url = "http://{0}/identitytoolkit.googleapis.com" .format (emulator_host )
61
+ id_toolkit_endpoints ["v1" ] = base_url + "/v1"
62
+ id_toolkit_endpoints ["v2beta1" ] = base_url + "/v2beta1"
63
+ else :
64
+ credential = app .credential .get_credential ()
65
+
41
66
http_client = _http_client .JsonHttpClient (
42
- credential = credential , headers = { 'X-Client-Version' : version_header } )
67
+ credential = credential , headers = http_headers )
43
68
44
69
self ._tenant_id = tenant_id
45
- self ._token_generator = _token_gen .TokenGenerator (app , http_client )
70
+ self ._token_generator = _token_gen .TokenGenerator (app , http_client , id_toolkit_endpoints [ "v1" ] )
46
71
self ._token_verifier = _token_gen .TokenVerifier (app )
47
- self ._user_manager = _user_mgt .UserManager (http_client , app .project_id , tenant_id )
72
+ self ._user_manager = _user_mgt .UserManager (http_client , app .project_id , tenant_id , id_toolkit_endpoints [ "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 , id_toolkit_endpoints [ "v2beta1" ] )
50
75
51
76
@property
52
77
def tenant_id (self ):
0 commit comments