authcha learn

Learn / Integrations

The API and automation

Module 6 of 6 · 25 minutes

By the end you can

  • Create an API token and call the API with it
  • Read the live OpenAPI description and the built-in API page
  • Handle the error statuses correctly in a script
  • Create a user through the API and from the host command line

Everything goes through the API

Everything the console does goes through its REST API under /api, so anything you can do in the console you can script. Terraform, Ansible and CI jobs use the same endpoints as the console, with the same role permissions.

Tokens

  • Scripts: create an API token under Settings, API tokens (or POST /api/tokens) and send it as Authorization: Bearer ra_…. A token has the role it was created with: an admin token may do everything an administrator may, a viewer token may only read. There are no finer scopes, so give automation the narrowest role that works.
  • Browsers: the ra_sid session cookie from POST /api/auth/login.

The plaintext token is shown once at creation and Authcha stores only a hash, so a leaked token can be revoked but not recovered. A request with a Bearer header that is not valid, unexpired and unrevoked is refused with 401, even if a cookie is also sent. Changes made with a token appear in the audit log like any other. The API tokens page now warns when a token has 14 days or fewer left, because automation using it stops when it expires.

The OpenAPI description

WhereWhat
GET /api/openapi.jsonThe live description for your version. Needs a session or token
/api/docsA built-in page listing every operation, who may call it and what it does. Works offline, with no external scripts or fonts. Sign in to the console in the same browser first
docs/product/openapi.jsonA copy for tools that read a file: code generators, Postman, linters

Each operation lists the roles allowed through (x-roles) and the error model. A test fails whenever a route is added without being described, so the description cannot fall behind. GET /api/guides lists the product guides, and GET /api/guides/<slug> returns one as Markdown, for scripts and for anyone without a browser.

Errors

A failed request answers with a status and, mostly, { "error": "message" }.

StatusMeaning
400Invalid input or malformed JSON. The message names the problem
401Not signed in, session expired, or the token is invalid, revoked or expired
403The role, the administrator's provisioning scope, the license, the edition or the public demo refuses it. The message says which
404No such object
409Already exists, or in the wrong state
415The body was not sent as Content-Type: application/json
422The JSON parsed but a field is missing or the wrong type. The answer is plain text, not the JSON shape
429Rate limited. Read the Retry-After header
502, 503A service the request depends on refused or is unavailable
500Internal error. Details only in the server log

A good script retries 429 and 503 with a delay, and fails loudly on everything else.

Create a user in one call

curl -sS -X POST https://authcha.example.internal/api/users \
  -H "Authorization: Bearer $AUTHCHA_TOKEN" \
  -H "Content-Type: application/json" \
  --data @- <<'JSON'
{
  "username": "alice",
  "password": "a-long-passphrase",
  "user_type": "human-read",
  "mobile_number": "",
  "email": "alice@example.com",
  "display_name": "Alice Example"
}
JSON

username is 1 to 64 characters from A-Z a-z 0-9 @ . _ -. The password is stored as a bcrypt hash and never returned. user_type must be an enabled entry of GET /api/user-types that your administrator account may provision. mobile_number is required, so send "" if there is none. A name that already exists, even one in the trash, answers 409. Change a password later with PATCH /api/users/{username}.

Create a user from the host

Without the console or a token, on the Authcha host:

sudo authcha user-create --username alice --user-type human-read \
     --mobile "+15550100" --password-file ./alice.pw
printf '%s\n' "$PW" | sudo authcha user-set-password --username alice --password-stdin

The password is never an argument, because it would show in the process list. Give it on standard input, in a file only its owner can read (mode 0600), in AUTHCHA_USER_PASSWORD, or at a prompt. --update changes a user if it exists and creates it if not. Exit codes: 0 done, 1 unexpected, 2 usage, 3 already exists (or does not exist), 4 refused (invalid name, unknown or disabled type, or the license). Each success writes an audit entry shown as cli.

Voucher redemption is the one public call

POST /api/voucher/redeem is public, for captive portals, with body {"code", "mac_address"}. Every failure has the same body and a randomised delay, and the rate limit answers 429.

LabHands-on

On your install (the demo's API is read-only).

  1. Create a viewer token under Settings, API tokens. Save it once.
  2. Call curl -s -H "Authorization: Bearer $TOKEN" https://<host>/api/openapi.json | head and then try a write call with it. Read the 403.
  3. Open /api/docs in your signed-in browser and find the operation that creates a user and its x-roles.
  4. Create a user with the API using an admin token. Run it twice and read the 409.
  5. Send the call without the Content-Type header and read the 415.
  6. Create a user from the host with authcha user-create, using a password file. Check the audit entry says cli.
  7. Revoke the tokens you made.

Check yourselfQuiz

1A token is sent together with a session cookie. The token is revoked. What happens?
2Can you give a token narrower rights than its role?
3A script creates a user that already exists. What status?
4Why is the password never an argument to authcha user-create?
5Which statuses should a script retry?

Cheat sheetKeep this

  • Token: Authorization: Bearer ra_…. Roles admin or viewer only. Shown once. Warns at 14 days left.
  • Description: /api/openapi.json, /api/docs. Guides: /api/guides.
  • Errors: 400 input, 401 auth, 403 role/license/edition/demo, 409 exists, 415 wrong content type, 422 wrong field, 429 rate limit (Retry-After).
  • Create a user: POST /api/users, or sudo authcha user-create (no password as an argument).
  • Public: POST /api/voucher/redeem only.
  • Read more: API reference.