Open
Show file tree
Hide file tree
Changes from all commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
44586d7
1. Adding Multi Factor Auth Configuration
pragatimodiMar 15, 2023
b625e13
Updating list[ProviderConfig] to List[ProviderConfig]
pragatimodiMar 15, 2023
c002232
1. Added a project_config_mgt file
pragatimodiMar 15, 2023
2a73f41
Import changes
pragatimodiMar 15, 2023
b5e31b1
1. Changing auth url from `v2beta1` to `v2`
pragatimodiMar 15, 2023
7ff0089
Add new line (lint fix)
pragatimodiMar 15, 2023
81b1fa3
`v2beta1` -> `v2`
pragatimodiMar 15, 2023
1f5c366
corrections
pragatimodiMar 15, 2023
7e53e61
chore: Fix pypy tests (#694)
lahirumarambaApr 5, 2023
6a4f451
chore(auth): Update Auth API to `v2` (#691)
pragatimodiApr 6, 2023
0684915
Add release notes to project URLs in PyPI (#679)
samueldgApr 6, 2023
160f983
Merge branch 'master' into mfa-totp
pragatimodiMay 28, 2023
8b87e10
Addressing feedback
pragatimodiJun 12, 2023
b5b3dce
Merge branch 'master' into mfa-totp
pragatimodiJun 12, 2023
4bc2c83
Apply suggestions from code review
pragatimodiJun 15, 2023
e542f91
Addressing PR feedback
pragatimodiJul 6, 2023
58fe7ef
Merge branch 'mfa-totp' of https://.com/firebase/firebase-admin…
pragatimodiJul 6, 2023
b709148
Apply suggestions from code review
pragatimodiSep 27, 2023
083d670
Update firebase_admin/multi_factor_config_mgt.py
pragatimodiSep 27, 2023
ba9d8f4
Merge branch 'master' into mfa-totp
pragatimodiSep 27, 2023
c67e047
fix test error messages
pragatimodiDec 20, 2023
cde81fb
Merge branch 'master' into mfa-totp
pragatimodiDec 20, 2023
3245d6d
lint fixes
pragatimodiDec 20, 2023
b7e9eba
fix indent
pragatimodiDec 20, 2023
6725d41
succinct import types
pragatimodiDec 21, 2023
021156a
Merge branch 'master' into mfa-totp
pragatimodiJan 9, 2024
907bf2d
change copyright year
pragatimodiJan 10, 2024
e5c4c74
Merge branch 'master' into mfa-totp
pragatimodiJan 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Failed to load files.
Original file line numberDiff line numberDiff 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)
Loading