-
Notifications
You must be signed in to change notification settings - Fork 331
/
Copy pathproject_config_mgt.py
135 lines (111 loc) · 4.87 KB
/
project_config_mgt.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
# Copyright 2024 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 project configuration management module.
This module contains functions for managing projects.
"""
import requests
import firebase_admin
from firebase_admin import _auth_utils
from firebase_admin import _http_client
from firebase_admin import _utils
from firebase_admin.multi_factor_config_mgt import MultiFactorConfig
from firebase_admin.multi_factor_config_mgt import MultiFactorServerConfig
_PROJECT_CONFIG_MGT_ATTRIBUTE = '_project_config_mgt'
__all__ = [
'ProjectConfig',
'get_project_config',
'update_project_config',
]
def get_project_config(app=None):
"""Gets the project config corresponding to the current project_id.
Args:
app: An App instance (optional).
Returns:
Project: A project object.
Raises:
ValueError: If the project ID is None, empty or not a string.
ProjectNotFoundError: If no project exists by the given ID.
FirebaseError: If an error occurs while retrieving the project.
"""
project_config_mgt_service = _get_project_config_mgt_service(app)
return project_config_mgt_service.get_project_config()
def update_project_config(multi_factor_config: MultiFactorConfig = None, app=None):
"""Update the project config with the given options.
Args:
multi_factor_config: Updated multi-factor authentication configuration
(optional)
app: An App instance (optional).
Returns:
Project: An updated ProjectConfig object.
Raises:
ValueError: If any of the given arguments are invalid.
FirebaseError: If an error occurs while updating the project.
"""
project_config_mgt_service = _get_project_config_mgt_service(app)
return project_config_mgt_service.update_project_config(multi_factor_config=multi_factor_config)
def _get_project_config_mgt_service(app):
return _utils.get_app_service(app, _PROJECT_CONFIG_MGT_ATTRIBUTE,
_ProjectConfigManagementService)
class ProjectConfig:
"""Represents a project config in an application.
"""
def __init__(self, data):
if not isinstance(data, dict):
raise ValueError(
'Invalid data argument in Project constructor: {0}'.format(data))
self._data = data
@property
def multi_factor_config(self):
data = self._data.get('mfa')
if data:
return MultiFactorServerConfig(data)
return None
class _ProjectConfigManagementService:
"""Firebase project management service."""
PROJECT_CONFIG_MGT_URL = 'https://identitytoolkit.googleapis.com/v2/projects'
def __init__(self, app):
credential = app.credential.get_credential()
version_header = 'Python/Admin/{0}'.format(firebase_admin.__version__)
base_url = '{0}/{1}/config'.format(
self.PROJECT_CONFIG_MGT_URL, app.project_id)
self.app = app
self.client = _http_client.JsonHttpClient(
credential=credential, base_url=base_url, headers={'X-Client-Version': version_header})
def get_project_config(self) -> ProjectConfig:
"""Gets the project config"""
try:
body = self.client.body('get', url='')
except requests.exceptions.RequestException as error:
raise _auth_utils.handle_auth_backend_error(error)
else:
return ProjectConfig(body)
def update_project_config(self, multi_factor_config: MultiFactorConfig = None) -> ProjectConfig:
"""Updates the specified project with the given parameters."""
payload = {}
if multi_factor_config is not None:
if not isinstance(multi_factor_config, MultiFactorConfig):
raise ValueError('multi_factor_config must be of type MultiFactorConfig.')
payload['mfa'] = multi_factor_config.build_server_request()
if not payload:
raise ValueError(
'At least one parameter must be specified for update.')
update_mask = ','.join(_auth_utils.build_update_mask(payload))
params = 'updateMask={0}'.format(update_mask)
try:
body = self.client.body(
'patch', url='', json=payload, params=params)
except requests.exceptions.RequestException as error:
raise _auth_utils.handle_auth_backend_error(error)
else:
return ProjectConfig(body)