authtoken

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: 14 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 authtoken provides JWT token generation and validation.

Index

Constants

View Source
const GoFile = "pkg/sdk/types/permissions.gen.go"

GoFile is where the SDK keeps the permission constants that Go writes, from the root of the repository.

View Source
const PermissionRule = "permission"

PermissionRule is the validate rule of a permission: one that osapi knows, such as the permissions of a token or of a custom role.

View Source
const TargetScopeRule = "target_scope"

TargetScopeRule is the validate rule of an entry of a token's target scope: a hostname, such as web-01, or a label, such as group:web.

Variables

View Source
var ErrNoKeyID = errors.New("token has no kid")

ErrNoKeyID is a token without a kid header.

View Source
var ErrUnknownKeyID = errors.New("unknown kid")

ErrUnknownKeyID is a token whose kid names no key of the config.

Functions

func AdminOnly added in v0.3.0

func AdminOnly(
	p Permission,
) bool

AdminOnly reports whether only the admin role grants p: its verb is shell or exec, or a method that needs it is marked AdminOnly.

func Check added in v0.3.0

func Check() []error

Check is each problem with the declared permissions: a method that names a permission nothing declares, which no role grants, so each call gets 403; and a permission that one method marks AdminOnly and another does not.

func ConstName added in v0.3.0

func ConstName(
	p Permission,
) string

ConstName is the name of the SDK's constant for permission p: Perm, then the resource and the verb, each with a capital, such as PermDiskRead for disk:read.

func Go added in v0.3.0

func Go() string

Go is the SDK's constant for each permission, from AllPermissions: a behavior's permission has one, as an endpoint group's does. The built-in roles are written by hand in pkg/sdk/types. go generate ./internal/behaviors/all writes it to GoFile, and a test fails when the file is stale.

func Granted added in v0.3.0

func Granted(
	ctx context.Context,
	p Permission,
) bool

Granted reports whether the token of ctx has the permission p. A ctx with no token, as on a public route, has no permission.

func HasPermission

func HasPermission(
	resolved map[Permission]bool,
	required Permission,
) bool

HasPermission checks whether the resolved set contains the required permission.

func InScope added in v0.3.0

func InScope(
	ctx context.Context,
	target string,
) bool

InScope reports whether the token of ctx may send work to target.

func OperationPermissions added in v0.3.0

func OperationPermissions(
	spec []byte,
) (map[string]Permission, error)

OperationPermissions is the permission of each operation of spec, such as the full spec, by its operationId. An operation with no scope, such as a health probe, needs no permission and is not in it. A test holds Operations to the full spec with it.

func Operations added in v0.3.0

func Operations() map[string]Permission

Operations is the permission of each operation that the API serves, by its operationId, such as GetNodeStatus. The endpoint groups and the behaviors give theirs from their definitions. The full spec is output, so nothing here reads it. A public method needs no permission and is not in it. The catalog endpoint serves it, so the UI asks it before a call.

func ResolvePermissions

func ResolvePermissions(
	roles []Role,
	directPermissions []Permission,
	customRoles CustomRoles,
) map[Permission]bool

ResolvePermissions computes the effective permission set for a token. If directPermissions is non-empty, it is returned directly (IdP override). Otherwise roles are expanded through customRoles first, then RolePermissions.

func RoleDescription added in v0.3.0

func RoleDescription(r Role) string

RoleDescription is what a built-in role is for, in one line, or "" for a custom role.

func RoleTable added in v0.3.0

func RoleTable() string

RoleTable is the permissions of each built-in role as a markdown table, for the configuration page of the docs site.

func Sees added in v0.3.0

func Sees(
	ctx context.Context,
	target string,
) bool

Sees reports whether the token of ctx may read a job, a trace or an audit entry whose target is target. A token with no scope sees each one. A scoped token sees one only when its target is in the scope, so a record with no target, or with the target _all or _any, is hidden from it.

func Strings added in v0.3.0

func Strings(
	perms []Permission,
) []string

Strings is perms as strings, for a token's claims and the output of a command, which hold a permission as text.

func SubjectFrom added in v0.3.0

func SubjectFrom(
	ctx context.Context,
) string

SubjectFrom is the subject that WithSubject put in ctx, or "" when the request has no token.

func TargetsFrom added in v0.3.0

func TargetsFrom(
	ctx context.Context,
) []string

TargetsFrom is the target scope that WithTargets put in ctx, or nil when the token has none, which covers each host.

func WithPermissions added in v0.3.0

func WithPermissions(
	ctx context.Context,
	resolved map[Permission]bool,
) context.Context

WithPermissions is ctx with the permissions that the token of the request resolves to, from its roles or its own permissions.

func WithSubject added in v0.3.0

func WithSubject(
	ctx context.Context,
	subject string,
) context.Context

WithSubject is ctx with the subject of the token of the request, such as ops@example.com.

func WithTargets added in v0.3.0

func WithTargets(
	ctx context.Context,
	targets []string,
) context.Context

WithTargets is ctx with the target scope of the token of the request.

Types

type CustomClaims

type CustomClaims struct {
	Roles       []types.Role `json:"roles"                 validate:"required,dive,required"`
	Permissions []string     `json:"permissions,omitempty"`
	// Targets are the hosts that the token may act on: hostnames and
	// labels, such as web-01 or group:web. A token without targets acts on
	// each host.
	Targets []string `json:"targets,omitempty" validate:"omitempty,dive,target_scope"`
	// KeyID is the kid header of the token: the ID of the key that signed
	// it. Validate sets it. It is not a claim.
	KeyID string `json:"-"`
	jwt.RegisteredClaims
}

CustomClaims defines the structure of your token claims

type CustomRoles added in v0.3.0

type CustomRoles map[Role][]Permission

CustomRoles are the custom roles of the config, each with its permissions.

type Key added in v0.3.0

type Key struct {
	// ID names the key in the kid header.
	ID string
	// Secret is the HS256 key.
	Secret string
}

Key is one key that signs and verifies tokens. A token carries the ID of its key in its kid header.

type Keys added in v0.3.0

type Keys []Key

Keys are the keys that the controller accepts. The first key signs, and each key verifies the tokens that carry its ID.

func (Keys) Lookup added in v0.3.0

func (k Keys) Lookup(
	id string,
) (Key, bool)

Lookup is the key with id.

func (Keys) Signer added in v0.3.0

func (k Keys) Signer() (Key, bool)

Signer is the key that signs a new token: the first.

type Permission

type Permission = types.Permission

Permission is a type alias for types.Permission.

func AllPermissions

func AllPermissions() []Permission

AllPermissions is every permission osapi knows: each built-in one, and each one that an endpoint group or a behavior declares, once. A permission that an endpoint method only names is not one.

func PermissionsOf added in v0.3.0

func PermissionsOf(
	perms []string,
) []Permission

PermissionsOf is perms, as a token's claims hold them, as Permissions.

func RolePermissions added in v0.3.0

func RolePermissions(
	role Role,
) ([]Permission, bool)

RolePermissions is what a built-in role grants, and whether role is one. admin grants each permission. operator grants each read and each execute permission that is not admin only, and operatorWrites, and automation each read and each write that is not admin only. read grants each read that is not admin only. auditor and observer grant their lists, of the permissions that osapi knows. Thus a new behavior's permissions go to the roles of their verbs, and nobody adds them to a role by hand.

type Role added in v0.3.0

type Role = types.Role

Role is a type alias for types.Role: a built-in role or a custom role of the config.

func Roles added in v0.3.0

func Roles() []Role

Roles are the built-in roles.

type Token

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

Token implementation of the token operations.

func New

func New(
	logger *slog.Logger,
) *Token

New factory to create a new instance.

func (*Token) Generate

func (t *Token) Generate(
	key Key,
	roles []types.Role,
	subject string,
	permissions []string,
	targets []string,
) (string, error)

Generate generates a JWT signed with key, with the given roles, optional permissions and optional target scope. The kid header is the ID of key.

func (*Token) Validate

func (t *Token) Validate(
	tokenString string,
	keys Keys,
) (*CustomClaims, error)

Validate parses and validates the JWT with the key that its kid header names. A token without a kid, or with a kid that keys does not have, is refused: Validate does not try each key. The claims carry the kid as KeyID.

Directories

Path Synopsis
Package main writes the SDK's constants, authtoken.Go, under --root.
Package main writes the SDK's constants, authtoken.Go, under --root.

Jump to

Keyboard shortcuts

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