token

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: MIT Imports: 11 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

Overview

Package token is the token group of local commands: osapi controller token generate and validate, which sign and read the bearer tokens of the API with the signing key of the controller's config. They run in the CLI, and call no API.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoRole = errors.New("no such role")

ErrNoRole is a role that neither the built-in roles nor the config have.

View Source
var ErrNoSigningKey = errors.New("no signing key in controller.api.security.signing_keys")

ErrNoSigningKey is a config without a signing key.

View Source
var Group = command.Register(command.Define(
	"token", "Generate and validate API tokens",
	command.Uses[Deps](),
	command.Under(command.ParentController),
	command.Method[GenerateInput, GenerateOutput]("generate").
		Short("Generate a new token").
		Example("--subject user123 --roles=read").
		Prints(sampleGenerate).
		Example("--subject svc@example.com --roles=read \\\n    -p status:read -p health:read").
		Example("--subject team-web --roles=operator \\\n    -t group:web -t web-01").
		Fails("no roles", GenerateInput{Subject: "ops"}).
		Fails("no subject", GenerateInput{Roles: []types.Role{types.RoleRead}}).
		Fails("an unknown permission", GenerateInput{
			Roles: []types.Role{types.RoleRead}, Subject: "ops", Permissions: []string{"root:all"},
		}).
		Fails("a target scope of _all", GenerateInput{
			Roles: []types.Role{types.RoleRead}, Subject: "ops", Targets: []string{types.TargetAll},
		}).
		Handle(Generate),
	command.Method[ValidateInput, Claims]("validate").
		Short("Validate a token for authenticity and claims").
		Example("--token -").
		Prints(sampleClaims).
		Fails("no token", ValidateInput{}).
		Handle(Validate),
))

Group is the token group's definition. The commands, their flags, their help and their pages come from it. osapi dsl command spec get lists every option.

Functions

This section is empty.

Types

type Claims

type Claims struct {
	// KeyID is the kid of the token: the ID of the key that signed it.
	KeyID       string       `json:"kid"                   label:"Key ID"`
	Subject     string       `json:"subject"`
	Roles       []types.Role `json:"roles"`
	Permissions []string     `json:"permissions,omitempty"`
	// Targets are the hosts the token may act on; none is each host.
	Targets []string `json:"targets,omitempty"`
	// EffectivePermissions are what the roles and the permissions grant,
	// sorted.
	EffectivePermissions []string  `json:"effective_permissions"`
	Audience             []string  `json:"audience"`
	IssuedAt             time.Time `json:"issued_at,omitzero"    render:"time"`
	ExpiresAt            time.Time `json:"expires_at,omitzero"   render:"time"`
}

Claims are what a valid token carries.

func Validate

func Validate(
	ctx context.Context,
	d Deps,
	in ValidateInput,
) (Claims, error)

Validate checks a token against the key of the config that its kid names, and is its claims and its kid.

type Config

type Config interface {
	// SigningKeys are controller.api.security.signing_keys. The first
	// signs. A key that is a reference, secret://<vault>/<key>, is
	// resolved from the vaults of the config.
	SigningKeys(ctx context.Context) (authtoken.Keys, error)
	// CustomRoles are the roles of controller.api.security.roles, each with
	// its permissions. Without a config file there are none.
	CustomRoles() (authtoken.CustomRoles, error)
}

Config is what the methods read from the osapi config.

type Deps

type Deps struct {
	// Tokens signs and checks tokens.
	Tokens Tokens
	// Config is what the methods read from the osapi config.
	Config Config
	// Audit writes an entry to the audit log of the controller, or is nil.
	Audit func(ctx context.Context, e audit.Entry) error
}

Deps is what the token group's methods use. The CLI gives it with command.Provide in cmd/commands.go; a test gives fakes.

type GenerateInput

type GenerateInput struct {
	Roles       []types.Role `` /* 135-byte string literal not displayed */
	Subject     string       `` /* 132-byte string literal not displayed */
	Permissions []string     `` /* 173-byte string literal not displayed */
	Targets     []string     `` /* 164-byte string literal not displayed */
}

GenerateInput is what a new token carries.

type GenerateOutput

type GenerateOutput struct {
	Token string `json:"token"`
	// KeyID is the kid of the token: the ID of the key that signed it.
	KeyID       string       `json:"kid"                   label:"Key ID"`
	Subject     string       `json:"subject"`
	Roles       []types.Role `json:"roles"`
	Permissions []string     `json:"permissions,omitempty"`
	Targets     []string     `json:"targets,omitempty"`
}

GenerateOutput is a new token, with what it carries.

func Generate

func Generate(
	ctx context.Context,
	d Deps,
	in GenerateInput,
) (GenerateOutput, error)

Generate signs a new token with the first signing key of the config, and sets its kid.

type Tokens

type Tokens interface {
	Generate(
		key authtoken.Key,
		roles []types.Role,
		subject string,
		permissions []string,
		targets []string,
	) (string, error)
	Validate(
		tokenString string,
		keys authtoken.Keys,
	) (*authtoken.CustomClaims, error)
}

Tokens signs and checks tokens. *authtoken.Token is one.

type ValidateInput

type ValidateInput struct {
	Token secret.Value `json:"token" short:"t" validate:"required" doc:"The token"`
}

ValidateInput is the token to check.

Directories

Path Synopsis
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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