Karta.Credentials (Karta v1.0.0)

API keys and SMTP credentials of mail servers (Karta.Credentials.Credential).

Secrets are generated, shown once, and never stored in a form that gives them back.

  • An API key is karta_ + an 8-character lookup prefix + _ + a 32-character secret (160 random bits). The secret is kept as an Argon2id hash (16 MiB, 3 passes, 1 lane); authenticate_api_key/1 finds the row by its prefix and verifies the hash.
  • An SMTP credential's user name is its 8-character prefix and its password 32 random characters. Only SCRAM-SHA-256 keys are kept (PBKDF2 with 100,000 iterations and a 16-byte salt): enough to check a password given in clear (authenticate_smtp/2, for AUTH PLAIN and LOGIN) and to run a SCRAM exchange (scram_lookup/1).
  • An IP range credential (create_ip_credential/2) names a CIDR range of client addresses and has no secret at all: a client whose address is in the range sends through its server without AUTH (Karta.SMTP.Relay). The range is kept in canonical form and checked against the instance's minimum prefix length (Karta.SMTP.Relay.Range).

Characters of keys and passwords are lower-case base32 (a-z, 2-7): no character that needs quoting in a configuration file or SASLprep.

Unknown user names and revoked credentials take the same work as a wrong password, and a SCRAM lookup for them returns a made-up salt that is the same every time, so neither timing nor the exchange tells whether a user name exists.

Summary

Functions

Every active IP range credential, with its server, oldest first.

The credential (with its server) an API key belongs to, or :error.

The SMTP credential (with its server) for a user name and password, or :error.

Creates an API key. attrs: name, scopes (default: send and read messages), rate_limit_per_minute. Returns the key once.

Creates an IP range credential. attrs: name, cidr, tls_required (default true) and rate_limit_per_minute. There is no secret to show: the range itself lets a client in.

Creates a Kantoor key (Karta.Kantoor): an API key accepted on /kantoor/v1 only. attrs as for create_api_key/2 (default scopes: Karta.Kantoor.default_scopes/0), plus allowed_domains and allowed_mailboxes (lists of patterns, see Karta.Kantoor.Restrictions; absent: no restriction). Returns the key once.

Creates an SMTP credential. attrs: name, rate_limit_per_minute. Returns the password once; the user name is username/1.

The scopes a new API key gets when none are given.

The credential with its server, when it is not revoked; otherwise nil.

A credential of the server, or nil.

True when an API key has the scope.

The server's credentials, oldest first, revoked ones included.

Requests (API) or messages (SMTP) per minute the credential may make.

Revokes a credential: it never authenticates again.

Every scope an API key can have.

What a SCRAM-SHA-256 exchange for username needs. For an unknown user name or a revoked credential, credential is nil and the keys are made up: the exchange then fails at the proof, like a wrong password.

Changes a credential's name, rate limit or (API keys) scopes.

Changes the restrictions of a Kantoor key (allowed_domains, allowed_mailboxes).

Records a successful authentication. The write is debounced in ETS and made in one statement a minute and at shutdown (Karta.Credentials.Used), so a request does not write.

The SMTP user name of a credential.

Types

scram()

@type scram() :: %{
  salt: binary(),
  iterations: pos_integer(),
  stored_key: binary(),
  server_key: binary(),
  credential: Karta.Credentials.Credential.t() | nil
}

Functions

active_ip_credentials()

@spec active_ip_credentials() :: [Karta.Credentials.Credential.t()]

Every active IP range credential, with its server, oldest first.

authenticate_api_key(token)

@spec authenticate_api_key(String.t()) ::
  {:ok, Karta.Credentials.Credential.t()} | :error

The credential (with its server) an API key belongs to, or :error.

authenticate_smtp(username, password)

@spec authenticate_smtp(String.t(), String.t()) ::
  {:ok, Karta.Credentials.Credential.t()} | :error

The SMTP credential (with its server) for a user name and password, or :error.

create_api_key(server, attrs)

@spec create_api_key(Karta.Servers.Server.t(), map()) ::
  {:ok, Karta.Credentials.Credential.t(), String.t()}
  | {:error, Ecto.Changeset.t()}

Creates an API key. attrs: name, scopes (default: send and read messages), rate_limit_per_minute. Returns the key once.

create_ip_credential(server, attrs)

@spec create_ip_credential(Karta.Servers.Server.t(), map()) ::
  {:ok, Karta.Credentials.Credential.t()} | {:error, Ecto.Changeset.t()}

Creates an IP range credential. attrs: name, cidr, tls_required (default true) and rate_limit_per_minute. There is no secret to show: the range itself lets a client in.

create_kantoor_key(server, attrs)

@spec create_kantoor_key(Karta.Servers.Server.t(), map()) ::
  {:ok, Karta.Credentials.Credential.t(), String.t()}
  | {:error, Ecto.Changeset.t()}

Creates a Kantoor key (Karta.Kantoor): an API key accepted on /kantoor/v1 only. attrs as for create_api_key/2 (default scopes: Karta.Kantoor.default_scopes/0), plus allowed_domains and allowed_mailboxes (lists of patterns, see Karta.Kantoor.Restrictions; absent: no restriction). Returns the key once.

create_smtp_credential(server, attrs)

@spec create_smtp_credential(Karta.Servers.Server.t(), map()) ::
  {:ok, Karta.Credentials.Credential.t(), String.t()}
  | {:error, Ecto.Changeset.t()}

Creates an SMTP credential. attrs: name, rate_limit_per_minute. Returns the password once; the user name is username/1.

default_scopes()

@spec default_scopes() :: [String.t()]

The scopes a new API key gets when none are given.

get_active(id)

@spec get_active(Ecto.UUID.t()) :: Karta.Credentials.Credential.t() | nil

The credential with its server, when it is not revoked; otherwise nil.

get_credential(server, id)

@spec get_credential(Karta.Servers.Server.t(), term()) ::
  Karta.Credentials.Credential.t() | nil

A credential of the server, or nil.

has_scope?(credential, scope)

@spec has_scope?(Karta.Credentials.Credential.t(), String.t()) :: boolean()

True when an API key has the scope.

list_credentials(server)

@spec list_credentials(Karta.Servers.Server.t()) :: [Karta.Credentials.Credential.t()]

The server's credentials, oldest first, revoked ones included.

rate_limit(credential)

@spec rate_limit(Karta.Credentials.Credential.t()) :: pos_integer()

Requests (API) or messages (SMTP) per minute the credential may make.

revoke_credential(credential)

@spec revoke_credential(Karta.Credentials.Credential.t()) ::
  {:ok, Karta.Credentials.Credential.t()} | {:error, Ecto.Changeset.t()}

Revokes a credential: it never authenticates again.

scopes()

@spec scopes() :: [String.t()]

Every scope an API key can have.

scram_lookup(username)

@spec scram_lookup(String.t()) :: scram()

What a SCRAM-SHA-256 exchange for username needs. For an unknown user name or a revoked credential, credential is nil and the keys are made up: the exchange then fails at the proof, like a wrong password.

update_credential(credential, attrs)

@spec update_credential(Karta.Credentials.Credential.t(), map()) ::
  {:ok, Karta.Credentials.Credential.t()} | {:error, Ecto.Changeset.t()}

Changes a credential's name, rate limit or (API keys) scopes.

update_kantoor_restrictions(credential, attrs)

@spec update_kantoor_restrictions(Karta.Credentials.Credential.t(), map()) ::
  {:ok, Karta.Credentials.Credential.t()} | {:error, Ecto.Changeset.t()}

Changes the restrictions of a Kantoor key (allowed_domains, allowed_mailboxes).

used(credential)

Records a successful authentication. The write is debounced in ETS and made in one statement a minute and at shutdown (Karta.Credentials.Used), so a request does not write.

username(credential)

@spec username(Karta.Credentials.Credential.t()) :: String.t()

The SMTP user name of a credential.