Documentation
¶
Overview ¶
Package jwt provides helpers for signing and verifying RS256 JSON Web Tokens on top of github.com/golang-jwt/jwt/v5, including support for key rotation via deprecated verification keys.
A Key pairs an RSA public key with an optional private key and an identifier that is written to and read from the token's "kid" header. JWT signs tokens with its current key and verifies them using DefaultKeyFunc unless a custom key function is supplied:
key, deprecated, err := jwt.NewKeysFromFilenames("public.pem", "private.pem", nil)
j := jwt.New(key, jwt.WithDeprecatedKeys(deprecated...))
token, err := j.GetSignedToken(jwt.NewRegisteredClaimsWithLifetime(time.Hour))
Index ¶
- Variables
- func DefaultKeyFunc(key Key, deprecatedKeys map[string]Key) jwt.Keyfunc
- func NewKeysFromFilenames(publicKeyPemFilename, privateKeyPemFilename string, ...) (Key, []Key, error)
- func NewRegisteredClaims(opts ...RegisteredClaimsOption) jwt.RegisteredClaims
- func NewRegisteredClaimsWithLifetime(lifetime time.Duration, opts ...RegisteredClaimsOption) jwt.RegisteredClaims
- func NewStandardClaims() jwt.RegisteredClaimsdeprecated
- func NewStandardClaimsWithLifetime(lifetime time.Duration) jwt.RegisteredClaimsdeprecated
- type JWT
- type Key
- type Option
- type RegisteredClaimsOption
Constants ¶
This section is empty.
Variables ¶
var MaxTimeDifferenceBetweenNodes = 30 * time.Second
MaxTimeDifferenceBetweenNodes represents an offset that should be taken into account when creating e.g. jwt tokens with the `notBefore` flag. It is the default offset used by NewRegisteredClaims.
Functions ¶
func DefaultKeyFunc ¶
DefaultKeyFunc returns a jwt.Keyfunc that accepts only RS256 tokens and selects the public key by the token's "kid" header, looking it up in deprecatedKeys first and then comparing it to key.ID. It returns an error for other signing methods and for missing, non-string or unknown key IDs.
func NewKeysFromFilenames ¶
func NewKeysFromFilenames(publicKeyPemFilename, privateKeyPemFilename string, deprecatedPublicKeyPemFilenames []string) (Key, []Key, error)
NewKeysFromFilenames loads the current key via NewKeyFromFilenames and the deprecated keys via NewDeprecatedKeysFromFilenames.
func NewRegisteredClaims ¶ added in v0.23.0
func NewRegisteredClaims(opts ...RegisteredClaimsOption) jwt.RegisteredClaims
NewRegisteredClaims returns jwt.RegisteredClaims with IssuedAt and NotBefore set to the current time minus the offset. The offset accounts for clock differences between nodes in a distributed system; offsets under one millisecond are ignored. If no WithOffset option is provided, MaxTimeDifferenceBetweenNodes is used.
func NewRegisteredClaimsWithLifetime ¶ added in v0.23.0
func NewRegisteredClaimsWithLifetime(lifetime time.Duration, opts ...RegisteredClaimsOption) jwt.RegisteredClaims
NewRegisteredClaimsWithLifetime returns claims as created by NewRegisteredClaims with ExpiresAt set to IssuedAt plus lifetime. Since IssuedAt is shifted back by the offset, the effective remaining lifetime is lifetime minus the offset.
func NewStandardClaims
deprecated
func NewStandardClaims() jwt.RegisteredClaims
NewStandardClaims returns registered claims using MaxTimeDifferenceBetweenNodes as offset.
Deprecated: Use NewRegisteredClaims instead.
func NewStandardClaimsWithLifetime
deprecated
func NewStandardClaimsWithLifetime(lifetime time.Duration) jwt.RegisteredClaims
NewStandardClaimsWithLifetime returns registered claims expiring after lifetime, using MaxTimeDifferenceBetweenNodes as offset.
Deprecated: Use NewRegisteredClaimsWithLifetime instead.
Types ¶
type JWT ¶
type JWT struct {
// Key is the current key used for signing and verification.
Key Key
// KeyFunc resolves the verification key for a parsed token.
KeyFunc jwt.Keyfunc
// DeprecatedKeys holds verification-only keys indexed by key ID,
// e.g. kept after a key rotation.
DeprecatedKeys map[string]Key
}
JWT signs and parses RS256 tokens. Create it with New.
func New ¶
New returns a JWT that signs with key. If no key function is set via WithKeyFun, DefaultKeyFunc is used with key and any keys added via WithDeprecatedKeys. Nil options are ignored.
func (*JWT) GetSignedToken ¶
GetSignedToken returns claims as a token signed with RS256 using the private part of JWT.Key, with the "kid" header set to its ID. It returns an error if signing fails, e.g. when no private key is set.
func (*JWT) ParseWithClaims ¶
ParseWithClaims parses and validates token into claims, resolving the verification key with JWT.KeyFunc.
type Key ¶
type Key struct {
// ID (required) is the key identifier used as the token's "kid" header,
// e.g. a hash of the public key.
ID string
// Public (required) is the RSA public key used for verification.
Public *rsa.PublicKey
// Private (optional) is the RSA private key used for signing.
Private *rsa.PrivateKey
}
Key is an RSA key pair identified by ID.
func NewDeprecatedKeysFromFilenames ¶
NewDeprecatedKeysFromFilenames loads public-only keys from the given PEM files, for use with WithDeprecatedKeys. It returns the first load error.
func NewKeyFromFilenames ¶
NewKeyFromFilenames loads a Key from PEM files. The private key is optional and skipped if privateKeyPemFilename is empty. Literal \n escape sequences in the file contents are converted to newlines before parsing. The key ID is the hex-encoded SHA-256 of the trimmed public key file contents. It returns an error if a file cannot be read or parsed.
type Option ¶
type Option func(*JWT)
Option configures a JWT.
func WithDeprecatedKeys ¶
WithDeprecatedKeys adds keys that are still accepted for verification, indexed by their Key.ID.
func WithKeyFun ¶
WithKeyFun sets the key function used to verify tokens. Defaults to DefaultKeyFunc over the current and deprecated keys.
type RegisteredClaimsOption ¶ added in v0.23.0
type RegisteredClaimsOption func(*registeredClaimsOptions)
RegisteredClaimsOption configures how NewRegisteredClaims creates claims.
func WithOffset ¶ added in v0.23.0
func WithOffset(offset time.Duration) RegisteredClaimsOption
WithOffset sets the offset subtracted from the current time to account for clock differences between nodes. Defaults to MaxTimeDifferenceBetweenNodes.