Documentation
¶
Overview ¶
Package jwtregistry provides a way to store the current keys to be used for creating and validating JWTs. Multiple purposes can be used, each of which has a "keyset", "current key", and parameters such as expiry time, issuer to set, default validation options, and other usually unchanging items.
These registries would usually be created once, and used many times. Passing this deeply into each method is a pain... which this package hopes to reduce.
Index ¶
- func Clear()
- func Delete(purpose string)
- func Register(purpose string, issuer string, opts ...Option) error
- func Sign(purpose string, claims map[string]interface{}, clock jwt.Clock) (signed []byte, err error)
- func Validate(purpose string, signed []byte, clock jwt.Clock) (claims map[string]interface{}, err error)
- type Context
- type Option
- type TimeClock
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Register ¶
Register creates a new Context, and stores it in the globally available registry under the provided named purpose.
Various initial values are set using opts. Once set, the objects used in these Options should be treated as immutable, as they will be accessed by multiple threads.
func Sign ¶
func Sign(purpose string, claims map[string]interface{}, clock jwt.Clock) (signed []byte, err error)
Sign will create a new JWT based on the map of input data, the Context's configuration, and current signing key. If the signing key name is not set, an error will be returned. The issuer ("iss") will be set from the name provided at creation time, and inception ("iat") will always be set to whatever the provided clock returns as Now(). If a duration is configured, expirtation ("exp") will also be added to the claims.
Additional claims provided will also be added prior to signing.
func Validate ¶
func Validate(purpose string, signed []byte, clock jwt.Clock) (claims map[string]interface{}, err error)
Validate will validate the intregrity a given JWT using the named Context's validation configuration. The issuer and start time are always validated, and if the expiration time is present it will be included. A map containing all the claims will be returned.
Types ¶
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
Context holds a named JWT signer, validator, and other configuration for a specific named JWT use/purpose.
Once created, these should be treated as immutable. If changing a registry's configuration is desired, New() should be called to recreate it entirely. This keeps things thread-safe.
type Option ¶
type Option func(*Context)
Option specifies non-default overrides at creation time.
func WithKeyset ¶
WithKeyset specifies the keyset (named keys) to be used for signing or validating. This keyset is used as a list of possible keys to validate JWTs, as well as selecting which named key to use when signing.
func WithSigningKeyName ¶
WithSigningKeyName selects a key from one of the keys passed into WithKeyset to sign new requests. If signing is not needed, setting this is not required.
func WithSigningValidityPeriod ¶
WithSigningValidityPeriod sets the time between the issued time and the expiry time. If set to 0, no expiration time is set when signing. If a JWT has an expiration time, it will be validated regardless of this duration.