|
| 1 | +import logging |
| 2 | + |
1 | 3 | from fastapi import APIRouter, Depends, HTTPException, status
|
2 | 4 | from pydantic.error_wrappers import ErrorWrapper, ValidationError
|
3 | 5 |
|
|
17 | 19 | from dispatch.database.service import CommonParameters, search_filter_sort_paginate
|
18 | 20 | from dispatch.enums import UserRoles
|
19 | 21 | from dispatch.models import OrganizationSlug, PrimaryKey
|
| 22 | +from dispatch.plugin import service as plugin_service |
| 23 | +from dispatch.plugins.dispatch_core.exceptions import MfaException |
20 | 24 | from dispatch.organization.models import OrganizationRead
|
21 | 25 |
|
22 | 26 | from .models import (
|
| 27 | + MfaPayload, |
| 28 | + MfaPayloadResponse, |
23 | 29 | UserLogin,
|
24 | 30 | UserLoginResponse,
|
25 | 31 | UserOrganization,
|
|
33 | 39 | from .service import get, get_by_email, update, create
|
34 | 40 |
|
35 | 41 |
|
| 42 | +log = logging.getLogger(__name__) |
| 43 | + |
36 | 44 | auth_router = APIRouter()
|
37 | 45 | user_router = APIRouter()
|
38 | 46 |
|
@@ -246,6 +254,49 @@ def register_user(
|
246 | 254 | return user
|
247 | 255 |
|
248 | 256 |
|
| 257 | +@auth_router.post("/mfa", response_model=MfaPayloadResponse) |
| 258 | +def mfa_check( |
| 259 | + payload_in: MfaPayload, |
| 260 | + current_user: CurrentUser, |
| 261 | + db_session: DbSession, |
| 262 | +): |
| 263 | + log.info(f"MFA check initiated for user: {current_user.email}") |
| 264 | + log.debug(f"Payload received: {payload_in.dict()}") |
| 265 | + |
| 266 | + try: |
| 267 | + log.info(f"Attempting to get active MFA plugin for project: {payload_in.project_id}") |
| 268 | + mfa_auth_plugin = plugin_service.get_active_instance( |
| 269 | + db_session=db_session, project_id=payload_in.project_id, plugin_type="auth-mfa" |
| 270 | + ) |
| 271 | + |
| 272 | + if not mfa_auth_plugin: |
| 273 | + log.error(f"MFA plugin not enabled for project: {payload_in.project_id}") |
| 274 | + raise HTTPException( |
| 275 | + status_code=400, detail="MFA plugin is not enabled for the project." |
| 276 | + ) |
| 277 | + |
| 278 | + log.info(f"MFA plugin found: {mfa_auth_plugin.__class__.__name__}") |
| 279 | + |
| 280 | + log.info("Validating MFA token") |
| 281 | + status = mfa_auth_plugin.instance.validate_mfa_token(payload_in, current_user, db_session) |
| 282 | + |
| 283 | + log.info("MFA token validation successful") |
| 284 | + return MfaPayloadResponse(status=status) |
| 285 | + |
| 286 | + except MfaException as e: |
| 287 | + log.error(f"MFA Exception occurred: {str(e)}") |
| 288 | + log.debug(f"MFA Exception details: {type(e).__name__}", exc_info=True) |
| 289 | + raise HTTPException(status_code=400, detail=str(e)) from e |
| 290 | + |
| 291 | + except Exception as e: |
| 292 | + log.critical(f"Unexpected error in MFA check: {str(e)}") |
| 293 | + log.exception("Full traceback:") |
| 294 | + raise HTTPException(status_code=500, detail="An unexpected error occurred") from e |
| 295 | + |
| 296 | + finally: |
| 297 | + log.info("MFA check completed") |
| 298 | + |
| 299 | + |
249 | 300 | if DISPATCH_AUTH_REGISTRATION_ENABLED:
|
250 | 301 | register_user = auth_router.post("/register", response_model=UserRegisterResponse)(
|
251 | 302 | register_user
|
|
0 commit comments