jwt

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidToken = errors.New("jwt: invalid token")

ErrInvalidToken is returned for all token validation failures. The specific reason is logged server-side but never exposed to callers.

Functions

This section is empty.

Types

type Claims

type Claims struct {
	JTI       string         `json:"jti,omitempty"`
	Subject   string         `json:"sub,omitempty"`
	Issuer    string         `json:"iss,omitempty"`
	ExpiresAt int64          `json:"exp,omitempty"`
	IssuedAt  int64          `json:"iat,omitempty"`
	Role      string         `json:"role,omitempty"`
	Extra     map[string]any `json:"-"`
}

Claims represents standard + custom JWT claims.

type Driver

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

Driver implements JWT-based authentication using HMAC signing (HS256/HS384/HS512). All crypto uses Go's stdlib — no third-party JWT library. Tokens are tracked in a jwt_tokens table for revocation support.

func NewDriver

func NewDriver(env func(string, string) string, db *sql.DB) *Driver

NewDriver creates a JWT auth driver. Config is read from environment:

  • JWT_SECRET: HMAC signing key (required)
  • JWT_ISSUER: expected issuer claim (optional)
  • JWT_EXPIRY: token lifetime in seconds (default: 3600)
  • JWT_ALGORITHM: HS256, HS384, or HS512 (default: HS256)

func (*Driver) Authenticate

func (d *Driver) Authenticate(r *http.Request) (*pickle.AuthInfo, error)

Authenticate extracts the Bearer token from the request, validates it, and returns AuthInfo on success.

func (*Driver) RevokeAllForUser

func (d *Driver) RevokeAllForUser(userID string) error

RevokeAllForUser revokes all tokens for the given user ID.

func (*Driver) RevokeToken

func (d *Driver) RevokeToken(jti string) error

RevokeToken revokes a single token by JTI.

func (*Driver) SignToken

func (d *Driver) SignToken(claims Claims) (string, error)

SignToken creates a signed JWT from the given claims and registers it in the jwt_tokens table for revocation tracking. The token is not valid unless it exists in the table.

func (*Driver) ValidateToken

func (d *Driver) ValidateToken(tokenStr string) (*pickle.AuthInfo, error)

ValidateToken parses and validates a JWT string, returning AuthInfo on success.

Jump to

Keyboard shortcuts

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