Open
Changes from all commits
Show all changes
28 commitsSelect commit Hold shift + click to select a range
44586d7
1. Adding Multi Factor Auth Configuration
pragatimodib625e13
Updating list[ProviderConfig] to List[ProviderConfig]
pragatimodic002232
1. Added a project_config_mgt file
pragatimodi2a73f41
Import changes
pragatimodib5e31b1
1. Changing auth url from `v2beta1` to `v2`
pragatimodi7ff0089
Add new line (lint fix)
pragatimodi81b1fa3
`v2beta1` -> `v2`
pragatimodi1f5c366
corrections
pragatimodi7e53e61
chore: Fix pypy tests (#694)
lahirumaramba6a4f451
chore(auth): Update Auth API to `v2` (#691)
pragatimodi0684915
Add release notes to project URLs in PyPI (#679)
samueldg160f983
Merge branch 'master' into mfa-totp
pragatimodi8b87e10
Addressing feedback
pragatimodib5b3dce
Merge branch 'master' into mfa-totp
pragatimodi4bc2c83
Apply suggestions from code review
pragatimodie542f91
Addressing PR feedback
pragatimodi58fe7ef
Merge branch 'mfa-totp' of https://.com/firebase/firebase-admin…
pragatimodib709148
Apply suggestions from code review
pragatimodi083d670
Update firebase_admin/multi_factor_config_mgt.py
pragatimodiba9d8f4
Merge branch 'master' into mfa-totp
pragatimodic67e047
fix test error messages
pragatimodicde81fb
Merge branch 'master' into mfa-totp
pragatimodi3245d6d
lint fixes
pragatimodib7e9eba
fix indent
pragatimodi6725d41
succinct import types
pragatimodi021156a
Merge branch 'master' into mfa-totp
pragatimodi907bf2d
change copyright year
pragatimodie5c4c74
Merge branch 'master' into mfa-totp
pragatimodiFile filter
Filter by extension
Conversations
Failed to load comments.
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Failed to load files.
Uh oh!
There was an error while loading. Please reload this page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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( | ||
'', url='', json=payload, params=params) | ||
except requests.exceptions.RequestException as error: | ||
raise _auth_utils.handle_auth_backend_error(error) | ||
else: | ||
return ProjectConfig(body) |
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.