jwt

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package jwt provides explicit, stateless HS256 access-token primitives.

Index

Constants

View Source
const (
	MinimumSecretBytes = 32
	DefaultMaxTTL      = 24 * time.Hour
	DefaultClockSkew   = time.Duration(0)
)

Variables

View Source
var (
	ErrInvalidToken = errors.New("tango jwt: invalid token")
	ErrExpiredToken = errors.New("tango jwt: expired token")
	ErrMissingToken = errors.New("tango jwt: missing token")
)

Functions

func BearerToken

func BearerToken(request *http.Request) (string, error)

BearerToken reads an Authorization: Bearer token header.

func Require

func Require(next tango.View) tango.View

Require rejects requests without Claims installed by Service.Middleware. If no JWT middleware runs before this View wrapper, it always returns 401.

Types

type Claims

type Claims struct {
	Subject   string
	Issuer    string
	Audience  string
	IssuedAt  time.Time
	ExpiresAt time.Time
}

Claims is tanGO's fixed, registered-claims-only JWT payload.

func FromContext

func FromContext(ctx context.Context) (Claims, bool)

FromContext returns the JWT Claims installed by Service.Middleware.

type Extractor

type Extractor func(*http.Request) (string, error)

Extractor obtains a JWT string from one request.

func QueryToken

func QueryToken(name string) Extractor

QueryToken returns an Extractor that reads a named query parameter.

type Key

type Key struct {
	ID     string
	Secret []byte
}

Key is one host-owned HS256 signing key identified by kid.

type Service

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

Service issues and verifies tokens for one issuer and audience. Its key set is immutable after construction.

func NewService

func NewService(active Key, verificationKeys []Key, issuer string, audience string, opts ...ServiceOption) (*Service, error)

NewService constructs an HS256 service. active signs new tokens and is automatically included alongside verificationKeys during verification.

func (*Service) Issue

func (s *Service) Issue(subject string, ttl time.Duration) (string, error)

Issue signs a token for subject with the active key.

func (*Service) Middleware

func (s *Service) Middleware(extract Extractor) tango.Middleware

Middleware verifies an extracted token once per request and stores Claims on the standard request context. Missing tokens pass through for optional authentication; supplied invalid tokens receive a generic JSON 401.

func (*Service) Verify

func (s *Service) Verify(encoded string) (Claims, error)

Verify validates token's algorithm, key, signature, issuer, audience and timestamps. It never reads a database.

type ServiceOption

type ServiceOption func(*serviceConfig)

ServiceOption configures a Service at construction.

func WithClockSkew

func WithClockSkew(skew time.Duration) ServiceOption

WithClockSkew permits the configured amount of clock drift during time validation. Values above five minutes are rejected by NewService.

func WithMaxTTL

func WithMaxTTL(ttl time.Duration) ServiceOption

WithMaxTTL changes the maximum lifetime Issue accepts.

Jump to

Keyboard shortcuts

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