| 1 |
# Non-interactive session refresh |
| 2 |
|
| 3 |
By default, `openvpn-auth-oauth2` doesn't store user tokens. |
| 4 |
This means users must log in interactively each time they authenticate, including during TLS soft-resets |
| 5 |
(triggered by `reneg-sec`). |
| 6 |
|
| 7 |
However, you can change this behavior by enabling the `oauth2.refresh.enabled=true` setting. |
| 8 |
This allows `openvpn-auth-oauth2` to store either the connection ID or SessionID (`oauth2.refresh.use-session-id=true`), |
| 9 |
accepting connections without additional login checks. SessionIDs are available in OpenVPN, if |
| 10 |
`auth-gen-token [lifetime] external-auth` is configured on server-side. |
| 11 |
|
| 12 |
When `oauth2.refresh.validate-user=true` is set, `openvpn-auth-oauth2` |
| 13 |
requests a [refresh token](https://auth0.com/blog/refresh-tokens-what-are-they-and-when-to-use-them/) |
| 14 |
during the initial connection and stores it. |
| 15 |
|
| 16 |
The refresh tokens are stored in an in-memory key-value store and |
| 17 |
[encrypted](Security%20considerations#encryption-of-sensitive-data). |
| 18 |
Each token is tied to either the OpenVPN client ID or OpenVPN session ID. |
| 19 |
|
| 20 |
If a non-interactive login attempt with the refresh token fails against the OIDC provider, |
| 21 |
the system reverts to an interactive login process. |
| 22 |
|
| 23 |
References: |
| 24 |
|
| 25 |
- https://learn.microsoft.com/en-us/entra/identity-platform/v2-oauth2-auth-code-flow#refresh-the-access-token |
| 26 |
- https://curity.io/resources/learn/oauth-refresh/ |
| 27 |
- https://developer.okta.com/docs/guides/refresh-tokens/main/ |
| 28 |
|
| 29 |
# Security Considerations |
| 30 |
|
| 31 |
If `oauth2.refresh.validate-user` is set to `false`, a refresh token is not requested and validated against the OIDC |
| 32 |
provider. |
| 33 |
openvpn-auth-oauth2 assumes the user is still valid and allows the user to connect without further validation. |
| 34 |
|
| 35 |
Example: If the user opens a VPN connection and the user is deleted from the OIDC provider, the connection remains valid |
| 36 |
until the connection lifetime is reached. Restarting the OpenVPN server will invalidate the connection unless |
| 37 |
[non-interactive session refresh across disconnects](#non-interactive-session-refresh-across-disconnects) is configured. |
| 38 |
|
| 39 |
# Non-interactive session refresh across disconnects |
| 40 |
|
| 41 |
To facilitate non-interactive session refresh across disconnects, |
| 42 |
you must enable `auth-gen-token [lifetime] external-auth` on the OpenVPN server. |
| 43 |
|
| 44 |
- `[lifetime]` represents the duration of the token in seconds. |
| 45 |
Once generated, the token's lifetime cannot be extended. |
| 46 |
It must consider as maximum lifetime of an VPN session. |
| 47 |
For instance, setting the lifetime to 8 hours means |
| 48 |
the client will disconnect after 8 hours from the initial authentication and will need to re-authenticate. |
| 49 |
|
| 50 |
- Setting the lifetime to 0 disables the lifetime check, |
| 51 |
which can be beneficial for mobile devices with unstable connections or during device sleep cycles. |
| 52 |
|
| 53 |
If `auth-gen-token-secret [keyfile]` is configured, OpenVPN access server restarts can verify auth-tokens. |
| 54 |
To generate a new secret, utilize `openvpn --genkey auth-token [keyfile]`. |
| 55 |
|
| 56 |
> [!NOTE] |
| 57 |
> OpenVPN 3-based clients, such as OpenVPN Connect, do not reuse auth-tokens after an OpenVPN server restart. |
| 58 |
> As a result, `auth-gen-token-secret` cannot provide non-interactive reconnections for these clients. |
| 59 |
> See [issue #1124](https://github.com/jkroepke/openvpn-auth-oauth2/issues/1124) for details. |
| 60 |
|
| 61 |
> [!WARNING] |
| 62 |
> Keep the key file secret. Anyone with access to it can generate authentication |
| 63 |
> tokens that the OpenVPN server accepts as valid. |
| 64 |
|
| 65 |
References: |
| 66 |
|
| 67 |
- https://openvpn.net/community-resources/reference-manual-for-openvpn-2-6/#server-options |
| 68 |
|
| 69 |
# Relationship between reneg-sec and auth-gen-token |
| 70 |
|
| 71 |
Understanding the interaction between `reneg-sec` and `auth-gen-token` is crucial for optimal session management. |
| 72 |
|
| 73 |
## How They Work Together |
| 74 |
|
| 75 |
- **`reneg-sec`**: Controls the TLS renegotiation (soft reset) interval. Can be set on both client and server; the lower value determines when renegotiation occurs. Default is 3600 seconds (1 hour). |
| 76 |
- **`auth-gen-token [lifetime] [renewal-time] external-auth`**: Generates authentication tokens that can be renewed during TLS renegotiations. |
| 77 |
- `lifetime`: Maximum duration the token remains valid (in seconds). Once expired, the user must re-authenticate. |
| 78 |
- `renewal-time`: (Optional) The token expires if it remains idle (not renewed) for more than `2 * renewal-time` seconds. Defaults to the value of `reneg-sec` if not specified. |
| 79 |
|
| 80 |
## Token Expiration Rules |
| 81 |
|
| 82 |
An auth token expires under either condition: |
| 83 |
1. The `lifetime` has been reached (absolute expiration) |
| 84 |
2. The token has not been renewed for more than `2 * renewal-time` seconds (idle timeout) |
| 85 |
|
| 86 |
## Best Practices |
| 87 |
|
| 88 |
For optimal configuration: |
| 89 |
|
| 90 |
1. **Set `reneg-sec` to a reasonably low value** (e.g., 3600 seconds = 1 hour) |
| 91 |
- This ensures regular token renewal attempts |
| 92 |
- Prevents tokens from becoming idle for too long |
| 93 |
|
| 94 |
2. **Set `lifetime` to a reasonably high value** (e.g., 86400 seconds = 24 hours) |
| 95 |
- Allows long-lived sessions without re-authentication |
| 96 |
- Users only need to log in once per day (or per lifetime period) |
| 97 |
|
| 98 |
3. **Optional: Explicitly set `renewal-time`** if you need different renewal behavior |
| 99 |
- If not specified, it defaults to `reneg-sec`. |
| 100 |
- Setting it to a higher value than `reneg-sec` can extend the idle timeout. |
| 101 |
|
| 102 |
## Example Configuration |
| 103 |
|
| 104 |
``` |
| 105 |
# OpenVPN server configuration |
| 106 |
reneg-sec 3600 # Renegotiate every hour |
| 107 |
auth-gen-token 86400 external-auth # 24-hour lifetime (renewal-time omitted, defaults to reneg-sec) |
| 108 |
auth-gen-token-secret /path/to/token.key # Persist tokens across server restarts |
| 109 |
``` |
| 110 |
|
| 111 |
Alternatively, explicitly specify renewal-time: |
| 112 |
``` |
| 113 |
# OpenVPN server configuration |
| 114 |
reneg-sec 3600 # Renegotiate every hour |
| 115 |
auth-gen-token 86400 3600 external-auth # 24-hour lifetime, 1-hour renewal-time |
| 116 |
auth-gen-token-secret /path/to/token.key # Persist tokens across server restarts |
| 117 |
``` |
| 118 |
|
| 119 |
With this configuration: |
| 120 |
- TLS renegotiation occurs every hour (server's `reneg-sec 3600` setting) |
| 121 |
- The auth token is renewed during each renegotiation (non-interactive if refresh is enabled) |
| 122 |
- The token remains valid for up to 24 hours from initial authentication (`lifetime 86400`) |
| 123 |
- The token expires if not renewed for more than 2 hours (`2 * renewal-time` = `2 * 3600` = 7200 seconds) |
| 124 |
- This provides a grace period allowing one missed renegotiation attempt before token expiration |
| 125 |
- In practice: if the client is disconnected/unreachable for more than 2 hours, the token expires |
| 126 |
|
| 127 |
## Important Notes |
| 128 |
|
| 129 |
- If `oauth2.refresh.enabled=true` is configured in `openvpn-auth-oauth2`, token renewals during `reneg-sec` renegotiations will be non-interactive (no browser popup). |
| 130 |
- Without refresh enabled, users must log in interactively at each `reneg-sec` interval. |
| 131 |
- Setting `reneg-sec 0` on the client-side disables TLS renegotiation, which may be useful for mobile devices but requires careful consideration of security implications. |
| 132 |
|
| 133 |
# Troubleshooting |
| 134 |
|
| 135 |
## OIDC Provider Issues with Refresh Tokens |
| 136 |
|
| 137 |
Some OIDC providers may generate new refresh tokens or behave unexpectedly during non-interactive refresh requests. If you experience issues where refresh tokens are invalidated or users need to re-authenticate frequently, you can adjust the nonce behavior using the `oauth2.refresh-nonce` parameter: |
| 138 |
|
| 139 |
- `auto` (default): Try with nonce, retry without nonce on error |
| 140 |
- `empty`: Always use empty nonce for refresh requests |
| 141 |
- `equal`: Use the same nonce as initial authentication |
| 142 |
|
| 143 |
For providers like Authentik that return empty nonces on refresh (per OIDC spec), use `refresh-nonce: empty` to avoid retry logic that could invalidate refresh tokens. |
| 144 |
|
| 145 |
## openvpn-auth-oauth2 config |
| 146 |
|
| 147 |
<table> |
| 148 |
<thead><tr><td>env/sysconfig configuration</td></tr></thead> |
| 149 |
<tbody><tr><td> |
| 150 |
|
| 151 |
```ini |
| 152 |
OPENVPN_AUTH_OAUTH2_OAUTH2_REFRESH_ENABLED=true |
| 153 |
OPENVPN_AUTH_OAUTH2_OAUTH2_REFRESH_EXPIRES=8h |
| 154 |
OPENVPN_AUTH_OAUTH2_OAUTH2_REFRESH_SECRET= # a static secret to encrypt token. Must be 16, 24 or 32 |
| 155 |
OPENVPN_AUTH_OAUTH2_OAUTH2_REFRESH_USE_SESSION_ID=true |
| 156 |
OPENVPN_AUTH_OAUTH2_OPENVPN_AUTH_TOKEN_USER=true |
| 157 |
``` |
| 158 |
</td></tr></tbody> |
| 159 |
<thead><tr><td>yaml configuration</td></tr></thead> |
| 160 |
<tbody><tr><td> |
| 161 |
|
| 162 |
```yaml |
| 163 |
oauth2: |
| 164 |
refresh: |
| 165 |
enabled: true |
| 166 |
expires: 8h |
| 167 |
secret: "..." # 16 or 24 characters |
| 168 |
use-session-id: true |
| 169 |
openvpn: |
| 170 |
auth-token-user: true |
| 171 |
``` |
| 172 |
</td></tr></tbody> |
| 173 |
</table> |