Documentation
¶
Overview ¶
Package token provides token generation and verification helpers used by go-service.
This package defines common token concepts and shared helpers used by concrete token implementations (for example JWT, PASETO, and SSH).
It also provides a small facade type (Token) that delegates token generation and verification to the configured implementation so callers can depend on a single entry point when the token kind is selected by configuration.
Supported token kinds ¶
The top-level Token facade supports the following kinds, selected by Config.Kind:
- "jwt": JSON Web Tokens signed using Ed25519 (see the token/jwt package).
- "paseto": PASETO v4 public tokens (see the token/paseto package).
- "ssh": SSH-style signed tokens (see the token/ssh package).
Concrete packages document their own token formats, claims, cryptographic algorithms, and validation semantics.
Facade behavior and unknown kinds ¶
The Token facade is intentionally conservative when Config.Kind is unknown:
- Generate returns (nil, nil).
- Verify returns (strings.Empty, nil).
This makes “unknown token kind” behave like “feature disabled” in some wiring scenarios, but it also means callers should treat a nil/empty successful result as a signal to check configuration.
Configuration and enablement ¶
This package does not enforce that nested config blocks (JWT/Paseto/SSH) are present when the corresponding kind is selected. The concrete token constructors typically treat a nil *Config as disabled and may return nil implementations. Ensure your configuration is consistent with the selected kind.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// Access configures access control policy used by token/access.
//
// This is used to answer authorization checks (for example "user has permission X")
// and is typically used in addition to authentication (token verification).
Access *access.Config `yaml:"access,omitempty" json:"access,omitempty" toml:"access,omitempty"`
// JWT configures the JWT token implementation.
//
// When Kind == "jwt", this configuration is consumed by token/jwt.
JWT *jwt.Config `yaml:"jwt,omitempty" json:"jwt,omitempty" toml:"jwt,omitempty"`
// Paseto configures the PASETO token implementation.
//
// When Kind == "paseto", this configuration is consumed by token/paseto.
Paseto *paseto.Config `yaml:"paseto,omitempty" json:"paseto,omitempty" toml:"paseto,omitempty"`
// SSH configures the SSH token implementation.
//
// When Kind == "ssh", this configuration is consumed by token/ssh.
SSH *ssh.Config `yaml:"ssh,omitempty" json:"ssh,omitempty" toml:"ssh,omitempty"`
// Kind selects the token implementation to use.
//
// Supported values: "jwt", "paseto", "ssh".
Kind string `yaml:"kind,omitempty" json:"kind,omitempty" toml:"kind,omitempty"`
}
Config configures token generation and verification for a go-service application.
This type is typically embedded into a larger service configuration and consumed by the top-level token facade (see token.NewToken), which delegates to the configured token implementation.
Enablement model ¶
Enablement is modeled by presence:
- A nil *Config means "token support disabled" at the top level.
- When enabled, individual token implementations may still be disabled if their nested configuration is nil (for example JWT == nil while Kind == "jwt").
Selecting an implementation (Kind) ¶
Kind selects the token implementation used by the token facade. Supported kinds are:
- "jwt": JSON Web Tokens (see package token/jwt)
- "paseto": PASETO v4 public tokens (see package token/paseto)
- "ssh": SSH-style signed tokens (see package token/ssh)
The selected implementation’s nested configuration should typically be provided in the corresponding field (JWT/Paseto/SSH).
If Kind is unknown, the token facade treats the configuration as invalid and Generate/Verify return token/errors.ErrInvalidConfig.
Access control (Access) ¶
Access configures optional access-control policy wiring (see package token/access). It is orthogonal to the token kind: some services may use token verification to establish identity (subject) and then evaluate permissions via Access.
type Generator ¶
type Generator interface {
// Generate creates a new token for the given audience and subject.
//
// Implementations should treat aud/sub as logical identity inputs and produce a token
// suitable for later validation by a corresponding Verifier.
Generate(aud, sub string) ([]byte, error)
}
Generator generates authentication tokens for a given audience and subject.
This interface represents the “issuance” side of a token system. Concrete implementations may generate different token formats depending on configuration (for example JWT, PASETO, or other schemes).
Parameters ¶
Generate accepts two identity inputs:
- aud: the intended audience for the token (who the token is meant for).
- sub: the subject identifier (who/what the token represents).
In claim-based token formats (for example JWT/PASETO), aud and sub are typically encoded into standard claims.
Some token kinds may ignore one or both parameters (for example formats that do not carry claims or that encode identity differently). Callers should consult the concrete implementation’s documentation for exact semantics.
Return value ¶
Generate returns the serialized token bytes. For text-based token formats, these bytes are typically UTF-8 encoded.
Errors ¶
Generate returns an error when token issuance fails (for example invalid configuration, missing key material, signing failures, or serialization errors).
type Token ¶
type Token struct {
// contains filtered or unexported fields
}
Token is a facade that generates and verifies tokens using the implementation selected by configuration.
It standardizes the call sites for token issuance and validation, while allowing the actual token format and crypto scheme to be chosen by configuration.
func NewToken ¶
func NewToken(name env.Name, cfg *Config, fs *os.FS, sig *ed25519.Signer, ver *ed25519.Verifier, gen id.Generator) *Token
NewToken constructs a Token facade that can generate and verify tokens for multiple kinds.
The facade delegates to the implementation selected by cfg.Kind:
- "jwt": token/jwt
- "paseto": token/paseto
- "ssh": token/ssh
The underlying implementations are constructed eagerly from the corresponding nested configuration blocks (cfg.JWT, cfg.Paseto, cfg.SSH). Individual implementations may be nil when their nested configuration is nil.
A nil cfg is treated as disabled and returns nil. If Kind selects an implementation whose nested config is missing, Generate/Verify return token/errors.ErrInvalidConfig instead of panicking.
Unknown kinds are treated as invalid configuration by the facade methods: Generate and Verify return token/errors.ErrInvalidConfig.
func (*Token) Generate ¶
Generate creates a token for the configured kind.
Semantics by kind:
"jwt" and "paseto": the token is minted for the provided audience (aud) and subject (sub).
"ssh": audience and subject are ignored; the SSH token kind uses its own encoding/signature format and typically identifies a key rather than a subject.
If the configured kind is unknown, Generate returns token/errors.ErrInvalidConfig.
func (*Token) Verify ¶
Verify validates token for the configured kind and returns the subject identifier.
Semantics by kind:
"jwt" and "paseto": verifies the token for the provided audience (aud) and returns the subject ("sub") claim.
"ssh": audience is ignored and the returned string is the selected key name (not a JWT/PASETO "sub" claim).
If the configured kind is unknown, Verify returns token/errors.ErrInvalidConfig.
type Verifier ¶
type Verifier interface {
// Verify validates token for the given audience and returns the subject identifier.
Verify(token []byte, aud string) (string, error)
}
Verifier verifies authentication tokens for an expected audience and returns the subject identifier.
This interface represents the “verification” side of a token system. Concrete implementations may verify different token formats (for example JWT, PASETO, or other schemes) and may impose additional checks such as issuer matching, algorithm constraints, key ID matching, or time validity.
Parameters ¶
Verify accepts:
- token: the serialized token bytes to verify.
- aud: the expected audience value for which the token must be valid.
In claim-based token formats (for example JWT/PASETO), aud is typically validated against an "aud" claim. Some token kinds may ignore aud entirely (for example formats without claims). Callers should consult the concrete implementation’s documentation for exact semantics.
Return value ¶
On success, Verify returns the subject identifier represented by the token (commonly the "sub" claim). If the token kind does not carry a subject claim, the implementation may return an alternate identifier.
Errors ¶
Verify returns an error when validation fails (for example malformed token, signature mismatch, wrong issuer/audience, expired/not-yet-valid token, key mismatch, or missing key material). When an error is returned, the subject return value should not be trusted.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package access provides authorization (access control) helpers used by go-service.
|
Package access provides authorization (access control) helpers used by go-service. |
|
Package errors defines shared sentinel errors used by go-service token implementations.
|
Package errors defines shared sentinel errors used by go-service token implementations. |
|
Package jwt provides JSON Web Token (JWT) issuance and verification for go-service.
|
Package jwt provides JSON Web Token (JWT) issuance and verification for go-service. |
|
Package paseto provides PASETO token generation and verification for go-service.
|
Package paseto provides PASETO token generation and verification for go-service. |
|
Package ssh provides an SSH-style token format for go-service.
|
Package ssh provides an SSH-style token format for go-service. |