claudecodeauth

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 13, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package claudecodeauth wraps the `claude auth` subcommand so the Web UI can drive Claude Code login/logout/status from the browser. The login flow is inherently interactive: `claude auth login` prints a one-shot OAuth URL and then blocks on stdin reading a paste-back authorization code. We model that as a server-side session: Start spawns the CLI, captures the URL, and keeps the process handle; SubmitCode writes the code to stdin and waits for the process to exit; Cancel/Stop kills it.

Only one in-flight login session is supported at a time per process. That matches the single-user assumption of the local dashboard and avoids leaking processes if the operator opens multiple browser tabs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Logout

func Logout(ctx context.Context) error

Logout invokes `claude auth logout`. The CLI is non-interactive in this path so a 10-second timeout is sufficient.

Types

type LoginOptions

type LoginOptions struct {
	// Console switches the OAuth target to api.anthropic.com (console billing)
	// instead of claude.ai subscription auth.
	Console bool
	// Email pre-populates the login page's email field.
	Email string
	// SSO forces the SSO login flow.
	SSO bool
}

LoginOptions controls how `claude auth login` is invoked. The defaults match the CLI's default flow (Claude subscription, no email pre-fill).

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager owns the single in-flight login session per process. Methods are safe for concurrent use by multiple HTTP handlers.

func NewManager

func NewManager() *Manager

NewManager returns a fresh login session manager.

func (*Manager) Cancel

func (m *Manager) Cancel()

Cancel kills the current login session (if any) and clears the manager's reference so a fresh Start can begin.

func (*Manager) Snapshot

func (m *Manager) Snapshot() State

Snapshot returns the current session's state, or an inactive zero State if no session is in flight.

func (*Manager) Start

func (m *Manager) Start(ctx context.Context, opts LoginOptions) (*Session, error)

Start spawns `claude auth login` and waits for it to emit the OAuth URL, returning a Session that the caller can later complete by passing the authorization code to SubmitCode. If another session is already active it is killed first so the new flow can take over.

func (*Manager) SubmitCode

func (m *Manager) SubmitCode(code string) (State, error)

SubmitCode pipes the OAuth authorization code to the running login session and waits up to 30 seconds for the CLI to exit. On success the session is cleared. The returned State describes the final outcome regardless of whether the CLI exited cleanly.

type Session

type Session struct {
	StartedAt time.Time
	URL       string
	// contains filtered or unexported fields
}

Session represents a live `claude auth login` subprocess waiting for the pasted authorization code on stdin.

func (*Session) Snapshot

func (s *Session) Snapshot() State

Snapshot returns a serializable view of the session's progress without taking ownership of any goroutines or channels. Safe for concurrent reads.

type State

type State struct {
	Active    bool      `json:"active"`
	StartedAt time.Time `json:"started_at,omitempty"`
	URL       string    `json:"url,omitempty"`
	Done      bool      `json:"done,omitempty"`
	Success   bool      `json:"success,omitempty"`
	Output    string    `json:"output,omitempty"`
	Error     string    `json:"error,omitempty"`
}

State is a snapshot of session progress safe to serialize for the UI.

type Status

type Status struct {
	LoggedIn         bool   `json:"loggedIn"`
	AuthMethod       string `json:"authMethod,omitempty"`
	APIProvider      string `json:"apiProvider,omitempty"`
	Email            string `json:"email,omitempty"`
	OrgID            string `json:"orgId,omitempty"`
	OrgName          string `json:"orgName,omitempty"`
	SubscriptionType string `json:"subscriptionType,omitempty"`
}

Status mirrors the JSON shape that `claude auth status --json` returns. Fields not emitted by the CLI are simply left zero-valued.

func FetchStatus

func FetchStatus(ctx context.Context) (*Status, error)

FetchStatus runs `claude auth status --json` and parses the result. The CLI returns exit code 0 even when logged out (loggedIn=false in the JSON), so any non-nil error here is a real environmental failure (binary missing, timeout, malformed output) and the UI should surface it as such.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL