authn

package
v4.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package authn verifies the kernel's user access tokens in one place: the HTTP middleware, the admin monitor WebSocket and the gRPC interceptor all call it. It pins the token algorithm and enforces revocations.

Index

Constants

View Source
const (
	IdentityIssuer   = "anixops-identity"
	IdentityAudience = "anix-control"
)

The kernel accepts identity tokens with this issuer and audience only.

Variables

View Source
var (
	// ErrInvalidToken covers malformed, forged, expired and wrong-algorithm tokens.
	ErrInvalidToken = errors.New("invalid or expired token")
	// ErrRevokedToken is a valid token whose user or session was revoked.
	ErrRevokedToken = errors.New("token has been revoked")
)
View Source
var ErrInvalidRevocation = errors.New("invalid revocation")

ErrInvalidRevocation wraps every reason a revocation is refused.

Functions

func LegacyTokensRefused

func LegacyTokensRefused() bool

LegacyTokensRefused reports whether RefuseLegacyTokens was called.

func RefuseLegacyTokens

func RefuseLegacyTokens()

RefuseLegacyTokens makes the default verifier refuse the kernel's own HS256 tokens from now on. The server calls it once identity is finalized: only identity then issues tokens.

func Remember

func Remember(r Revocation)

Remember applies a committed revocation to the default store, if any.

func SetDefaultIdentityKeys

func SetDefaultIdentityKeys(keys IdentityKeys)

SetDefaultIdentityKeys installs the identity keys the default verifier uses; the server sets them at start.

func SetDefaultStore

func SetDefaultStore(store *Store)

SetDefaultStore installs the revocation store the default verifier and Remember use; the server sets it at start.

func Write

func Write(db *gorm.DB, r Revocation) error

Write records a revocation with db, which may be a transaction, so that the change that caused it and the revocation commit together. Bounds only move forward. Call Store.Remember after the commit to apply it at once.

Types

type IdentityKeys

type IdentityKeys interface {
	KeySet() identitytoken.KeySet
	// Kick asks for a refresh: a token named a key the kernel does not know.
	Kick()
}

IdentityKeys supplies the identity module's token keys.

func DefaultIdentityKeys

func DefaultIdentityKeys() IdentityKeys

DefaultIdentityKeys returns the installed identity keys, or nil.

type Revocation

type Revocation struct {
	UserID           uint
	TokenVersion     uint64
	NotBefore        time.Time
	SessionID        string
	SessionExpiresAt time.Time
	Reason           string
}

Revocation makes tokens stop working. It mirrors KernelIdentity.PublishRevocation:

  • UserID with NotBefore revokes that user's kernel tokens (no tv claim) issued up to NotBefore;
  • with TokenVersion as well, the user's identity tokens (tv claim) stop working when their tv is below TokenVersion, so a token identity issues afterwards works even within the same second; without it, identity tokens issued up to NotBefore stop working too;
  • SessionID revokes one session until SessionExpiresAt.

func UserRevocation

func UserRevocation(userID uint, reason string) Revocation

UserRevocation revokes every token of a user issued before now.

type Store

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

Store answers revocation checks from memory. It loads the tables at start, applies revocations this process makes at once (Remember), and reloads periodically to pick up revocations written elsewhere.

func DefaultStore

func DefaultStore() *Store

DefaultStore returns the installed revocation store, or nil.

func NewStore

func NewStore(db *gorm.DB, maxTokenLifetime time.Duration) *Store

NewStore returns a store over db. maxTokenLifetime is the longest token lifetime the kernel accepts (jwt.expire plus leeway).

func (*Store) Publish

func (s *Store) Publish(ctx context.Context, r Revocation) error

Publish writes a revocation and applies it at once.

func (*Store) Reload

func (s *Store) Reload(ctx context.Context) error

Reload replaces the cache with the tables, after deleting rows that no longer revoke any unexpired token.

func (*Store) Remember

func (s *Store) Remember(r Revocation)

Remember applies a revocation that was written and committed.

func (*Store) Revoked

func (s *Store) Revoked(claims *utils.Claims) bool

Revoked reports whether claims belong to a revoked token.

func (*Store) Run

func (s *Store) Run(ctx context.Context, interval time.Duration, report func(error))

Run reloads every interval until ctx ends; errors go to report.

type Verifier

type Verifier struct {
	// Secret returns the current HS256 secret.
	Secret func() string
	// Revocations may be nil, which disables revocation checks (tools and
	// tests without a database).
	Revocations *Store
	// IdentityKeys may be nil: identity tokens are then refused.
	IdentityKeys IdentityKeys
}

Verifier checks the kernel's own HS256 tokens (signed with the configured secret) and the identity module's EdDSA tokens (signed with a published identity key), then the revocation store.

func Default

func Default() *Verifier

Default returns a verifier over the loaded configuration's secret and the default revocation store. It has no secret once legacy tokens are refused.

func (*Verifier) Verify

func (v *Verifier) Verify(_ context.Context, token string) (*utils.Claims, error)

Verify returns the claims of a valid, unrevoked token.

Jump to

Keyboard shortcuts

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