fix: improve authentication error handling in check_auth#1069
fix: improve authentication error handling in check_auth#1069Vidhushaaa30 wants to merge 2 commits intofossasia:masterfrom
Conversation
Reviewer's guide (collapsed on small PRs)Reviewer's GuideUpdates the _check_auth wrapper to return a proper HTTP 401 response with a JSON error body when authentication is required but missing, instead of a generic 400 with no message. Sequence diagram for updated _check_auth unauthorized response handlingsequenceDiagram
actor Client
participant VisdomServerHandler as VisdomServerHandler
participant _check_auth as _check_auth
participant ProtectedHandler as ProtectedHandler
Client->>VisdomServerHandler: HTTP request to protected endpoint
VisdomServerHandler->>_check_auth: call _check_auth(handler, *args, **kwargs)
_check_auth->>_check_auth: update handler.last_access
alt login_enabled and no current_user
_check_auth->>VisdomServerHandler: set_status(401)
_check_auth->>VisdomServerHandler: write({error: Authentication required})
_check_auth-->>VisdomServerHandler: return
VisdomServerHandler-->>Client: HTTP 401 with JSON error body
else authenticated or login disabled
_check_auth->>ProtectedHandler: call f(handler, *args, **kwargs)
ProtectedHandler-->>VisdomServerHandler: normal response
VisdomServerHandler-->>Client: HTTP 2xx/other success response
end
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- The new lines inside
_check_authare misindented relative to the surrounding block; align them with the existinghandler.set_statusindentation to maintain consistency and avoid potential syntax issues. - Consider also setting a
WWW-Authenticateheader (e.g.,handler.set_header('WWW-Authenticate', 'Basic realm="..."')) along with the 401 response to fully conform with common HTTP authentication practices if applicable to this handler.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The new lines inside `_check_auth` are misindented relative to the surrounding block; align them with the existing `handler.set_status` indentation to maintain consistency and avoid potential syntax issues.
- Consider also setting a `WWW-Authenticate` header (e.g., `handler.set_header('WWW-Authenticate', 'Basic realm="..."')`) along with the 401 response to fully conform with common HTTP authentication practices if applicable to this handler.
## Individual Comments
### Comment 1
<location path="py/visdom/utils/server_utils.py" line_range="56-57" />
<code_context>
if handler.login_enabled and not handler.current_user:
- handler.set_status(400)
- return
+ handler.set_status(401)
+ handler.write({"error": "Authentication required"})
+ return
f(handler, *args, **kwargs)
</code_context>
<issue_to_address>
**issue (bug_risk):** The indentation on these new lines is inconsistent with the surrounding block and may cause an `IndentationError`.
The new lines under `if handler.login_enabled and not handler.current_user:` have one fewer leading space than the original ones. In Python, that mismatch within the same block can raise an `IndentationError`. Please align the indentation of the new `set_status`, `write`, and `return` lines exactly with the original ones.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| handler.set_status(401) | ||
| handler.write({"error": "Authentication required"}) |
There was a problem hiding this comment.
issue (bug_risk): The indentation on these new lines is inconsistent with the surrounding block and may cause an IndentationError.
The new lines under if handler.login_enabled and not handler.current_user: have one fewer leading space than the original ones. In Python, that mismatch within the same block can raise an IndentationError. Please align the indentation of the new set_status, write, and return lines exactly with the original ones.
Description
Improved authentication handling in
check_authby returning a proper HTTP 401 status code along with a descriptive error message when a user is not authenticated.Motivation and Context
Previously, unauthorized access returned a 400 status with no message, which made it unclear for clients. This change improves API clarity and follows standard HTTP practices.
How Has This Been Tested?
Screenshots (if appropriate):
N/A
Types of changes
Checklist:
py/visdom/VERSIONSummary by Sourcery
Bug Fixes: