Skip to main content

JWT Token Structure

The application uses JSON Web Tokens (JWT) for session management. Tokens are stored in httpOnly cookies for security.

Token Payload

string
required
Subject - User ID as a string
integer
required
Expiration time - Unix timestamp when the token expires
integer
required
Issued at - Unix timestamp when the token was created

Token Generation

Tokens are created using the create_access_token function in app/auth/jwt_utils.py:6.

Configuration

string
required
Secret key used for signing JWT tokens. Must be kept secure.
string
default:"HS256"
Algorithm used for JWT encoding/decoding
integer
Token expiration time in minutes

Token Validation

Tokens are validated using the decode_access_token function in app/auth/jwt_utils.py:17.

Validation Rules

  1. Signature Verification - Token must be signed with the correct SECRET_KEY
  2. Expiration Check - Token must not be expired
  3. Subject Presence - Token must contain a “sub” claim with the user ID
  4. Algorithm Match - Token must use the configured algorithm

Return Values

integer
The user ID extracted from the token
null
Returned when:
  • Token signature is invalid
  • Token is expired
  • Token is malformed
  • “sub” claim is missing

Authentication Dependency

The get_current_user dependency function validates the JWT cookie and retrieves the authenticated user from the database.

Implementation

Defined in app/auth/dependencies.py:9:

Behavior

  1. Extracts the access_token from the httpOnly cookie
  2. Decodes and validates the JWT token
  3. Queries the database for the user by ID
  4. Returns the User object if found
  5. Raises HTTPException 401 for any authentication failure

Error Responses

error
Not authenticated - Cookie is missing
error
Invalid or expired token - JWT validation failed
error
User not found - User ID from token doesn’t exist in database

Logout

Clears the authentication cookie, effectively logging out the user.

Response

string
Confirmation message: “Logged out”

Behavior

  • Deletes the access_token httpOnly cookie
  • Cookie deletion respects the same security settings (secure, samesite) as when it was set
  • No authentication required (can be called even if not logged in)