Identity verification
Why identify() needs a token from your backend, how to mint one with the secret key, and how long it lives.
Since 0.26.0, identify() binds an install to an app user id only when your
backend vouches for that user. Your backend mints a short-lived identity
token with the project's secret key, hands it to the app, and the app passes it
to identify(). Rovenue checks the token before it binds anything.
Needs SDK 0.26.0 and server v0.9.0
Server v0.9.0 refuses a public-key identify without a valid token. SDK 0.25.0
and older never send one, so against v0.9.0 their identify binds nobody:
the native and React Native SDKs keep retrying the bind until the app is
upgraded, and the web SDK never posted it. Upgrade to 0.26.0 and pass the
token your backend mints.
Why
An app user id is not a secret. Apps log it, send it to analytics and show it in
support tools. Without a check, anyone who knew one could call identify() with
it from their own phone or browser, using your public key, which ships in every
app. Their install would then be merged into that user, and they could:
- read the user's entitlements,
- open the user's Stripe billing portal (cancel, change the card, read invoices),
- have their own purchases land on that user.
The identity token closes this. Only your backend holds the secret key, and it mints a token only for the user who just signed in to it.
Types on this page
// POST /v1/identity-tokens (secret key), request body
type MintIdentityTokenBody = {
appUserId: string; // the signed-in user's id, as you will pass it to identify()
};
// response: { data: MintIdentityTokenResponse }
type MintIdentityTokenResponse = {
identityToken: string; // opaque; pass it to identify() unchanged
expiresAt: string; // ISO 8601, 15 minutes after minting
};The flow
The user signs in to your backend
Your own authentication runs first, as it does today. Rovenue is not involved.
Your backend mints a token for that user
Add an endpoint to your backend that only a signed-in session can call. It asks Rovenue for a token for that session's user and returns it. The secret key stays on your server.
// Your backend (Node 18+). ROVENUE_SECRET_KEY is the project's secret key
// (rov_sk_…), from the same project and environment as the app's public key.
app.post("/api/rovenue/identity-token", requireSession, async (req, res) => {
const appUserId = req.session.userId; // never read it from the request body
const r = await fetch("https://api.rovenue.io/v1/identity-tokens", {
method: "POST",
headers: {
authorization: `Bearer ${process.env.ROVENUE_SECRET_KEY}`,
"content-type": "application/json",
},
body: JSON.stringify({ appUserId }),
});
if (!r.ok) return res.status(502).json({ error: "could not mint an identity token" });
const { data } = await r.json(); // { identityToken, expiresAt }
res.json(data);
});Take the user id from your own session, never from the request. An endpoint that mints a token for any id it is sent gives the attack straight back.
The app passes the token to identify()
const { identityToken } = await fetch(`${BACKEND}/api/rovenue/identity-token`, {
method: 'POST',
headers: { authorization: `Bearer ${sessionToken}` },
}).then((r) => r.json());
await Rovenue.identify(userId, identityToken);let identityToken = try await myBackend.rovenueIdentityToken() // your endpoint above
try await Rovenue.shared.identify(appUserId: userId, identityToken: identityToken)val identityToken = myBackend.rovenueIdentityToken() // your endpoint above
Rovenue.shared.identify(userId, identityToken)final identityToken = await myBackend.rovenueIdentityToken(); // your endpoint above
await Rovenue.instance.identify(userId, identityToken);const { identityToken } = await fetch("/api/rovenue/identity-token", { method: "POST" }).then((r) => r.json());
await rovenue.identify(userId, { identityToken });Rovenue verifies the token, then binds
The server accepts the token only when all of these hold:
- it was minted by this Rovenue server, for this project;
- it was minted with a key of the same environment as the public key the app calls with (a Sandbox token is refused on a Production key, and the other way round);
- its user is the
appUserIdin the same call; - it has not expired (30 seconds of clock leeway).
Then the bind runs as before: when the appUserId already belongs to another
install, this install is merged into it and its purchases and balances follow.
See Identifying users.
Lifetime and refresh
A token lives 15 minutes. Mint one for each identify() call, right before
it, instead of caching one. Apps already identify on sign-in and on launch for a
signed-in user, so fetch a fresh token at both points.
On the native SDKs, identify() is optimistic: the local identity changes at
once, and the server bind runs in the background, retried with the same token if
the network fails. If the server refuses the token, the SDK drops that bind (it
is never retried) and identify() throws
IdentityTokenInvalid. Mint a new token
and identify again. On the web, identify() is a server call: it rejects with a
RovenueApiError whose code is IDENTITY_TOKEN_INVALID, and the local
identity stays as it was.
A token cannot be revoked before it expires. It is only useful to whoever already holds your user's session, for one user, one project and one environment.
Errors
| Status | Code | When |
|---|---|---|
400 | VALIDATION_ERROR | identify with the public key and no identityToken. |
401 | IDENTITY_TOKEN_INVALID | The token is expired, tampered, or minted for another user, project or environment. The response never says which. |
403 | FORBIDDEN | POST /v1/identity-tokens was called with a public key. Only the secret key can mint. |
500 | INTERNAL_ERROR | The server has no ENCRYPTION_KEY (a self-hosted misconfiguration). It then refuses every token, so public-key identify answers 401. |
Calling identify from your server
A call to POST /v1/identify made with the secret key is trusted and needs
no token: the secret key already proves the caller is your backend. It binds an
app user id to an install that already exists; it never creates one.
Never put the secret key in an app or a web page to mint tokens on the device. Anyone can extract it, and it can do far more than mint tokens.