Debian Codegraph @

internal/oauth2/client_config.go

raw
1 package oauth2
2
3 import (
4 "errors"
5 "fmt"
6 "io/fs"
7 "reflect"
8 "strings"
9 "unicode"
10
11 "cel.dev/cel-go/cel"
12 "github.com/jkroepke/openvpn-auth-oauth2/v2/internal/oauth2/idtoken"
13 "github.com/jkroepke/openvpn-auth-oauth2/v2/internal/oauth2/types"
14 "github.com/jkroepke/openvpn-auth-oauth2/v2/internal/state"
15 )
16
17 // initializeClientConfigResolver compiles the optional CEL expression used to resolve client configs.
18 func (c *Client) initializeClientConfigResolver() error {
19 if c.conf.OpenVPN.ClientConfig.Expression == "" {
20 return nil
21 }
22
23 program, err := compileCELProgram(c.conf.OpenVPN.ClientConfig.Expression, cel.ListType(cel.StringType))
24 if err != nil {
25 return fmt.Errorf("failed to initialize client config CEL expression: %w", err)
26 }
27
28 c.configsCELPrg = program
29
30 return nil
31 }
32
33 // ResolveClientConfigNames returns the ordered client config names for an authenticated user.
34 func (c *Client) ResolveClientConfigNames(
35 authMode CELAuthMode,
36 session state.State,
37 tokens *idtoken.IDToken,
38 user types.UserInfo,
39 ) ([]string, error) {
40 if !c.conf.OpenVPN.ClientConfig.Enabled {
41 return nil, nil
42 }
43
44 if c.conf.OpenVPN.ClientConfig.Expression == "" {
45 return nil, errors.New("client config expression is not configured")
46 }
47
48 return c.resolveClientConfigNamesCEL(authMode, session, tokens, user)
49 }
50
51 func (c *Client) resolveClientConfigNamesCEL(
52 authMode CELAuthMode,
53 session state.State,
54 tokens *idtoken.IDToken,
55 user types.UserInfo,
56 ) ([]string, error) {
57 if c.configsCELPrg == nil {
58 return nil, errors.New("client config CEL expression is not initialized")
59 }
60
61 out, _, err := c.configsCELPrg.Eval(newCELActivation(authMode, session, tokens, user))
62 if err != nil {
63 return nil, fmt.Errorf("failed to evaluate CEL expression for client configs: %w", err)
64 }
65
66 nativeValue, err := out.ConvertToNative(reflect.TypeFor[[]string]())
67 if err != nil {
68 return nil, fmt.Errorf("%w: CEL expression for client configs did not evaluate to a string list", types.ErrInvalidClaimType)
69 }
70
71 names, ok := nativeValue.([]string)
72 if !ok {
73 return nil, fmt.Errorf("%w: CEL expression for client configs did not evaluate to a string list: %T", types.ErrInvalidClaimType, nativeValue)
74 }
75
76 return validateClientConfigNames(names)
77 }
78
79 func validateClientConfigNames(names []string) ([]string, error) {
80 for _, name := range names {
81 if name == "" {
82 return nil, fmt.Errorf("%w: client config name is empty", types.ErrInvalidClaimType)
83 }
84
85 clientConfigPath := name + ".conf"
86 if !fs.ValidPath(clientConfigPath) {
87 return nil, fmt.Errorf("%w: invalid client config path %q", types.ErrInvalidClaimType, clientConfigPath)
88 }
89
90 if clientConfigNameUnsafe(name) {
91 return nil, fmt.Errorf("%w: unsafe client config name %q", types.ErrInvalidClaimType, name)
92 }
93 }
94
95 return names, nil
96 }
97
98 func clientConfigNameUnsafe(name string) bool {
99 if strings.ContainsAny(name, `<>"'&`+"`") {
100 return true
101 }
102
103 return strings.ContainsFunc(name, unicode.IsControl)
104 }