Documentation
¶
Overview ¶
Package jwt provides strict JWT and JWK authentication using lestrrat-go/jwx.
Index ¶
- Variables
- type Clockdeprecated
- type Config
- type KeyProvider
- type KeyProviderFunc
- type Remote
- type RemoteOption
- func WithHTTPClient(client *http.Client) RemoteOption
- func WithInitializationTimeout(timeout time.Duration) RemoteOption
- func WithInsecureHTTP() RemoteOption
- func WithMaxJWKBodyBytes(maximum int64) RemoteOption
- func WithMaxJWKHeaderBytes(maximum int64) RemoteOption
- func WithMaxJWKKeys(maximum int) RemoteOption
- func WithRefreshBounds(minimum, maximum time.Duration) RemoteOption
- func WithRefreshJitter(maximumFraction float64) RemoteOption
- type Validator
Examples ¶
Constants ¶
This section is empty.
Variables ¶
ErrKeyProviderUnavailable is the redacted cause for key-provider failures.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
Issuer string
Audience string
Algorithms []jwa.SignatureAlgorithm
KeySet jwk.Set
Provider KeyProvider
Clock Clock
Skew time.Duration
MaxTokenBytes int
MaxClaims int
MaxClaimDepth int
MaxKeys int
Subjects []string
RequiredClaims []string
ScopeClaim string
TenantClaim string
}
Config defines a strict JWT trust boundary.
type KeyProvider ¶
KeyProvider returns a current read-only JWK set for one validation attempt.
type KeyProviderFunc ¶
KeyProviderFunc adapts a function to KeyProvider.
type Remote ¶
type Remote struct {
// contains filtered or unexported fields
}
Remote owns a bounded JWK cache and all of its background goroutines.
func NewRemote ¶
NewRemote registers and initially fetches one exact JWK URL. The caller owns the returned provider and must call Close.
type RemoteOption ¶
type RemoteOption func(*remoteConfig)
RemoteOption configures a network-backed JWK provider.
func WithHTTPClient ¶
func WithHTTPClient(client *http.Client) RemoteOption
WithHTTPClient supplies an HTTP client. JWX timeout and redirect hardening is layered onto a shallow copy of the client.
func WithInitializationTimeout ¶
func WithInitializationTimeout(timeout time.Duration) RemoteOption
WithInitializationTimeout bounds the initial fetch and cache registration.
func WithInsecureHTTP ¶
func WithInsecureHTTP() RemoteOption
WithInsecureHTTP permits an HTTP JWK URL. It is intended only for isolated tests and trusted development networks.
func WithMaxJWKBodyBytes ¶
func WithMaxJWKBodyBytes(maximum int64) RemoteOption
WithMaxJWKBodyBytes bounds a JWK HTTP response body.
func WithMaxJWKHeaderBytes ¶
func WithMaxJWKHeaderBytes(maximum int64) RemoteOption
WithMaxJWKHeaderBytes bounds the aggregate response-header bytes accepted from the JWK endpoint.
func WithMaxJWKKeys ¶
func WithMaxJWKKeys(maximum int) RemoteOption
WithMaxJWKKeys bounds the number of keys accepted in a remote JWK set.
func WithRefreshBounds ¶
func WithRefreshBounds(minimum, maximum time.Duration) RemoteOption
WithRefreshBounds configures minimum and maximum automatic refresh intervals.
func WithRefreshJitter ¶
func WithRefreshJitter(maximumFraction float64) RemoteOption
WithRefreshJitter configures the maximum fractional deviation applied to provider refresh intervals. Zero disables jitter; values must be below one.
type Validator ¶
type Validator struct {
// contains filtered or unexported fields
}
Validator authenticates signed compact JWT bearer credentials.
func New ¶
New validates and defensively copies a static JWK trust configuration.
Example ¶
package main
import (
"context"
"fmt"
"time"
"github.com/faustbrian/go-authentication/authtest"
authjwt "github.com/faustbrian/go-authentication/jwt"
"github.com/lestrrat-go/jwx/v3/jwa"
"github.com/lestrrat-go/jwx/v3/jwk"
upstreamjwt "github.com/lestrrat-go/jwx/v3/jwt"
)
func main() {
key, err := jwk.Import([]byte("01234567890123456789012345678901"))
if err != nil {
return
}
_ = key.Set(jwk.KeyIDKey, "key")
_ = key.Set(jwk.AlgorithmKey, jwa.HS256())
keys := jwk.NewSet()
_ = keys.AddKey(key)
now := time.Unix(1_800_000_000, 0).UTC()
validator, err := authjwt.New(authjwt.Config{
Issuer: "https://issuer.example.test", Audience: "service",
Algorithms: []jwa.SignatureAlgorithm{jwa.HS256()}, KeySet: keys,
Clock: authtest.NewClock(now),
})
if err != nil {
return
}
token := upstreamjwt.New()
_ = token.Set("sub", "service")
_ = token.Set("iss", "https://issuer.example.test")
_ = token.Set("aud", "service")
_ = token.Set("iat", now)
_ = token.Set("exp", now.Add(time.Hour))
signed, _ := upstreamjwt.Sign(token, upstreamjwt.WithKey(jwa.HS256(), key))
principal, err := validator.ValidateBearer(context.Background(), string(signed))
fmt.Println(err, principal.Subject())
}
Output: <nil> service
func (*Validator) Authenticate ¶
func (v *Validator) Authenticate(ctx context.Context, credential authentication.Credential) (authentication.Result, error)
Authenticate validates a bearer credential and returns a JWT principal.
func (*Validator) ValidateBearer ¶
func (v *Validator) ValidateBearer(ctx context.Context, token string) (authentication.Principal, error)
ValidateBearer verifies a bounded compact JWT and constructs an immutable principal.