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

auth

package
v0.5.8 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: MIT Imports: 27 Imported by: 0

Documentation

Overview

Package auth is the two credentials a harness checks. An api key lets a connector call every HTTP route; api keys live hashed in a table in the harness's directory. A join secret lets a machine join the mesh as a worker; it can add a worker and nothing else. It derives from the device code, which is what a person types (code.go). Both are held as a token: the kind, the key of the harness its holder finds through the pointer, and the secret itself.

mp_<kind>_<harness key>.<secret>

Index

Constants

View Source
const (
	Key  = "key"  // a connector's credential: every HTTP route
	Join = "join" // a worker's credential: joining the mesh
)

Variables

View Source
var ErrCode = errors.New("wrong device code")

ErrCode is a device code the harness does not hold.

View Source
var ErrTaken = errors.New("name in use")

ErrTaken is a name that already has an entry of its kind.

Functions

func Addr added in v0.2.0

func Addr(r *http.Request) string

Addr is who sent r: the client address cloudflared puts in CF-Connecting-IP, else the peer. The harness listens on loopback only, so every peer is cloudflared or this machine, and the header can be trusted.

func Code added in v0.5.2

func Code(s string) (string, error)

Code is s as a device code. Case and the hyphen do not matter, nor the letters people read for digits: O is 0, I and L are 1.

func Hash

func Hash(secret string) string

func JoinHandler added in v0.5.2

func JoinHandler(k ed25519.PrivateKey, key string, code func() (string, error), l *Limit) http.Handler

JoinHandler answers the device code exchange of the harness with k, whose code is code() now. Every exchange counts as a refusal in l, since the harness cannot tell a right code from a wrong one: so an address gets few guesses a window.

func NewCode added in v0.5.2

func NewCode() string

NewCode is a random device code.

func Parse

func Parse(kind, token string) (key, secret string, err error)

Parse is a token's harness key and secret, refused when it is not of kind.

func Redeem added in v0.5.2

func Redeem(ctx context.Context, url, key, code string) (string, error)

Redeem trades code for the join token of the harness with key at url, or ErrCode when that harness holds another code or another key.

func Secret added in v0.5.2

func Secret(k ed25519.PrivateKey, code string) string

Secret is the join secret a worker that knew code joins the mesh of k's harness with. It derives from the code, so a new code refuses the old secret and no table holds it.

func Token

func Token(kind, key, secret string) string

Types

type Entry

type Entry struct {
	Name string `json:"name"`
	Hash string `json:"hash"`
}

type Limit added in v0.2.0

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

Limit counts refused credentials and device code exchanges by address, and turns an address away for the rest of a window once it has too many. The window is one for every address, so an address may come back early. A 26-character secret needs no limit against guessing; an 8-character device code does, and this is it.

func (*Limit) Refused added in v0.2.0

func (l *Limit) Refused(addr string)

Refused counts a refused credential from addr, in a new window once the last has passed.

func (*Limit) Take added in v0.5.2

func (l *Limit) Take(addr string) error

Take counts a try from addr, or is why addr is turned away now: the check and the count are one step, so tries at once cannot all pass the check.

func (*Limit) Wait added in v0.2.0

func (l *Limit) Wait(addr string) error

Wait is why addr is turned away now, or nil.

type Store

type Store struct{ Path string }

Store is the table as a file, read on every check so a new or revoked entry counts at once.

func (Store) Add added in v0.5.0

func (s Store) Add(kind, name, secret string) error

Add makes secret an entry of kind named name, beside any before it.

func (Store) Bearer

func (s Store) Bearer(l *Limit, next http.Handler) http.Handler

Bearer refuses a request whose Authorization: Bearer is not a live api key, and one from an address l turns away. A request with no key is refused but not counted: it guesses nothing.

func (Store) Check

func (s Store) Check(kind, secret string) bool

Check is whether secret is a live entry of kind.

func (Store) Issue

func (s Store) Issue(kind, name string) (string, error)

Issue adds an entry and returns its secret: the one time it is in the clear.

func (Store) Load

func (s Store) Load() (Table, error)

func (Store) Revoke

func (s Store) Revoke(kind, name string) error

type Table

type Table map[string][]Entry

Table is the live entries by kind. A revoked entry is gone, not marked.

Jump to

Keyboard shortcuts

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