|
11 | 11 |
|
12 | 12 | from fief.apps.auth.forms.auth import ConsentForm, LoginForm |
13 | 13 | from fief.apps.auth.forms.mfa import MfaRecoveryForm, TotpVerifyForm |
14 | | -from fief.apps.auth.forms.verify_email import VerifyEmailForm |
| 14 | +from fief.apps.auth.forms.verify_email import VerifyEmailForm, VerifyEmailLinkForm |
15 | 15 | from fief.dependencies.auth import ( |
16 | 16 | BaseContext, |
17 | 17 | check_unsupported_request_parameter, |
@@ -615,54 +615,90 @@ async def _generic_invalid_code_response(): |
615 | 615 | return await form_helper.get_response() |
616 | 616 |
|
617 | 617 |
|
618 | | -@router.get("/verify-link", name="auth:verify_email_link") |
| 618 | +@router.api_route( |
| 619 | + "/verify-link", methods=["GET", "POST"], name="auth:verify_email_link" |
| 620 | +) |
619 | 621 | async def verify_email_link( |
620 | 622 | request: Request, |
621 | | - code: str = Query(...), |
| 623 | + code: str | None = Query(default=None), |
622 | 624 | user_manager: UserManager = Depends(get_user_manager), |
623 | 625 | tenant: Tenant = Depends(get_current_tenant), |
| 626 | + context: BaseContext = Depends(get_base_context), |
624 | 627 | ip_info: ClientIpInfo = Depends(get_client_ip_info), |
625 | 628 | rate_limiter: RateLimiter = Depends(get_rate_limiter), |
626 | 629 | audit_logger: AuditLogger = Depends(get_audit_logger), |
627 | 630 | ): |
628 | | - """Sessionless one-click email activation. The ``code`` query param is the |
629 | | - secret (delivered to the address), so possession proves ownership and no |
630 | | - login session is required — this is what makes the link clickable straight |
631 | | - from the inbox. On success/failure we bounce to /login with a flag the |
632 | | - template can surface.""" |
| 631 | + """Email activation from a one-click link, gated behind an explicit POST. |
| 632 | +
|
| 633 | + Verification is performed only when the confirmation page is *submitted*, |
| 634 | + never on the GET. Recipient-side mail-security scanners (Microsoft Safe |
| 635 | + Links, Gmail, corporate proxies) automatically follow the link's GET the |
| 636 | + instant the message is delivered; if that GET consumed the one-time code, |
| 637 | + the real user — clicking the button or typing the same code — would always |
| 638 | + land on an already-spent code and see ``verify_error=1``. Requiring a human |
| 639 | + form submission keeps the code intact until the recipient acts. |
| 640 | +
|
| 641 | + Possession of ``code`` (delivered to the address) is the proof of ownership, |
| 642 | + so no login session is required. ``tenant_id`` scopes the lookup so a code |
| 643 | + can only verify a user of the current tenant. |
| 644 | + """ |
633 | 645 | login_url = tenant.url_path_for(request, "auth:login") |
634 | 646 |
|
635 | | - if settings.rate_limit_enabled: |
| 647 | + form_helper = FormHelper( |
| 648 | + VerifyEmailLinkForm, |
| 649 | + "auth/verify_email_link.html", |
| 650 | + request=request, |
| 651 | + context={**context}, |
| 652 | + ) |
| 653 | + |
| 654 | + if request.method == "POST": |
| 655 | + if not await form_helper.is_submitted_and_valid(): |
| 656 | + return await form_helper.get_response() |
| 657 | + |
| 658 | + form = await form_helper.get_form() |
| 659 | + |
| 660 | + if settings.rate_limit_enabled: |
| 661 | + try: |
| 662 | + await rate_limiter.check( |
| 663 | + scope="verify_ip", |
| 664 | + key=ip_info.rate_limit_key, |
| 665 | + window=RateLimitWindow( |
| 666 | + settings.rate_limit_verify_per_ip_per_min, 60 |
| 667 | + ), |
| 668 | + ) |
| 669 | + except RateLimitExceeded: |
| 670 | + audit_logger( |
| 671 | + AuditLogMessage.USER_RATE_LIMIT_EXCEEDED, |
| 672 | + extra={ |
| 673 | + "scope": "verify_ip", |
| 674 | + "key_hash": _hash_key(ip_info.rate_limit_key), |
| 675 | + "endpoint": "/verify-link", |
| 676 | + "client_ip": ip_info.raw, |
| 677 | + }, |
| 678 | + ) |
| 679 | + return RedirectResponse(login_url, status_code=status.HTTP_302_FOUND) |
| 680 | + |
636 | 681 | try: |
637 | | - await rate_limiter.check( |
638 | | - scope="verify_ip", |
639 | | - key=ip_info.rate_limit_key, |
640 | | - window=RateLimitWindow(settings.rate_limit_verify_per_ip_per_min, 60), |
| 682 | + await user_manager.verify_email_by_code( |
| 683 | + form.code.data, tenant_id=tenant.id, request=request |
641 | 684 | ) |
642 | | - except RateLimitExceeded: |
643 | | - audit_logger( |
644 | | - AuditLogMessage.USER_RATE_LIMIT_EXCEEDED, |
645 | | - extra={ |
646 | | - "scope": "verify_ip", |
647 | | - "key_hash": _hash_key(ip_info.rate_limit_key), |
648 | | - "endpoint": "/verify-link", |
649 | | - "client_ip": ip_info.raw, |
650 | | - }, |
| 685 | + except InvalidEmailVerificationCodeError: |
| 686 | + return RedirectResponse( |
| 687 | + f"{login_url}?verify_error=1", status_code=status.HTTP_302_FOUND |
651 | 688 | ) |
652 | | - return RedirectResponse(login_url, status_code=status.HTTP_302_FOUND) |
653 | 689 |
|
654 | | - try: |
655 | | - await user_manager.verify_email_by_code( |
656 | | - code, tenant_id=tenant.id, request=request |
657 | | - ) |
658 | | - except InvalidEmailVerificationCodeError: |
659 | 690 | return RedirectResponse( |
660 | | - f"{login_url}?verify_error=1", status_code=status.HTTP_302_FOUND |
| 691 | + f"{login_url}?verified=1", status_code=status.HTTP_302_FOUND |
661 | 692 | ) |
662 | 693 |
|
663 | | - return RedirectResponse( |
664 | | - f"{login_url}?verified=1", status_code=status.HTTP_302_FOUND |
665 | | - ) |
| 694 | + # GET: render the confirmation page WITHOUT verifying. A link scanner that |
| 695 | + # follows this GET only renders the page; the code is spent solely on POST. |
| 696 | + if code is None: |
| 697 | + return RedirectResponse(login_url, status_code=status.HTTP_302_FOUND) |
| 698 | + |
| 699 | + form = await form_helper.get_form() |
| 700 | + form.code.data = code |
| 701 | + return await form_helper.get_response() |
666 | 702 |
|
667 | 703 |
|
668 | 704 | # --------------------------------------------------------------------------- |
@@ -1068,9 +1104,7 @@ async def mfa_recover( |
1068 | 1104 | # post-MFA cookie just minted by ``complete_login_after_mfa`` is |
1069 | 1105 | # passed in as ``current_session_id`` so it survives. Audit logged |
1070 | 1106 | # with ``trigger_reason="recovery_code_used"``. |
1071 | | - new_session_token_id = ( |
1072 | | - authentication_flow.last_minted_session_token_id |
1073 | | - ) |
| 1107 | + new_session_token_id = authentication_flow.last_minted_session_token_id |
1074 | 1108 | await device_sessions_service.auto_revoke_others( |
1075 | 1109 | user.id, |
1076 | 1110 | current_session_id=new_session_token_id, |
@@ -1218,12 +1252,16 @@ async def mfa_passkey_verify( |
1218 | 1252 | # ``reason=credential_not_found``; we just bump the counter to |
1219 | 1253 | # match the TOTP lockout ladder. |
1220 | 1254 | await _record_failed_mfa_attempt(login_session, login_session_repository) |
1221 | | - return JSONResponse({"error": "invalid"}, status_code=status.HTTP_401_UNAUTHORIZED) |
| 1255 | + return JSONResponse( |
| 1256 | + {"error": "invalid"}, status_code=status.HTTP_401_UNAUTHORIZED |
| 1257 | + ) |
1222 | 1258 | except InvalidAssertion: |
1223 | 1259 | # Service already audit-logged USER_PASSKEY_VERIFY_FAILED with |
1224 | 1260 | # ``reason=invalid_signature``. |
1225 | 1261 | await _record_failed_mfa_attempt(login_session, login_session_repository) |
1226 | | - return JSONResponse({"error": "invalid"}, status_code=status.HTTP_401_UNAUTHORIZED) |
| 1262 | + return JSONResponse( |
| 1263 | + {"error": "invalid"}, status_code=status.HTTP_401_UNAUTHORIZED |
| 1264 | + ) |
1227 | 1265 | except SignCountRollback: |
1228 | 1266 | # The credential is suspect, not the attempt — do NOT burn the |
1229 | 1267 | # user's lockout budget on a sign-count regression. The service |
|
0 commit comments