apptoken

package
v0.16.355 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: AGPL-3.0 Imports: 19 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 apptoken is the platform's token for an app's backend (docs/build-apps/10-auth.md): a short JWT the host puts in the calls to an app's MCP server so the server knows who calls, for which app, without a sign-in of its own. The hub signs it with one Ed25519 key of the deployment and publishes the public half at /.well-known/jwks.json; a machine without a hub signs with its own key. A backend checks the signature, the issuer, its own audience and the expiry.

Nothing here holds a person's password or an identity provider's token: the token says who the hub signed in, and lives minutes.

Index

Constants

View Source
const (
	TTL    = 5 * time.Minute
	MaxTTL = 10 * time.Minute
	// Type is the JWT's typ header: a backend may insist on it, so an ID token of the same issuer is never taken for it.
	Type = "harnessly-app+jwt"
	// JWKSPath is where a hub (and a machine without one) publishes its public keys.
	JWKSPath = "/.well-known/jwks.json"
	// DiscoveryPath is the OpenID discovery document of the issuer: its iss and where its keys are, so a backend checks
	// the tokens with a stock OIDC library.
	DiscoveryPath = "/.well-known/openid-configuration"
)

TTL is how long a token lives; MaxTTL bounds what a caller may ask.

View Source
const (
	GrantTokenExchange = "urn:ietf:params:oauth:grant-type:token-exchange"
	TokenTypeJWT       = "urn:ietf:params:oauth:token-type:jwt"
	TokenTypeAccess    = "urn:ietf:params:oauth:token-type:access_token"
)

The grant and token types of RFC 8693.

Variables

View Source
var (
	ErrNoAudience = errors.New("apptoken: a token needs an audience")
	ErrNoSubject  = errors.New("apptoken: a token needs a subject")
	ErrInvalid    = errors.New("apptoken: the token is not valid")
)

Errors of Mint and Verify.

View Source
var (
	ErrExchangeDenied    = errors.New("apptoken: the identity provider refused the exchange")
	ErrIdPUnreachable    = errors.New("apptoken: the identity provider did not answer")
	ErrExchangeMisconfig = errors.New("apptoken: the token exchange is not set up")
)

Errors of Exchange: the provider refused (no new sign-in on our side mends it), or did not answer.

Functions

func KeyID

func KeyID(pub ed25519.PublicKey) string

KeyID is the key's id in the JWKS: the base64url SHA-256 of the public key, cut to 16 characters. The same key has the same id on every replica of a hub.

func LoadOrCreateKey

func LoadOrCreateKey(path string) (ed25519.PrivateKey, error)

LoadOrCreateKey reads an Ed25519 private key (PKCS#8 PEM) from path, or makes one and writes it there (0600) when the file does not exist. Every replica of a hub must read the same file.

func ParseKey

func ParseKey(raw []byte) (ed25519.PrivateKey, error)

ParseKey reads an Ed25519 private key from PKCS#8 PEM.

func ParsePublicKey

func ParsePublicKey(raw []byte) (ed25519.PublicKey, error)

ParsePublicKey reads an Ed25519 public key (PKIX PEM): a key of an earlier rotation.

Types

type Claims

type Claims struct {
	Issuer   string    `json:"iss"`
	Subject  string    `json:"sub"`
	Audience string    `json:"aud"`
	Email    string    `json:"email,omitempty"`
	Name     string    `json:"name,omitempty"`
	Org      string    `json:"org,omitempty"`
	Space    string    `json:"space,omitempty"`
	App      string    `json:"app"`
	Server   string    `json:"server,omitempty"`
	Machine  string    `json:"machine,omitempty"`
	IssuedAt time.Time `json:"-"`
	Expiry   time.Time `json:"-"`
	ID       string    `json:"-"`
}

Claims are what the token says. Subject is the person's stable id at the issuer; Email and Name are for display and logs, not for access decisions unless the backend trusts the issuer's emails. Org and Space are the person's organization and team on the hub (empty on a machine without one). App and Server name the app and its MCP server the call is for; Audience is the backend the token is good for, and nothing else.

func Verify

func Verify(token string, jwks []byte, issuer, audience string, now time.Time) (Claims, error)

Verify checks a token against a JWKS the way a backend should: the EdDSA signature by a key of the set, the typ, the issuer, the audience (required) and the expiry with a minute of leeway. It is what the Go example of the docs does.

type ExchangeConfig

type ExchangeConfig struct {
	TokenURL     string
	ClientID     string
	ClientSecret string
	// SubjectTokenType is how the provider is told what the subject token is; TokenTypeJWT when empty.
	SubjectTokenType string
}

ExchangeConfig is the hub's client at the corporate provider.

type Exchanged

type Exchanged struct {
	AccessToken     string
	IssuedTokenType string
	ExpiresAt       time.Time
}

Exchanged is the provider's answer.

func Exchange

func Exchange(ctx context.Context, client *http.Client, cfg ExchangeConfig, subject, audience, scope string, now time.Time) (Exchanged, error)

Exchange trades subject (the platform's JWT) for the provider's token for audience. scope may be empty.

type Signer

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

Signer mints the tokens of one issuer. The first key signs; the others are still published so the tokens they signed before a rotation keep verifying until they expire.

func NewSigner

func NewSigner(issuer string, key ed25519.PrivateKey, previous ...ed25519.PublicKey) (*Signer, error)

NewSigner is a signer for issuer with key; previous are public keys of earlier rotations, published but not used.

func (*Signer) Issuer

func (s *Signer) Issuer() string

Issuer is the iss of the signer's tokens.

func (*Signer) JWKS

func (s *Signer) JWKS() []byte

JWKS is the public keys as a JSON Web Key Set: the signing one first.

func (*Signer) Mint

func (s *Signer) Mint(c Claims, ttl time.Duration) (string, Claims, error)

Mint signs c for ttl (TTL when 0, at most MaxTTL). The issuer, issue time, expiry and id are the signer's.

func (*Signer) SetClock

func (s *Signer) SetClock(now func() time.Time)

SetClock replaces the signer's clock (tests).

Jump to

Keyboard shortcuts

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