Sign-up, OAuth & Invitations
restheart-accounts
|
Note
|
|
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 |
|---|---|---|---|
|
|
public |
Signup with email + password; creates user and team |
|
|
public |
Verify email via token link; auto-login via cookie or URL fragment ( |
|
|
public |
Request password reset link; the reset also verifies an account that never completed email verification (always 202) |
|
|
public |
Apply reset token; auto-login |
|
|
owner / admin |
Invite a user to the caller’s team † |
|
|
public |
Accept invitation, set password; auto-login |
|
|
owner / admin |
Re-send an expired invitation (email in request body) † |
|
|
owner / admin |
List pending (non-expired) invitations for the caller’s active team † |
|
|
authenticated |
List the caller’s team memberships (includes |
|
|
authenticated |
Create an additional team for the caller; becomes the newly active team, reissues JWT cookie † |
|
|
authenticated |
List the caller’s active team’s members (name, email, role, joinedAt) † |
|
|
owner |
Rename/edit the caller’s active team (name, description) † |
|
|
owner |
Delete the caller’s active team, only if it has no other members ( |
|
|
authenticated |
Switch active team; reissues JWT cookie † |
|
|
authenticated |
Self-service update of the caller’s own profile ( |
|
|
authenticated |
In-session password change (current password → new password); also lets OAuth-only accounts set their first password, with no current one to confirm |
|
|
public |
Redirect browser to the OAuth provider’s consent screen. Optional query parameters, all carried through the flow in the state token: |
|
|
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.
-
PUTandPOSTare always rejected. A user document is created or replaced only through an endpoint ofrestheart-accounts:/auth/register,/auth/verify,/auth/activate,/auth/accept-invite, the OAuth callback. -
PATCHis rejected if it touchespassword,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.*,consentsand 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.
| Request attribute | Overrides | Effect |
|---|---|---|
|
|
Users and teams are read from and written to this database. |
|
|
The JWT authentication cookie is scoped to this domain |
|
|
Application name used in email subjects and bodies |
|
|
The tenant’s default language of the emails, a code of 2 to 5 letters. Since 9.17.2: an earlier version ignores it |
|
|
Base URL used in transactional email links |
|
|
Redirect target after successful auto-login |
|
file path / built-in template |
Inline HTML string for the email-verification template (stored in MongoDB) |
|
file path / built-in template |
Inline HTML string for the password-reset template |
|
file path / built-in template |
Inline HTML string for the invitation template |
|
|
Per-team Google OAuth client ID |
|
|
Per-team Google OAuth client secret |
|
|
Roles exempt from the |
|
(none — always active by default) |
When |
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-dbandoverride-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
mongoRealmAuthenticatorandjwtTokenManagerenabled. -
An SMTP provider configured in the
emailssection, required by every flow that sends an email. -
OAuth provider credentials, only for the providers enabled under
oauthConfig.providers.