This document describes the implementation of the withdraw_mfa_enrollment feature for the Firebase Admin SDK for Python.
The withdraw_mfa_enrollment function allows administrators to programmatically withdraw (reset) a user's enrolled second factor authentication method. This feature was previously available in the Node.js SDK but missing from the Python SDK.
firebase_admin/_mfa.py- New module containing the core MFA functionalityfirebase_admin/auth.py- Updated to export the new function and MfaErrortests/test_mfa_withdraw.py- Comprehensive test suite
def withdraw_mfa_enrollment(
uid: str,
mfa_enrollment_id: str,
api_key: str,
tenant_id: str | None = None,
app=None
) -> dict:Parameters:
uid: Firebase Auth UID of the usermfa_enrollment_id: The MFA enrollment ID to revokeapi_key: Web API key from Firebase project settingstenant_id: Optional tenant ID for multi-tenancyapp: Optional Firebase app instance
Returns: Dictionary response from the Identity Toolkit API
Raises:
MfaError: If the operation failsValueError: For invalid arguments
- Create Custom Token: Uses the Firebase Admin SDK to mint a custom token for the user
- Exchange for ID Token: Calls the Identity Toolkit
signInWithCustomTokenendpoint - Withdraw MFA: Uses the ID token to call the
mfaEnrollment:withdrawendpoint
- Custom
MfaErrorexception for MFA-specific failures - Proper HTTP error handling with detailed error messages
- Input validation for required parameters
import firebase_admin
from firebase_admin import auth, credentials
# Initialize the SDK
cred = credentials.Certificate("service-account-key.json")
firebase_admin.initialize_app(cred)
# Withdraw MFA enrollment
try:
result = auth.withdraw_mfa_enrollment(
uid="user123",
mfa_enrollment_id="enrollment456",
api_key="your-web-api-key"
)
print("MFA withdrawn successfully:", result)
except auth.MfaError as e:
print("MFA operation failed:", e)The implementation includes comprehensive tests covering:
- Successful withdrawal scenarios
- Error handling for API failures
- Input validation
- Integration with the auth module
Run tests with:
python -m pytest tests/test_mfa_withdraw.py -vThis implementation follows the same pattern as the Node.js SDK, ensuring consistency across Firebase Admin SDKs.
- Integration Testing: Test with actual Firebase project
- Documentation: Add to official SDK documentation
- Code Review: Submit for Firebase team review
- Release: Include in next SDK version
- Requires Web API key (different from service account key)
- Uses Identity Toolkit v2 API endpoints
- Supports multi-tenant projects via
tenant_idparameter - Follows existing SDK patterns for error handling and app management