Skip to content

feat: Implement User Authentication and Session Ownership#5

Merged
DhruvSrikanth merged 24 commits intomainfrom
feat-user-auth
Apr 23, 2025
Merged

feat: Implement User Authentication and Session Ownership#5
DhruvSrikanth merged 24 commits intomainfrom
feat-user-auth

Conversation

@yuxiang-wu
Copy link
Contributor

Description:

This PR introduces user authentication to the Weco API, allowing users to create accounts and associate optimization sessions with their accounts. It enhances security and enables personalized experiences.

Motivation:

Previously, all sessions were anonymous. This change adds a layer of user management, enabling:

  • Users to track their past optimization sessions.
  • Protection of session data, ensuring only the owner (if authenticated) can access or modify their sessions.
  • Future features that rely on user accounts.

Changes Made:

  • Authentication Endpoints: Added new endpoints under /v1/auth for user registration (/signup), login (/token), and potentially retrieving user info.
  • JWT Implementation: Integrated JWT for handling user authentication tokens. Login returns an access token, which should be included in subsequent requests as a Bearer token.
  • Supabase Integration: Leverages Supabase Auth for user management (signup, login verification).
  • Middleware/Dependency: Implemented FastAPI dependencies (get_current_user_id) to extract the user ID from valid JWT tokens in request headers.
  • Session Endpoint Updates:
    • Modified POST /v1/sessions/ (create_session) to associate the new session with the authenticated user's ID, if available.
    • Updated POST /v1/sessions/{session_id}/suggest (suggest_solution) and GET /v1/sessions/{session_id} (get_session_status) to verify that if a session has a user_id, the requesting user (identified via token) matches the session owner. Unauthorized access attempts result in a 403 Forbidden error.
    • Sessions created without authentication remain accessible without a token but are not associated with any user.
  • Database Schema: (Implicit) Assumes the sessions table now includes a nullable user_id column (likely a UUID referencing the auth.users table).
  • Configuration: Added necessary environment variables for Supabase URL and keys.

Testing:

  • Manually tested signup, login, and token retrieval.
  • Verified that authenticated requests to session endpoints succeed for the owner.
  • Verified that unauthenticated requests to owned sessions fail (403).
  • Verified that requests to unowned sessions fail (403).
  • Verified that sessions created without authentication can still be accessed and managed without a token.
  • Tested session creation both with and without authentication tokens.

Notes:

  • API keys for LLM models are still passed via request metadata, separate from user authentication.
  • Ensure necessary Supabase environment variables are set in deployment environments.

- Introduced `auth.py` for managing API key storage and retrieval.
- Updated `__init__.py` to allow custom base URL via environment variable.
- Enhanced API functions in `api.py` to accept authentication headers.
- Implemented login flow in `cli.py` for user authentication and session management.
- Improved command-line interface with subcommands for running optimizations and logging out.
…asks, update API key setup instructions, and introduce `weco run` and `weco logout` commands with detailed examples and argument descriptions.
…g for authentication, update README.md to reflect changes in login options and API key storage location, and improve command-line prompts for user clarity.
- Introduced a new parser definition for the "logout" command in `cli.py` to allow users to log out and clear saved API keys.
…ent. Added handling for 202 Accepted status with authorization pending, and removed redundant checks for authorization pending in 400 errors. Enhanced error reporting for unexpected responses.
This commit merges the user authentication feature branch (`feat-user-auth`) into main and resolves the resulting merge conflicts.

Key changes include:

- **Resolved Merge Conflicts:** Addressed conflicts in `README.md` and `weco/cli.py`.
- **Integrated User Authentication:**
    - Introduced `weco run` and `weco logout` subcommands.
    - Implemented the device authentication grant flow (`perform_login`) for user login via the CLI.
    - Added logic to prompt the user to log in or proceed anonymously if no Weco API key is found.
    - Updated API calls (`api.py`, `cli.py`) to include `Authorization` headers when logged in and pass client LLM keys in `metadata`.
    - Added `session_id` display to the log path in the `SummaryPanel` (`panels.py`).
    - Updated `README.md` to document the login/logout commands and the device flow.
- **Integrated `main` Branch Features:**
    - Preserved `main`'s README structure and content for conflicting sections (Example, Arguments Table, Performance).
    - Retained `--log-dir` and `--preserve-source` arguments and their functionality in `cli.py`.
    - Ensured file handling logic uses correct file suffixes and respects `--preserve-source`.
- **Fixes:**
    - Resolved syntax errors in `cli.py` introduced by merge conflict markers.
    - Applied minor code formatting and comment cleanup.
@DhruvSrikanth DhruvSrikanth added bug Something isn't working documentation Improvements or additions to documentation enhancement New feature or request labels Apr 22, 2025
@DhruvSrikanth DhruvSrikanth self-assigned this Apr 23, 2025
Copy link
Member

@DhruvSrikanth DhruvSrikanth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@DhruvSrikanth DhruvSrikanth merged commit 8abfcad into main Apr 23, 2025
1 check passed
@DhruvSrikanth DhruvSrikanth deleted the feat-user-auth branch April 23, 2025 15:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working documentation Improvements or additions to documentation enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants