-
Notifications
You must be signed in to change notification settings - Fork 297
/
__init__.py
261 lines (212 loc) · 9.25 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# Copyright 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Firebase Admin SDK for Python."""
import datetime
import threading
import six
from firebase_admin import credentials
# Declaring module version as per https://www.python.org/dev/peps/pep-0396/#specification
# Update this accordingly for each release.
__version__ = '2.0.0'
_apps = {}
_apps_lock = threading.RLock()
_clock = datetime.datetime.utcnow
_DEFAULT_APP_NAME = '[DEFAULT]'
_CLOCK_SKEW_SECONDS = 300
def initialize_app(credential=None, options=None, name=_DEFAULT_APP_NAME):
"""Initializes and returns a new App instance.
Creates a new App instance using the specified options
and the app name. If an instance already exists by the same
app name a ValueError is raised. Use this function whenever
a new App instance is required. Do not directly invoke the
App constructor.
Args:
credential: A credential object used to initialize the SDK (optional). If none is provided,
Google Application Default Credentials are used.
options: A dictionary of configuration options (optional).
name: Name of the app (optional).
Returns:
App: A newly initialized instance of App.
Raises:
ValueError: If the app name is already in use, or any of the
provided arguments are invalid.
"""
if credential is None:
credential = credentials.ApplicationDefault()
app = App(name, credential, options)
with _apps_lock:
if app.name not in _apps:
_apps[app.name] = app
return app
if name == _DEFAULT_APP_NAME:
raise ValueError((
'The default Firebase app already exists. This means you called '
'initialize_app() more than once without providing an app name as '
'the second argument. In most cases you only need to call '
'initialize_app() once. But if you do want to initialize multiple '
'apps, pass a second argument to initialize_app() to give each app '
'a unique name.'))
else:
raise ValueError((
'Firebase app named "{0}" already exists. This means you called '
'initialize_app() more than once with the same app name as the '
'second argument. Make sure you provide a unique name every time '
'you call initialize_app().').format(name))
def delete_app(app):
"""Gracefully deletes an App instance.
Args:
app: The app instance to be deleted.
Raises:
ValueError: If the app is not initialized.
"""
if not isinstance(app, App):
raise ValueError('Illegal app argument type: "{}". Argument must be of '
'type App.'.format(type(app)))
with _apps_lock:
if _apps.get(app.name) is app:
del _apps[app.name]
app._cleanup() # pylint: disable=protected-access
return
if app.name == _DEFAULT_APP_NAME:
raise ValueError(
'The default Firebase app is not initialized. Make sure to initialize '
'the default app by calling initialize_app().')
else:
raise ValueError(
('Firebase app named "{0}" is not initialized. Make sure to initialize '
'the app by calling initialize_app() with your app name as the '
'second argument.').format(app.name))
def get_app(name=_DEFAULT_APP_NAME):
"""Retrieves an App instance by name.
Args:
name: Name of the App instance to retrieve (optional).
Returns:
App: An App instance with the given name.
Raises:
ValueError: If the specified name is not a string, or if the specified
app does not exist.
"""
if not isinstance(name, six.string_types):
raise ValueError('Illegal app name argument type: "{}". App name '
'must be a string.'.format(type(name)))
with _apps_lock:
if name in _apps:
return _apps[name]
if name == _DEFAULT_APP_NAME:
raise ValueError(
'The default Firebase app does not exist. Make sure to initialize '
'the SDK by calling initialize_app().')
else:
raise ValueError(
('Firebase app named "{0}" does not exist. Make sure to initialize '
'the SDK by calling initialize_app() with your app name as the '
'second argument.').format(name))
class _AppOptions(object):
"""A collection of configuration options for an App."""
def __init__(self, options):
if options is None:
options = {}
if not isinstance(options, dict):
raise ValueError('Illegal Firebase app options type: {0}. Options '
'must be a dictionary.'.format(type(options)))
self._options = options
def get(self, key):
"""Returns the option identified by the provided key."""
return self._options.get(key)
class App(object):
"""The entry point for Firebase Python SDK.
Represents a Firebase app, while holding the configuration and state
common to all Firebase APIs.
"""
def __init__(self, name, credential, options):
"""Constructs a new App using the provided name and options.
Args:
name: Name of the application.
credential: A credential object.
options: A dictionary of configuration options.
Raises:
ValueError: If an argument is None or invalid.
"""
if not name or not isinstance(name, six.string_types):
raise ValueError('Illegal Firebase app name "{0}" provided. App name must be a '
'non-empty string.'.format(name))
self._name = name
if not isinstance(credential, credentials.Base):
raise ValueError('Illegal Firebase credential provided. App must be initialized '
'with a valid credential instance.')
self._credential = credential
self._options = _AppOptions(options)
self._token = None
self._lock = threading.RLock()
self._services = {}
@property
def name(self):
return self._name
@property
def credential(self):
return self._credential
@property
def options(self):
return self._options
def get_token(self):
"""Returns an OAuth2 bearer token.
This method may return a cached token. But it handles cache invalidation, and therefore
is guaranteed to always return unexpired tokens.
Returns:
string: An unexpired OAuth2 token.
"""
if not self._token_valid():
self._token = self._credential.get_access_token()
return self._token.access_token
def _token_valid(self):
if self._token is None:
return False
skewed_expiry = self._token.expiry - datetime.timedelta(seconds=_CLOCK_SKEW_SECONDS)
return _clock() < skewed_expiry
def _get_service(self, name, initializer):
"""Returns the service instance identified by the given name.
Services are functional entities exposed by the Admin SDK (e.g. auth, database). Each
service instance is associated with exactly one App. If the named service
instance does not exist yet, _get_service() calls the provided initializer function to
create the service instance. The created instance will be cached, so that subsequent
calls would always fetch it from the cache.
Args:
name: Name of the service to retrieve.
initializer: A function that can be used to initialize a service for the first time.
Returns:
object: The specified service instance.
Raises:
ValueError: If the provided name is invalid, or if the App is already deleted.
"""
if not name or not isinstance(name, six.string_types):
raise ValueError(
'Illegal name argument: "{0}". Name must be a non-empty string.'.format(name))
with self._lock:
if self._services is None:
raise ValueError(
'Service requested from deleted Firebase App: "{0}".'.format(self._name))
if name not in self._services:
self._services[name] = initializer(self)
return self._services[name]
def _cleanup(self):
"""Cleans up any services associated with this App.
Checks whether each service contains a close() method, and calls it if available.
This is to be called when an App is being deleted, thus ensuring graceful termination of
any services started by the App.
"""
with self._lock:
for service in self._services.values():
if hasattr(service, 'close') and hasattr(service.close, '__call__'):
service.close()
self._services = None