Skip to content
SupportDashboard

Elevated Permissions

Require step-up re-authentication with a security key, TOTP, or a one-time password sent by email or SMS before sensitive actions using an elevated session claim.

elevated permissions step-up authentication MFA multi-factor TOTP OTP security key security re-authentication sensitive actions

In some scenarios you may want to add an extra layer of security to perform certain actions or view certain data. For instance, you may wish to allow users to view their profile information after authenticating but require them to confirm changes to their profile by performing an extra verification step, such as touching a security key, entering a code from their authenticator app, or entering a one-time password sent to their email or phone.

overview-flow

This is accomplished by adding the claim x-hasura-auth-elevated: $user-id to the access token in response to the extra security challenge. With this new claim in mind you can start writing permissions that require this extra step.

It is important to keep in mind the claim is added to the access token so, for as long as you have that access token, you will have elevated access. Once the access token is renewed (due to expiration or any other reason), the claim will be lost and a new security challenge will be required to add it back.

A signed-in user can elevate their session with any of the following methods. A method is only available when it is enabled for the project and the user has set it up:

Method Enabled with (nhost.toml) Available to the user when
webauthn auth.method.webauthn.enabled They have at least one security key registered
totp auth.totp.enabled They have activated TOTP on their account
otp-email auth.method.otp.email.enabled They have a verified email address
otp-sms auth.method.otp.sms.enabled They have a verified phone number

Every method ends with the same result: a new session whose access token carries the x-hasura-auth-elevated claim. The JavaScript SDK stores that session automatically, so subsequent requests made with the same client use the elevated token. If you call the REST API directly, store the returned session, including its access and refresh tokens, and use the new access token for the protected request.

Use GET /elevate to find out whether the current user needs to elevate their session, which methods they can use, and whether their session is already elevated:

const { body } = await nhost.auth.getElevationMethods();
// {
// elevationRequired: true,
// methods: ['webauthn', 'totp', 'otp-email'],
// sessionElevated: false
// }
  • elevationRequired tells you whether the protected auth endpoints require an elevated session for this user under the current mode. It describes the policy, not the token, so it can be true even when sessionElevated is already true.
  • methods lists the methods enabled for the project and available to this user, even in disabled mode.
  • sessionElevated is true when the current access token has an x-hasura-auth-elevated claim matching its user ID, so you can skip the elevation step.

If elevationRequired is true, sessionElevated is false, and methods is empty, offer first-factor setup rather than a code prompt. The setup exceptions let the user register their first security key or activate TOTP before elevating.

import { startAuthentication } from '@simplewebauthn/browser';
const response = await nhost.auth.elevateWebauthn();
const credential = await startAuthentication({
optionsJSON: response.body,
});
await nhost.auth.verifyElevateWebauthn({ credential });

All elevation endpoints require an Authorization: Bearer <access-token> header for the user being elevated; the SDK supplies it automatically. Email and SMS elevation use the signed-in user’s verified address or phone number, so neither the request nor the verification call accepts an email address or phone number.

Signing in, including completing a TOTP MFA challenge, does not itself add the elevated claim. Use the elevation endpoints for this extra step.

  • Email elevation codes expire after 10 minutes; SMS elevation codes expire after 5 minutes. Both are six-digit, single-use codes.
  • Only one email or SMS OTP can be outstanding per user. Requesting another code, including for sign-in or a phone-number change, replaces the previous one. When offering resend or switching methods, prompt for the newest code.
  • An incorrect or expired code returns invalid-otp. After five incorrect attempts, the code is invalidated and verification returns otp-too-many-attempts; request a new code to continue. Error concealment can replace this with a generic error.
Method Endpoints SDK
Discovery GET /elevate getElevationMethods
Security key POST /elevate/webauthn, POST /elevate/webauthn/verify elevateWebauthn, verifyElevateWebauthn
TOTP POST /elevate/totp elevateTotp
Email OTP POST /elevate/otp/email, POST /elevate/otp/email/verify elevateOTPEmail, verifyElevateOTPEmail
SMS OTP POST /elevate/otp/sms, POST /elevate/otp/sms/verify elevateOTPSms, verifyElevateOTPSms

All /elevate/* endpoints are subject to the brute-force rate limit. Requesting an OTP also counts against the email or SMS rate limit respectively.

You can use the claim x-hasura-auth-elevated in exactly the same way you would normally use X-Hasura-User-Id for added security. For instance, the following permissions would allow users to see their data without any extra security:

example-select-permissions

While the following permissions would require them first to elevate their access in order to update them:

example-update-permissions

Some user information needs to be changed via Auth’s API rather than via GraphQL mutations. These endpoints are:

To protect these endpoints and require the elevated claim you can use the following configuration option:

nhost.toml
[auth.elevatedPrivileges]
mode = 'required'

The mode can be one of disabled (default), recommended, required.

In disabled mode the elevated claim isn’t required for any of the endpoints above. This is the default behavior. Users can still elevate their sessions, and GraphQL permissions that check x-hasura-auth-elevated still apply independently of this setting.

In recommended mode, the elevated claim is only required if the user has at least one elevation method available. Users with no available method can perform the actions above without elevating. A registered security key or activated TOTP only counts while that method is enabled for the project.

In required mode, all of the endpoints above require an access token with the elevated claim. The only exception is setting up a first factor: if the user has no elevation method available, they can register a security key (/user/webauthn/add and /user/webauthn/verify) or start TOTP setup (/mfa/totp/generate) without the elevated claim. Every other endpoint still requires it, so such a user can’t perform any other protected action until they set up a factor and elevate their session. Once the user has any method available, setting up additional factors requires the elevated claim as well.

To demonstrate this functionality we have implemented them in our react-apollo (source) and vue-apollo (source) examples.

To demonstrate how the elevated claim can work for permissions you can check the “Secret Notes” example. In this example, we are allowing users to see their secret notes by giving the select permissions notes.user_id eq X-Hasura-User-Id

view secret notes

However, we are requiring elevated permissions to insert, update and delete with the permissions notes.user_id eq x-hasura-auth-elevated. In our example we automatically initiate the elevation process if the claim isn’t already present:

add secret notes

Note that after elevating permissions the secret note is added and the elevated claim persists until there is a token refreshed.

after adding secret notes

In addition, to demonstrate the auth.elevatedPrivileges setting, we have set it to required in this example requiring elevated access to perform certain changes. For instance, if you try to change your password you will first have to elevate your access:

change password

After elevating access the password is changed:

password changed