jwt

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package jwt provides authit's token signing/verification, kept deliberately free of any notion of users, sessions, or storage — it knows only how to turn claims into a signed string and back.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidSecret = errors.New("authit/jwt: secret must be at least 32 bytes")

ErrInvalidSecret is returned by NewHMACSigner when the secret is too short to be a safe HMAC-SHA256 key.

Functions

This section is empty.

Types

type Claims

type Claims struct {
	jwt.RegisteredClaims
	Email string `json:"email,omitempty"`
	// ActorID, when set, means this token was minted by an operator
	// impersonating this Subject rather than the user logging in directly
	// (see the superuser package's Impersonate). It carries no special
	// privilege on its own — it exists purely so downstream audit/log code
	// can record who was really acting.
	ActorID string `json:"actor_id,omitempty"`
}

Claims is the default access-token claim set for regular users. Subject carries the user ID. Host applications that need extra fields (e.g. a team ID) can define their own struct embedding jwtlib.RegisteredClaims and sign it directly through Signer.Sign/Verify instead of using this type.

func (Claims) IsImpersonation

func (c Claims) IsImpersonation() bool

IsImpersonation reports whether these claims were minted by an operator impersonating the subject rather than by the subject logging in themselves.

type Defaults

type Defaults struct {
	Issuer string
	TTL    time.Duration
}

Defaults are applied by Signer.Generate/Validate (the Claims-typed convenience methods); callers using Sign/Verify with a custom claims type apply them explicitly.

type HMACSigner

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

HMACSigner is a Signer backed by HMAC-SHA256 with a shared secret.

func NewHMACSigner

func NewHMACSigner(secret []byte, defaults Defaults) (*HMACSigner, error)

NewHMACSigner constructs an HMACSigner. secret must be at least 32 bytes.

func (*HMACSigner) Defaults

func (s *HMACSigner) Defaults() Defaults

func (*HMACSigner) Generate

func (s *HMACSigner) Generate(claims Claims) (string, error)

Generate signs claims after applying issuer/TTL defaults for any zero fields.

func (*HMACSigner) Sign

func (s *HMACSigner) Sign(claims jwt.Claims) (string, error)

Sign signs an arbitrary claims value.

func (*HMACSigner) Validate

func (s *HMACSigner) Validate(token string) (Claims, error)

Validate verifies token as Claims.

func (*HMACSigner) Verify

func (s *HMACSigner) Verify(token string, dst jwt.Claims) error

Verify parses and validates token, populating dst on success. dst must be a pointer to a jwt.Claims-satisfying type.

type Signer

type Signer interface {
	Sign(claims jwt.Claims) (string, error)
	Verify(token string, dst jwt.Claims) error
	Generate(claims Claims) (string, error)
	Validate(token string) (Claims, error)
	Defaults() Defaults
}

Signer signs and verifies JWTs. The generic Sign/Verify methods work with any jwt.Claims-satisfying type, so a host application can define its own claims struct (e.g. embedding a team ID) without authit needing to know about it. Generate/Validate are a convenience pair fixed to Claims.

Jump to

Keyboard shortcuts

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