Kantoor. AppPasswords
(Kantoor v1.0.0)
Per-device app passwords for protocol clients (IMAP, POP3, SMTP, DAV).
A password looks like kapw_<id>_<secret>. Only an Argon2id hash of the
secret is stored and the id is the lookup key, so verification is one indexed
read plus one hash comparison. The plaintext is returned once, by create/2.
verify/5 is what protocol servers call. It never says why a credential was
refused (wrong secret, revoked, expired, disabled user, wrong scope, wrong
login all give :invalid_credentials), does the same hashing work whether or
not the id exists, and throttles failures (see verify/5).
Throttling
Failures are counted per fixed window (default 10 minutes) on three keys, all derived from the normalised login (trimmed, lowercased; kept only as a keyed digest) and the normalised client IP (IPv6 collapsed to /64):
{ip, login}: more thanpair_limit(5) failures blocks that IP for that login onlyip: more thanip_limit(30) failures blocks the IP for every login (password spraying)login: never a hard lock. Pastlogin_threshold(20) failures, IPs that did not verify this login in the lastknown_ip_ttl_days(30) are refused; known IPs (Kantoor.KnownLoginIps) keep working
Tune with config :kantoor, Kantoor.AppPasswords, window: .., pair_limit: .., ip_limit: .., login_threshold: .., known_ip_ttl_days: .., known_ip_max: ...
Throttled attempts return before any hashing (a cheap refusal is the point);
every failed attempt that reaches the check does the same Argon2 work.
Telemetry: [:kantoor, :auth, :throttled | :lockout].
Summary
Functions
Counts active (not revoked, not expired) app passwords per user id (admin
only): %{user_id => count}; users without any are absent.
Creates an app password for the scope's user. Returns the record and the plaintext token, which cannot be recovered afterwards.
Lists the scope's own app passwords, newest first.
Revokes one of the scope's own app passwords (idempotent).
Revokes every active app password of a user (admin only). Returns the number revoked.
Verifies login + password for a protocol scope (:imap, :pop3,
:smtp or :dav) from client address ip (tuple, string or nil).
Functions
@spec count_active_by_user(Kantoor.Accounts.Scope.t() | nil) :: {:ok, %{required(Ecto.UUID.t()) => non_neg_integer()}} | {:error, :unauthorized}
Counts active (not revoked, not expired) app passwords per user id (admin
only): %{user_id => count}; users without any are absent.
@spec create(Kantoor.Accounts.Scope.t() | nil, map()) :: {:ok, Kantoor.Accounts.AppPassword.t(), String.t()} | {:error, :unauthorized | Ecto.Changeset.t()}
Creates an app password for the scope's user. Returns the record and the plaintext token, which cannot be recovered afterwards.
@spec list(Kantoor.Accounts.Scope.t() | nil) :: [Kantoor.Accounts.AppPassword.t()]
Lists the scope's own app passwords, newest first.
@spec revoke(Kantoor.Accounts.Scope.t() | nil, term()) :: {:ok, Kantoor.Accounts.AppPassword.t()} | {:error, :not_found}
Revokes one of the scope's own app passwords (idempotent).
@spec revoke_all_for_user(Kantoor.Accounts.Scope.t() | nil, Ecto.UUID.t()) :: {:ok, non_neg_integer()} | {:error, :unauthorized}
Revokes every active app password of a user (admin only). Returns the number revoked.
@spec verify(String.t() | nil, String.t() | nil, atom(), term(), keyword()) :: {:ok, Kantoor.Accounts.Scope.t(), Kantoor.Accounts.AppPassword.t()} | {:error, :invalid_credentials | :rate_limited}
Verifies login + password for a protocol scope (:imap, :pop3,
:smtp or :dav) from client address ip (tuple, string or nil).
login is the user's email or the address of a mailbox they may open.
Returns {:ok, scope, app_password} and records last use, or
{:error, :invalid_credentials} / {:error, :rate_limited}. Servers should
answer :rate_limited with a temporary failure (IMAP [UNAVAILABLE], POP3
-ERR [SYS/TEMP], HTTP 429/503) and may close the connection.
Failures are counted on three keys (see the moduledoc), so an attacker's failures never lock the real user out: only the attacker's own IP is blocked for that login.
Options: :now (a DateTime, for tests).