jwtauth

package
v0.44.2 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2026 License: MIT Imports: 9 Imported by: ? 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 jwtauth is the hub's attach-time authentication: it issues and verifies the HMAC-SHA256 JWTs peers present, holds the signing secret so it can be rotated live (Secret), and enforces the whole check in front of the tunnel handler (Guard). The token's subject is the peer id the hub uses as the tunnel key.

Index

Constants

View Source
const (
	// ReasonUnauthorized is a missing, malformed, expired, or
	// wrongly-signed token.
	ReasonUnauthorized = "unauthorized"
	// ReasonInvalidName is a valid token naming a peer the proxy could
	// never address.
	ReasonInvalidName = "invalid-peer-name"
	// ReasonBlocked is a valid token for a denylisted peer.
	ReasonBlocked = "blocked"
)

The reasons a RejectHook is called with.

Variables

View Source
var ErrUnauthenticated = errors.New("unauthenticated")

ErrUnauthenticated is returned by PeerFrom for a context the guard never stamped, which can only happen if the handler is mounted without it.

Functions

func Issue

func Issue(secret []byte, peer, tunnelURL string, ttl time.Duration) (string, error)

Issue mints a signed JWT for peer, valid for ttl. tunnelURL is carried in the audience claim: it is the hub the token is for, and the peer reads it (unverified, it holds no key) to know where to dial. That makes the signed token the whole join token, with no envelope around it.

func PeerFrom

func PeerFrom(ctx context.Context) (string, error)

PeerFrom returns the peer id the guard verified for this request. Its signature is attach.Identity, so it wires straight into attach.NewHandler.

func Verify

func Verify(secret []byte, token string) (string, error)

Verify checks a token's signature and expiry and returns the peer id (subject). The signing method is pinned to HS256.

Types

type Blocker

type Blocker interface {
	IsBlocked(peer string) bool
}

Blocker is the peer-id denylist the guard consults on every attach. The ban is on the identity, not one token, so a blocked peer stays out even holding a valid, unexpired token.

type Guard

type Guard struct {
	Secret   *Secret
	Blocked  Blocker
	OnReject RejectHook
}

Guard is the attach-time gate in front of the hub's tunnel handler: it verifies the Bearer JWT, refuses unroutable and blocked peers, and stamps the peer id onto the request context for PeerFrom.

guard := jwtauth.Guard{Secret: secret, Blocked: blocks, OnReject: metrics.RecordReject}
mux.Handle("/", guard.Middleware(attach.NewHandler(registry, jwtauth.PeerFrom, logger)))

Only Secret is required: without a Blocker nothing is denylisted (every valid token attaches), and without a RejectHook refusals are simply not counted.

func (Guard) Middleware

func (g Guard) Middleware(next http.Handler) http.Handler

Middleware wraps next with the attach-time checks. The secret is read per request, so a rotate takes effect on the next attach.

type RejectHook

type RejectHook func(ctx context.Context, reason string)

RejectHook observes refused attaches with a low-cardinality reason, which makes it a metric counter's attribute as-is.

type Secret

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

Secret is the hub's JWT signing secret, held behind an atomic pointer so the console's rotate-secret can swap it live — invalidating every issued token — without a process restart. Every read goes through Get, so the next verification after a rotate already uses the new secret.

func NewSecret

func NewSecret(b []byte) *Secret

NewSecret holds b as the current signing secret.

func (*Secret) Get

func (s *Secret) Get() []byte

Get returns the current signing secret, or nil before one is set.

func (*Secret) Set

func (s *Secret) Set(b []byte)

Set replaces the signing secret. Tokens signed with the previous one stop verifying immediately.

Jump to

Keyboard shortcuts

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