Backend: Authentication
Relevant source files
The following files were used as context for generating this wiki page:
The authentication system in OpenCode manages user identity for both the command-line interface (CLI) and the web console. It facilitates user login/logout, session management, and token handling. The CLI uses a local token file for persistence, while the web console uses a database-backed session mechanism, often initiated via an OAuth-like flow for services such as Claude Pro.
The system is split between the opencode package, which handles CLI authentication and local token storage, and the console package, which manages web-based authentication, database interactions, and the OAuth callback process.
CLI Authentication
The opencode auth command provides a set of tools for managing user authentication from the terminal. It allows users to log in, log out, and check their current authentication status.
Sources: packages/opencode/src/cli/cmd/auth.ts, packages/opencode/src/auth/index.ts
Commands
The auth command group includes the following subcommands:
| Command | Description |
|---|---|
auth login | Initiates the login process by opening a browser window for authentication. |
auth logout | Logs the user out by removing the locally stored authentication token. |
auth status | Checks and displays the current authentication status and associated user. |
Sources: packages/opencode/src/cli/cmd/auth.ts:20-56
CLI Login Flow
The opencode auth login command triggers a browser-based authentication flow. The Auth.login() function constructs a URL with a unique token and opens it in the user's default browser. After successful authentication on the web, the CLI can use the authenticated session.
The diagram below illustrates the CLI login process.
Token Management
The Auth namespace in packages/opencode/src/auth/index.ts is responsible for managing the lifecycle of the authentication token on the local machine.
- Storage: The token is stored in a file at
~/.opencode/token. Auth.set(token): Writes the provided token to the token file.Auth.remove(): Deletes the token file, effectively logging the user out.Auth.token(): Reads and returns the token from the file.
Sources: packages/opencode/src/auth/index.ts:9-23
Web Console Authentication
The web console authentication handles user sessions for the web interface, including logging in with external providers like Anthropic for Claude Pro access.
Sources: packages/console/app/src/routes/auth/callback.ts, packages/console/core/src/auth.sql.ts
OAuth Callback Handling
The GET /auth/callback endpoint is the entry point for completing the web authentication flow. It receives a temporary token, validates it, creates a user and session in the database, and then sets a session cookie in the user's browser before redirecting them.
Key steps in the callback handler:
- Extracts the
tokenfrom the URL query parameters. - Calls
Auth.Service.fromToken()to validate the token and retrieve user information. - Creates a user record if one does not already exist using
User.create(). - Creates a new session for the user with
Session.create(). - Sets the
sst_sessioncookie in the browser response. - Redirects the user to the home page (
/).
Sources: packages/console/app/src/routes/auth/callback.ts:3-23
The sequence diagram below shows the web authentication callback flow.
Data Model
The authentication system relies on two primary tables in the database: users and sessions. These are managed through SQL queries defined in packages/console/core/src/auth.sql.ts.
Database Schema
The relationship between users and sessions is one-to-many.
Tables
users Table
Stores information about individual users.
| Column | Type | Constraints | Description |
|---|---|---|---|
id | text | PRIMARY KEY | Unique identifier for the user. |
email | text | NOT NULL | The user's email address. |
time_created | datetime | NOT NULL | Timestamp of user creation. |
time_updated | datetime | NOT NULL | Timestamp of last user update. |
Sources: packages/console/core/src/auth.sql.ts:5-9
sessions Table
Stores session information for authenticated users.
| Column | Type | Constraints | Description |
|---|---|---|---|
id | text | PRIMARY KEY | Unique identifier for the session. |
user_id | text | NOT NULL | Foreign key referencing the users table. |
time_created | datetime | NOT NULL | Timestamp of session creation. |
time_expires | datetime | NOT NULL | Timestamp when the session expires. |
Sources: packages/console/core/src/auth.sql.ts:18-22
Key Database Queries
The auth.sql.ts file defines several functions for interacting with the database.
User.create(email, id): Creates a new user.User.fromID(id): Retrieves a user by their ID.User.fromEmail(email): Retrieves a user by their email.Session.create(userID): Creates a new session for a given user ID.Session.fromID(id): Retrieves a session by its ID.Session.remove(id): Deletes a session.
Sources: packages/console/core/src/auth.sql.ts:27-70
The frontend also provides UI elements for authentication, such as the "Claude Pro" login option, which indicates integration with external authentication providers.