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,42 @@ 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
+ # None lets us 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
52
+ # endpoint URLs to use 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
+ # Used instead of credential
60
+ http_headers ["Authorization" ] = "Bearer owner"
61
+ base_url = "http://{0}/identitytoolkit.googleapis.com" .format (emulator_host )
62
+ id_toolkit_endpoints ["v1" ] = base_url + "/v1"
63
+ id_toolkit_endpoints ["v2beta1" ] = base_url + "/v2beta1"
64
+ else :
65
+ credential = app .credential .get_credential ()
66
+
41
67
http_client = _http_client .JsonHttpClient (
42
- credential = credential , headers = { 'X-Client-Version' : version_header } )
68
+ credential = credential , headers = http_headers )
43
69
44
70
self ._tenant_id = tenant_id
45
- self ._token_generator = _token_gen .TokenGenerator (app , http_client )
71
+ self ._token_generator = _token_gen .TokenGenerator (
72
+ app , http_client , id_toolkit_endpoints ["v1" ])
46
73
self ._token_verifier = _token_gen .TokenVerifier (app )
47
- self ._user_manager = _user_mgt .UserManager (http_client , app .project_id , tenant_id )
74
+ self ._user_manager = _user_mgt .UserManager (
75
+ http_client , app .project_id , tenant_id , id_toolkit_endpoints ["v1" ])
48
76
self ._provider_manager = _auth_providers .ProviderConfigClient (
49
- http_client , app .project_id , tenant_id )
77
+ http_client , app .project_id , tenant_id , id_toolkit_endpoints [ "v2beta1" ] )
50
78
51
79
@property
52
80
def tenant_id (self ):
0 commit comments