Documentation
¶
Overview ¶
Package authtoken provides JWT token generation and validation.
Index ¶
- Constants
- Variables
- func AdminOnly(p Permission) bool
- func Check() []error
- func ConstName(p Permission) string
- func Go() string
- func Granted(ctx context.Context, p Permission) bool
- func HasPermission(resolved map[Permission]bool, required Permission) bool
- func InScope(ctx context.Context, target string) bool
- func OperationPermissions(spec []byte) (map[string]Permission, error)
- func Operations() map[string]Permission
- func ResolvePermissions(roles []Role, directPermissions []Permission, customRoles CustomRoles) map[Permission]bool
- func RoleDescription(r Role) string
- func RoleTable() string
- func Sees(ctx context.Context, target string) bool
- func Strings(perms []Permission) []string
- func SubjectFrom(ctx context.Context) string
- func TargetsFrom(ctx context.Context) []string
- func WithPermissions(ctx context.Context, resolved map[Permission]bool) context.Context
- func WithSubject(ctx context.Context, subject string) context.Context
- func WithTargets(ctx context.Context, targets []string) context.Context
- type CustomClaims
- type CustomRoles
- type Key
- type Keys
- type Permission
- type Role
- type Token
Constants ¶
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.
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.
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 ¶
var ErrNoKeyID = errors.New("token has no kid")
ErrNoKeyID is a token without a kid header.
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 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
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
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
SubjectFrom is the subject that WithSubject put in ctx, or "" when the request has no token.
func TargetsFrom ¶ added in v0.3.0
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
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
WithSubject is ctx with the subject of the token of the request, such as ops@example.com.
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.
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
Role is a type alias for types.Role: a built-in role or a custom role of the config.
type Token ¶
type Token struct {
// contains filtered or unexported fields
}
Token implementation of the token operations.
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.