Trusting attributes on your backend
A user's attributes (Plan: Pro, Tester: Yes) are what support set on them. Your app can read them, but your app runs on the user's device, and whoever controls the device controls what the app sees. A user can rewrite the answers ALIAS sends (with a proxy, or the browser's developer tools), and they can change your app's own code. Anything you unlock in the app alone can be unlocked by them.
So when an attribute decides something that matters, decide it on your backend, from a token ALIAS signed:
- Your app asks ALIAS for a token:
attributesToken(nonce). - Your app sends the token to your backend, with the request it's about.
- Your backend checks the token with the deployment's public keys and acts on what it says: this user, this product, these attributes.
A changed token fails the check. A token from another user names that user, and every token stops being good 5 minutes after it's made.
Checking the token in the app or the browser proves nothing: the user can change that check too. Only a check on a machine you control counts.
The token
A JWT signed with Ed25519 (alg EdDSA), from GET /api/v1/me/token:
| Claim | |
|---|---|
iss | your ALIAS deployment's URL, as your app reaches it (https://support.example.com) |
aud | the product's id |
sub | the user's id |
attributes | the user's attributes, id → value ({"plan": "pro"}); {} for none |
nonce | what your app sent, if anything |
iat, exp | when it was made, and 5 minutes on |
The public keys are at /.well-known/jwks.json on your deployment, a standard JWK Set. Any JWT library reads it. Each token's header names its key (kid). After a key is rotated, the key before stays published until its tokens have expired.
The nonce
A token is good for 5 minutes, so one taken earlier could be sent again. Where that matters, have your backend make a nonce (a random string, 128 characters at most) for the request, have the app ask for a token with it, and check that the token carries it.
In the app
// TypeScript (browser or Node)
const nonce = await (await fetch('/api/nonce')).text(); // your backend's
const { token } = await alias.attributesToken(nonce);
await fetch('/api/unlock-pro', { method: 'POST', headers: { Authorization: 'Bearer ' + token } });// Swift
let token = try await alias.attributesToken(nonce: nonce).token# Godot
var r: Dictionary = await alias.attributes_token(nonce)
var token: String = r.data.tokenOn your backend
Check the signature, the issuer, the audience and the expiry, then the nonce, then the attributes. These use the common JWT library of each language.
// Node, with jose
import { createRemoteJWKSet, jwtVerify } from 'jose';
const ALIAS = 'https://support.example.com';
const keys = createRemoteJWKSet(new URL(ALIAS + '/.well-known/jwks.json')); // cached, read again on a new kid
export async function aliasUser(token, nonce) {
const { payload } = await jwtVerify(token, keys, { issuer: ALIAS, audience: 'road-builder', algorithms: ['EdDSA'] });
if (payload.nonce !== nonce) throw new Error('not this request\'s token');
return { id: payload.sub, attributes: payload.attributes }; // payload.attributes.plan === 'pro'
}# Python, with PyJWT (pip install "pyjwt[crypto]")
import jwt
ALIAS = "https://support.example.com"
keys = jwt.PyJWKClient(ALIAS + "/.well-known/jwks.json") # cached
def alias_user(token: str, nonce: str) -> dict:
key = keys.get_signing_key_from_jwt(token)
claims = jwt.decode(token, key.key, algorithms=["EdDSA"], issuer=ALIAS, audience="road-builder")
if claims.get("nonce") != nonce:
raise ValueError("not this request's token")
return {"id": claims["sub"], "attributes": claims["attributes"]}// Go, with github.com/golang-jwt/jwt/v5 and github.com/MicahParks/keyfunc/v3
const alias = "https://support.example.com"
keys, err := keyfunc.NewDefaultCtx(ctx, []string{alias + "/.well-known/jwks.json"}) // refreshed in the background
type claims struct {
Attributes map[string]string `json:"attributes"`
Nonce string `json:"nonce"`
jwt.RegisteredClaims
}
func aliasUser(token, nonce string) (*claims, error) {
var c claims
_, err := jwt.ParseWithClaims(token, &c, keys.Keyfunc, jwt.WithValidMethods([]string{"EdDSA"}),
jwt.WithIssuer(alias), jwt.WithAudience("road-builder"), jwt.WithExpirationRequired())
if err != nil {
return nil, err
}
if c.Nonce != nonce {
return nil, errors.New("not this request's token")
}
return &c, nil // c.Subject: the user; c.Attributes["plan"] == "pro"
}What it doesn't cover
- What the user sends. A report's telemetry and context come from the user's device, and support should treat them as what the app said, not as proof.
- Your app's own screens. A token tells your backend the truth; it can't stop the user from changing what your app shows them.
The keys
One key set for the deployment. By default ALIAS makes its key the first time it's needed and keeps it in its database, the same for every replica. To bring your own, set ALIAS_SIGNING_KEY (see Deploying). aliasctl signing show names the keys, never their values. aliasctl signing rotate makes a new one (it needs settings.edit), and the one before stays published until its tokens expire.