restapi

package
v0.138.0 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const KeyHeader = "x-api-key" // Header for API key

Variables

View Source
var (
	ErrInvalidApiKey     = errors.New("invalid api key")
	ErrInvalidAction     = errors.New("invalid action")
	ErrInvalidHTTPMethod = errors.New("invalid http method")
)

Functions

func JSONError added in v0.115.0

func JSONError(w http.ResponseWriter, err, details any, jobId string, code int)

JSONError writes an error response to the client in JSON format.

func JSONResponse added in v0.115.0

func JSONResponse(w http.ResponseWriter, content any, jobId string, code int)

JSONResponse writes a successful response in the common JSON envelope.

func ResolveRequestIP added in v0.115.0

func ResolveRequestIP(remoteAddr, trustedProxyHeader string, headers http.Header, trustedProxyNetworks []netip.Prefix) string

ResolveRequestIP determines the client IP address from the request, considering trusted proxy headers and networks.

Types

type APIKey added in v0.134.0

type APIKey struct {
	Key         string       `yaml:"key" json:"-"`
	Role        Role         `yaml:"role,omitempty"`
	Permissions []Permission `yaml:"permissions,omitempty"`
}

APIKey binds a credential to a role or an explicit set of permissions.

func (APIKey) Grant added in v0.134.0

func (key APIKey) Grant() (Grant, error)

Grant resolves a key's role or explicit permissions without including its secret.

type ErrorResponse added in v0.115.0

type ErrorResponse struct {
	Error string `json:"error"`
	Response
}

ErrorResponse inherits from Response and adds an error message.

type Grant added in v0.134.0

type Grant struct {
	Role Role
	// contains filtered or unexported fields
}

Grant contains the permissions associated with an authenticated API key.

func (Grant) Allows added in v0.134.0

func (grant Grant) Allows(required Permission) bool

Allows reports whether the grant contains the required permission.

func (Grant) Signature added in v0.134.0

func (grant Grant) Signature() string

Signature identifies an effective permission set independently of its role or order.

type Keyring added in v0.134.0

type Keyring []APIKey

Keyring contains the configured API credentials.

func (Keyring) Authenticate added in v0.134.0

func (keys Keyring) Authenticate(r *http.Request) (Grant, bool)

Authenticate returns the grant associated with the request's API key.

func (Keyring) Validate added in v0.134.0

func (keys Keyring) Validate() error

Validate rejects invalid and ambiguous credentials without exposing their values.

type Permission added in v0.134.0

type Permission string

Permission identifies an API capability granted to a key.

const (
	PermissionHealthRead      Permission = "health:read"
	PermissionJobsRead        Permission = "jobs:read"
	PermissionJobsTrigger     Permission = "jobs:trigger"
	PermissionMirrorsCompact  Permission = "mirrors:compact"
	PermissionPollTrigger     Permission = "poll:trigger"
	PermissionProjectsControl Permission = "projects:control"
	PermissionProjectsDelete  Permission = "projects:delete"
	PermissionProjectsInspect Permission = "projects:inspect"
	PermissionRunsRead        Permission = "runs:read"
	PermissionStacksControl   Permission = "stacks:control"
	PermissionStacksDelete    Permission = "stacks:delete"
	PermissionStacksInspect   Permission = "stacks:inspect"
	PermissionSyncWindowsRead Permission = "sync_windows:read"
)

type Response added in v0.115.0

type Response struct {
	Content any    `json:"content,omitempty"`
	JobID   string `json:"job_id,omitempty"`
}

Response is the common JSON envelope for successful REST responses.

type Role added in v0.134.0

type Role string

Role identifies the operations an API key can perform.

const (
	RoleRead Role = "read"
	// RoleInspect also allows project and stack details, which can contain secrets.
	RoleInspect Role = "inspect"
	RoleWrite   Role = "write"
	RoleFull    Role = "full"
)

func MinimumRole added in v0.134.0

func MinimumRole(permission Permission) (Role, bool)

MinimumRole returns the least-privileged preset that grants a permission.

func (Role) Allows added in v0.134.0

func (role Role) Allows(required Permission) bool

Allows reports whether the role grants the required permission.

func (Role) Valid added in v0.134.0

func (role Role) Valid() bool

Valid reports whether the role is a configured role preset.

Jump to

Keyboard shortcuts

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