auth

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2026 License: AGPL-3.0, AGPL-3.0-or-later Imports: 7 Imported by: 0

Documentation

Overview

Package auth provides authentication and pairing for Hub connections.

Index

Constants

View Source
const (
	// CodeLength is the number of digits in a pairing code.
	CodeLength = 6
	// CodeExpiry is how long a pairing code is valid.
	CodeExpiry = 60 * time.Second
	// TokenLength is the number of random bytes for tokens.
	TokenLength = 32
	// MaxFailedAttempts before rate limiting.
	MaxFailedAttempts = 3
	// RateLimitDuration after max failed attempts.
	RateLimitDuration = 5 * time.Minute
)

Variables

View Source
var (
	ErrCodeExpired      = errors.New("pairing code expired")
	ErrCodeInvalid      = errors.New("invalid pairing code")
	ErrRateLimited      = errors.New("too many failed attempts, try again later")
	ErrHubNotFound      = errors.New("hub not found")
	ErrTokenInvalid     = errors.New("invalid token")
	ErrNoPendingPairing = errors.New("no pending pairing")
)

Errors returned by auth operations.

Functions

This section is empty.

Types

type AuthorizedHub

type AuthorizedHub struct {
	ID       string    `json:"id"`
	Name     string    `json:"name"`
	Platform string    `json:"platform,omitempty"`
	Token    string    `json:"token"`
	PairedAt time.Time `json:"pairedAt"`
	LastSeen time.Time `json:"lastSeen"`
}

AuthorizedHub represents a Hub that has been paired with this Agent.

type ConfigStorageAdapter

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

ConfigStorageAdapter adapts config.Manager to implement auth.Storage.

func NewConfigStorage

func NewConfigStorage(cfg *config.Manager) *ConfigStorageAdapter

NewConfigStorage creates a new storage adapter from a config manager.

func (*ConfigStorageAdapter) AddAuthorizedHub

func (s *ConfigStorageAdapter) AddAuthorizedHub(hub AuthorizedHub) error

AddAuthorizedHub adds a Hub to the authorized list.

func (*ConfigStorageAdapter) GetAuthorizedHubs

func (s *ConfigStorageAdapter) GetAuthorizedHubs() []AuthorizedHub

GetAuthorizedHubs returns the list of authorized Hubs.

func (*ConfigStorageAdapter) RemoveAuthorizedHub

func (s *ConfigStorageAdapter) RemoveAuthorizedHub(hubID string) error

RemoveAuthorizedHub removes a Hub from the authorized list.

func (*ConfigStorageAdapter) Save

func (s *ConfigStorageAdapter) Save() error

Save persists the storage (no-op, config saves on each change).

func (*ConfigStorageAdapter) UpdateHubLastSeen

func (s *ConfigStorageAdapter) UpdateHubLastSeen(hubID string, lastSeen time.Time) error

UpdateHubLastSeen updates the LastSeen timestamp for a Hub.

type Manager

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

Manager handles authentication and pairing operations.

func NewManager

func NewManager(storage Storage) *Manager

NewManager creates a new auth Manager.

func (*Manager) CancelPendingPairing

func (m *Manager) CancelPendingPairing()

CancelPendingPairing cancels any pending pairing session.

func (*Manager) GenerateCode

func (m *Manager) GenerateCode(hubID, hubName, hubPlatform string) (string, error)

GenerateCode creates a new 6-digit pairing code for a Hub.

func (*Manager) GetAuthorizedHubs

func (m *Manager) GetAuthorizedHubs() []AuthorizedHub

GetAuthorizedHubs returns the list of authorized Hubs.

func (*Manager) GetPendingPairing

func (m *Manager) GetPendingPairing() *PairingSession

GetPendingPairing returns the current pending pairing session, if any.

func (*Manager) IsHubAuthorized

func (m *Manager) IsHubAuthorized(hubID string) bool

IsHubAuthorized checks if a Hub is in the authorized list (by ID only).

func (*Manager) RevokeHub

func (m *Manager) RevokeHub(hubID string) error

RevokeHub removes a Hub from the authorized list.

func (*Manager) SetPairingCodeCallback

func (m *Manager) SetPairingCodeCallback(cb func(code string, expiresIn time.Duration))

SetPairingCodeCallback sets the callback for when a pairing code is generated.

func (*Manager) ValidateCode

func (m *Manager) ValidateCode(hubID, hubName, code string) (string, error)

ValidateCode checks if the provided code matches the pending pairing. Returns the generated token on success.

func (*Manager) ValidateToken

func (m *Manager) ValidateToken(hubID, token string) bool

ValidateToken checks if a Hub's token is valid.

type PairingSession

type PairingSession struct {
	Code        string
	HubID       string
	HubName     string
	HubPlatform string
	ExpiresAt   time.Time
}

PairingSession holds the state of an active pairing attempt.

type Storage

type Storage interface {
	GetAuthorizedHubs() []AuthorizedHub
	AddAuthorizedHub(hub AuthorizedHub) error
	RemoveAuthorizedHub(hubID string) error
	UpdateHubLastSeen(hubID string, lastSeen time.Time) error
	Save() error
}

Storage defines the interface for persisting authorized hubs.

Jump to

Keyboard shortcuts

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