ws_auth

package
v1.7.6 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidToken = errors.New("invalid token")
	ErrTokenReused  = errors.New("refresh token already used")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	AccessTTL         time.Duration // WS_ACCESS_TOKEN_TTL_MINUTES, default 5
	RefreshTTL        time.Duration // WS_REFRESH_TOKEN_TTL_MINUTES, default 30
	RenewGrace        time.Duration // WS_RENEW_GRACE_SECONDS, default 30
	MaxConcurrent     int64         // WS_UPLOAD_MAX_CONCURRENT, default 3 (per user)
	CounterTTL        time.Duration // WS_UPLOAD_COUNTER_TTL_MINUTES, default 120 (crash safety)
	TokenRatePerMin   int64         // WS_TOKEN_RATE_LIMIT_PER_MIN, default 10
	AccessSecret      string        // WS_JWT_ACCESS_SECRET
	RefreshSecret     string        // WS_JWT_REFRESH_SECRET
	SignMethod        string        // JWT_HMAC_HASH
	EncryptionKey     string        // WS_TOKEN_ENCRYPTION_KEY
	EncryptionKeyPrev string        // WS_TOKEN_ENCRYPTION_KEY_PREVIOUS (decrypt only, for key rotation)
}

Config holds the WS upload auth settings, all read from env.

func ConfigFromEnv

func ConfigFromEnv() Config

type Service

type Service interface {
	Config() Config

	// Issue creates a new token pair (new family) for the user.
	Issue(ctx context.Context, req *http.Request, user token_generator.UserDetails) (*TokenPair, error)
	// Authenticate decrypts and validates an access token against JWT and the live Redis record.
	Authenticate(ctx context.Context, encryptedAccess string) (*Session, time.Time, error)
	// Renew rotates the pair using an encrypted refresh token, with reuse detection.
	Renew(ctx context.Context, req *http.Request, encryptedRefresh string) (*TokenPair, *Session, error)
	// SessionAlive reports whether the family has not been revoked or expired.
	SessionAlive(ctx context.Context, familyID string) bool

	// AllowTokenRequest is a fixed window (1 minute) limiter for token issue/refresh.
	AllowTokenRequest(ctx context.Context, key string) (bool, error)
	// AcquireUpload takes one concurrent-upload slot for the user. ok is false when the cap is reached.
	// release is safe to call more than once.
	AcquireUpload(ctx context.Context, userID string) (release func(), ok bool, err error)
}

func NewService

func NewService(redisClient redis.UniversalClient, cfg Config) Service

NewService panics on missing secrets/keys so a misconfigured deploy fails at startup.

type Session

type Session struct {
	Uuid     string    `json:"uuid"`
	FamilyID string    `json:"family_id"`
	UserID   string    `json:"user_id"`
	Username string    `json:"username"`
	IssuedAt time.Time `json:"issued_at"`
}

Session is the value stored in Redis for a WS token pair. FamilyID is the uuid of the first pair of a rotation chain and stays the same across renewals.

type TokenPair

type TokenPair struct {
	AccessToken      string    `json:"access_token"`
	RefreshToken     string    `json:"refresh_token"`
	AccessExpiredAt  time.Time `json:"access_expired_at"`
	RefreshExpiredAt time.Time `json:"refresh_expired_at"`
}

Jump to

Keyboard shortcuts

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