auth

package
v0.1.0-alpha.2 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package auth provides credential persistence and renewal for the BuildMax client. It depends on internal/config for the file location and internal/interface/client to exchange a refresh token, and on nothing from the TUI, desktop, or Cobra layers above it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ArtifactPublisherForSession

func ArtifactPublisherForSession() tool.ArtifactPublisher

ArtifactPublisherForSession returns this session's artifact capability, or nil when it has none.

Being logged in is the whole precondition, and it is answered here at assembly time rather than discovered when the model calls the tool. A session with no server keeps its ordinary local output behaviour, and its agent is never offered a tool that could only fail — see docs/design/unified-artifacts.md section 7.1.

func CanAuthenticate

func CanAuthenticate(serverURL string) error

CanAuthenticate reports whether a call to serverURL could authenticate, without making one.

TokenForServer answers the same question by renewing when it has to, which contacts the server and rotates the stored refresh token. That is the wrong thing for a diagnostic to do: `buildmax doctor` is read-only, and a check that quietly spends a credential is not a check.

func Clear

func Clear(path string) error

Clear removes the credentials file at path. It is not an error if the file does not exist.

func IsLoggedIn

func IsLoggedIn() (bool, error)

IsLoggedIn reports whether valid credentials are present.

func Logout

func Logout() error

Logout clears the stored credentials.

func LogoutAndRevoke

func LogoutAndRevoke() error

LogoutAndRevoke clears the stored credentials and asks the server to revoke the session behind them.

The local file is cleared whether or not the server can be reached. Someone who typed `buildmax logout` on a machine with no network is signed out of that machine; leaving the credentials behind because a call failed would be the opposite of what they asked for. The returned error reports what could not be revoked, and is worth printing but not worth failing on.

func RequireLogin

func RequireLogin() error

RequireLogin returns an error when no valid credentials are present.

func Save

func Save(creds *Credentials, path string) error

Save marshals creds to JSON and writes them to path, creating parent directories as needed.

The write goes to a temporary file and is renamed into place. Refreshing rewrites this file while other BuildMax processes may be reading it, and a truncate-then-write would let one of them load a half-written file and conclude it is not signed in.

func SaveCredentials

func SaveCredentials(creds *Credentials) error

SaveCredentials persists a login result.

func TokenForServer

func TokenForServer(serverURL string) (string, error)

TokenForServer returns a usable access token for calls to serverURL, renewing it first if it is spent or nearly so.

It refuses to hand the credential to any other host. A managed model entry names its own server, and settings.yaml is editable by anything running as the user, so a mismatch would otherwise send a BuildMax token — and every prompt that follows — wherever that file said.

Types

type AuthInfo

type AuthInfo struct {
	LoggedIn  bool   `json:"logged_in"`
	ServerURL string `json:"server_url,omitempty"`
	UserID    string `json:"user_id,omitempty"`
	Email     string `json:"email,omitempty"`
	Name      string `json:"name,omitempty"`
}

AuthInfo is the caller-facing authentication view.

func Info

func Info() (AuthInfo, error)

Info returns the current caller-facing auth state.

type Credentials

type Credentials struct {
	ServerURL string `json:"server_url"`
	// Token is the access token. The field keeps its original name so that a
	// credentials file written before refresh tokens existed still loads.
	Token string `json:"token"`
	// RefreshToken renews the access token without another login code. Empty
	// means this login ends when Token expires — either an old file or a server
	// that stores no refresh tokens.
	RefreshToken string `json:"refresh_token,omitempty"`
	UserID       string `json:"user_id"`
	Email        string `json:"email"`
	Name         string `json:"name"`
	SavedAt      int64  `json:"saved_at"`
}

Credentials holds the persisted authentication state (server URL, both halves of the login, and basic user info). Stored as JSON on disk.

func Load

func Load(path string) (*Credentials, error)

Load reads credentials from path. If the file does not exist, it returns (nil, nil) — not an error. Any other read or parse failure is an error.

func (*Credentials) IsUsable

func (c *Credentials) IsUsable() bool

IsUsable reports whether these credentials can still authenticate a call, either because the access token is good or because it can be renewed.

This is the question every command actually has: "am I signed in?" — not "is this particular token still fresh?", which stopped being the same question once a refresh token existed.

func (*Credentials) IsValid

func (c *Credentials) IsValid() bool

IsValid returns true when the credentials contain a non-empty, unexpired access token. It parses the exp claim from the token payload without verifying the signature (clients don't have the server secret).

Jump to

Keyboard shortcuts

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