fix: API token 422 error on GET /auth/email/me#2728
Conversation
9daa3d1 to
280ae41
Compare
Signed-off-by: Shoumi <shoumimukherjee@gmail.com>
Signed-off-by: Shoumi <shoumimukherjee@gmail.com>
Signed-off-by: Shoumi <shoumimukherjee@gmail.com>
0edcc46 to
076c2e6
Compare
ReviewRebased cleanly onto Root cause confirmed: Fix is correct and complete. All 5
No other No security or performance concerns. Default values are safe/restrictive. No changes to auth logic. Minor note: LGTM. |
* fix(auth): add missing fields to EmailUser instantiations Signed-off-by: Shoumi <shoumimukherjee@gmail.com> * add regression test for API token /me endpoint serialization Signed-off-by: Shoumi <shoumimukherjee@gmail.com> * test fixes Signed-off-by: Shoumi <shoumimukherjee@gmail.com> --------- Signed-off-by: Shoumi <shoumimukherjee@gmail.com>
🐛 Bug-fix PR
📌 Summary
Fixed 422 validation error when calling
GET /auth/email/mewith API tokens. TheEmailUserResponseschema requiresauth_providerandpassword_change_requiredfields, but some code paths inauth.pywere creatingEmailUserobjects without setting these fields, causing Pydantic validation to fail with a 422 error.🔗 Related Issue
Closes #2700
🐞 Root Cause
The
EmailUserResponseschema (mcpgateway/schemas.py:5318-5319) requiresauth_providerandpassword_change_requiredas mandatory fields. However, 5 locations inmcpgateway/auth.pythat instantiateEmailUserobjects were not setting these fields:_get_user_by_email_sync()(line 375)_user_from_cached_dict()(line 475)When
get_current_user_profile()calledEmailUserResponse.from_email_user(), it attempted to access these missing fields, triggering Pydantic validation errors.💡 Fix Description
Implementation Changes:
EmailUser()instantiation points inauth.pyto include:auth_provider(defaults to"local","api_token"for API tokens)password_change_required(defaults toFalse)_get_auth_context_batched_sync()to include both fields in cached user dicts (line 436)Test Changes:
test_api_token_authentication_success()to use realistic mocks with all required fieldstest_emailuser_response_serialization_with_api_token()regression test to verify serialization works without validation errorsAll changes maintain backward compatibility and follow existing patterns in the codebase.
🧪 Verification
make lintmake testmake coverageTests added:
tests/unit/mcpgateway/routers/test_email_auth_router.py::test_emailuser_response_serialization_with_api_tokentests/unit/mcpgateway/test_auth.py::TestGetCurrentUser::test_api_token_authentication_success📐 MCP Compliance (if relevant)
✅ Checklist
make black isort pre-commit)