auth

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrAPIKeysNotFound is returned if no api key file was found.
	ErrAPIKeysNotFound = errors.New("api key file not found")

	// ErrAPIKeysEmpty is returned if the provided api key file does not contain any keys.
	ErrAPIKeysEmpty = errors.New("api key file contains no keys")

	// ErrAPIKeysInvalid is returned when the api key file is malformed.
	ErrAPIKeysInvalid = errors.New("api key file is invalid")
)

Functions

func DefaultAPIKeysPath

func DefaultAPIKeysPath() (string, error)

DefaultAPIKeysPath returns the default path to the API keys file (~/.centian/api_keys.json).

func GenerateAPIKey

func GenerateAPIKey() (string, error)

GenerateAPIKey creates a new API key string with a stable prefix.

func WriteAPIKeyFile

func WriteAPIKeyFile(path string, file *APIKeyFile) error

WriteAPIKeyFile writes API keys to disk using secure permissions.

Types

type APIKeyEntry

type APIKeyEntry struct {
	ID        string   `json:"id"`
	Hash      string   `json:"hash"`
	CreatedAt string   `json:"created_at"`
	Gateways  []string `json:"gateways,omitempty"`
}

APIKeyEntry represents a stored API key hash and metadata.

func NewAPIKeyEntry

func NewAPIKeyEntry(plainKey string) (APIKeyEntry, error)

NewAPIKeyEntry hashes a plaintext API key and returns a stored entry.

func (*APIKeyEntry) AllowsGateway added in v0.0.5

func (e *APIKeyEntry) AllowsGateway(gateway string) bool

AllowsGateway checks whether this key is allowed to access the given gateway.

Backward compatibility: - Empty Gateway lists are treated as allow-all. - "*" is treated as allow-all.

type APIKeyFile

type APIKeyFile struct {
	Keys []APIKeyEntry `json:"keys"`
}

APIKeyFile stores hashed API keys on disk.

func AppendAPIKey

func AppendAPIKey(path string, entry APIKeyEntry) (*APIKeyFile, error)

AppendAPIKey appends an entry to the API key file, creating it if needed.

func ReadAPIKeyFile

func ReadAPIKeyFile(path string) (*APIKeyFile, error)

ReadAPIKeyFile loads API key data from disk without validating contents.

type APIKeyStore

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

APIKeyStore stores API keys loaded from disk for quick validation.

func LoadAPIKeys

func LoadAPIKeys(path string) (*APIKeyStore, error)

LoadAPIKeys loads API keys from a JSON file.

func LoadDefaultAPIKeys

func LoadDefaultAPIKeys() (*APIKeyStore, error)

LoadDefaultAPIKeys loads API keys from the default path.

func (*APIKeyStore) Count

func (s *APIKeyStore) Count() int

Count returns the number of unique API keys in the store.

func (*APIKeyStore) Lookup added in v0.0.4

func (s *APIKeyStore) Lookup(key string) (*APIKeyEntry, bool)

Lookup returns the matching API key entry for the provided plaintext key.

func (*APIKeyStore) Path

func (s *APIKeyStore) Path() string

Path returns the file path the keys were loaded from.

func (*APIKeyStore) Validate

func (s *APIKeyStore) Validate(key string) bool

Validate returns true if the provided API key exists in the store.

Jump to

Keyboard shortcuts

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