Help us shape the future of Go! Take the 2026 Go Developer Survey

auth

package
v2.0.1 Latest Latest
Warning

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

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

Documentation

Overview

Package auth manages API tokens shared by every protocol: REST requests (Authorization: Bearer / X-API-Token / ?token=), and RESP/TCP clients (AUTH <token>).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromRequest

func FromRequest(r *http.Request) string

FromRequest extracts a token from an HTTP request: Authorization header ("Bearer x", "Token x" or the bare token), X-API-Token header or the token query parameter (needed for browser WebSockets).

func Mask

func Mask(t string) string

Mask hides most of a token: "dev-token-123" -> "dev-…123".

Types

type Manager

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

Manager stores tokens. It is safe for concurrent use.

func NewManager

func NewManager(enabled bool, tokens []string) *Manager

NewManager creates a manager. When enabled is false every check succeeds.

func (*Manager) Check

func (m *Manager) Check(candidate string) bool

Check validates a token using constant-time comparison and records usage.

func (*Manager) Enabled

func (m *Manager) Enabled() bool

Enabled reports whether authentication is required.

func (*Manager) Generate

func (m *Manager) Generate() string

Generate creates, stores and returns a new random token. Generated tokens live in memory only and disappear on restart.

func (*Manager) List

func (m *Manager) List() []TokenInfo

List returns token metadata sorted by creation time.

func (*Manager) Revoke

func (m *Manager) Revoke(idOrToken string) (removed bool, fromConfig bool)

Revoke removes a token identified by its value or its masked ID. Tokens from the config file can't be revoked at runtime.

type TokenInfo

type TokenInfo struct {
	ID         string    `json:"id"` // masked token, safe to display
	CreatedAt  time.Time `json:"created_at"`
	LastUsed   time.Time `json:"last_used,omitzero"`
	UsageCount int64     `json:"usage_count"`
	FromConfig bool      `json:"from_config"`
}

TokenInfo describes a token without exposing it.

Jump to

Keyboard shortcuts

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