Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrJWKUseWhitelist indicates that the given JWK was found, but its "use" parameter's value was not whitelisted. ErrJWKUseWhitelist = errors.New(`the given JWK was found, but its "use" parameter's value was not whitelisted`) // ErrKIDNotFound indicates that the given key ID was not found in the JWKS. ErrKIDNotFound = errors.New("the given key ID was not found in the JWKS") // ErrMissingAssets indicates there are required assets are missing to create a public key. ErrMissingAssets = errors.New("required assets are missing to create a public key") )
var ErrInvalidHTTPStatusCode = errors.New("invalid HTTP status code")
ErrInvalidHTTPStatusCode indicates that the HTTP status code is invalid.
var ( // ErrKID indicates that the JWT had an invalid kid. ErrKID = errors.New("the JWT has an invalid kid") )
Functions ¶
func ResponseExtractorStatusAny ¶ added in v1.4.0
ResponseExtractorStatusAny is meant to be used as the ResponseExtractor field for Options. It returns the raw JSON from the response body regardless of the response status code.
func ResponseExtractorStatusOK ¶ added in v1.3.0
ResponseExtractorStatusOK is meant to be used as the ResponseExtractor field for Options. It confirms that response status code is 200 OK and returns the raw JSON from the response body.
Types ¶
type ErrorHandler ¶
type ErrorHandler func(err error)
ErrorHandler is a function signature that consumes an error.
type GivenKey ¶ added in v0.8.0
type GivenKey struct {
// contains filtered or unexported fields
}
GivenKey represents a cryptographic key that resides in a JWKS. In conjuncture with Options.
func NewGivenCustom ¶ added in v0.8.0
func NewGivenCustom(key interface{}) (givenKey GivenKey)
NewGivenCustom creates a new GivenKey given an untyped variable. The key argument is expected to be a supported by the jwt package used.
See the https://pkg.go.dev/github.com/golang-jwt/jwt/v4#RegisterSigningMethod function for registering an unsupported signing method.
func NewGivenECDSA ¶ added in v0.8.0
NewGivenECDSA creates a new GivenKey given an ECDSA public key.
func NewGivenEdDSA ¶ added in v1.0.0
NewGivenEdDSA creates a new GivenKey given an EdDSA public key.
func NewGivenHMAC ¶ added in v0.8.0
NewGivenHMAC creates a new GivenKey given an HMAC key in a byte slice.
func NewGivenRSA ¶ added in v0.8.0
NewGivenRSA creates a new GivenKey given an RSA public key.
type JWKS ¶
type JWKS struct {
// contains filtered or unexported fields
}
JWKS represents a JSON Web Key Set (JWK Set).
func NewJSON ¶ added in v0.8.0
func NewJSON(jwksBytes json.RawMessage) (jwks *JWKS, err error)
NewJSON creates a new JWKS from a raw JSON message.
func (*JWKS) EndBackground ¶
func (j *JWKS) EndBackground()
EndBackground ends the background goroutine to update the JWKS. It can only happen once and is only effective if the JWKS has a background goroutine refreshing the JWKS keys.
func (*JWKS) Keyfunc ¶ added in v1.0.0
Keyfunc matches the signature of github.com/golang-jwt/jwt/v4's jwt.Keyfunc function.
func (*JWKS) RawJWKS ¶ added in v1.2.0
RawJWKS returns a copy of the raw JWKS received from the given JWKS URL.
func (*JWKS) ReadOnlyKeys ¶ added in v1.0.0
ReadOnlyKeys returns a read-only copy of the mapping of key IDs (`kid`) to cryptographic keys.
type JWKUse ¶ added in v1.5.0
type JWKUse string
JWKUse is a set of values for the "use" parameter of a JWK. See https://tools.ietf.org/html/rfc7517#section-4.2.
const ( // UseEncryption is a JWK "use" parameter value indicating the JSON Web Key is to be used for encryption. UseEncryption JWKUse = "enc" // UseOmitted is a JWK "use" parameter value that was not specified or was empty. UseOmitted JWKUse = "" // UseSignature is a JWK "use" parameter value indicating the JSON Web Key is to be used for signatures. UseSignature JWKUse = "sig" )
type Options ¶
type Options struct {
// Client is the HTTP client used to get the JWKS via HTTP.
Client *http.Client
// Ctx is the context for the keyfunc's background refresh. When the context expires or is canceled, the background
// goroutine will end.
Ctx context.Context
// GivenKeys is a map of JWT key IDs, `kid`, to their given keys. If the JWKS has a background refresh goroutine,
// these values persist across JWKS refreshes. By default, if the remote JWKS resource contains a key with the same
// `kid` any given keys with the same `kid` will be overwritten by the keys from the remote JWKS. Use the
// GivenKIDOverride option to flip this behavior.
GivenKeys map[string]GivenKey
// GivenKIDOverride will make a GivenKey override any keys with the same ID (`kid`) in the remote JWKS. The is only
// effectual if GivenKeys is provided.
GivenKIDOverride bool
// JWKUseWhitelist is a whitelist of JWK `use` parameter values that will restrict what keys can be returned for
// jwt.Keyfunc. The assumption is that jwt.Keyfunc is only used for JWT signature verification.
// The default behavior is to only return a JWK if its `use` parameter has the value `"sig"`, an empty string, or if
// the parameter was omitted entirely.
JWKUseWhitelist []JWKUse
// JWKUseNoWhitelist overrides the JWKUseWhitelist field and its default behavior. If set to true, all JWKs will be
// returned regardless of their `use` parameter value.
JWKUseNoWhitelist bool
// RefreshErrorHandler is a function that consumes errors that happen during a JWKS refresh. This is only effectual
// if a background refresh goroutine is active.
RefreshErrorHandler ErrorHandler
// RefreshInterval is the duration to refresh the JWKS in the background via a new HTTP request. If this is not nil,
// then a background goroutine will be used to refresh the JWKS once per the given interval. Make sure to call the
// JWKS.EndBackground method to end this goroutine when it's no longer needed.
RefreshInterval time.Duration
// RefreshRateLimit limits the rate at which refresh requests are granted. Only one refresh request can be queued
// at a time any refresh requests received while there is already a queue are ignored. It does not make sense to
// have RefreshInterval's value shorter than this.
RefreshRateLimit time.Duration
// RefreshTimeout is the duration for the context timeout used to create the HTTP request for a refresh of the JWKS.
// This defaults to one minute. This is used for the HTTP request and any background goroutine refreshes.
RefreshTimeout time.Duration
// RefreshUnknownKID indicates that the JWKS refresh request will occur every time a kid that isn't cached is seen.
// This is done through a background goroutine. Without specifying a RefreshInterval a malicious client could
// self-sign X JWTs, send them to this service, then cause potentially high network usage proportional to X. Make
// sure to call the JWKS.EndBackground method to end this goroutine when it's no longer needed.
RefreshUnknownKID bool
// RequestFactory creates HTTP requests for the remote JWKS resource located at the given url. For example, an
// HTTP header could be added to indicate a User-Agent.
RequestFactory func(ctx context.Context, url string) (*http.Request, error)
// ResponseExtractor consumes a *http.Response and produces the raw JSON for the JWKS. By default, the
// ResponseExtractorStatusOK function is used. The default behavior changed in v1.4.0.
ResponseExtractor func(ctx context.Context, resp *http.Response) (json.RawMessage, error)
}
Options represents the configuration options for a JWKS.
If RefreshInterval and or RefreshUnknownKID is not nil, then a background goroutine will be launched to refresh the remote JWKS under the specified circumstances.
When using a background refresh goroutine, make sure to use RefreshRateLimit if paired with RefreshUnknownKID. Also make sure to end the background refresh goroutine with the JWKS.EndBackground method when it's no longer needed.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
aws_cognito
command
|
|
|
ctx
command
|
|
|
custom
command
|
|
|
given
command
|
|
|
hmac
command
|
|
|
interval
command
|
|
|
json
command
|
|
|
keycloak
command
|
|
|
recommended_options
command
|