KartaWeb.APIAuth (Karta v1.0.0)

Authenticates a request to a sending API with an API key of a mail server (Karta.Credentials) and assigns the credential, with its server, as :credential.

  1. The key is read from the X-Server-API-Key header (token: :postal, the Postal-compatible API), from the X-Postmark-Server-Token header (token: :postmark, the Postmark-compatible API), from Authorization: Bearer (token: :bearer), or from Authorization: Basic with the user name api and the key as the password (token: :basic, as Mailgun's clients send it). Never from the query string.
  2. A key verified in the last minutes is recognised from Karta.Credentials.KeyCache (the credential is still read from the database); any other key is verified with Argon2id. Verifications are limited per client address (verifications_per_minute, default 60, or config :karta, KartaWeb.APIAuth, verifications_per_minute:), so that nobody can make Karta hash without limit; keys found in the cache do not count. A client is an IPv4 address or an IPv6 /64 (Karta.CIDR.client_key/1), after KartaWeb.ClientIP has found the address behind trusted proxies.
  3. A suspended server's key is refused.
  4. A Kantoor key (Karta.Kantoor) is accepted only where the plug is given kantoor: true (the /kantoor/v1 scope), and every other key only where it is not: elsewhere it is "not valid" (401), as an unknown key is.
  5. Each credential's requests are counted (Karta.Limits, the credential's Karta.Credentials.rate_limit/1 per minute). The count is a row in the database, so every node counts against the same limit: two nodes do not allow twice the requests. A counter that cannot be read refuses the request rather than letting it through.

Failures halt the request with a reply from the errors module (KartaWeb.APIErrors): :missing_key, :invalid_key, :server_suspended, or {:rate_limited, seconds}. KartaWeb.APIScope then checks each action's scope.