Debian Codegraph @

internal/config/types.go

raw
1 package config
2
3 import (
4 "encoding/json"
5 "fmt"
6 "log/slog"
7 "strings"
8 "time"
9
10 "github.com/jkroepke/openvpn-auth-oauth2/v2/internal/config/types"
11 "golang.org/x/oauth2"
12 )
13
14 //goland:noinspection GoUnusedConst
15 const (
16 Plugin = iota
17 ManagementClient
18 )
19
20 type Config struct {
21 HTTP HTTP `json:"http" yaml:"http"`
22 Debug Debug `json:"debug" yaml:"debug"`
23 Log Log `json:"log" yaml:"log"`
24 OAuth2 OAuth2 `json:"oauth2" yaml:"oauth2"`
25 OpenVPN OpenVPN `json:"openvpn" yaml:"openvpn"`
26 Provider Provider `json:"provider" yaml:"provider"`
27 }
28
29 // Provider holds provider-specific configuration that is not shared across all
30 // OAuth2 providers. The active provider is selected via OAuth2.Provider.
31 type Provider struct {
32 Google ProviderGoogle `json:"google" yaml:"google"`
33 }
34
35 type ProviderGoogle struct {
36 Validate ProviderGoogleValidate `json:"validate" yaml:"validate"`
37 }
38
39 type ProviderGoogleValidate struct {
40 // GroupsTransitive enables transitive (direct or nested) group membership
41 // resolution via the Google Cloud Identity API instead of direct-only checks.
42 GroupsTransitive bool `json:"groups-transitive" yaml:"groups-transitive"`
43 }
44
45 type HTTP struct {
46 BaseURL types.URL `json:"baseurl" yaml:"baseurl"`
47 AssetPath types.FS `json:"assets-path" yaml:"assets-path"`
48 Template types.Template `json:"template" yaml:"template"`
49 Listen string `json:"listen" yaml:"listen"`
50 CertFile string `json:"cert" yaml:"cert"`
51 KeyFile string `json:"key" yaml:"key"`
52 Secret types.Secret `json:"secret" yaml:"secret"`
53 TrustedProxies types.StringSlice `json:"trusted-proxies" yaml:"trusted-proxies"`
54 TLS bool `json:"tls" yaml:"tls"`
55 Check HTTPCheck `json:"check" yaml:"check"`
56 EnableProxyHeaders bool `json:"enable-proxy-headers" yaml:"enable-proxy-headers"`
57 }
58
59 type HTTPCheck struct {
60 IPAddr bool `json:"ipaddr" yaml:"ipaddr"`
61 }
62
63 type Log struct {
64 Format string `json:"format" yaml:"format"`
65 Level slog.Level `json:"level" yaml:"level"`
66 VPNClientIP bool `json:"vpn-client-ip" yaml:"vpn-client-ip"`
67 }
68
69 type OpenVPN struct {
70 Addr types.URL `json:"addr" yaml:"addr"`
71 Password types.Secret `json:"password" yaml:"password"`
72 Bypass OpenVPNBypass `json:"bypass" yaml:"bypass"`
73 CommonName OpenVPNCommonName `json:"common-name" yaml:"common-name"`
74 Passthrough OpenVPNPassthrough `json:"pass-through" yaml:"pass-through"`
75 ClientConfig OpenVPNConfig `json:"client-config" yaml:"client-config"`
76 AuthPendingTimeout time.Duration `json:"auth-pending-timeout" yaml:"auth-pending-timeout"`
77 CommandTimeout time.Duration `json:"command-timeout" yaml:"command-timeout"`
78 EnforceUniqueUser bool `json:"enforce-unique-user" yaml:"enforce-unique-user"`
79 OverrideUsername bool `json:"override-username" yaml:"override-username"`
80 ReAuthentication bool `json:"reauthentication" yaml:"reauthentication"`
81 AuthTokenUser bool `json:"auth-token-user" yaml:"auth-token-user"`
82 }
83
84 type OpenVPNBypass struct {
85 CommonNames types.RegexpSlice `json:"common-names" yaml:"common-names"`
86 }
87 type OpenVPNConfig struct {
88 Path types.FS `json:"path" yaml:"path"`
89 Expression string `json:"expression" yaml:"expression"`
90 Strategy OpenVPNConfigStrategy `json:"strategy" yaml:"strategy"`
91 Enabled bool `json:"enabled" yaml:"enabled"`
92 IgnoreNotFound bool `json:"ignore-not-found" yaml:"ignore-not-found"`
93 }
94
95 type OpenVPNCommonName struct {
96 EnvironmentVariableName string `json:"environment-variable-name" yaml:"environment-variable-name"`
97 }
98
99 type OAuth2 struct {
100 Endpoints OAuth2Endpoints `json:"endpoint" yaml:"endpoint"`
101 Issuer types.URL `json:"issuer" yaml:"issuer"`
102 Client OAuth2Client `json:"client" yaml:"client"`
103 OpenVPNUsername string `json:"openvpn-username" yaml:"openvpn-username"`
104 GroupsClaim string `json:"groups-claim" yaml:"groups-claim"`
105 AuthorizeParams string `json:"authorize-params" yaml:"authorize-params"`
106 Provider string `json:"provider" yaml:"provider"`
107 Scopes types.StringSlice `json:"scopes" yaml:"scopes"`
108 Validate OAuth2Validate `json:"validate" yaml:"validate"`
109 Refresh OAuth2Refresh `json:"refresh" yaml:"refresh"`
110 RefreshNonce OAuth2RefreshNonce `json:"refresh-nonce" yaml:"refresh-nonce"`
111 AuthStyle OAuth2AuthStyle `json:"auth-style" yaml:"auth-style"`
112 Nonce bool `json:"nonce" yaml:"nonce"`
113 PKCE bool `json:"pkce" yaml:"pkce"`
114 UserInfo bool `json:"user-info" yaml:"user-info"`
115 }
116
117 type OAuth2Client struct {
118 ID string `json:"id" yaml:"id"`
119 Secret types.Secret `json:"secret" yaml:"secret"`
120 PrivateKey types.Secret `json:"private-key" yaml:"private-key"`
121 PrivateKeyID string `json:"private-key-id" yaml:"private-key-id"`
122 }
123
124 type OAuth2Endpoints struct {
125 Discovery types.URL `json:"discovery" yaml:"discovery"`
126 Auth types.URL `json:"auth" yaml:"auth"`
127 Token types.URL `json:"token" yaml:"token"`
128 }
129
130 type OAuth2Validate struct {
131 Expression string `json:"expression" yaml:"expression"`
132 Groups types.StringSlice `json:"groups" yaml:"groups"`
133 }
134
135 type OAuth2Refresh struct {
136 Secret types.Secret `json:"secret" yaml:"secret"`
137 Expires time.Duration `json:"expires" yaml:"expires"`
138 Enabled bool `json:"enabled" yaml:"enabled"`
139 UseSessionID bool `json:"use-session-id" yaml:"use-session-id"`
140 ValidateUser bool `json:"validate-user" yaml:"validate-user"`
141 }
142
143 type OpenVPNPassthrough struct {
144 Address types.URL `json:"address" yaml:"address"`
145 Password types.Secret `json:"password" yaml:"password"`
146 SocketGroup string `json:"socket-group" yaml:"socket-group"`
147 SocketMode types.FileMode `json:"socket-mode" yaml:"socket-mode"`
148 Enabled bool `json:"enabled" yaml:"enabled"`
149 }
150
151 type Debug struct {
152 Listen string `json:"listen" yaml:"listen"`
153 Pprof bool `json:"pprof" yaml:"pprof"`
154 }
155
156 type OpenVPNConfigStrategy int
157
158 const (
159 OpenVPNConfigStrategyMerge OpenVPNConfigStrategy = iota
160 OpenVPNConfigStrategyUserSelector
161 )
162
163 // String returns the string representation of the client config strategy.
164 //
165 //goland:noinspection GoMixedReceiverTypes
166 func (s OpenVPNConfigStrategy) String() string {
167 text, err := s.MarshalText()
168 if err != nil {
169 panic(err)
170 }
171
172 return string(text)
173 }
174
175 // MarshalText implements the [encoding.TextMarshaler] interface.
176 //
177 //goland:noinspection GoMixedReceiverTypes
178 func (s OpenVPNConfigStrategy) MarshalText() ([]byte, error) {
179 switch s {
180 case OpenVPNConfigStrategyMerge:
181 return []byte("merge"), nil
182 case OpenVPNConfigStrategyUserSelector:
183 return []byte("user-selector"), nil
184 default:
185 return nil, fmt.Errorf("unknown client config strategy %d", s)
186 }
187 }
188
189 // UnmarshalText implements the [encoding.TextUnmarshaler] interface.
190 //
191 //goland:noinspection GoMixedReceiverTypes
192 func (s *OpenVPNConfigStrategy) UnmarshalText(text []byte) error {
193 strategy := strings.ToLower(string(text))
194 switch strategy {
195 case "", "merge":
196 *s = OpenVPNConfigStrategyMerge
197 case "user-selector":
198 *s = OpenVPNConfigStrategyUserSelector
199 default:
200 return fmt.Errorf("invalid value %s", strategy)
201 }
202
203 return nil
204 }
205
206 type OAuth2AuthStyle oauth2.AuthStyle
207
208 // String returns the string representation of the auth style.
209 //
210 //goland:noinspection GoMixedReceiverTypes
211 func (s OAuth2AuthStyle) String() string {
212 text, err := s.MarshalText()
213 if err != nil {
214 panic(err)
215 }
216
217 return string(text)
218 }
219
220 // AuthStyle converts the wrapper type to [oauth2.AuthStyle].
221 //
222 //goland:noinspection GoMixedReceiverTypes
223 func (s OAuth2AuthStyle) AuthStyle() oauth2.AuthStyle {
224 return oauth2.AuthStyle(s)
225 }
226
227 // MarshalText implements the [encoding.TextMarshaler] interface.
228 //
229 //goland:noinspection GoMixedReceiverTypes
230 func (s OAuth2AuthStyle) MarshalText() ([]byte, error) {
231 switch s {
232 case OAuth2AuthStyle(oauth2.AuthStyleAutoDetect):
233 return []byte("auto"), nil
234 case OAuth2AuthStyle(oauth2.AuthStyleInParams):
235 return []byte("params"), nil
236 case OAuth2AuthStyle(oauth2.AuthStyleInHeader):
237 return []byte("header"), nil
238 default:
239 return nil, fmt.Errorf("unknown auth-style: %d", s)
240 }
241 }
242
243 // UnmarshalText implements the [encoding.TextUnmarshaler] interface.
244 //
245 //goland:noinspection GoMixedReceiverTypes
246 func (s *OAuth2AuthStyle) UnmarshalText(text []byte) error {
247 switch strings.ToLower(string(text)) {
248 case "auto":
249 *s = OAuth2AuthStyle(oauth2.AuthStyleAutoDetect)
250 case "params":
251 *s = OAuth2AuthStyle(oauth2.AuthStyleInParams)
252 case "header":
253 *s = OAuth2AuthStyle(oauth2.AuthStyleInHeader)
254 default:
255 return fmt.Errorf("unknown auth-style: %s", text)
256 }
257
258 return nil
259 }
260
261 type OAuth2RefreshNonce int
262
263 const (
264 OAuth2RefreshNonceAuto OAuth2RefreshNonce = iota // Try the original nonce, then retry without it if nonce validation fails.
265 OAuth2RefreshNonceEmpty // Always use an empty nonce for refresh requests.
266 OAuth2RefreshNonceEqual // Use the same nonce as the initial authentication.
267 )
268
269 // String returns the string representation of the refresh nonce mode.
270 //
271 //goland:noinspection GoMixedReceiverTypes
272 func (s OAuth2RefreshNonce) String() string {
273 text, err := s.MarshalText()
274 if err != nil {
275 panic(err)
276 }
277
278 return string(text)
279 }
280
281 // MarshalText implements the [encoding.TextMarshaler] interface.
282 //
283 //goland:noinspection GoMixedReceiverTypes
284 func (s OAuth2RefreshNonce) MarshalText() ([]byte, error) {
285 switch s {
286 case OAuth2RefreshNonceAuto:
287 return []byte("auto"), nil
288 case OAuth2RefreshNonceEmpty:
289 return []byte("empty"), nil
290 case OAuth2RefreshNonceEqual:
291 return []byte("equal"), nil
292 default:
293 return nil, fmt.Errorf("unknown refresh-nonce %d", s)
294 }
295 }
296
297 // UnmarshalText implements the [encoding.TextUnmarshaler] interface.
298 //
299 //goland:noinspection GoMixedReceiverTypes
300 func (s *OAuth2RefreshNonce) UnmarshalText(text []byte) error {
301 config := strings.ToLower(string(text))
302 switch config {
303 case "auto":
304 *s = OAuth2RefreshNonceAuto
305 case "empty":
306 *s = OAuth2RefreshNonceEmpty
307 case "equal":
308 *s = OAuth2RefreshNonceEqual
309 default:
310 return fmt.Errorf("invalid value %s", config)
311 }
312
313 return nil
314 }
315
316 //goland:noinspection GoMixedReceiverTypes
317 func (c Config) String() string {
318 jsonString, err := json.Marshal(c)
319 if err != nil {
320 panic(err)
321 }
322
323 return string(jsonString)
324 }
325
326 func (h HTTP) MarshalJSON() ([]byte, error) {
327 type Alias HTTP
328
329 //nolint:wrapcheck
330 return json.Marshal(&struct {
331 Alias
332 }{
333 Alias: Alias(h),
334 })
335 }