Skip to content

feat(auth): implement cookie-based JWT authentication and logout functionality#105

Open
Ndevu12 wants to merge 2 commits intomainfrom
fx/auth-cookies
Open

feat(auth): implement cookie-based JWT authentication and logout functionality#105
Ndevu12 wants to merge 2 commits intomainfrom
fx/auth-cookies

Conversation

@Ndevu12
Copy link
Copy Markdown
Owner

@Ndevu12 Ndevu12 commented Mar 25, 2026

  • Introduced CookieJWTAuthentication to support both header-based and cookie-based JWT authentication, prioritizing headers for explicit auth.
  • Updated login and refresh views to set JWT tokens in HttpOnly cookies, enhancing security for browser clients.
  • Added a logout endpoint to clear authentication cookies.
  • Refactored frontend auth logic to remove reliance on access tokens stored in state, transitioning to cookie-based management.
  • Updated environment settings and documentation to reflect new JWT cookie configurations.

closes: #97

…tionality

- Introduced `CookieJWTAuthentication` to support both header-based and cookie-based JWT authentication, prioritizing headers for explicit auth.
- Updated login and refresh views to set JWT tokens in HttpOnly cookies, enhancing security for browser clients.
- Added a logout endpoint to clear authentication cookies.
- Refactored frontend auth logic to remove reliance on access tokens stored in state, transitioning to cookie-based management.
- Updated environment settings and documentation to reflect new JWT cookie configurations.
@Ndevu12 Ndevu12 self-assigned this Mar 25, 2026
@Ndevu12 Ndevu12 added enhancement New feature or request fix Pull requests that fix some issues labels Mar 25, 2026
@vercel
Copy link
Copy Markdown

vercel bot commented Mar 25, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
the-inventory Ready Ready Preview, Comment Mar 25, 2026 7:30pm
the-inventory-ui Ready Ready Preview, Comment Mar 25, 2026 7:30pm

try:
return super().get_validated_token(raw_token)
except InvalidToken as e:
raise InvalidToken(f"Invalid token: {str(e)}")

Check warning

Code scanning / CodeQL

Information exposure through an exception Medium

Stack trace information
flows to this location and may be exposed to an external user.

Copilot Autofix

AI 7 days ago

In general, to fix information exposure through exceptions, do not propagate raw exception messages or stack traces to layers that may be visible to end users. Instead, log the detailed exception server-side (if needed) and raise or return a generic, non-sensitive message to the client.

For this specific file, the best fix is to stop embedding str(e) in the new InvalidToken message in get_validated_token. We can simply raise a new InvalidToken with a generic message such as "Invalid token" or reuse the original exception without altering its message, depending on what is safer and less revealing in this codebase. Since CodeQL flags the flow from e to the formatted string, the minimal, non-breaking change is to keep the try/except structure (so behavior of wrapping super().get_validated_token errors remains the same) but replace f"Invalid token: {str(e)}" with a constant, non-tainted string like "Invalid token". No new imports or additional methods are required.

Concretely:

  • In src/api/authentication.py, in CookieJWTAuthentication.get_validated_token, replace line 29 raise InvalidToken(f"Invalid token: {str(e)}") with raise InvalidToken("Invalid token").
  • This preserves the exception type and overall control flow (views will still receive InvalidToken), but no longer includes the inner exception’s message, avoiding potential leakage of stack trace or internal validation details.
Suggested changeset 1
src/api/authentication.py

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/api/authentication.py b/src/api/authentication.py
--- a/src/api/authentication.py
+++ b/src/api/authentication.py
@@ -25,8 +25,9 @@
         """Validate JWT token with proper error handling."""
         try:
             return super().get_validated_token(raw_token)
-        except InvalidToken as e:
-            raise InvalidToken(f"Invalid token: {str(e)}")
+        except InvalidToken:
+            # Raise a generic error message to avoid leaking internal details
+            raise InvalidToken("Invalid token")
 
     def authenticate(self, request):
         """Authenticate request using header or cookie.
EOF
@@ -25,8 +25,9 @@
"""Validate JWT token with proper error handling."""
try:
return super().get_validated_token(raw_token)
except InvalidToken as e:
raise InvalidToken(f"Invalid token: {str(e)}")
except InvalidToken:
# Raise a generic error message to avoid leaking internal details
raise InvalidToken("Invalid token")

def authenticate(self, request):
"""Authenticate request using header or cookie.
Copilot is powered by AI and may make mistakes. Always verify output.
…cale parsing

- Implemented cookie-based JWT access management in middleware to enforce authentication on protected routes.
- Added utility functions for parsing locale paths and determining access requirements based on JWT presence.
- Updated the `AuthGuard` tests to reflect changes in authentication logic and error handling.
- Introduced new tests for authentication path utilities to ensure correct locale handling and access checks.
- Refactored the `AuthProvider` to support server-side authentication state hydration and improved state synchronization.
- Enhanced the `Providers` component to manage dehydrated state for better performance in server-rendered contexts.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request fix Pull requests that fix some issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: store sensitive auth utilities like token in cookies

2 participants