desktopmode

package
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

Package desktopmode lets the desktop app trade a per-install token for a dashboard session over authenticated loopback, and pins Host and Origin.

Index

Constants

View Source
const (
	ExchangePath = "/api/desktop/exchange"
	RotatePath   = "/api/desktop/rotate"
	RevokePath   = "/api/desktop/revoke"
)

Route paths served by Wrap. They sit outside the API router's route table.

View Source
const Host = "levelrail.localhost"

Host is the only name desktop mode answers to. Browsers share cookies across ports of one host name, so a name of its own keeps the session away from other local servers.

Variables

View Source
var ErrNotSingleUser = errors.New("desktopmode: install is not single-user")

ErrNotSingleUser means the install has several users, which desktop mode does not support.

Functions

func AllowedHosts

func AllowedHosts(ports []string) map[string]bool

AllowedHosts expands Host over the given ports into the exact Host header values allowed.

func Mode

func Mode(getenv func(string) string) (bool, error)

Mode reads APP_DESKTOP_MODE. Unset is off; a value that is not a usual boolean is an error so the caller can fail closed instead of silently dropping the checks.

func TokenPath

func TokenPath(dataDir string) string

TokenPath is the file the host side reads the token from, mode 0600.

Types

type Config

type Config struct {
	Tokens   *Tokens
	Sessions Sessions
	// Admin returns the root admin of this single-user install.
	Admin func(ctx context.Context) (authengine.LegacyUser, error)
	// TTL is read per exchange so the runtime setting applies live.
	TTL func(ctx context.Context) time.Duration
	// Audit records one row; it must never receive secret values.
	Audit  func(ctx context.Context, e store.AuditEntry)
	Logger *slog.Logger
	// Ports are the dashboard ports (guest and host side) the Host header may carry.
	Ports []string
	// FailuresPerMin bad tokens from one peer start a block of Block; 0 disables the limit.
	FailuresPerMin int
	Block          time.Duration
	Now            func() time.Time
	// AllowRemotePeer lifts the loopback peer requirement; tests only.
	AllowRemotePeer bool
}

Config wires a Service. Zero limits disable the matching guard.

type Service

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

Service is desktop mode: token exchange plus Host and Origin enforcement.

func New

func New(cfg Config) *Service

New builds a Service.

func (*Service) Wrap

func (s *Service) Wrap(next http.Handler) http.Handler

Wrap guards every request and serves the three desktop routes; all else goes to next.

type Sessions

type Sessions interface {
	IssueSessionTTL(ctx context.Context, u authengine.LegacyUser, userAgent, ip string, ttl time.Duration) (string, error)
	Revoke(ctx context.Context, token string)
	RevokeAll(ctx context.Context, legacyUserID string) error
}

Sessions is the slice of the auth library desktop mode needs.

type Tokens

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

Tokens owns the per-install token. Only its hash stays in memory and in the hash file; the plaintext lives in the handoff file for the host app.

func NewTokens

func NewTokens(dataDir string) (*Tokens, error)

NewTokens loads the stored hash, or creates a token on first use. A handoff file that is missing, not a plain 0600 file, or not matching the hash (a crash between the two writes) mints a fresh token, since the host could not use the old one.

func (*Tokens) Revoke

func (t *Tokens) Revoke() error

Revoke deletes the token and its hash. It lasts until the next boot, which creates a fresh token like a first run; to keep desktop mode closed, turn APP_DESKTOP_MODE off.

func (*Tokens) Rotate

func (t *Tokens) Rotate() error

Rotate replaces the token and rewrites both files atomically; the old token stops working at once.

func (*Tokens) Verify

func (t *Tokens) Verify(presented string) bool

Verify reports whether presented is the current token, in constant time.

Jump to

Keyboard shortcuts

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