Authentication - OAuth2 / OIDC
This feature enables support for delegating DMS account authentication through to an external Identity Provider (IdP).
Receiving mail requires a DMS account to exist
If you expect DMS to receive mail, you must provision an account into DMS in advance. Otherwise DMS has no awareness of your externally manmaged users and will reject delivery.
There are plans to implement support to provision users through a SCIM 2.0 API. An IdP that can operate as a SCIM Client (eg: Authentik) would then integrate with DMS for user provisioning. Until then you must keep your user accounts in sync manually via your configured ACCOUNT_PROVISIONER.
How the feature works
- A mail client must have support to acquire an OAuth2 token from your IdP (however many clients lack generic OAuth2 / OIDC provider support).
- The mail client then provides that token as the user password via the login mechanism
XOAUTH2orOAUTHBEARER. - DMS (Dovecot) will then check the validity of that token against the Authentication Service it was configured with.
- If the response returned is valid for the user account, authentication is successful.
Dovecot does not authenticate the user itself. It sends the token to the configured URL and only checks that a field of the JSON response (email by default, see username_attribute) matches the login username. Authentication is delegated to the service that issued the token.
OAUTH2_INTROSPECTION_URL is not limited to /introspect endpoints
OAUTH2_INTROSPECTION_URL sets the Dovecot introspection_url setting. Despite the name, it is not tied to the RFC 7662 token introspection endpoint (/introspect), which requires client credentials.
Dovecot's default introspection_mode = auth sends a GET request with the token as an Authorization: Bearer header. That fits the OIDC /userinfo endpoint, which needs no client credentials, as used by the Authentik example below. To query an /introspect endpoint instead, see this example.
Keycloak
Use the /userinfo endpoint of your realm: OAUTH2_INTROSPECTION_URL=https://keycloak.example.com/realms/<realm>/protocol/openid-connect/userinfo
XOAUTH2 (Googles widely adopted implementation) and OAUTHBEARER (the newer variant standardized by RFC 7628 in 2015) are supported as standards for verifying that a OAuth Bearer Token (RFC 6750 from 2012) is valid at the identity provider that created the token. The token itself in both cases is expected to be can an opaque Access Token, but it is possible to use a JWT ID Token (which encodes additional information into the token itself).
A mail client like Thunderbird has limited OAuth2 / OIDC support. The software maintains a hard-coded list of providers supported. Roundcube is a webmail client that does have support for generic providers, allowing you to integrate with a broader range of IdP services.
Documentation for this feature is WIP
See the initial feature support and existing issues for guidance that has not yet been documented officially.
Verify authentication works
If you have a compatible mail client you can verify login through that.
CLI - Verify with curl
# Shell into your DMS container:
docker exec -it dms bash
# Adjust these variables for the methods below to use:
export AUTH_METHOD='OAUTHBEARER' USER_ACCOUNT='hello@example.com' ACCESS_TOKEN='DMS_YWNjZXNzX3Rva2Vu'
# Authenticate via IMAP (Dovecot):
curl --silent --url 'imap://localhost:143' \
--login-options "AUTH=${AUTH_METHOD}" --user "${USER_ACCOUNT}" --oauth2-bearer "${ACCESS_TOKEN}" \
--request 'LOGOUT' \
&& grep "dovecot: imap-login: Login: user=<${USER_ACCOUNT}>, method=${AUTH_METHOD}" /var/log/mail/mail.log
# Authenticate via SMTP (Postfix), sending a mail with the same sender(from) and recipient(to) address:
# NOTE: `curl` seems to require `--upload-file` with some mail content provided to test SMTP auth.
curl --silent --url 'smtp://localhost:587' \
--login-options "AUTH=${AUTH_METHOD}" --user "${USER_ACCOUNT}" --oauth2-bearer "${ACCESS_TOKEN}" \
--mail-from "${USER_ACCOUNT}" --mail-rcpt "${USER_ACCOUNT}" --upload-file - <<< 'RFC 5322 content - not important' \
&& grep "postfix/submission/smtpd.*, sasl_method=${AUTH_METHOD}, sasl_username=${USER_ACCOUNT}" /var/log/mail/mail.log
Troubleshooting:
- Add
--verboseto the curl options. This will output the protocol exchange which includes if authentication was successful or failed. - The above example chains the
curlcommands withgrepon DMS logs (for Dovecot and Postfix services). When not runningcurlfrom the DMS container, ensure you check the logs correctly, or inspect the--verboseoutput instead.
curl bug with XOAUTH2
Older releases of curl have a bug with XOAUTH2 support since 7.80.0 (Nov 2021) but fixed from 8.6.0 (Jan 2024). It treats XOAUTH2 as OAUTHBEARER.
If you use docker exec to run curl from within DMS, releases up to DMS v15 (Debian 12 with curl 7.88.1) are affected by this bug. Since DMS v16 (Debian 13), the bundled curl is not affected.
Config Examples
Authentik with Roundcube
This example assumes you have already set up:
- A working DMS server
- An Authentik server (documentation)
- A Roundcube server (docker image or bare metal install)
Setup Instructions
Update your Docker Compose ENV config to include:
services:
mailserver:
env:
# Enable the feature:
- ENABLE_OAUTH2=1
# The `/userinfo` endpoint of the OAuth2 server. Dovecot requests it with the token to look up the user:
- OAUTH2_INTROSPECTION_URL=https://authentik.example.com/application/o/userinfo/
- Create a new OAuth2 provider.
- Note the client id and client secret. Roundcube will need this.
- Set the allowed redirect url to the equivalent of
https://roundcube.example.com/index.php/login/oauthfor your RoundCube instance.
Add the following to oauth2.inc.php (documentation):
$config['oauth_provider'] = 'generic';
$config['oauth_provider_name'] = 'Authentik';
$config['oauth_client_id'] = '<insert client id here>';
$config['oauth_client_secret'] = '<insert client secret here>';
$config['oauth_auth_uri'] = 'https://authentik.example.com/application/o/authorize/';
$config['oauth_token_uri'] = 'https://authentik.example.com/application/o/token/';
$config['oauth_identity_uri'] = 'https://authentik.example.com/application/o/userinfo/';
// Optional: disable SSL certificate check on HTTP requests to OAuth server. For possible values, see:
// http://docs.guzzlephp.org/en/stable/request-options.html#verify
$config['oauth_verify_peer'] = false;
$config['oauth_scope'] = 'email openid profile';
$config['oauth_identity_fields'] = ['email'];
// Boolean: automatically redirect to OAuth login when opening Roundcube without a valid session
$config['oauth_login_redirect'] = false;
Token introspection endpoint with client credentials
An IdP can offer an RFC 7662 /introspect endpoint. Unlike /userinfo, it requires client credentials (the client ID and secret you issued to DMS) via HTTP Basic authentication, and expects a POST request with the token.
OAUTH2_INTROSPECTION_URL cannot switch Dovecot to POST requests, and anyone who can inspect the container can read a secret from its ENV. Configure the oauth2 settings with a dovecot.cf override instead:
oauth2 {
introspection_mode = post
introspection_url = https://dms-dovecot:hunter2@auth.example.com/oauth2/introspect
# Reject tokens that the IdP reports as no longer valid:
active_attribute = active
active_value = true
}
- Keep
ENABLE_OAUTH2=1, but leaveOAUTH2_INTROSPECTION_URLunset. Theoauth2block above overrides the DMS defaults. dms-dovecot:hunter2is theclient_id:client_secretpair. Dovecot sends it as theAuthorization: Basicheader.- The
username_attribute(defaultemail) must be a field of the/introspectresponse. Not every IdP returnsemailthere, so adjust the setting to a field that is returned (RFC 7662 definesusername).
Credentials only work as part of the URL
The Dovecot settings client_id and client_secret are not sent with introspection_mode = post requests (Dovecot 2.4, checked up to 2.4.3).
The credentials must be in the URL as shown above. Percent-encode reserved characters in them (:, @, /, %), for example p@ss becomes p%40ss.