|
| 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:** Although openvpn-auth-oauth2 theoretically doesn't require client-side authentication, the OpenVPN client expects it. |
| 14 | + |
| 15 | +You have two options: |
| 16 | + |
| 17 | +1. **Interactive Mode**: Use `auth-user-pass` without credentials, prompting the user for input: |
| 18 | + ``` |
| 19 | + auth-user-pass |
| 20 | + ``` |
| 21 | + |
| 22 | +2. **Inline Mode**: Define dummy credentials inline to prevent prompting (recommended for SSO-only authentication): |
| 23 | + ``` |
| 24 | + <auth-user-pass> |
| 25 | + username |
| 26 | + password |
| 27 | + </auth-user-pass> |
| 28 | + ``` |
| 29 | + |
| 30 | + Note: The username/password can be any dummy value as they won't be validated by openvpn-auth-oauth2 or OpenVPN itself during the OAuth2 flow. |
| 31 | + |
| 32 | +**Upstream Issue:** [`OpenVPN/openvpn` #501](https://github.com/OpenVPN/openvpn/issues/501) (Please react with :+1: if you're affected.) |
| 33 | + |
| 34 | +### Error: "No client-side authentication method is specified" |
| 35 | + |
| 36 | +If you encounter this error, ensure that `auth-user-pass` is configured in your client configuration as described above. |
| 37 | + |
| 38 | +## Using `username-as-common-name` on OpenVPN Server |
| 39 | + |
| 40 | +When setting up `username-as-common-name` on the OpenVPN server, you **must** also configure `openvpn.common-name.environment-variable-name` to `username`: |
| 41 | + |
| 42 | +```bash |
| 43 | +--openvpn.common-name.environment-variable-name=username |
| 44 | +``` |
| 45 | + |
| 46 | +Or via environment variable: |
| 47 | + |
| 48 | +```dotenv |
| 49 | +CONFIG_OPENVPN_COMMON__NAME_ENVIRONMENT__VARIABLE__NAME=username |
| 50 | +``` |
| 51 | + |
| 52 | +### Why This Configuration Is Required |
| 53 | + |
| 54 | +This configuration is essential because `username-as-common-name` functions **post-authentication**. By aligning the environment variable name with `username`, you ensure smooth operation. |
| 55 | + |
| 56 | +**Important Note:** During authentication, it's expected that the common-name is not the value of the username. This may be misleading because after authentication, the common name has the correct value in OpenVPN logs. |
| 57 | + |
| 58 | +**Upstream Issue:** [`OpenVPN/openvpn` #498](https://github.com/OpenVPN/openvpn/issues/498#issuecomment-1939194149) |
| 59 | + |
| 60 | +## Passing Usernames from OAuth2 Provider to OpenVPN |
| 61 | + |
| 62 | +### Default Behavior |
| 63 | + |
| 64 | +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. |
| 65 | + |
| 66 | +**Limitation:** The IP persistence file or statistics in OpenVPN may contain empty usernames when using the default configuration. |
| 67 | + |
| 68 | +**Upstream Issue:** For native OpenVPN support, please up-vote the feature request on GitHub: [`OpenVPN/openvpn` #299](https://github.com/OpenVPN/openvpn/issues/299) |
| 69 | + |
| 70 | +### Using `openvpn.override-username` (Recommended) |
| 71 | + |
| 72 | +**Requires OpenVPN Server 2.7+** |
| 73 | + |
| 74 | +The `openvpn.override-username` configuration option enables passing the username from OAuth2 token claims to OpenVPN using the `override-username` command. This allows real usernames to appear in OpenVPN statistics and logs. |
| 75 | + |
| 76 | +#### Configuration |
| 77 | + |
| 78 | +Enable this feature using: |
| 79 | + |
| 80 | +```bash |
| 81 | +--openvpn.override-username |
| 82 | +``` |
| 83 | + |
| 84 | +Or via environment variable: |
| 85 | + |
| 86 | +```bash |
| 87 | +CONFIG_OPENVPN_OVERRIDE__USERNAME=true |
| 88 | +``` |
| 89 | + |
| 90 | +#### Username Source |
| 91 | + |
| 92 | +The username is extracted from the OAuth2 ID token using one of these configurations (in order of precedence): |
| 93 | + |
| 94 | +1. **`oauth2.openvpn-username-claim`** - Extract username from a specific token claim (default: `preferred_username`) |
| 95 | +2. **`oauth2.openvpn-username-cel`** - Use a CEL expression to extract or transform the username from token claims |
| 96 | + |
| 97 | +Example configurations: |
| 98 | + |
| 99 | +```bash |
| 100 | +# Use a specific claim |
| 101 | +--oauth2.openvpn-username-claim=email |
| 102 | + |
| 103 | +# Use CEL expression for complex transformations |
| 104 | +--oauth2.openvpn-username-cel='oauth2TokenClaims.email.split("@")[0]' |
| 105 | +``` |
| 106 | + |
| 107 | +For more details on CEL expressions, see the [Client token values](Client%20token%20validation.md#cel-language-features) documentation. |
| 108 | + |
| 109 | +#### Important Limitations |
| 110 | + |
| 111 | +⚠️ **OpenVPN Client-Config-Dir Compatibility:** |
| 112 | + |
| 113 | +When `openvpn.override-username` is enabled, OpenVPN's native `client-config-dir` functionality **will not work** because the username is set **after** client configs are read. |
| 114 | + |
| 115 | +**Workaround:** Use openvpn-auth-oauth2's built-in [Client specific configuration](Client%20specific%20configuration.md) feature instead, which: |
| 116 | +- Works seamlessly with `openvpn.override-username` |
| 117 | +- Uses token claims to lookup configuration files |
| 118 | +- Provides additional features like profile selection UI |
| 119 | + |
| 120 | +For more details, see the OpenVPN man page regarding `override-username` limitations. |
| 121 | + |
| 122 | +### Alternative: `openvpn.auth-token-user` |
| 123 | + |
| 124 | +If you're using OpenVPN Server < 2.7 or cannot use `override-username`, the `openvpn.auth-token-user` option provides limited username support: |
| 125 | + |
| 126 | +```bash |
| 127 | +--openvpn.auth-token-user |
| 128 | +``` |
| 129 | + |
| 130 | +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`. |
0 commit comments