tmplfunc

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: MIT Imports: 35 Imported by: 0

Documentation

Overview

Package tmplfunc holds the functions of vlatte's Go template engine.

Index

Constants

This section is empty.

Variables

View Source
var ErrEnvNotAllowed = errors.New("is not allowed")

ErrEnvNotAllowed says a template read an environment variable its Env doesn't allow.

View Source
var ErrUnknownKey = errors.New("is not a signing key of the server")

ErrUnknownKey says a template named a signing key its Keys doesn't hold.

Functions

func CheckKeySpecs

func CheckKeySpecs(specs []string) error

CheckKeySpecs returns the error of the first spec ParseKeySpec refuses.

func Funcs

func Funcs() map[string]any

Funcs returns the functions templates can call besides those of text/template, but env, which an Env gives. Each call returns a new map, so an engine can add to its own.

Types

type Env

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

Env is the env function of templates: it reads the environment variables it allows, by exact, case-sensitive name, and no other. The zero Env allows none.

func (*Env) Allow

func (e *Env) Allow(names ...string) error

Allow adds names to the variables e allows. A name that is empty or holds "=" or NUL is an error.

func (*Env) Check

func (e *Env) Check(t *template.Template) error

Check returns the error env would fail with when t runs, for the first name t gives env as a literal and e doesn't allow, so a template can fail when it compiles. Names env reads from data are checked when it runs.

func (*Env) Get

func (e *Env) Get(name string) (string, error)

Get returns the value of the environment variable name, "" when it is unset, or ErrEnvNotAllowed. The error never holds the value.

type KeySpec

type KeySpec struct {
	Kid  string
	Alg  string
	Path string
}

KeySpec is a signing key as a setting gives it: "kid=ALG", a key generated when it loads, or "kid=ALG:path", one read from a PEM file. It holds a path, never key material, so a configuration that shows it holds no secret.

func ParseKeySpec

func ParseKeySpec(s string) (KeySpec, error)

ParseKeySpec parses s. An empty kid, a missing or unsupported algorithm, such as HS256, or an empty path after ":" is an error.

func (KeySpec) Load

func (ks KeySpec) Load(resolve func(path string) string) (crypto.Signer, error)

Load returns the key of ks: read from Path, which resolve turns into the file to read, or generated without one. RSA keys generated are 2048 bits. Errors never hold key material.

type Keys

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

Keys is the jwt and jwks functions of templates: they sign with, and publish the public half of, the keys added to it, by kid. The zero Keys holds none. Nothing reads a private key back out of it.

func (*Keys) Add

func (k *Keys) Add(kid, alg string, key crypto.Signer) error

Add adds key as kid, to sign with alg. An empty or repeated kid, an algorithm the jose package refuses, such as HS256, or a key that doesn't fit alg is an error, which never holds key material.

func (*Keys) Check

func (k *Keys) Check(t *template.Template) error

Check returns the error jwt or jwks would fail with when t runs, for the first kid given them as a literal that k doesn't hold, so a template can fail when it compiles. A kid read from data is checked when it runs.

func (*Keys) JWKS

func (k *Keys) JWKS(kids ...string) (string, error)

JWKS returns the JWK Set of the public halves of the keys kids, in that order, as JSON text.

func (*Keys) JWT

func (k *Keys) JWT(kid string, claims any) (string, error)

JWT returns claims signed with the key kid, as a compact JWS whose header has the key's alg, the kid and typ JWT. claims is JSON text, such as a template wrote, or a value that marshals to a JSON object, such as fromJSON returns. The token holds those claims and no other: no exp or iat is added.

type SpecKey

type SpecKey struct {
	Kid string
	Alg string
	Key crypto.Signer
}

SpecKey is a key a KeySpec loaded.

func LoadKeySpecs

func LoadKeySpecs(specs []string, resolve func(path string) string) ([]SpecKey, error)

LoadKeySpecs parses and loads each spec, in order: see KeySpec.Load.

Jump to

Keyboard shortcuts

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