identity

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2026 License: AGPL-3.0 Imports: 17 Imported by: 0

Documentation

Overview

Package identity owns gateway API-key identity and persistence.

Index

Constants

View Source
const (
	// StorageSchemaVersion identifies the only supported identity schema.
	StorageSchemaVersion = 1
	// StoragePrefix is the identity v1 storage namespace.
	StoragePrefix = "identity:v1:"
)

Variables

View Source
var (
	// ErrMissingID reports an identity without a durable ID.
	ErrMissingID = errors.New("missing id")
	// ErrMissingHash reports an identity without a key hash.
	ErrMissingHash = errors.New("missing hash")
	// ErrMissingScopes reports an identity without any granted scope.
	ErrMissingScopes = errors.New("missing scopes")
	// ErrInvalidName reports an invalid identity name.
	ErrInvalidName = errors.New("invalid name: must be 1-255 characters")
	// ErrInvalidScope reports an empty identity scope.
	ErrInvalidScope = errors.New("invalid scope: must be non-empty")
	// ErrInvalidModel reports an empty allowed-model entry.
	ErrInvalidModel = errors.New("invalid model: must be non-empty")
	// ErrInvalidExpiration reports an expiration before identity creation.
	ErrInvalidExpiration = errors.New("expires_at must be after created_at")
)
View Source
var (
	// ErrRepositoryRequired reports a missing storage adapter.
	ErrRepositoryRequired = errors.New("identity storage is required")
	// ErrNotFound reports a missing identity.
	ErrNotFound = errors.New("identity not found")
	// ErrConflict reports an existing identity or stale revision.
	ErrConflict = errors.New("identity revision conflict")
	// ErrCorruptRecord reports invalid durable identity data.
	ErrCorruptRecord = errors.New("identity record is invalid")
	// ErrHashImmutable reports an attempted hash mutation.
	ErrHashImmutable = errors.New("identity hash is immutable")
)
View Source
var ErrIssuerRequired = errors.New("identity issuer repository is required")

ErrIssuerRequired reports an absent identity repository.

Functions

func ValidateName added in v1.0.1

func ValidateName(name string) error

ValidateName checks the public identity-name contract.

Types

type APIKey

type APIKey struct {
	ID              string         `json:"id"`
	Name            string         `json:"name"`
	Hash            string         `json:"hash"`
	Scopes          []string       `json:"scopes"`
	AllowedModels   []string       `json:"allowed_models,omitempty"`
	RateLimitConfig map[string]any `json:"rate_limit_config,omitempty"`
	Metadata        map[string]any `json:"metadata,omitempty"`
	Active          bool           `json:"active"`
	CreatedAt       time.Time      `json:"created_at"`
	ExpiresAt       *time.Time     `json:"expires_at,omitempty"`
}

APIKey is one gateway authentication identity.

func (APIKey) CanUseModel

func (k APIKey) CanUseModel(model string) bool

CanUseModel reports whether the identity can use a model.

func (APIKey) HasScope

func (k APIKey) HasScope(scope string) bool

HasScope reports whether the identity grants a scope.

func (APIKey) IsExpired

func (k APIKey) IsExpired() bool

IsExpired reports whether the identity is expired now.

func (APIKey) IsExpiredAt

func (k APIKey) IsExpiredAt(now time.Time) bool

IsExpiredAt reports whether the identity expired at the supplied time.

func (APIKey) Validate

func (k APIKey) Validate() error

Validate checks the API-key invariants.

type IssueRequest added in v1.0.1

type IssueRequest struct {
	Name      string
	Scopes    []string
	Metadata  map[string]any
	ExpiresAt *time.Time
}

IssueRequest contains the durable attributes for a new gateway identity.

type IssueResult added in v1.0.1

type IssueResult struct {
	APIKey APIKey
	Secret string
}

IssueResult contains a new identity and its one-time plaintext credential. The repository stores only the credential hash.

type Issuer added in v1.0.1

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

Issuer creates gateway credentials and their durable identity records.

func NewIssuer added in v1.0.1

func NewIssuer(repository Repository) (*Issuer, error)

NewIssuer returns an identity issuer backed by repository.

func (*Issuer) Issue added in v1.0.1

func (i *Issuer) Issue(ctx context.Context, request IssueRequest) (IssueResult, error)

Issue creates one identity and returns its plaintext credential once.

func (*Issuer) IssueInitial added in v1.0.1

func (i *Issuer) IssueInitial(ctx context.Context, request IssueRequest) (IssueResult, error)

IssueInitial atomically creates the first identity in a repository.

type Record

type Record struct {
	Revision uint64
	APIKey   APIKey
}

Record is one versioned identity repository value.

type Repository

type Repository interface {
	Create(context.Context, APIKey) (Record, error)
	CreateInitial(context.Context, APIKey) (Record, error)
	ReleaseInitial(context.Context, string) error
	GetByID(context.Context, string) (Record, error)
	GetByHash(context.Context, string) (Record, error)
	List(context.Context, int) ([]Record, error)
	Update(context.Context, APIKey, uint64) (Record, error)
	Delete(context.Context, string, uint64) error
}

Repository is the durable identity contract.

func Open

func Open(store storage.KVStore) (Repository, error)

Open returns a storage-backed identity repository.

Jump to

Keyboard shortcuts

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