Debian Codegraph @

docs/Client token validation.md

raw
1 # Client Token Validation with CEL
2
3 openvpn-auth-oauth2 supports identity validation using the [Common Expression Language (CEL)](https://github.com/google/cel-spec).
4 CEL rules can evaluate the normalized user, raw OAuth2 ID token claims, and the
5 OpenVPN session context.
6
7 ## Overview
8
9 CEL validation provides a flexible way to enforce security policies by allowing you to write custom expressions that evaluate to `true` or `false`. This validation happens:
10
11 1. **During interactive authentication** - After the OAuth2 authentication flow completes but before the OpenVPN connection is established
12 2. **During token refresh** - When an existing OpenVPN session is refreshed using a refresh token (non-interactive authentication)
13
14 This ensures that access policies are continuously enforced throughout the lifecycle of the VPN connection, not just during initial authentication.
15
16 ## Configuration
17
18 To enable CEL validation, configure the `oauth2.validate.expression` property in your configuration file:
19
20 ### YAML Configuration
21
22 ```yaml
23 oauth2:
24 validate:
25 expression: 'openvpn.commonName == user.username'
26 ```
27
28 ### Environment Variable
29
30 ```bash
31 OPENVPN_AUTH_OAUTH2_OAUTH2_VALIDATE_EXPRESSION='openvpn.commonName == user.username'
32 ```
33
34 > [!IMPORTANT]
35 > CEL validation is performed **both during initial OAuth2 authentication and during token refresh**. This means your validation rules will be continuously enforced throughout the entire lifecycle of a VPN session. Make sure your expressions account for both scenarios using `auth.mode` if needed.
36
37 ## Available Variables
38
39 The following variables are available in your CEL expressions:
40
41 | Variable | Type | Description |
42 |----------|------|-------------|
43 | `auth.mode` | `string` | The authentication mode: `"interactive"` (initial OAuth2 login) or `"non-interactive"` (token refresh) |
44 | `openvpn.sessionState` | `string` | The OpenVPN session state (e.g., `""`, `"Empty"`, `"Initial"`, `"Authenticated"`, `"Expired"`, `"Invalid"`, `"AuthenticatedEmptyUser"`, `"ExpiredEmptyUser"`) |
45 | `openvpn.commonName` | `string` | The common name (CN) of the OpenVPN client certificate |
46 | `openvpn.ip` | `string` | The IP address of the OpenVPN client |
47 | `token.ip` | `string` | The IP address claim from the OAuth2 ID token |
48 | `token.claims` | `map<string, dynamic>` | All claims from the OAuth2 ID token |
49 | `user.subject` | `string` | The normalized provider subject |
50 | `user.email` | `string` | The normalized email address |
51 | `user.username` | `string` | The final username after `oauth2.openvpn-username` and common-name fallback |
52 | `user.groups` | `list<string>` | The normalized groups |
53 | `user.roles` | `list<string>` | The normalized roles |
54
55 The `auth`, `openvpn`, `token`, and `user` variables are typed objects. Unknown
56 fields and incompatible operations are rejected when the configuration loads.
57 Only `token.claims` remains a dynamic map because its keys depend on the OAuth2
58 provider.
59
60 Prefer `user.*` for rules that should work with generic OIDC, UserInfo, GitHub,
61 and refresh authentication. Use `token.claims.*` for provider-specific claims.
62 See [CEL Language Features](CEL%20Language%20Features.md) for the normalization
63 rules and the context shared by all CEL settings.
64
65 ## Expression Requirements
66
67 - The CEL expression **must evaluate to a boolean** (`true` or `false`)
68 - Syntax errors, unknown fields, and statically incompatible result types prevent startup
69 - If the expression evaluates to `true`, the user is granted access
70 - If the expression evaluates to `false`, the user is denied access
71 - If evaluation fails during authentication (e.g., when accessing a non-existent claim), the user is denied access
72
73 ## Safe Claim Access
74
75 Use the `has()` function to safely check for claim existence before accessing it:
76
77 ```yaml
78 oauth2:
79 validate:
80 expression: |
81 has(token.claims.department) &&
82 token.claims.department == 'engineering'
83 ```
84
85 > [!IMPORTANT]
86 > If you try to access a claim that doesn't exist without using `has()`, the expression evaluation will fail, and the user will be denied access.
87
88 ## Examples
89
90 ### Basic Username Validation
91
92 Ensure the OpenVPN common name matches the OAuth2 username claim:
93
94 ```yaml
95 oauth2:
96 validate:
97 expression: 'openvpn.commonName == user.username'
98 ```
99
100 ### Email Domain Validation
101
102 Only allow users with email addresses from specific domains:
103
104 ```yaml
105 oauth2:
106 validate:
107 expression: |
108 user.email.endsWith('@example.com')
109 ```
110
111 ### Multiple Condition Validation
112
113 Combine multiple conditions with logical operators:
114
115 ```yaml
116 oauth2:
117 validate:
118 expression: |
119 openvpn.commonName == user.username &&
120 has(token.claims.email_verified) &&
121 token.claims.email_verified == true
122 ```
123
124 ### Group-Based Validation
125
126 Allow access only if the user belongs to specific groups:
127
128 ```yaml
129 oauth2:
130 validate:
131 expression: |
132 'vpn-users' in user.groups || 'administrators' in user.groups
133 ```
134
135 ### IP Address Claim Validation
136
137 Validate that the VPN client IP matches the IP address claim from the token:
138
139 ```yaml
140 oauth2:
141 validate:
142 expression: 'openvpn.ip == token.ip'
143 ```
144
145 ### Case-Insensitive Username Validation
146
147 Compare usernames in a case-insensitive manner using the `lowerAscii()` function:
148
149 ```yaml
150 oauth2:
151 validate:
152 expression: |
153 openvpn.commonName.lowerAscii() == user.username.lowerAscii()
154 ```
155
156 > [!IMPORTANT]
157 > When accessing claims from `token.claims` that you want to use with string functions, you may need to cast them to string using `string()` since claims are stored as dynamic types.
158
159 ### Complex Custom Logic
160
161 Combine multiple conditions for sophisticated validation rules:
162
163 ```yaml
164 oauth2:
165 validate:
166 expression: |
167 openvpn.commonName == token.claims.sub &&
168 (
169 (has(token.claims.role) && token.claims.role == 'admin') ||
170 (has(token.claims.vpn_access) && token.claims.vpn_access == true)
171 ) &&
172 (!has(token.claims.account_locked) || token.claims.account_locked == false)
173 ```
174
175 ### Email Prefix Validation
176
177 Extract and validate the prefix of an email address:
178
179 ```yaml
180 oauth2:
181 validate:
182 expression: |
183 user.email.split('@')[0] == openvpn.commonName
184 ```
185
186 ### Username Format Validation
187
188 Validate that a username contains only allowed characters using regular expression:
189
190 ```yaml
191 oauth2:
192 validate:
193 expression: |
194 user.username.matches('^[a-zA-Z0-9._-]+$')
195 ```
196
197 ### String Length Validation
198
199 Ensure usernames meet minimum length requirements:
200
201 ```yaml
202 oauth2:
203 validate:
204 expression: |
205 openvpn.commonName.size() >= 3 &&
206 user.username.size() >= 3
207 ```
208
209 ### Domain-Based Routing
210
211 Allow different IP ranges based on email domain:
212
213 ```yaml
214 oauth2:
215 validate:
216 expression: |
217 (
218 (user.email.endsWith('@internal.company.com') &&
219 openvpn.ip.startsWith('10.0.')) ||
220 (user.email.endsWith('@company.com') &&
221 openvpn.ip.startsWith('192.168.'))
222 )
223 ```
224
225 ### Authentication Mode Based Validation
226
227 Apply different validation rules based on whether this is an initial login or a token refresh:
228
229 ```yaml
230 oauth2:
231 validate:
232 expression: |
233 auth.mode == 'interactive' ||
234 (auth.mode == 'non-interactive' && has(token.claims.refresh_allowed) && token.claims.refresh_allowed == true)
235 ```
236
237 ### Session State Validation
238
239 Validate based on the current OpenVPN session state:
240
241 ```yaml
242 oauth2:
243 validate:
244 expression: |
245 openvpn.sessionState in ['Initial', 'Authenticated', 'AuthenticatedEmptyUser'] &&
246 openvpn.commonName == user.username
247 ```
248
249 ### Combined Mode and State Validation
250
251 Combine authentication mode and session state for fine-grained control:
252
253 ```yaml
254 oauth2:
255 validate:
256 expression: |
257 (auth.mode == 'interactive' && openvpn.sessionState == 'Initial') ||
258 (auth.mode == 'non-interactive' && openvpn.sessionState == 'Authenticated')
259 ```