Identities and realms

This page explains, for technical readers, how subscription and Keycloak share identities: the realms, what each holds, and how a user is created, disabled and deleted. The screens are described in Users.

Realms

A Keycloak realm is a directory of accounts, with its own sign-in rules. The platform accepts several:

  • security.jwt.allowed-realms: the realms whose tokens the services accept. They are the only realms a user or a client can be given; GET /api/v1/realms gives them to the screens, with the default realm.
  • application.identity.default-realm (mla by default): the realm of a user created without one, and of a membership lookup that names none. It must be one of the accepted realms, or subscription does not start.
  • security.jwt.platform-realms ([mla, shared] by default, a security-library setting): the realms whose tokens may be a platform administrator’s or a service’s. Administrators come from mla, service accounts from shared.

Who opens the console

The console signs in through the gateway, on the mla realm by default. It reads the signed-in account from the gateway (/api/account) and opens its pages only to an account carrying the ROLE_ADMIN authority; any other account sees the Access restricted page (“Subscription is reserved for MLAdvisory administrators, and your account does not have that role.”), which offers only Log out.

The server makes its own check: a platform administrator is a token issued by a platform realm and carrying exactly the ROLE_ADMIN realm role (ReglabAuthorities.isPlatformAdmin). Every subscription endpoint is reserved to them, except those listed in Rights and Security: the services’ lookups, the public read of a client by its subdomain, the signed-in user’s language, and so on.

The Admin role a user holds on a tenant is subscription data, not a Keycloak role: it gives no access to the console.

What subscription holds, what Keycloak holds

subscription holds (reglab_user and the accesses) Keycloak holds (the realm’s account)
The account’s realm and username The account itself, its password and how it signs in
First name, last name, e-mail Whether the account is enabled
The home client, Multi-client, the guest clients The open sessions
The mail language (the language property) The realm roles (ROLE_ADMIN, SUPPORT…)
When the user was disabled (disabledSince) The login events
The last login seen (lastLoginAt), copied from Keycloak
The accesses: role and plans on each tenant, suspension

subscription never creates nor deletes a Keycloak account, and touches neither its password nor its roles. It does one thing to an account: disable it (closing its sessions) or enable it again (see Deactivation). It also reads the realms’ logins (see Last login).

A user: a realm and a username

A username is unique only inside its realm: a user is the pair (realm, username). The reglab_user table has a uniqueness constraint on that pair. The e-mail, for its part, is unique across the whole platform.

A token is tied to its user by its preferred_username and by its issuer’s realm (iss, …/realms/<realm>). The services pass that realm to subscription in their membership lookups; a service that passes none gets the default realm.

A client’s realm

A client may have a realm (client.realm) and a subdomain (client.subdomain). Together they say where its users sign in:

  • A client’s front answers on <subdomain>.irrc-<environment>.…. Before anyone signs in, it reads GET /api/v1/public/clients/by-subdomain/{subdomain}, open to all, which gives the realm to sign in to, the client’s name and whether it has a logo. That read answers only for a client with a subdomain and a realm still accepted (404 otherwise); caches may keep it 5 minutes.
  • A client’s realm has no default: empty, it stays unset.
  • The accounts of the users of a client with a realm must be in that realm. subscription refuses to create, correct or import a user in another realm, and refuses to give a client a realm while some of its users are elsewhere (error.client.usersOutsideRealm). Guest clients do not count: only the home client decides.
  • A realm is checked only when written. Taking a realm out of security.jwt.allowed-realms leaves its clients pointing at it: at startup, subscription logs the clients whose realm is no longer accepted (ClientRealmCheck). The public read no longer finds them, and their users cannot sign in until the realm is accepted again or the client gets another one.

Creation and import

Creating a user (POST /api/v1/reglab-users) or importing users (POST /api/v1/reglab-users/import) creates their record in subscription, and nothing in Keycloak.

The user’s realm is, in that order: the one the request names, else their client’s, else the default realm. It must be accepted (400 otherwise) and, when the client has one, be the client’s. The creation screen and the import offer the default realm already chosen: for a client with its own realm, that realm must be chosen instead.

When editing, the realm can be corrected, for an account registered under the wrong realm, but never to a refused realm. The screen does not let the home client change after creation.

Deactivation

PUT /api/v1/reglab-users/{id}/deactivation disables a user; DELETE on the same address enables them again. In one transaction, disabling:

  1. sets disabledSince: from then on, the membership lookups find the user in no tenant;
  2. keeps their roles and plans, which enabling gives back as they were;
  3. sends irrc UPDATE_PLAN for each of their tenants;
  4. records the Account disabled event in the access history;
  5. adds a Keycloak account task DISABLE (ENABLE when enabling) to keycloak_account_task.

Disabling a disabled user, or enabling an active one, does nothing.

The task is carried out by KeycloakAccountJob, every minute:

  • An account has one task at most: a new one replaces the pending one. Disabling then enabling before the task runs leaves only the enabling.
  • The task looks for the account in the user’s realm by username, else by e-mail.
  • DISABLE disables the account and closes its open sessions; ENABLE enables it again. The account is never deleted.
  • With no matching account (the user never signed in), the task counts as done.
  • Two matching accounts make the task fail: it waits for a person to sort out the duplicate.
  • A failed task keeps its error and is retried after a wait that doubles at each failure, from one minute up to application.keycloak-accounts.max-retry-delay (6 hours).
  • Each task is handled in its own transaction, under a FOR UPDATE SKIP LOCKED lock: a single replica handles it.

The pending and failing tasks show at the top of the user list (GET /api/v1/reglab-users/account-tasks, see Keycloak account changes).

subscription calls Keycloak’s admin API with, in each realm, a confidential client subscription-provisioning (application.keycloak-accounts.client-id) whose service account holds realm-management’s view-users and manage-users roles. Its secret, per realm, comes from Vault (application.keycloak-accounts.client-secrets). A realm without a secret leaves its tasks failing. Without application.keycloak-accounts.server-url, the job does nothing (one warning at startup) and the tasks wait.

Deletion

DELETE /api/v1/reglab-users/{id} is refused (403, error.user.hasAccesses) while the user has an access to a tenant. Otherwise, in one transaction, the deletion:

  • adds a DISABLE task for their Keycloak account, which is disabled but not deleted;
  • records Account disabled, with the cause “because the user was deleted”, unless they were disabled already;
  • deletes their record.

Last login

LastLoginJob reads, every 15 minutes (application.keycloak-accounts.login-cron), the LOGIN events of each realm with a configured secret, since its last read. A realm’s first read goes back login-history (90 days), within what Keycloak keeps.

  • The user is found by realm and username, else by e-mail, ignoring case.
  • A login opened by a support impersonation is no login of the user’s: it is skipped.
  • lastLoginAt only ever moves forward.

The realm must save its events (Realm settings › Events › Save events) and the provisioning client’s service account must hold view-events. “Never” in the screens therefore means “no login seen since recording started”, not necessarily “never signed in”.

The services’ membership lookups

The services ask subscription for a user’s accesses:

  • GET /api/v1/user-tenants/{service}/{username}?realm=: their accesses for a service. An end user reads only their own, in their token’s realm; the services of the user-tenants group and platform administrators read anyone’s, in the realm asked for or the default realm.
  • GET /api/v1/user-tenants/{tenantId}/{service}/{username}?realm=: their access to one tenant for a service, which the services check before serving the user.

These lookups return only the accesses in force: see What gives access to a tenant. The rights the plans carry are described in Rights.

Every user, whoever they are, also reads and changes their own mail language (GET and PUT /api/v1/reglab-users/me/language, fr or en).

Settings

Property Default Role
security.jwt.allowed-realms Accepted realms.
application.identity.default-realm mla Default realm.
application.keycloak-accounts.server-url Keycloak’s address; without it, no task is carried out.
application.keycloak-accounts.client-id subscription-provisioning Provisioning client, the same in every realm.
application.keycloak-accounts.client-secrets The client’s secret, per realm (Vault).
application.keycloak-accounts.cron every minute The account tasks’ runs; - disables them.
application.keycloak-accounts.max-retry-delay 6 hours Longest wait between two attempts of a task.
application.keycloak-accounts.timeout 10 seconds Longest a call to Keycloak may take.
application.keycloak-accounts.login-cron every 15 minutes Reading the logins; - disables it.
application.keycloak-accounts.login-history 90 days How far back a realm’s first read of logins goes.