|
| 1 | +# Copyright 2023 Google Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""Firebase project configuration management module. |
| 15 | +
|
| 16 | +This module contains functions for managing various project operations like update and create |
| 17 | +""" |
| 18 | + |
| 19 | +import requests |
| 20 | + |
| 21 | +import firebase_admin |
| 22 | +from firebase_admin import _auth_utils |
| 23 | +from firebase_admin import _http_client |
| 24 | +from firebase_admin import _utils |
| 25 | +from firebase_admin.multi_factor_config_mgt import MultiFactorConfig |
| 26 | +from firebase_admin.multi_factor_config_mgt import MultiFactorServerConfig |
| 27 | + |
| 28 | +_PROJECT_CONFIG_MGT_ATTRIBUTE = '_project_config_mgt' |
| 29 | + |
| 30 | +__all__ = [ |
| 31 | + 'ProjectConfig', |
| 32 | + |
| 33 | + 'get_project_config', |
| 34 | + 'update_project_config', |
| 35 | +] |
| 36 | + |
| 37 | + |
| 38 | +def get_project_config(app=None): |
| 39 | + """Gets the project config corresponding to the given project_id. |
| 40 | +
|
| 41 | + Args: |
| 42 | + app: An App instance (optional). |
| 43 | +
|
| 44 | + Returns: |
| 45 | + Project: A project object. |
| 46 | +
|
| 47 | + Raises: |
| 48 | + ValueError: If the project ID is None, empty or not a string. |
| 49 | + ProjectNotFoundError: If no project exists by the given ID. |
| 50 | + FirebaseError: If an error occurs while retrieving the project. |
| 51 | + """ |
| 52 | + project_config_mgt_service = _get_project_config_mgt_service(app) |
| 53 | + return project_config_mgt_service.get_project_config() |
| 54 | + |
| 55 | +def update_project_config(multi_factor_config: MultiFactorConfig = None, app=None): |
| 56 | + """Update the Project Config with the given options. |
| 57 | + Args: |
| 58 | + multi_factor_config: Updated Multi Factor Authentication configuration |
| 59 | + (optional) |
| 60 | + app: An App instance (optional). |
| 61 | + Returns: |
| 62 | + Project: An updated ProjectConfig object. |
| 63 | + Raises: |
| 64 | + ValueError: If any of the given arguments are invalid. |
| 65 | + FirebaseError: If an error occurs while updating the project. |
| 66 | + """ |
| 67 | + project_config_mgt_service = _get_project_config_mgt_service(app) |
| 68 | + return project_config_mgt_service.update_project_config(multi_factor_config=multi_factor_config) |
| 69 | + |
| 70 | + |
| 71 | +def _get_project_config_mgt_service(app): |
| 72 | + return _utils.get_app_service(app, _PROJECT_CONFIG_MGT_ATTRIBUTE, |
| 73 | + _ProjectConfigManagementService) |
| 74 | + |
| 75 | +class ProjectConfig: |
| 76 | + """Represents a project config in an application. |
| 77 | + """ |
| 78 | + |
| 79 | + def __init__(self, data): |
| 80 | + if not isinstance(data, dict): |
| 81 | + raise ValueError( |
| 82 | + 'Invalid data argument in Project constructor: {0}'.format(data)) |
| 83 | + self._data = data |
| 84 | + |
| 85 | + @property |
| 86 | + def multi_factor_config(self): |
| 87 | + data = self._data.get('mfa') |
| 88 | + if data: |
| 89 | + return MultiFactorServerConfig(data) |
| 90 | + return None |
| 91 | + |
| 92 | +class _ProjectConfigManagementService: |
| 93 | + """Firebase project management service.""" |
| 94 | + |
| 95 | + PROJECT_CONFIG_MGT_URL = 'https://identitytoolkit.googleapis.com/v2/projects' |
| 96 | + |
| 97 | + def __init__(self, app): |
| 98 | + credential = app.credential.get_credential() |
| 99 | + version_header = 'Python/Admin/{0}'.format(firebase_admin.__version__) |
| 100 | + base_url = '{0}/{1}/config'.format( |
| 101 | + self.PROJECT_CONFIG_MGT_URL, app.project_id) |
| 102 | + self.app = app |
| 103 | + self.client = _http_client.JsonHttpClient( |
| 104 | + credential=credential, base_url=base_url, headers={'X-Client-Version': version_header}) |
| 105 | + |
| 106 | + def get_project_config(self) -> ProjectConfig: |
| 107 | + """Gets the project config""" |
| 108 | + try: |
| 109 | + body = self.client.body('get', url='') |
| 110 | + except requests.exceptions.RequestException as error: |
| 111 | + raise _auth_utils.handle_auth_backend_error(error) |
| 112 | + else: |
| 113 | + return ProjectConfig(body) |
| 114 | + |
| 115 | + def update_project_config(self, multi_factor_config: MultiFactorConfig = None) -> ProjectConfig: |
| 116 | + """Updates the specified project with the given parameters.""" |
| 117 | + |
| 118 | + payload = {} |
| 119 | + if multi_factor_config is not None: |
| 120 | + if not isinstance(multi_factor_config, MultiFactorConfig): |
| 121 | + raise ValueError('multi_factor_config must be of type MultiFactorConfig.') |
| 122 | + payload['mfa'] = multi_factor_config.build_server_request() |
| 123 | + if not payload: |
| 124 | + raise ValueError( |
| 125 | + 'At least one parameter must be specified for update.') |
| 126 | + |
| 127 | + update_mask = ','.join(_auth_utils.build_update_mask(payload)) |
| 128 | + params = 'updateMask={0}'.format(update_mask) |
| 129 | + try: |
| 130 | + body = self.client.body( |
| 131 | + 'patch', url='', json=payload, params=params) |
| 132 | + except requests.exceptions.RequestException as error: |
| 133 | + raise _auth_utils.handle_auth_backend_error(error) |
| 134 | + else: |
| 135 | + return ProjectConfig(body) |
0 commit comments