This repository has been archived by the owner on Jan 16, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
91 lines (82 loc) · 2.77 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package jcp
import (
"errors"
"fmt"
"net/url"
"time"
"github.com/MicahParks/jsontype"
)
const (
// DefaultRefreshInterval is the default time between refreshes of the JWKS.
DefaultRefreshInterval = time.Hour
// DefaultRefreshTimeout is the default time to wait for a refresh of the JWKS before cancelling and logging an
// error.
DefaultRefreshTimeout = 10 * time.Second
// DefaultListenAddress is the default address to listen on.
DefaultListenAddress = ":8080"
// DefaultLogFormat is the default log format.
DefaultLogFormat = LogFormatJSON
// DefaultRequestMaxBytes is the default maximum number of bytes to read from a request.
DefaultRequestMaxBytes = 1 << 20 // 1 MB as defined by http.DefaultMaxHeaderBytes.
)
// ErrInvalidConfig is returned when the configuration is invalid.
var ErrInvalidConfig = errors.New("invalid configuration")
const (
// LogFormatJSON is the JSON log format.
LogFormatJSON = "json"
// LogFormatHuman is the human-readable log format.
LogFormatHuman = "human"
)
// LogFormat is the set of enums for the format of logs.
type LogFormat string
// Config contains the configuration for the JWKS client proxy.
type Config struct {
JWKS map[string]JWKSConfig `json:"jwks"`
ListenAddress string `json:"listenAddress"`
LogFormat string `json:"logFormat"`
RequestMaxBytes int64 `json:"requestMaxBytes"`
}
// DefaultsAndValidate helps implement the jsontype.Config interface.
func (c Config) DefaultsAndValidate() (Config, error) {
if len(c.JWKS) == 0 {
return c, fmt.Errorf("%w: no JWKS provided", ErrInvalidConfig)
}
for k, v := range c.JWKS {
u, err := url.Parse(k)
if err != nil {
return c, fmt.Errorf("failed to parse JWK Set URL: %q: %s: %w", k, err, ErrInvalidConfig)
}
if u.Scheme != "http" && u.Scheme != "https" {
return c, fmt.Errorf("invalid JWK Set URL scheme: %q: %w", u.Scheme, ErrInvalidConfig)
}
if c.JWKS[k].RefreshInterval.Get() == 0 {
v.RefreshInterval = jsontype.New(DefaultRefreshInterval)
c.JWKS[k] = v
}
if c.JWKS[k].RefreshTimeout.Get() == 0 {
v.RefreshTimeout = jsontype.New(DefaultRefreshTimeout)
c.JWKS[k] = v
}
}
if c.ListenAddress == "" {
c.ListenAddress = DefaultListenAddress
}
if c.LogFormat == "" {
c.LogFormat = DefaultLogFormat
} else {
switch c.LogFormat {
case LogFormatJSON, LogFormatHuman:
default:
return Config{}, fmt.Errorf("invalid log format: %q: %w", c.LogFormat, ErrInvalidConfig)
}
}
if c.RequestMaxBytes == 0 {
c.RequestMaxBytes = DefaultRequestMaxBytes
}
return c, nil
}
// JWKSConfig contains the configuration for a JWKS.
type JWKSConfig struct {
RefreshInterval *jsontype.JSONType[time.Duration] `json:"refreshInterval"`
RefreshTimeout *jsontype.JSONType[time.Duration] `json:"refreshTimeout"`
}