agentaccess

package
v0.137.0 Latest Latest
Warning

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

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

Documentation

Overview

Package agentaccess is the shared-token access model of a local DataTug agent (`datatug serve`, and the daemon that runs it): loopback plus a token. The agent creates a random secret when it starts and keeps it in the state directory (mode 0600); everything the browser and the CLI need is derived from it:

  • the session token, which every route but the preflight and the exchange requires;
  • a one-time code, which `datatug daemon url` and `datatug serve` mint locally (no HTTP request, no daemon needed) and put in the fragment of a link, and which the web UI exchanges once for the session token.

A code is `v1.<expiry unix seconds>.<nonce>.<signature>`: signed with the secret, valid for CodeTTL, accepted once. The secret, the token and the codes are never logged.

Index

Constants

View Source
const (
	// CodeTTL is how long a minted code can be exchanged.
	CodeTTL = 10 * time.Minute
	// DirEnv overrides the state directory (default ~/datatug/daemon); it is the daemon's own.
	DirEnv = "DATATUG_DAEMON_DIR"
	// ExchangePath is the route (under /datatug) that trades a code for the session token.
	ExchangePath = "/datatug/session/exchange"
)

Variables

View Source
var ErrBadCode = errors.New("the link code is not valid")

ErrBadCode is the one answer for a code that is malformed, tampered, expired or used.

Functions

func Apply

func Apply(req *http.Request)

Apply adds the session token of the local agent that req addresses, when req is for a loopback address and that agent has left its key in the state directory. It is how the CLI's own HTTP clients (and the daemon's probe) talk to an agent that requires the token; any other request is left as it is, so the token never goes to another host.

func ApplyIn

func ApplyIn(dir func() string, req *http.Request)

ApplyIn is Apply with the state directory given by dir.

func KeyPath

func KeyPath(dir string, port int) string

KeyPath is the access key file of the agent that listens on port.

func MintCode

func MintCode(secret []byte, now time.Time) (string, error)

MintCode returns a one-time code valid for CodeTTL from now. It needs the secret only.

func NewSecret

func NewSecret() ([]byte, error)

NewSecret returns a fresh random secret.

func ReadKey

func ReadKey(dir string, port int) ([]byte, error)

ReadKey reads the secret of the agent on port.

func RemoveKey

func RemoveKey(dir string, port int)

RemoveKey deletes the key of the agent on port.

func SessionToken

func SessionToken(secret []byte) string

SessionToken is the token that every route requires, derived from secret.

func StateDir

func StateDir() string

StateDir is the directory holding an agent's access key (and the daemon's record).

func WriteKey

func WriteKey(dir string, port int, secret []byte) error

WriteKey stores secret for the agent on port, readable by the current user only. The state directory must not be writable by anyone else (another user could plant a link or a file), and the file is created fresh, never through an existing file or link.

Types

type Access

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

Access holds the secret of a running agent and the codes it has accepted.

func New

func New(secret []byte) *Access

New returns the Access of an agent with secret.

func (*Access) Authorized

func (a *Access) Authorized(r *http.Request) bool

Authorized reports whether r carries the session token as `Authorization: Bearer <token>`.

func (*Access) Exchange

func (a *Access) Exchange(code string) (string, error)

Exchange accepts code once and returns the session token. A code that is malformed, tampered, expired or already used is ErrBadCode.

Jump to

Keyboard shortcuts

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