Skip to content

Authentication User Guide

This guide explains how to use the authentication features of Controlvar, including registration, login, password management, and security best practices.

Cognito only

The password-reset and token-refresh endpoints (POST /api/v1/auth/forgot-password, /reset-password and /refresh) are available only when AUTH_PROVIDER=cognito. The sign-up and confirmation endpoints (POST /api/v1/auth/signup and /confirm) also need COGNITO_SELF_SIGNUP_ENABLED=true; without it they answer 404 and an administrator creates users (Adding a user). With any other provider, including the default local, all five answer 404. With local, sign in with POST /api/v1/auth/login; an administrator creates accounts and resets passwords.

The dashboard does not yet sign in with Cognito. Sign in through the API with POST /api/v1/auth/token. SSO sign-in needs AUTH_PROVIDER=local.

Account Registration

Only with COGNITO_SELF_SIGNUP_ENABLED=true and a user pool that allows self sign-up; otherwise see Adding a user.

Registration Requirements

To create an account on Controlvar, you will need to provide:

  • Username: 3 to 50 characters
  • Email: A valid email address you can access for verification
  • Password: Must satisfy the following requirements:
  • At least 8 characters
  • At most 100 characters
  • At least one uppercase letter
  • At least one lowercase letter
  • At least one digit (0-9)
  • First Name and Last Name: Your real name or preferred name

Registration Process

The dashboard has no registration page and no sign-up link: registration is through the API only, and only with COGNITO_SELF_SIGNUP_ENABLED=true. Without it, both endpoints below answer 404 and an administrator creates your account (Adding a user).

  1. POST /api/v1/auth/signup with username, password, email, given_name and family_name. Cognito emails a verification code to the address.
  2. POST /api/v1/auth/confirm with username and the confirmation_code from the email.
  3. Sign in with POST /api/v1/auth/token (form fields username and password). The dashboard does not yet sign in with Cognito.

Email Verification

  • If you don't receive a verification code, check your spam folder

Login Process

Standard Login

  1. Navigate to the login page at /login
  2. Enter your email address and password
  3. Click "Sign in"

This is the sign-in for AUTH_PROVIDER=local. With cognito, the dashboard does not yet sign in; see the note at the top of this page.

Login Security Features

  • Session Management: A sign-in lasts 12 hours by default (LOCAL_AUTH_TOKEN_TTL_MINUTES); then you sign in again
  • Account Protection: Too many failed login attempts will temporarily lock your account

After Login

Once logged in successfully: - You will be redirected to the dashboard - An access token is stored in your browser - This token is used for all subsequent API requests

Password Management

Password Reset Process

The dashboard has no reset page and no reset link, under any AUTH_PROVIDER. The login page's Forgot password? only says "Ask an administrator to reset it." What happens next depends on the provider.

With AUTH_PROVIDER=local (the default) there is no self-service reset. An administrator with a superuser account sets a new password for you, without knowing the old one, in the dashboard's Edit dialog on Admin → Users (see Managing users in the dashboard). Through the API it is PUT /api/v1/admin/users/{user_id} with a JSON body of your account's username and email and "password": "<new password>", and the administrator's bearer token. PUT /api/v1/users/{user_id} accepts the same password field. Nobody can reset their own password this way, administrators included; sign in with the new password and then change it yourself (see Changing Your Password).

With AUTH_PROVIDER=cognito the password lives in the Cognito user pool, and either of these works:

  • an administrator sets it with aws cognito-idp admin-set-user-password ... --permanent, as in step 3 of Adding a user;
  • you reset it yourself through the API, if your account's email address is verified (email_verified=true): POST /api/v1/auth/forgot-password with {"username": "..."} emails you a code, and POST /api/v1/auth/reset-password with username, confirmation_code and new_password sets the new password. Then sign in with POST /api/v1/auth/token.

Password Reset Notes

  • New passwords must meet the same requirements as registration
  • Under cognito, a password set in the dashboard's database (the local route above) does not change the Cognito password, so it does not change how you sign in

Changing Your Password

With the default local provider, open More in the dashboard's top navigation and choose Change password (in a window narrower than 1280 px, open the menu button first). The page (/account/password) asks for your current password and the new one twice. The link is not shown under cognito, where the password lives in the user pool. An account created through single sign-on has no password of its own, and the page says so.

Through the API, send your current password and the new one to POST /api/v1/users/me/password; see Change Your Own Password for a runnable example.

  • A wrong current password is refused with 403 and counts toward the same limit as failed logins.
  • The new password must meet the same requirements as registration.
  • Changing your password signs out your other sessions. This one stays signed in. A script holding an older token gets 401 and logs in again.
  • With two-step sign-in on, or required for your role, every session ends, this one too, and you sign in again.
  • An administrator resets another user's password with PUT /api/v1/admin/users/{id}; nobody, administrators included, can change their own password that way.

Using Authentication Tokens

The platform uses JWT (JSON Web Tokens) for authentication. Here's how they work:

Token Types

  • With AUTH_PROVIDER=local (the default), POST /api/v1/auth/login returns one access token, valid for 12 hours by default; there is no ID token and no refresh token.
  • With AUTH_PROVIDER=cognito, POST /api/v1/auth/token returns Cognito's access token, ID token and refresh token.

Sample Token Usage

When making API requests, include the access token in the Authorization header:

// Example of an authenticated API request using fetch
async function fetchExperiments() {
  const response = await fetch('https://api.controlvar.example.com/api/v1/experiments', {
    method: 'GET',
    headers: {
      'Authorization': `Bearer ${localStorage.getItem('accessToken')}`,
      'Content-Type': 'application/json'
    }
  });

  return response.json();
}

Token Refresh

Under cognito, when your access token expires, use the refresh token to get a new one:

// Example of refreshing an expired token
async function refreshTokens() {
  const refreshToken = localStorage.getItem('refreshToken');

  const response = await fetch('https://api.controlvar.example.com/api/v1/auth/refresh', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({ refresh_token: refreshToken })
  });

  const data = await response.json();

  if (response.ok) {
    localStorage.setItem('accessToken', data.access_token);
    localStorage.setItem('idToken', data.id_token);
    return true;
  } else {
    // Refresh failed, user needs to log in again
    return false;
  }
}

Managing users in the dashboard

The dashboard's Admin → Users page (/admin/users) lists every account and lets you invite, edit and delete them. Opening it needs a superuser account, whatever its role; the table's Superuser column shows who has one. The Admin role alone does not open the admin area. With AUTH_PROVIDER=cognito superuser access comes from Cognito groups, and the Edit dialog shows it without changing it.

  • Invite User creates an account with a temporary password, shown once. An account invited with the Admin role is also made a superuser.
  • Edit changes an account's role and whether it is active, and its name, username and email. With AUTH_PROVIDER=local it also makes another account a superuser, asking first, or removes that, and sets another account's password, typed twice; it offers neither on your own account. Save is available once something has changed. The name, username, email, superuser flag and password go first, to PUT /api/v1/admin/users/{user_id}, with the username and email every time; then only a changed role or active status goes to PATCH /api/v1/admin/users/{user_id} (see the API reference). After a save the list reloads on the page and search you were on.
  • Permissions (full profile) shows what the user can do: their role plus their custom roles and direct grants.
  • Delete removes the account, and every API key it created, after you confirm it.

The four roles are Admin, Developer, Analyst and Viewer. A legacy account with no role shows "No role" in the table; pick one in the Edit dialog. Custom roles in the full edition are assigned through the RBAC API (POST /api/v1/rbac/roles/assign), not here.

Rules the page and the API both apply:

  • You can't change your own role or deactivate yourself; ask another administrator. You can't delete your own account either.
  • Through the API (PUT /api/v1/admin/users/{user_id} or PUT /api/v1/users/{user_id}) you also can't remove your own superuser access, so keep at least two superusers; with Cognito role sync on, though, a superuser removed from the admin group in Cognito loses superuser access on their next request, and nothing here stops that.
  • Deactivating a user stops them signing in, refuses requests with a token they already hold, and stops the API keys they created, including keys your applications use. When you clear Active, the Edit dialog lists the user's active keys, with each key's scopes and when it was last used, before you save. To keep an application running, switch it to a key created by an account that is staying, then deactivate the user. Reactivating the user makes their keys work again, unless they have expired.
  • With AUTH_PROVIDER=cognito and SYNC_ROLES_ON_LOGIN on (the default), roles come from Cognito groups and are rewritten on every request, so a role change is refused with "Roles on this deployment come from Cognito groups and are updated on every request. Change this user's group in Cognito instead." Deactivating still works.
  • With SSO (full edition), a role set here lasts until the user's next SSO sign-in with a group in the configuration's role_mapping, which replaces it (see Group-to-Role Mapping).

When a save fails, the dialog stays open with your choices and says why. "You don't have permission to change users" means your account is no longer a superuser; sign in again to check. "This user no longer exists" means someone deleted the account; closing the dialog refreshes the list. An email address the API refuses, or one another account already uses, is shown at the Email field, and nothing is saved. When the name, username, email, superuser flag or password is saved and the role or active status is not, the dialog says so.

Resetting two-step sign-in

An account with two-step sign-in that lost its authenticator app and its recovery codes cannot finish signing in. A superuser turns its two-step sign-in off with DELETE /api/v1/admin/users/{user_id}/2fa; the account then signs in with its password alone and can set two-step sign-in up again. An account whose role LOCAL_AUTH_2FA_REQUIRED_ROLES names is asked to set it up at its next sign-in instead. No one can reset their own.

When no superuser can sign in, or after SECRET_KEY has changed, reset accounts from where the API's database is reachable:

  • One account: python -m backend.app.db.two_factor_reset --email <address>, the address in any letter case.
  • Every account: python -m backend.app.db.two_factor_reset --all --yes-reset-every-account.

With Docker Compose, run it in the API container (docker compose exec api ...); with the Helm chart, in an API pod (kubectl exec ...). On AWS, run it as a deploy runs the migration: scripts/run_migration_task.sh with the cluster, migration task definition, network configuration and log group of Actions → Deploy's "Run the migration" step, and only the command changed, in the overrides: {"containerOverrides":[{"name":"backend","command":["python","-m","backend.app.db.two_factor_reset","--email","<address>"]}]}.

It prints how many accounts it reset and writes a user_2fa_reset audit entry for each, by system:operator-cli. An address no account has, --all without --yes-reset-every-account, or neither option exits with status 2 and changes nothing. A reset signs nobody out: a token issued before it keeps working until it expires.

Common Error Scenarios and Solutions

Registration Errors

Error Cause Solution
422, at ["body", "email"] Email address is not valid Check for typos in your email
422, at ["body", "password"], naming the rule it breaks The password breaks a requirement above Follow the password requirements listed above

Login Errors

What the dashboard's sign-in form shows (AUTH_PROVIDER=local):

Error Cause Solution
"Email or password is incorrect." No account has this email, the password does not match, or the account is deactivated. The API answers 401 the same way for all three Check both. An administrator can reset the password or reactivate the account
"Too many failed login attempts; account temporarily locked. Retry in N seconds." Ten failed sign-ins for this email within 15 minutes (LOCAL_AUTH_MAX_FAILED_ATTEMPTS, LOCAL_AUTH_LOCKOUT_MINUTES); the API answers 423 Wait the seconds it names. Every attempt until then is refused, even with the right password
"Too many attempts. Please wait a moment and try again." More than ten sign-ins in a minute from one address (429) Wait a minute
"Can't reach the API at …" The dashboard got no answer from the API Check that the API is running and that the dashboard's origin is in CORS_ORIGINS
"Sign-in did not finish: the API did not answer within 30 seconds. Try again in a moment." The API had not started to answer after 30 seconds, so the dashboard gave up Try again; if it keeps happening, check that the API is healthy

Token Errors

Error Cause Solution
401 "Could not validate credentials" The access token has expired, or is malformed or altered, or your password was changed after it was issued Log in again (under cognito, you can also refresh the token)

Password Reset Errors

These come from the Cognito API routes only. The route answers 400 with Cognito's own message, which names the error:

Error in the message Cause Solution
CodeMismatchException The code does not match the one emailed Check the code, or request a new one
ExpiredCodeException The code has expired Request a new code with /auth/forgot-password
InvalidPasswordException The new password does not meet the pool's policy Follow the pool's password requirements
LimitExceededException Too many attempts Wait before trying again

Security Best Practices

To keep your account secure:

Password Security

  • Use a unique password not used on other websites
  • Consider using a password manager
  • Change your password if you think someone else may know it
  • Never share your password with anyone

Account Protection

  • With local sign-in, turn on two-step sign-in and keep its recovery codes somewhere safe
  • Don't share your verification codes with anyone
  • Log out when using shared or public computers
  • Check for HTTPS in the browser address bar before logging in

Device Security

  • Keep your devices and browsers updated
  • Use antivirus software on your computers
  • Be cautious when logging in from public networks
  • Clear browser cookies and storage after using public computers

Suspicious Activity

If you notice suspicious activity on your account:

  1. Change your password (POST /api/v1/users/me/password), or ask an administrator to reset it; that signs out your other sessions.
  2. To be sure every session has ended while you check, ask an administrator to deactivate the account (is_active set to false). A deactivated account's tokens are refused only while it stays deactivated.
  3. The audit log records sign-ins with a password, through Cognito (POST /auth/token) or through SSO, and an administrator's changes to your account's role, superuser flag or active status. A password change that returns a new token is recorded as a sign-in; other password changes and profile changes are not (#502); keep the account deactivated while you check.