Skip to content

Sign-in configuration

The app can ask for a username and password (Basic authentication), send users to an identity provider (OAuth2/OIDC), or offer both. You set this up in Settings > Authentication or with environment variables.

What each person may see and do is set in Settings > Users. See Users.

Basic authentication

Add these lines to .env and run docker compose up -d:

BASIC_AUTH_ENABLED=true
AUTH_USERNAME=admin
AUTH_PASSWORD=replace-with-a-long-random-password

To set it up in the app instead, fill in Username and Password in Settings > Authentication, turn on Enable Basic Auth and save. The app asks you to type the username and password again, so you cannot lock yourself out with a typo, and it does not let you clear the password while Basic authentication is on.

This is the built-in admin. It always has full access and cannot be removed in the app. Scripts and API clients can send its username and password in an Authorization: Basic header with every request instead of signing in. Users added in Settings > Users sign in on the login page only.

Login page Login page

After 10 failed attempts within 15 minutes, the app refuses further Basic sign-in attempts from that address ("Too many login attempts") until the oldest failures are more than 15 minutes old. IPv6 addresses are counted per /64 network. Users who are already signed in are not affected.

Behind a reverse proxy, the app must know the real visitor address, or all visitors share one counter. See Reverse proxy. Maximum addresses tracked for failed sign-ins (AUTH_MAX_FAILURE_CLIENTS, default 10,000) caps how many addresses the app tracks.

OAuth2/OIDC

  1. At your identity provider, register a confidential web client (one with a client secret) that uses the authorization code flow. Set its redirect or callback URL to your app address followed by /api/auth/callback, for example https://logs.example.com/api/auth/callback.
  2. Copy the client ID and the client secret.
  3. Generate a session secret with openssl rand -hex 32.
  4. Add the lines below to .env, plus the provider lines from one of the recipes.
  5. Run docker compose up -d. The login page now shows a Login with button with your provider name.
OAUTH2_ENABLED=true
OAUTH2_PROVIDER_NAME=Company sign-in
OAUTH2_CLIENT_ID=replace-with-client-id
OAUTH2_CLIENT_SECRET=replace-with-client-secret
OAUTH2_REDIRECT_URI=https://logs.example.com/api/auth/callback
OAUTH2_SCOPES=openid profile email
SESSION_SECRET_KEY=replace-with-the-generated-secret
SESSION_EXPIRY_HOURS=24

To set it up in the app instead, fill in the values in Settings > Authentication, turn on Enable OAuth2 / OIDC and save. The app first asks you to sign in with your provider in a new tab, and shows who signed in and the access they would get. Save works only after a sign-in that works. It asks again when you turn Basic authentication off, or change the provider values while it is off; then the account must also get Admin, so nobody is locked out. When the test fails, Details shows the provider's answer, the endpoints and what the provider said about the person. It never shows the client secret or tokens.

The app uses PKCE (S256) on every login and sends the client secret when it exchanges the code. After sign-in it reads the user's details from the provider's UserInfo endpoint, so the access token must be allowed to call it.

Discovery or manual endpoints

With discovery on (the default), set OAUTH2_ISSUER_URL to the base address of the provider. The app adds /.well-known/openid-configuration and reads the three endpoints from there, so do not include that part yourself:

OAUTH2_USE_OIDC_DISCOVERY=true
OAUTH2_ISSUER_URL=https://identity.example.com

If the provider has no discovery document, turn discovery off and set the three endpoints:

OAUTH2_USE_OIDC_DISCOVERY=false
OAUTH2_AUTHORIZATION_URL=https://identity.example.com/oauth/authorize
OAUTH2_TOKEN_URL=https://identity.example.com/oauth/token
OAUTH2_USERINFO_URL=https://identity.example.com/oauth/userinfo

If discovery fails and all three endpoints are set, the app uses them.

Provider recipes

Authentik

Create an OAuth2/OIDC provider (confidential client, your callback URL, the openid, profile and email scope mappings) and an application that uses it. Bind a policy or group to the application so only mail administrators can use it. See Authentik's provider setup.

OAUTH2_PROVIDER_NAME=Authentik
OAUTH2_USE_OIDC_DISCOVERY=true
OAUTH2_ISSUER_URL=https://identity.example.com/application/o/mailcow-logs-viewer/
OAUTH2_SCOPES=openid profile email

Replace mailcow-logs-viewer with your application's slug. See Authentik's OAuth2 endpoints.

mailcow

Add an OAuth2 client in mailcow with your callback URL, then use these lines:

OAUTH2_PROVIDER_NAME=mailcow
OAUTH2_USE_OIDC_DISCOVERY=false
OAUTH2_AUTHORIZATION_URL=https://mail.example.com/oauth/authorize
OAUTH2_TOKEN_URL=https://mail.example.com/oauth/token
OAUTH2_USERINFO_URL=https://mail.example.com/oauth/profile
OAUTH2_SCOPES=profile

Do not assume that only mailcow administrators can sign in this way. If you cannot limit who uses this client, choose a provider with access control or use Basic authentication.

Keycloak

Create an OpenID Connect client in the realm, turn on client authentication and the standard (authorization code) flow, add your callback URL and copy the client secret. To keep other users out, use an authentication flow or policy that denies them; a role on its own does nothing here, because the app does not check roles.

OAUTH2_PROVIDER_NAME=Keycloak
OAUTH2_USE_OIDC_DISCOVERY=true
OAUTH2_ISSUER_URL=https://identity.example.com/realms/mail-admin
OAUTH2_SCOPES=openid profile email

Replace mail-admin with your realm. See Keycloak's OIDC endpoints.

Google

Create an OAuth client of type web application, add your callback URL and copy the client ID and secret. See Google's OpenID Connect setup.

OAUTH2_PROVIDER_NAME=Google
OAUTH2_USE_OIDC_DISCOVERY=true
OAUTH2_ISSUER_URL=https://accounts.google.com
OAUTH2_SCOPES=openid profile email

Limit access at Google. Even an internal audience usually includes more people than your mail administrators, and the app does not check groups or email domains.

Microsoft Entra ID

Register a single-tenant web application with your callback URL and create a client secret. In the enterprise application, turn on assignment required and assign the users who may sign in. See Microsoft's OIDC documentation.

OAUTH2_PROVIDER_NAME=Microsoft
OAUTH2_USE_OIDC_DISCOVERY=true
OAUTH2_ISSUER_URL=https://login.microsoftonline.com/replace-with-tenant-id/v2.0
OAUTH2_SCOPES=openid profile email

Use your own tenant ID. Do not use the shared multi-tenant address unless you limit who can use the client some other way.

Users

Settings > Users lists everyone who may sign in, starting with the built-in admin. Only admins see it.

  • Add user with a password adds someone who signs in on the login page. The password needs at least 8 characters and must not be on the list of the 100,000 most common passwords (NCSC). Generate creates one.
  • Add user with your provider adds someone who signs in with OAuth2, by the email address the provider reports for them. The app trusts that address, so your provider must not let people choose an address they do not own. An address the provider marks as not verified (email_verified: false) matches only after you turn on Trust unverified email addresses under Settings > Authentication. Turn it on only if people cannot set their own address at your provider. Until then, and as soon as you turn it off again, such a person gets the access for everyone else.
  • Access is Admin (everything), Operator (every page and action except editing Rspamd maps), Viewer (every page, no actions) or Custom (view or act per area). Only admins see Settings, Users and AI agents, and only admins can connect an agent.
  • Everyone else who signs in with your provider sets the access of people your provider lets in who are not on the list. It is Admin until you change it, which is how the app behaved before users existed. No access lets in only the people on the list.
  • A change applies at once, also to people who are signed in. A new password, other access or Disable sign-in signs a password user out. Before a change that lowers your own access, the app asks.
  • While Basic authentication is on, the built-in admin can always sign in. While it is off, a change on this page, or to Trust unverified email addresses, must come from an admin signed in with your provider who stays an admin, so you cannot remove your own admin access then.

Sessions and signing out

Both sign-in methods give the browser a cookie named session_id (HttpOnly, SameSite=Lax). The cookie is marked Secure when the request came over HTTPS, directly or through a proxy that sends X-Forwarded-Proto: https. Use HTTPS for anything outside a private network.

  • A session lasts at most Longest session (SESSION_EXPIRY_HOURS, default 24), counted from sign-in whatever the activity. Five minutes before, the page says so. A shorter value applies to open sessions at once; a longer one applies in full from the next sign-in.
  • Sign out after inactivity (SESSION_IDLE_ENABLED, off by default) ends a session after Inactivity limit minutes without activity (SESSION_IDLE_TIMEOUT_MINUTES, default 60, from 5 to 1440). Typing, clicking, scrolling with the wheel and touching count as activity; pages that refresh by themselves do not, so a dashboard left open on a screen is signed out too. A minute before, the page asks whether you are still there. Turning it on does not sign out people who are already signed in: their inactivity time starts then.
  • After a session ends this way, the login page says why. Both limits apply to Basic Auth and OAuth2 sign-ins alike. AI agents and API calls with a username and password are not affected.
  • Sessions are kept in memory, so every restart of the app signs everyone out.
  • Maximum active sessions (SESSION_MAX_ENTRIES, default 50) caps open sessions. When it is reached, existing sessions keep working and new sign-ins fail with "Login capacity reached" until a session expires or someone signs out.
  • Logout ends the session in this app only. It does not sign you out of your identity provider.
  • Saving a change to the sign-in settings signs everyone out. The person who saved gets a new session and stays signed in.

Apply changes and recover access

A variable set in .env always wins over the value saved in the app. After you change .env, run docker compose up -d. Changes saved in Settings apply right away.

If you lock yourself out, or the app does not start because of its sign-in settings:

  1. Stop the app: docker compose stop app.
  2. Run docker compose run --rm app python -m app.recover and type a new password twice. The command turns on Basic Auth in the app's saved settings for the built-in admin and prints its username. If OAuth2 is on without a client secret, it turns OAuth2 off. It changes nothing in .env.
  3. Run docker compose up -d, sign in with that username and password, and fix the sign-in settings in Settings > Authentication.

With AUTH_PASSWORD set in .env, the command asks for no password: it fixes the saved settings, and you sign in with the password from .env. With SETTINGS_EDIT_VIA_UI_ENABLED off, sign-in comes only from .env: fix it there and run docker compose up -d.

When sign-in fails

Problem What to check
No Login with button OAUTH2_ENABLED and the values in Settings > Authentication. About shows whether sign-in is required.
The provider button fails at once The issuer URL (the app adds /.well-known/openid-configuration), network access from the app to the provider, and the provider's certificate. The reason is in the app's log.
The provider rejects the callback The callback URL must be identical at the provider and in OAUTH2_REDIRECT_URI: scheme, host, port and path.
"Invalid authentication state" Start the login again in the same browser and finish it within 10 minutes.
"Your account has no access to this app" Your email has an entry with sign-in disabled in Settings > Users, or the access for everyone else is No access. An admin can change it.
"Login capacity reached" Sign out unused sessions or wait for them to expire, or raise Maximum active sessions.
Everyone gets "Too many login attempts" at once The app sees the proxy's address instead of the visitor's. Check FORWARDED_ALLOW_IPS in Reverse proxy.