Sign-up, OAuth & Invitations

restheart-accounts

RESTHeart Ulabase
Note

restheart-accounts is available from RESTHeart v9.4. Use the latest RESTHeart version: the plugin has received features and security fixes in almost every release since, such as the /users write restriction of 9.6.0 (see Security). Do not run it on v9.4 or v9.5.

restheart-accounts is a RESTHeart plugin that manages the accounts of an application. It provides HTTP endpoints for registration, email verification, team invitations, password reset and social login with Google and GitHub. The accounts are the documents of the users collection of mongoRealmAuthenticator.

Every feature is disabled until restheart.yml enables it. A login issues a standard RESTHeart JWT, so authentication and the ACL work on these accounts as on any other user.

flowchart LR
  subgraph Clients
    B[Browser / Mobile]
  end

  subgraph RA[restheart-accounts]
    direction TB
    EP["Endpoints\n/auth/register  /auth/verify\n/auth/forgot-password  /auth/reset-password\n/auth/invite  /auth/invitations  /auth/activate\n/auth/teams  /auth/team  /auth/team/members  /auth/switch-team\n/auth/profile  /auth/change-password\n/auth/oauth/authorize  /auth/oauth/callback"]
    MPS[MembershipProvider SPI]
    EP --> MPS
  end

  subgraph Storage
    M[(MongoDB\nusers · acl\nauth_invitations\noauth_codes)]
    E[Email\nSMTP / emails module]
  end

  P[OAuth Provider\nGoogle · GitHub]

  B -- HTTP + JWT --> RA
  RA --> M
  RA --> E
  RA -- OAuth flow --> P

Endpoints

Method URI Auth Description

POST

/auth/register

public

Signup with email + password; creates user and team

GET

/auth/verify

public

Verify email via token link; auto-login via cookie or URL fragment (delivery param)

POST

/auth/forgot-password

public

Request password reset link; the reset also verifies an account that never completed email verification (always 202)

PATCH

/auth/reset-password

public

Apply reset token; auto-login

POST

/auth/invite

owner / admin

Invite a user to the caller’s team †

PATCH

/auth/activate

public

Accept invitation, set password; auto-login

POST

/auth/resend-invite

owner / admin

Re-send an expired invitation (email in request body) †

GET

/auth/invitations

owner / admin

List pending (non-expired) invitations for the caller’s active team †

GET

/auth/teams

authenticated

List the caller’s team memberships (includes description when non-null) †

POST

/auth/teams

authenticated

Create an additional team for the caller; becomes the newly active team, reissues JWT cookie †

GET

/auth/team/members

authenticated

List the caller’s active team’s members (name, email, role, joinedAt) †

PATCH

/auth/team

owner

Rename/edit the caller’s active team (name, description) †

DELETE

/auth/team

owner

Delete the caller’s active team, only if it has no other members (409 otherwise) †

POST

/auth/switch-team

authenticated

Switch active team; reissues JWT cookie †

PATCH

/auth/profile

authenticated

Self-service update of the caller’s own profile (firstName, lastName, locale)

PATCH

/auth/change-password

authenticated

In-session password change (current password → new password); also lets OAuth-only accounts set their first password, with no current one to confirm

GET

/auth/oauth/authorize/{provider}

public

Redirect browser to the OAuth provider’s consent screen. Optional query parameters, all carried through the flow in the state token: pendingInviteToken, consentsAccepted, and returnTo — where the callback should land instead of frontendSuccessUrl, refused unless it is a path on this host

GET

/auth/oauth/callback/{provider}

public

OAuth callback: exchange code, upsert user, set JWT cookie

† Disabled when accountsConfig.membership-endpoints-enabled: false. See Custom Membership Providers for details.

/users Access

Caution
Since RESTHeart 9.6.0.

Once accountsInitializer is enabled, restheart-accounts manages the users collection. The MongoDB REST resource at /users then refuses the writes to the fields that define an account, whatever the ACL of the application allows.

  • PUT and POST are always rejected. A user document is created or replaced only through an endpoint of restheart-accounts: /auth/register, /auth/verify, /auth/activate, /auth/accept-invite, the OAuth callback.

  • PATCH is rejected if it touches password, roles, team, teams, sub, socialAuths, providerId, emailVerificationToken, emailVerificationCreatedAt, passwordResetToken, passwordResetCreatedAt, inviteToken, or _id. The form of the write does not matter: dot notation (team.role) and update operators ($set, $push, $addToSet) are checked too, so {"$set": {"teams.0.role": "owner"}} is rejected as {"teams": […​]} is.

  • Every other field, such as profile.*, consents and the fields of the application, is left to the ACL of the application.

The reason is a common permission. An ACL usually lets an authenticated user PATCH their own /users/{email} document, to edit the profile and the data of the application. Without this restriction the same permission would let the user rewrite roles, teams or password on their own document, for example to become owner of a team.

The restriction applies to every application built on restheart-accounts, whatever its ACL. The endpoints of restheart-accounts are not affected: they write roles, teams, password and the tokens to MongoDB with the driver, without going through the REST resource.

Exceptions. accountsConfig.users-unrestricted-roles lists the roles exempt from the restriction, both the PUT and POST block and the field list on PATCH. An internal admin console that manages roles and teams needs it:

accountsConfig:
  users-unrestricted-roles: [admin]

Unset, the default, the restriction applies to every caller. A multi-tenant deployment can override the list per team: see override-accounts-users-unrestricted-roles in the table below.

See Accounts / Security for the details.

The one-time tokens cannot be queried. Since v9.17 a request gets 403 when its filter or sort refers to emailVerificationToken, passwordResetToken or inviteToken in the users collection, or to token in auth_invitations. It applies to every method and to every role.

With one of these tokens a client could verify an address it does not own, or set the password of another account. A condition on a field reveals its value over several requests, also when the response hides the field.

The check is the denyFilterOnAccountTokens interceptor, enabled by default: see Security Hardening.

Multi-tenancy

One deployment can serve several tenants, each with its own users database and settings. Each service of restheart-accounts reads its settings from request attributes when they are present, and from the configuration otherwise. An interceptor that you write for your deployment attaches the attributes to the request.

See Multi-tenancy for the overrides, and Custom Membership Providers to replace the way teams are stored.

Table 1. Full list of override request attributes
Request attribute Overrides Effect

override-users-db

mongoRealmAuthenticator.users-db

Users and teams are read from and written to this database. mongoRealmAuthenticator honours the same attribute, so authentication follows. There is no equivalent override for the collection name.

override-cookie-domain

accountsConfig.cookie-domain

The JWT authentication cookie is scoped to this domain

override-accounts-app-name

accountsConfig.app-name

Application name used in email subjects and bodies

override-accounts-default-locale

accountsConfig.default-locale

The tenant’s default language of the emails, a code of 2 to 5 letters. Since 9.17.2: an earlier version ignores it

override-accounts-frontend-url

accountsConfig.frontend-url

Base URL used in transactional email links

override-accounts-frontend-app-url

accountsConfig.frontend-app-url

Redirect target after successful auto-login

override-accounts-tmpl-verification

file path / built-in template

Inline HTML string for the email-verification template (stored in MongoDB)

override-accounts-tmpl-password-reset

file path / built-in template

Inline HTML string for the password-reset template

override-accounts-tmpl-invite

file path / built-in template

Inline HTML string for the invitation template

override-accounts-oauth-google-client-id

oauthConfig.providers.google.client-id

Per-team Google OAuth client ID

override-accounts-oauth-google-client-secret

oauthConfig.providers.google.client-secret

Per-team Google OAuth client secret

override-accounts-users-unrestricted-roles

accountsConfig.users-unrestricted-roles

Roles exempt from the /users self-service write restriction (since 9.6.0)

override-accounts-signup-mgmt-enabled

(none — always active by default)

When false, skips the /users self-service write restriction entirely for the tenant, for every caller (since 9.6.0). Must be attached before authentication.

Without an attribute, the plugin uses the value in accountsConfig or oauthConfig.

A typical multi-team setup, e.g. Ulabase, has two interceptors of its own, both at REQUEST_BEFORE_EXCHANGE_INIT:

  • one maps the hostname of the request to its override-users-db and override-cookie-domain;

  • one reads the settings of the team from a MongoDB document and attaches the other attributes: app-name, frontend-url, the templates and the OAuth credentials of the team.

restheart-accounts does not ship these interceptors: they depend on how the deployment maps a request to a tenant. DbOverrideInterceptor, in the test plugins of RESTHeart, is a minimal example: it attaches override-users-db from a query parameter.

A new team needs no change to the configuration of the plugin.

OAuth & MCP

restheart-accounts implements social login with the /auth/oauth/* endpoints. Google and GitHub are built in, and a Java SPI registers other providers. The plugin exchanges the OAuth authorization code for the user information (with ScribeJava), creates or updates the user document in MongoDB, and issues a RESTHeart JWT cookie. The client only follows the redirects.

RESTHeart core also has an OAuth 2.1 authorization server, with the standard endpoints /authorize, /token and /introspect. That one authenticates tools and clients, mainly for MCP (Model Context Protocol). The two are independent: social login signs a person in with an external provider, and the authorization server issues tokens to the clients of RESTHeart.

See OAuth 2.0 Social Login for setup instructions for all providers, including the custom provider SPI.

Dependencies

  • RESTHeart ≥ 9.3 with mongoRealmAuthenticator and jwtTokenManager enabled.

  • An SMTP provider configured in the emails section, required by every flow that sends an email.

  • OAuth provider credentials, only for the providers enabled under oauthConfig.providers.