endpoint

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package endpoint is the standard form of a loopback data endpoint that a plugin result declares and a host admits or refuses. JSON lines cannot carry continuous data such as media or capture frames, so a plugin points at a local listener instead; this package validates the pointer, ties its lifetime to the plugin session and keeps its secret parts out of errors and logs.

An endpoint value is a credential: whoever can read it and reach the port can use the listener. Every error, String and %v/%+v/%#v rendering from this package omits the address, path and token. JSON encoding carries the real values because the protocol needs them.

Presenting the token. A client presents Endpoint.Token to the listener the same way for every consumer: for ws, wss, http and https, as the header "Authorization: Bearer <token>" on the handshake or request; for tcp, as the first line of the stream, the token followed by one "\n", before any other data. A listener that gets no token, or a wrong one, closes the connection. When Token is empty the listener needs none. The token is never put in a URL. Grant.AuthorizationHeader and Grant.DialTCP present it this way.

Rotation. A grant is bounded by its session and by ExpiresAt. For a listener that outlives one expiry the plugin announces a replacement endpoint and the host Admits it and then Revokes the old grant. Announcing is the consumer's contract (a reverse operation, conventionally named "endpoint.rotate", whose payload is {"endpoint": <Endpoint>}); the library only validates and admits.

Index

Constants

View Source
const (
	MaxAddressBytes = 255
	MaxPathBytes    = 1024
	MaxTokenBytes   = 512
	// DefaultMaxLifetime applies when a Policy sets none.
	DefaultMaxLifetime = time.Hour
)

Bounds on a declared endpoint.

View Source
const Key = "endpoint"

Key is the conventional JSON key under which a result carries an Endpoint: {"endpoint": {...}}. A contract may nest it elsewhere; Decode reads the object.

Variables

View Source
var (
	// ErrUsed is returned when a one-time token is read twice.
	ErrUsed = errors.New("plugin endpoint token already used")
	// ErrExpired is returned for a grant that was revoked, whose session closed
	// or whose time ran out.
	ErrExpired = errors.New("plugin endpoint expired")
)
View Source
var ErrDial = errors.New("plugin endpoint connection failed")

ErrDial is returned when DialTCP cannot connect or present the token. It carries no detail: the system's own dial errors name the address, which is as secret as the token.

Functions

This section is empty.

Types

type Endpoint

type Endpoint struct {
	// Scheme is one of ws, wss, http, https or tcp, and must be allowed by the
	// host's Policy.
	Scheme string `json:"scheme"`
	// Address is a loopback "host:port": "localhost", a loopback IP literal, or
	// a bracketed IPv6 loopback literal.
	Address string `json:"address"`
	// Path is optional and begins with "/". It may hold a secret identifier.
	Path string `json:"path,omitempty"`
	// Token is an optional secret the host presents to the listener. It is
	// reusable while the grant is active unless OneTime is set.
	Token string `json:"token,omitempty"`
	// OneTime makes the token single-use: the first Grant.Token succeeds and
	// later calls return ErrUsed. It limits how often the host may read the
	// token, not how many connections the listener accepts: a listener that wants
	// one attachment enforces that itself.
	OneTime bool `json:"oneTime,omitempty"`
	// ExpiresAt bounds the endpoint's life. It is required.
	ExpiresAt time.Time `json:"expiresAt"`
}

Endpoint is a loopback listener declared by a plugin.

func Decode

func Decode(raw json.RawMessage) (Endpoint, error)

Decode reads one Endpoint from JSON, rejecting unknown fields, duplicate keys and anything Check refuses. It applies no host policy: call Admit (or Validate) for that. An error never includes a value from the input.

func (Endpoint) Check

func (e Endpoint) Check() error

Check validates the endpoint's shape, which no policy changes: a supported scheme, a loopback host with a port, a well-formed path and token, and an expiry. Its errors never include a value from the endpoint.

func (Endpoint) Format

func (e Endpoint) Format(f fmt.State, _ rune)

func (Endpoint) GoString

func (e Endpoint) GoString() string

func (Endpoint) String

func (e Endpoint) String() string

String and the formatting methods print only the scheme.

func (Endpoint) Validate

func (e Endpoint) Validate(p Policy) error

Validate checks the endpoint against the policy: Check, then the admitted schemes and the lifetime. Its errors never include a value from the endpoint.

type Grant

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

Grant is an admitted endpoint. It stays usable until the session ends or the endpoint expires, whichever is first.

func Admit

func Admit(session Lifetime, e Endpoint, p Policy) (*Grant, error)

Admit validates the endpoint and returns a Grant tied to the session. A host that wants to refuse an endpoint returns an error before calling Admit.

func (*Grant) Active

func (g *Grant) Active() bool

Active reports whether the grant can still be used.

func (*Grant) AuthorizationHeader

func (g *Grant) AuthorizationHeader() (string, error)

AuthorizationHeader returns the value for the Authorization header that a client of a ws, wss, http or https endpoint sends ("Bearer <token>"), per the convention in the package documentation. It returns an empty string when the endpoint has no token, and ErrInvalid for a tcp endpoint, which presents its token as the first line instead (see DialTCP). It reads the token, so a one-time endpoint allows one call; an ended grant returns ErrExpired.

func (*Grant) DialTCP

func (g *Grant) DialTCP(ctx context.Context) (net.Conn, error)

DialTCP connects to a tcp endpoint and presents its token as the first line, the token followed by one "\n", before any other data, per the convention in the package documentation. An endpoint without a token sends nothing. The caller owns the returned connection and the grant's lifetime still applies: an ended grant returns ErrExpired, a one-time token is read only once, and a cancelled or expired ctx returns ctx.Err().

func (*Grant) Format

func (g *Grant) Format(f fmt.State, _ rune)

func (*Grant) GoString

func (g *Grant) GoString() string

func (*Grant) Revoke

func (g *Grant) Revoke()

Revoke ends the grant now; it is how a host retires an endpoint it replaced. It is safe to call more than once.

func (*Grant) Scheme

func (g *Grant) Scheme() string

Scheme returns the admitted scheme.

func (*Grant) String

func (g *Grant) String() string

func (*Grant) Target

func (g *Grant) Target() (scheme, address, path string, err error)

Target returns the scheme, address and path without the token, or ErrExpired. The result is secret: do not log it.

func (*Grant) Token

func (g *Grant) Token() (string, error)

Token returns the token, or an empty string when the endpoint has none. It can be called any number of times while the grant is active, so a host may give it to successive clients; a one-time endpoint allows one call and then returns ErrUsed. An ended grant returns ErrExpired.

type Lifetime

type Lifetime interface{ Done() <-chan struct{} }

Lifetime is anything that ends: a plugin session, normally.

type Policy

type Policy struct {
	// Schemes lists the admitted schemes. It is required: an empty list admits
	// nothing.
	Schemes []string
	// MaxLifetime bounds ExpiresAt from now; zero uses DefaultMaxLifetime.
	MaxLifetime time.Duration
	// Now overrides the clock for tests.
	Now func() time.Time
}

Policy is the host's decision about what it will admit.

Jump to

Keyboard shortcuts

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