server

package
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BearerAuth

func BearerAuth(token string, keyStore *KeyStore) func(http.Handler) http.Handler

BearerAuth returns middleware that validates Bearer token authentication. It checks the env-var token first (backwards compatible), then falls back to the KeyStore if provided. If both token and keyStore are empty/nil, the middleware is a no-op (passes through).

Types

type APIKey

type APIKey struct {
	Name      string   `yaml:"name"`
	Hash      string   `yaml:"hash"`       // SHA-256 hex of the plaintext key
	Projects  []string `yaml:"projects"`   // empty = all projects
	CreatedAt string   `yaml:"created_at"` // RFC3339
}

APIKey represents a stored API key with metadata.

type APIRegistrar

type APIRegistrar interface {
	RegisterRoutes(mux *http.ServeMux)
}

APIRegistrar can register REST API routes on an http.ServeMux.

type Config

type Config struct {
	Addr       string       // listen address, e.g. ":8080"
	AuthToken  string       //nolint:gosec // G117: config field, not a hardcoded secret
	KeyStore   *KeyStore    // YAML-backed API key store (may be nil)
	MCPHandler http.Handler // optional MCP protocol handler; nil keeps the 501 placeholder
	APIHandler APIRegistrar // optional REST API handler; nil keeps the 501 placeholder
}

Config holds server configuration.

type KeyStore

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

KeyStore manages API keys stored in a YAML file.

func NewKeyStore

func NewKeyStore(path string) (*KeyStore, error)

NewKeyStore loads a KeyStore from the given YAML file path. If the file does not exist, it is created with an empty key list.

func (*KeyStore) Create

func (ks *KeyStore) Create(name string, projects []string) (string, error)

Create generates a new API key, stores its hash, and returns the plaintext. The plaintext is only available at creation time.

func (*KeyStore) List

func (ks *KeyStore) List() []APIKey

List returns all stored keys. Hashes are included for display purposes.

func (*KeyStore) Revoke

func (ks *KeyStore) Revoke(name string) error

Revoke removes a key by name.

func (*KeyStore) Validate

func (ks *KeyStore) Validate(token string) (*APIKey, bool)

Validate checks a plaintext token against all stored key hashes. Returns the matching APIKey and true if found, or nil and false otherwise. Uses constant-time comparison to prevent timing attacks.

func (*KeyStore) ValidateForProject

func (ks *KeyStore) ValidateForProject(token, project string) bool

ValidateForProject checks a plaintext token and verifies it has access to the given project. Unscoped keys (empty Projects) have access to all projects.

type Server

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

Server is the main HTTP server.

func New

func New(cfg Config) *Server

New creates a new Server with routes registered.

func (*Server) Addr

func (s *Server) Addr() string

Addr returns the address the server is listening on. This is useful in tests when Addr is ":0" (random port). Safe for concurrent use.

func (*Server) Handler

func (s *Server) Handler() http.Handler

Handler returns the underlying http.Handler for use in httptest.NewServer.

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context) error

Shutdown gracefully stops the server.

func (*Server) Start

func (s *Server) Start(ctx context.Context) error

Start begins listening. It blocks until ctx is cancelled or an error occurs. When ctx is cancelled, Start calls Shutdown and returns nil.

Jump to

Keyboard shortcuts

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