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.
Cross-kind verification consistency ¶
The supported token kinds intentionally share the same high-level verification contract where their formats overlap:
- generated tokens bind the requested audience to the token,
- verification requires the expected audience to match,
- generated tokens include an issued-at time and expiration,
- verification rejects tokens that are not currently valid, and
- verification rejects tokens whose signed lifetime exceeds the verifier's configured Expiration.
For JWT and PASETO, the returned identity is the non-empty subject claim ("sub"). For SSH-style tokens, the returned identity is also "sub"; SSH requires that "sub" match the signed key id ("kid") because that format authenticates a trusted peer key.
Facade behavior and unknown kinds ¶
The Token facade is intentionally conservative when Config.Kind is unknown:
- Token.Generate returns (nil, github.com/alexfalkowski/go-service/v2/token/errors.ErrInvalidConfig).
- Token.Verify returns (strings.Empty, github.com/alexfalkowski/go-service/v2/token/errors.ErrInvalidConfig).
This makes "unknown token kind" fail closed instead of behaving like "feature disabled" in wiring scenarios. Callers should treat ErrInvalidConfig as a startup or deployment configuration issue.
Configuration and enablement ¶
The standard startup path through github.com/alexfalkowski/go-service/v2/config.NewConfig validates that the nested JWT, PASETO, or SSH block matching Config.Kind is present. Direct callers that construct Config without validation remain fail-closed: Token.Generate and Token.Verify return github.com/alexfalkowski/go-service/v2/token/errors.ErrInvalidConfig when the selected nested configuration is missing.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// 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" validate:"required_if=Kind jwt"`
// 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" validate:"required_if=Kind paseto"`
// 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" validate:"required_if=Kind ssh"`
// Kind selects the token implementation to use.
//
// Supported values: "jwt", "paseto", "ssh".
Kind string `yaml:"kind,omitempty" json:"kind,omitempty" toml:"kind,omitempty" validate:"required,oneof=jwt paseto ssh"`
}
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 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, Kind must select a supported implementation and that implementation's nested configuration must be present.
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 must be provided in the corresponding field (JWT/Paseto/SSH).
Config validation rejects unknown Kind values. If validation is bypassed, the token facade treats the configuration as invalid and Token.Generate/Token.Verify return github.com/alexfalkowski/go-service/v2/token/errors.ErrInvalidConfig.
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 ¶
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 github.com/alexfalkowski/go-service/v2/token/errors.ErrInvalidConfig instead of panicking.
Unknown kinds are treated as invalid configuration by the facade methods: Generate and Verify return github.com/alexfalkowski/go-service/v2/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": the token is minted for the provided audience (aud), while subject is derived from the active trusted key id.
If the configured kind is unknown, Generate returns github.com/alexfalkowski/go-service/v2/token/errors.ErrInvalidConfig.
The returned bytes should be treated as read-only. Generate may return a zero-allocation byte view over the generated token string. Callers that need to mutate the bytes should clone the returned slice first.
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": verifies the token for the provided audience (aud), and the returned string is the "sub" claim, which must match the signed key id.
If the configured kind is unknown, Verify returns github.com/alexfalkowski/go-service/v2/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 keys provides token key configuration helpers.
|
Package keys provides token key configuration helpers. |
|
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. |