| 1 |
# OpenVPN Username |
| 2 |
|
| 3 |
## Overview |
| 4 |
|
| 5 |
This document covers various aspects of username handling in openvpn-auth-oauth2, including how to pass usernames from OAuth2 providers to OpenVPN, client-side authentication requirements, and configuration options. |
| 6 |
|
| 7 |
## Client-Side Requirements |
| 8 |
|
| 9 |
### Mandatory `auth-user-pass` Configuration |
| 10 |
|
| 11 |
To use username functionality with openvpn-auth-oauth2, the OpenVPN client **must** have `auth-user-pass` configured. This is a mandatory requirement for the authentication flow to work properly. |
| 12 |
|
| 13 |
> [!IMPORTANT] |
| 14 |
> Although openvpn-auth-oauth2 does not validate a client-side password, the |
| 15 |
> OpenVPN client expects `auth-user-pass` for this authentication flow. |
| 16 |
|
| 17 |
You have two options: |
| 18 |
|
| 19 |
1. **Interactive Mode**: Use `auth-user-pass` without credentials, prompting the user for input: |
| 20 |
``` |
| 21 |
auth-user-pass |
| 22 |
``` |
| 23 |
|
| 24 |
2. **Inline Mode**: Define dummy credentials inline to prevent prompting (recommended for SSO-only authentication): |
| 25 |
``` |
| 26 |
<auth-user-pass> |
| 27 |
username |
| 28 |
password |
| 29 |
</auth-user-pass> |
| 30 |
``` |
| 31 |
|
| 32 |
The inline username and password can contain placeholder values. Neither |
| 33 |
openvpn-auth-oauth2 nor OpenVPN validates them during the OAuth2 flow. |
| 34 |
|
| 35 |
**Upstream Issue:** [`OpenVPN/openvpn` #501](https://github.com/OpenVPN/openvpn/issues/501) (Please react with :+1: if you're affected.) |
| 36 |
|
| 37 |
### Error: "No client-side authentication method is specified" |
| 38 |
|
| 39 |
If you encounter this error, ensure that `auth-user-pass` is configured in your client configuration as described above. |
| 40 |
|
| 41 |
## Using `username-as-common-name` on OpenVPN Server |
| 42 |
|
| 43 |
When setting up `username-as-common-name` on the OpenVPN server, you **must** also configure `openvpn.common-name.environment-variable-name` to `username`: |
| 44 |
|
| 45 |
```bash |
| 46 |
--openvpn.common-name.environment-variable-name=username |
| 47 |
``` |
| 48 |
|
| 49 |
Or via environment variable: |
| 50 |
|
| 51 |
```dotenv |
| 52 |
OPENVPN_AUTH_OAUTH2_OPENVPN_COMMON_NAME_ENVIRONMENT_VARIABLE_NAME=username |
| 53 |
``` |
| 54 |
|
| 55 |
### Why This Configuration Is Required |
| 56 |
|
| 57 |
This configuration is essential because `username-as-common-name` functions **post-authentication**. By aligning the environment variable name with `username`, you ensure smooth operation. |
| 58 |
|
| 59 |
> [!NOTE] |
| 60 |
> During authentication, the common name does not yet contain the username. |
| 61 |
> OpenVPN updates it after authentication, so later log entries show the |
| 62 |
> expected value. |
| 63 |
|
| 64 |
**Upstream Issue:** [`OpenVPN/openvpn` #498](https://github.com/OpenVPN/openvpn/issues/498#issuecomment-1939194149) |
| 65 |
|
| 66 |
## Passing Usernames from OAuth2 Provider to OpenVPN |
| 67 |
|
| 68 |
### Default Behavior |
| 69 |
|
| 70 |
By default, openvpn-auth-oauth2 does not pass the username from the OAuth2 provider to OpenVPN. This limitation is due to OpenVPN's authentication interface design, which does not provide a native mechanism to set the username post-authentication. |
| 71 |
|
| 72 |
**Limitation:** The IP persistence file or statistics in OpenVPN may contain empty usernames when using the default configuration. |
| 73 |
|
| 74 |
**Upstream Issue:** For native OpenVPN support, please up-vote the feature request on GitHub: [`OpenVPN/openvpn` #299](https://github.com/OpenVPN/openvpn/issues/299) |
| 75 |
|
| 76 |
### Using `openvpn.override-username` (Recommended) |
| 77 |
|
| 78 |
**Requires OpenVPN Server 2.7+** |
| 79 |
|
| 80 |
The `openvpn.override-username` configuration option passes the username from |
| 81 |
the normalized OAuth2 identity to OpenVPN using the `override-username` command. |
| 82 |
This allows real usernames to appear in OpenVPN statistics and logs. |
| 83 |
|
| 84 |
#### Configuration |
| 85 |
|
| 86 |
Enable this feature using: |
| 87 |
|
| 88 |
```bash |
| 89 |
--openvpn.override-username |
| 90 |
``` |
| 91 |
|
| 92 |
Or via environment variable: |
| 93 |
|
| 94 |
```bash |
| 95 |
OPENVPN_AUTH_OAUTH2_OPENVPN_OVERRIDE_USERNAME=true |
| 96 |
``` |
| 97 |
|
| 98 |
#### Username source |
| 99 |
|
| 100 |
The username is resolved from the normalized identity using |
| 101 |
`oauth2.openvpn-username`. The value is a CEL expression that must evaluate to |
| 102 |
a string. |
| 103 |
|
| 104 |
By default, the expression is `user.username`. Before this expression runs, |
| 105 |
`user.username` contains the provider's username candidate: |
| 106 |
|
| 107 |
- the `preferred_username` value from UserInfo or an OIDC ID token; |
| 108 |
- the GitHub login for the GitHub provider. |
| 109 |
|
| 110 |
The same expression is applied during interactive authentication, UserInfo |
| 111 |
authentication, and refresh validation. If the setting is empty, or if the |
| 112 |
expression returns an empty string, the OpenVPN common name is used. |
| 113 |
|
| 114 |
Example configurations: |
| 115 |
|
| 116 |
```bash |
| 117 |
# Use the normalized email address |
| 118 |
--oauth2.openvpn-username='user.email' |
| 119 |
|
| 120 |
# Use a provider-specific raw claim |
| 121 |
--oauth2.openvpn-username='string(token.claims.employee_id)' |
| 122 |
|
| 123 |
# Transform normalized identity data |
| 124 |
--oauth2.openvpn-username='user.email.split("@")[0]' |
| 125 |
``` |
| 126 |
|
| 127 |
The complete `auth`, `openvpn`, `token`, and `user` context is available. For |
| 128 |
more details, see [CEL Language Features](CEL%20Language%20Features.md). |
| 129 |
|
| 130 |
#### Important limitations |
| 131 |
|
| 132 |
> [!WARNING] |
| 133 |
> When `openvpn.override-username` is enabled, OpenVPN's native |
| 134 |
> `client-config-dir` does not use the resolved OIDC username because the |
| 135 |
> username is replaced after OpenVPN reads its client configuration. |
| 136 |
|
| 137 |
Use openvpn-auth-oauth2's built-in |
| 138 |
[Client-specific configuration](Client%20specific%20configuration.md) instead. |
| 139 |
It: |
| 140 |
|
| 141 |
- works with `openvpn.override-username`; |
| 142 |
- uses `openvpn.client-config.expression` to resolve configuration files from |
| 143 |
the normalized identity and raw token claims; and |
| 144 |
- can merge several configuration files or show a profile selector with |
| 145 |
`openvpn.client-config.strategy: user-selector`. |
| 146 |
|
| 147 |
For more details, see the OpenVPN man page regarding `override-username` limitations. |
| 148 |
|
| 149 |
### Limiting a Username to One Active Session |
| 150 |
|
| 151 |
Enable `openvpn.enforce-unique-user` to replace active sessions based on the |
| 152 |
value resolved by `oauth2.openvpn-username`. Before accepting a client, |
| 153 |
openvpn-auth-oauth2 compares that value with the exact `Username` field in |
| 154 |
OpenVPN's `status 3` output. |
| 155 |
|
| 156 |
> [!IMPORTANT] |
| 157 |
> This setting always requires a direct OpenVPN management-interface |
| 158 |
> connection. It is not compatible with the stable |
| 159 |
> [OpenVPN Plugin](OpenVPN%20Plugin), because the plugin does not expose the |
| 160 |
> `status 3` and `client-kill` commands used by this check. |
| 161 |
|
| 162 |
#### Enforce an existing OpenVPN username or common name |
| 163 |
|
| 164 |
This mode does not require OpenVPN 2.7 or `openvpn.override-username`. The value |
| 165 |
resolved by `oauth2.openvpn-username` must already match the `Username` field |
| 166 |
reported by OpenVPN. For example, an administrator can use the OpenVPN common |
| 167 |
name as the resolved username and require it to match a trusted OIDC subject: |
| 168 |
|
| 169 |
```yaml |
| 170 |
oauth2: |
| 171 |
openvpn-username: openvpn.commonName |
| 172 |
validate: |
| 173 |
expression: openvpn.commonName.lowerAscii() == user.subject.lowerAscii() |
| 174 |
|
| 175 |
openvpn: |
| 176 |
enforce-unique-user: true |
| 177 |
``` |
| 178 |
|
| 179 |
> [!WARNING] |
| 180 |
> Do not trust a client-controlled common name by itself. Bind it to a trusted |
| 181 |
> OIDC subject or claim with `oauth2.validate.expression`, and ensure the same |
| 182 |
> value appears in OpenVPN's `Username` status field. The correct expression |
| 183 |
> depends on the identity provider and OpenVPN configuration. |
| 184 |
|
| 185 |
When using OpenVPN's `username-as-common-name`, also set |
| 186 |
`openvpn.common-name.environment-variable-name: username` as described in |
| 187 |
[Using `username-as-common-name` on OpenVPN Server](#using-username-as-common-name-on-openvpn-server). |
| 188 |
See [issue #1117](https://github.com/jkroepke/openvpn-auth-oauth2/issues/1117) |
| 189 |
for the background to this mode. |
| 190 |
|
| 191 |
#### Enforce the resolved OIDC username |
| 192 |
|
| 193 |
When OpenVPN's existing username is a placeholder or several clients share a |
| 194 |
profile, use `openvpn.override-username` to write the resolved OIDC username to |
| 195 |
OpenVPN. This mode requires OpenVPN 2.7 or later: |
| 196 |
|
| 197 |
```yaml |
| 198 |
openvpn: |
| 199 |
override-username: true |
| 200 |
enforce-unique-user: true |
| 201 |
``` |
| 202 |
|
| 203 |
OpenVPN checks duplicate common names before `override-username` replaces the |
| 204 |
username. Consequently, OpenVPN's native common-name check alone cannot prevent |
| 205 |
two clients using the same shared profile from authenticating as the same OIDC |
| 206 |
user. |
| 207 |
|
| 208 |
Before accepting a client, openvpn-auth-oauth2 requests `status 3`, finds every |
| 209 |
active client with an exactly matching `Username` field, and sends |
| 210 |
`client-kill <CID> HALT` for each match except the CID currently |
| 211 |
authenticating. `HALT` tells the displaced client to clear cached |
| 212 |
authentication and exit instead of automatically reconnecting with a cached |
| 213 |
authentication token. The status lookup and client acceptance are serialized |
| 214 |
so two concurrent logins cannot both pass the check. A status, parsing, or kill |
| 215 |
error prevents the new client from being accepted. If a matching CID |
| 216 |
disconnected after the status lookup, authentication continues because the old |
| 217 |
session is already gone. |
| 218 |
|
| 219 |
> [!NOTE] |
| 220 |
> `HALT` does not revoke an `auth-gen-token` on the server. It prevents the |
| 221 |
> displaced client from immediately reusing an in-memory token; a later manual |
| 222 |
> connection starts a new authentication flow. |
| 223 |
|
| 224 |
The check applies to interactive authentication, silent reauthentication, and |
| 225 |
non-interactive reconnects. When `oauth2.refresh.validate-user=false`, the |
| 226 |
resolved username is retained in encrypted refresh state so these paths use the |
| 227 |
same identity. The current CID is excluded, so a TLS reauthentication does not |
| 228 |
terminate its own session. |
| 229 |
|
| 230 |
> [!NOTE] |
| 231 |
> This setting limits sessions only on the OpenVPN server connected to this |
| 232 |
> `openvpn-auth-oauth2` process. It does not coordinate sessions across |
| 233 |
> separate OpenVPN servers. |
| 234 |
|
| 235 |
### Alternative: `openvpn.auth-token-user` |
| 236 |
|
| 237 |
If you're using OpenVPN Server < 2.7 or cannot use `override-username`, the `openvpn.auth-token-user` option provides limited username support: |
| 238 |
|
| 239 |
```bash |
| 240 |
--openvpn.auth-token-user |
| 241 |
``` |
| 242 |
|
| 243 |
This option uses the `auth-token-user` push command to send a base64-encoded username, but only when the client username is empty. This has more limitations compared to `override-username`. |