auth

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrExpiredToken = errors.New("token has expired")
	ErrInvalidToken = errors.New("token is invalid")
	ErrTokenRevoked = errors.New("token has been revoked")
)

Functions

func IsAny

func IsAny(err error) bool

IsAny checks if a catch error in slices of errors with typically errors for Token Some errors from this slice: ErrExpiredToken, ErrInvalidToken, ErrTokenRevoked

Types

type BlacklistedTokenManager

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

BlacklistedTokenManager is a decorator around TokenManager that checks token revocation status on every VerifyToken call via a TokenBlacklist.

func NewBlacklistedTokenManager

func NewBlacklistedTokenManager(inner TokenManager, blacklist TokenBlacklist) *BlacklistedTokenManager

NewBlacklistedTokenManager wraps an existing TokenManager with blacklist verification.

func (*BlacklistedTokenManager) CreateRefreshToken

func (m *BlacklistedTokenManager) CreateRefreshToken(username, uid, role string) (string, *Payload, error)

func (*BlacklistedTokenManager) CreateToken

func (m *BlacklistedTokenManager) CreateToken(username, uid, role string) (string, error)

func (*BlacklistedTokenManager) VerifyToken

func (m *BlacklistedTokenManager) VerifyToken(token string) (*Payload, error)

VerifyToken delegates to the inner TokenManager and then checks the blacklist. Requires context — uses context.Background() as fallback since the interface is context-free.

type JWTMaker

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

JWTMaker implements TokenManager using HMAC-SHA256 JWT tokens.

func NewJWTMaker

func NewJWTMaker(secret string, ttl time.Duration) (*JWTMaker, error)

NewJWTMaker creates a new JWTMaker.

func (*JWTMaker) CreateRefreshToken

func (m *JWTMaker) CreateRefreshToken(username, uid, role string) (string, *Payload, error)

func (*JWTMaker) CreateToken

func (m *JWTMaker) CreateToken(username, uid, role string) (string, error)

func (*JWTMaker) VerifyToken

func (m *JWTMaker) VerifyToken(tokenStr string) (*Payload, error)

type PasetoMaker

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

PasetoMaker implements TokenManager using PASETO v4 local tokens.

func NewPasetoMaker

func NewPasetoMaker(keyHex string, ttl time.Duration) (*PasetoMaker, error)

NewPasetoMaker creates a new PasetoMaker. keyHex must be a 32-byte hex-encoded string.

func (*PasetoMaker) CreateRefreshToken

func (m *PasetoMaker) CreateRefreshToken(username, uid, role string) (string, *Payload, error)

func (*PasetoMaker) CreateToken

func (m *PasetoMaker) CreateToken(username, uid, role string) (string, error)

func (*PasetoMaker) VerifyToken

func (m *PasetoMaker) VerifyToken(tokenStr string) (*Payload, error)

type Payload

type Payload struct {
	JTI       string    `json:"jti"`
	Username  string    `json:"username"`
	UID       string    `json:"uid"`
	Role      string    `json:"role"`
	IssuedAt  time.Time `json:"issued_at"`
	ExpiredAt time.Time `json:"expired_at"`
}

Payload holds the claims embedded in a token.

func (*Payload) Valid

func (p *Payload) Valid() error

Valid checks whether the token payload has not expired.

type TokenBlacklist

type TokenBlacklist interface {
	// Blacklist adds a token's JTI to the blacklist until expiresAt.
	Blacklist(ctx context.Context, jti string, expiresAt time.Time) error
	// IsBlacklisted checks whether a token's JTI has been revoked.
	IsBlacklisted(ctx context.Context, jti string) (bool, error)
}

TokenBlacklist defines the interface for token revocation storage. Implementations store JTI (JWT ID) with TTL matching the token's expiration.

type TokenManager

type TokenManager interface {
	CreateToken(username, uid, role string) (string, error)
	CreateRefreshToken(username, uid, role string) (string, *Payload, error)
	VerifyToken(token string) (*Payload, error)
}

TokenManager defines the interface for token creation and verification.

Jump to

Keyboard shortcuts

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