BLOG-86 Checking authentication before uploading image #101
Loading…
x
Reference in New Issue
Block a user
No description provided.
Delete Branch "BLOG-86_image_can_only_be_uploaded_by_logged_in_user"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Description
This PR introduces a generic authentication middleware to protect application routes. The primary goal is to prevent unauthenticated users from uploading images.
Changes Implemented
Authentication Middleware:
auth_middleware
that checks the user's session for a validuser_id
.user_id
exists, it's added to the request extensions, making it available to downstream handlers.UserId
Extractor:UserId
type that implementsFromRequest
has been added.user_id: UserId
as a parameter. If the user is not logged in, the extractor automatically returns anErrorUnauthorized
response.Route Protection:
upload_image_handler
now includes theUserId
extractor, securing the endpoint./auth/me
route has been added for easily verifying the logged-in user's ID during development and testing.Minor Refinements:
logout_handler
now usessession.clear()
for more robust session termination.redis://127.0.1:6379
toredis://127.0.0.1:6379
.Package Changes
No response
Screenshots
No response
Reference
Resolves #86
Checklist
/improve
PR Code Suggestions ✨
Block unauthenticated requests in middleware
The
auth_middleware
currently allows requests to proceed ifsession.get
returns anerror or if the
user_id
is not found in the session. This bypasses authenticationfor unauthenticated or malformed session requests. The middleware should instead
return an
ErrorUnauthorized
in these cases to properly enforce authentication.backend/feature/auth/src/framework/web/auth_middleware.rs [21-32]
Suggestion importance[1-10]: 10
__
Why: The current implementation allows unauthenticated requests to proceed if
session.get
fails or returnsNone
, which is a critical security vulnerability. The suggestion correctly enforces authentication by returningErrorUnauthorized
in these cases.Addressed in
04c9d4dcf0
.Removed redoundent middleware.