Kantoor.ApiKeys (Kantoor v1.0.0)

Operator API keys (Kantoor.ApiKeys.ApiKey) for the provisioning API.

The secret is generated, shown once and never stored in a form that gives it back. A key is kprov_ + an 8-character lookup prefix + _ + a 32-character secret (160 random bits, lower-case base32). Only an Argon2id hash of the secret is kept; authenticate/1 finds the row by its prefix and verifies the hash, and does the same hashing work for an unknown prefix.

A key authenticates /api/v1/provisioning only. Nothing else in Kantoor (web sessions, IMAP, POP3, DAV, the Karta push) accepts it, and the provisioning scopes name no way to read mail, calendars or contacts.

Scopes: provisioning (read and write) and provisioning:read (read only).

Creating, listing and revoking are admin operations (Kantoor.Admin, which audits them); create_system/1 has no actor and is for the operator's own tooling (mix kantoor.api_key.create, Kantoor.Release.create_api_key/3), which is how the first key is made.

Summary

Functions

The scope a request needs: reading (GET/HEAD) is allowed with provisioning:read or provisioning, everything else needs provisioning.

The key a token belongs to, or :error (unknown, wrong secret, revoked, expired).

Creates a key. attrs: name, operator, scopes (default ["provisioning"]), rate_limit_per_minute, expires_at. Returns the key and its token; the token cannot be recovered afterwards.

Creates a key without an acting admin (first key, release tasks).

The errors of a changeset as ["field message"].

A key by id, or nil.

Whether the key has the scope.

All keys, newest first, revoked ones included.

Revokes a key: it never authenticates again. Idempotent.

Every scope a key can have.

Functions

allowed?(key, method)

@spec allowed?(Kantoor.ApiKeys.ApiKey.t(), String.t()) :: boolean()

The scope a request needs: reading (GET/HEAD) is allowed with provisioning:read or provisioning, everything else needs provisioning.

authenticate(token)

@spec authenticate(term()) :: {:ok, Kantoor.ApiKeys.ApiKey.t()} | :error

The key a token belongs to, or :error (unknown, wrong secret, revoked, expired).

create(attrs, created_by_id \\ nil)

@spec create(map(), Ecto.UUID.t() | nil) ::
  {:ok, Kantoor.ApiKeys.ApiKey.t(), String.t()} | {:error, Ecto.Changeset.t()}

Creates a key. attrs: name, operator, scopes (default ["provisioning"]), rate_limit_per_minute, expires_at. Returns the key and its token; the token cannot be recovered afterwards.

create_system(attrs)

@spec create_system(map()) ::
  {:ok, Kantoor.ApiKeys.ApiKey.t(), String.t()} | {:error, Ecto.Changeset.t()}

Creates a key without an acting admin (first key, release tasks).

error_messages(changeset)

@spec error_messages(Ecto.Changeset.t()) :: [String.t()]

The errors of a changeset as ["field message"].

get(id)

@spec get(term()) :: Kantoor.ApiKeys.ApiKey.t() | nil

A key by id, or nil.

has_scope?(api_key, scope)

@spec has_scope?(Kantoor.ApiKeys.ApiKey.t(), String.t()) :: boolean()

Whether the key has the scope.

list()

@spec list() :: [Kantoor.ApiKeys.ApiKey.t()]

All keys, newest first, revoked ones included.

revoke(key)

@spec revoke(Kantoor.ApiKeys.ApiKey.t()) ::
  {:ok, Kantoor.ApiKeys.ApiKey.t()} | {:error, Ecto.Changeset.t()}

Revokes a key: it never authenticates again. Idempotent.

scopes()

Every scope a key can have.