Golub

Single sign-on

With SSO, a control panel or portal can send its users straight into their mailbox, without asking for the mailbox password again. Golub verifies a short-lived signed token, then signs in to Dovecot as that user through a Dovecot master user.

1. Create a Dovecot master user

A master user can log in as any mailbox: user@example.com*mastername with the master password.

With docker-mailserver:

docker exec -it mailserver setup dovecot-master add golub 'a-long-random-password'

This writes config/dovecot-masters.cf. Dovecot also has to accept * in login names, so add this line to config/dovecot.cf and restart the mail server once:

auth_username_chars = abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890.-_@*

On other Dovecot installations, see the Dovecot documentation for master users; the separator must be *.

2. Configure Golub

    environment:
      GOLUB_SSO_SECRET: "at least 32 random characters, shared with the panel"
      GOLUB_MASTER_USER: golub
      GOLUB_MASTER_PASSWORD: "a-long-random-password"

Normal sign-in with a password keeps working next to SSO.

3. Create tokens in your panel

The panel builds a payload of five fields separated by |:

email|expires|nonce|lang|theme
FieldRules
emailThe mailbox to sign in to.
expiresUnix timestamp, at most 60 seconds in the future.
nonceRandom, at least 16 characters. Each token works only once.
langOptional: en, sr-Latn or ar. Leave empty to keep the user's choice.
themeOptional: system, light or dark. Leave empty to keep the user's choice.

The token is base64url(payload) + "." + base64url(HMAC-SHA256(secret, payload)), both without = padding. The panel then redirects the browser to:

https://mail.example.com/sso?token=<token>

Empty optional fields still keep their separators: ana@example.com|1764300000|3f9a…||.

PHP

$payload = implode('|', [$email, time() + 60, bin2hex(random_bytes(16)), 'en', 'system']);
$b64 = fn ($s) => rtrim(strtr(base64_encode($s), '+/', '-_'), '=');
$token = $b64($payload) . '.' . $b64(hash_hmac('sha256', $payload, $secret, true));
header('Location: https://mail.example.com/sso?token=' . $token);

Python

import base64, hashlib, hmac, secrets, time

def golub_token(email: str, secret: str, lang: str = "", theme: str = "") -> str:
    payload = f"{email}|{int(time.time()) + 60}|{secrets.token_hex(16)}|{lang}|{theme}".encode()
    b64 = lambda b: base64.urlsafe_b64encode(b).rstrip(b"=").decode()
    return b64(payload) + "." + b64(hmac.new(secret.encode(), payload, hashlib.sha256).digest())

Go

func golubToken(email, secret, lang, theme string) string {
	nonce := make([]byte, 16)
	rand.Read(nonce)
	payload := fmt.Sprintf("%s|%d|%x|%s|%s", email, time.Now().Add(time.Minute).Unix(), nonce, lang, theme)
	mac := hmac.New(sha256.New, []byte(secret))
	mac.Write([]byte(payload))
	enc := base64.RawURLEncoding
	return enc.EncodeToString([]byte(payload)) + "." + enc.EncodeToString(mac.Sum(nil))
}

What happens on errors

If the token is expired, reused, badly signed or malformed, or Dovecot refuses the master login, the browser lands on the login page with "The sign-in link is invalid or has expired. Please sign in again." The reason is logged as sso token rejected or sso master login rejected. If the mail server can't be reached, the login page says so instead.

Security notes

  • Keep GOLUB_SSO_SECRET and the master password only on the panel and in Golub's environment.
  • Tokens are single use: Golub remembers each nonce until the token expires. The nonces live in memory, so if you run several Golub instances behind a load balancer, send /sso requests for one user to the same instance. All instances need the same GOLUB_SECRET_KEY, so a session started on one works on the others.
  • Signing out of an SSO session only ends the Golub session; the mailbox password is never stored in the cookie for SSO users.

Edit this page on GitHub

Your mail server deserves a better webmail.

The real interface with made-up mail. Nothing to install, no sign-up.

Golub showing the inbox with an open message