Help us shape the future of Go! Take the 2026 Go Developer Survey

machineauth

package
v1.4.15 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package machineauth is the machine-surface bearer token: where it comes from, how it is created, how a request is checked against it, and which bind addresses may carry it.

Purpose: P7-SURF-25 (EPIC SURF D4). The MCP server (and later the HTTP

control API) needs one token source with one set of safety rules, so no
command re-implements them.

Inputs: the NSELF_MCP_TOKEN environment variable, the file

~/.nself/mcp-token, and the user's home directory.

Outputs: a token plus its source ("env", "file" or "generated"), an

http.Handler middleware, and bind-address checks. Every refusal is an
errs E426 error.

Constraints: Lookup never writes anything; only Resolve creates the token

file, and only when Lookup finds none. A token value is never written to
any stream (Resolve prints the file path only). Layer L1: this package
imports only the standard library and internal/errs.

SPORT: CLI-CAP-MACHINE-AUTH-001

Index

Constants

View Source
const (
	SourceEnv       = "env"
	SourceFile      = "file"
	SourceGenerated = "generated"
)

Token sources reported by Lookup and Resolve.

View Source
const EnvVar = "NSELF_MCP_TOKEN"

EnvVar is the environment variable that carries the machine token.

Variables

This section is empty.

Functions

func CheckBind

func CheckBind(host string, allowedHosts []string, source string) error

CheckBind returns nil for a loopback host. A non-loopback host passes only with at least one non-empty allowedHosts entry and a configured token (source "env" or "file"); otherwise it is refused with E426.

func IsLoopback

func IsLoopback(host string) bool

IsLoopback reports whether host is a loopback bind address: "localhost", any 127.0.0.0/8 address, or ::1 (with or without brackets). The empty host, 0.0.0.0 and :: bind every interface and are not loopback.

func Lookup

func Lookup() (tok, source string, err error)

Lookup finds the configured token without creating anything. The environment variable wins (source "env"); otherwise an existing token file is used (source "file"). With neither it returns "", "", nil. A token file that is a symlink, not a regular file, readable by group or others (non-Windows) or empty is refused with E426.

func Middleware

func Middleware(tok, realm string) func(http.Handler) http.Handler

Middleware returns a wrapper that serves the next handler only when the request presents "Authorization: Bearer <tok>". Otherwise it answers 401 with the body "unauthorized" and a WWW-Authenticate: Bearer realm header. An empty tok rejects every request (fail closed), and so does a request that carries more than one Authorization header.

func Resolve

func Resolve() (tok, source string, generated bool, err error)

Resolve is Lookup, then token generation when nothing is configured. It reports the new file path (never the token) on standard error.

func ResolveTo

func ResolveTo(notice io.Writer) (tok, source string, generated bool, err error)

ResolveTo is Resolve with the notice writer injected.

func TokenPath

func TokenPath() (string, error)

TokenPath returns the token file path, ~/.nself/mcp-token.

Types

This section is empty.

Jump to

Keyboard shortcuts

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