APIs and services
Protecting your API
Register the API in the realm with the scopes it understands:
nod api register acme https://api.example.com --scope orders:read --scope orders:write --ttl 900
Your API accepts Nod access tokens for it, and checks each one:
- **It's a JWT with header
typ: at+jwt** (RFC 9068). Refuse anything else, ID tokens included. - The signature verifies with the key named by
kidin<issuer>/jwks. Fetch the key set, cache it bykid, and refetch (rate-limited) when you meet an unknownkid. **Use the key's algorithm (EdDSA, or RS256 for an API set totoken_alg: RS256), never the header's choice,** and never acceptalg: noneor HMAC. - **
issis the realm's issuer, exactly.audis your API's identifier.exp** has not passed. - **
scope** (space-separated) contains what the operation needs.
Most JWT libraries do steps 1–3 if you pin the algorithm, issuer and audience. Alternatively, a confidential client can ask Nod with POST <issuer>/introspect (RFC 7662), which answers {"active": false} for anything not active.
People calling APIs (roles)
An app that signs people in can ask for a token for an API by adding resource=<API identifier> to the authorization request, and the API's scopes to scope:
/authorize?…&scope=openid orders:read orders:write&resource=https://api.example.com
What the token carries comes from roles:
nod role create acme clerk --description "reads orders"
nod role grant acme clerk https://api.example.com --scope orders:read
nod user role acme alice clerk # --remove to take it away
The access token's aud is the API and the issuer, so userinfo still works. Its scope holds the requested API scopes the person's roles allow, and its permissions claim lists everything they hold there, as Auth0's RBAC does. Roles are read again at every refresh, so removing a role takes effect at the next one. The management API manages roles too (roles:read, roles:write).
Roles by email domain, and a role on everything
A realm can give a role to everyone who proved an address at a domain, and a role can allow every scope of every API, those registered later included. Both are read at each token and refresh, and neither is listed as assigned:
nod role grant lunaor staff --all # --remove to stop; the CLI and config files only
nod realm set lunaor --domain-roles lunaor.com=staff # "" none; through the API, a nod
An address an administrator marked verified, an unverified one, a subdomain or a lookalike domain gives nothing. roles:write can't set --all, as it can't give permissions on the management API.
Email in access tokens
An API that grants by email (a team drive shared with alice@acme.example, or with everyone at acme.example) can read the person's address straight from its access token instead of calling userinfo:
nod api register acme https://drive.example.com --scope files:read --token-email
Its tokens then carry email and email_verified (RFC 9068's identity claims), when the person has an email and the app asked for the email scope. email_verified is true only for an address Nod verified, or one a federated connection (a Google company account, say) vouched for in a domain listed on that connection. Count an email only when it's true. The admin API and configuration files call the setting token_email. ID tokens and other APIs' tokens don't change.
The person's org in access tokens (nod_account)
An API that serves orgs can read which org the caller is acting in from its access token. Turn it on per API (off by default):
nod api set acme https://chat.example --claim nod_account # `api set acme https://chat.example` turns it off
(Admin API and configuration files: "claims": ["nod_account"] on the API.) Its tokens then carry nod_account, an org's account id (acct-…), as a string:
- The app may pick the org with
nod_account=acct-…on/authorize(also in PAR, request objects, and/device_authorization). The person must be an accepted member of it, or the sign-in ends withinvalid_request(for the device flow, their approval is refused). - Otherwise, if the person is an accepted member of exactly one org, that one. In several and not picked, or in none: no claim. Nod never guesses.
- Membership is the same rule nods use: members who accepted (an invitee doesn't count until they do), matched by their id or an address they proved themselves or a trusted connection vouched for, never one an administrator marked verified. In the accounts realm that's any org; in a realm an org owns (Lunaor's
lunaor), that org alone; elsewhere none. - At every refresh it's checked again. Taken out of the picked org, the refreshed token has no claim, and the pick is cleared from that sign-in for good (it doesn't fall back to another org): sign in again to pick.
- Agents' tokens (with
act) carry their person's org by the same rule; an agent can't name one. - A machine client's own token (client credentials) carries it only when the client is an account's own key: that account. Other services' tokens have none.
- Token exchange keeps the subject token's
nod_accountwhen the target API opts in too and the person is still a member; it never adds one the subject token didn't have. - Never in ID tokens, and never in tokens for APIs that didn't opt in.
Several APIs, one sign-in
An app that calls several APIs asks for all of them at once, repeating resource (RFC 8707), and then gets one token per API, each good only there:
/authorize?…&scope=openid offline_access orders:read billing:read
&resource=https://orders.example&resource=https://billing.example
POST /token grant_type=refresh_token&refresh_token=…&resource=https://billing.example
- Naming an API at the token endpoint (with a code or a refresh token) gives a token whose
audis that API alone, carrying only its scopes andpermissions. A billing token is no use at the orders API. A name the authorization didn't ask for isinvalid_target, and the code or refresh token stays usable, so the app can ask again. - Naming none: with one API, its token as above (
audthe API and the issuer). With several, a token for Nod only (userinfo), since there is no single API to bind it to. - PAR and request objects take several too:
resourcerepeated in the pushed form, or an array in the request object. Every other parameter may appear only once.
Services calling APIs (client credentials)
nod client register acme billing-service --machine # prints the secret once
nod client grant acme billing-service https://api.example.com --scope orders:read
curl -u billing-service:$SECRET https://auth.example.com/acme/token \
-d grant_type=client_credentials -d resource=https://api.example.com [-d scope=orders:read]
- **
resourceis required** (RFC 8707) and names one API. The token'saudis that API and nothing else. - **With no
scope,** the token carries everything the client was granted there. Withscope, it carries just what you asked for, all of which must be granted (invalid_scopeotherwise). - **A service with no grant on that API gets
invalid_target.** Revoking a grant (nod client grant …with no--scope) stops new tokens at once. Tokens already issued expire with their lifetime, and the management API checks the grant on every call.
With a key instead of a secret (private_key_jwt)
A service can prove who it is with its own private key, so no shared secret exists: nothing to leak from Nod, and nothing to copy between machines. Register the client with its public keys (a JWKS; never the private part):
nod client register acme deployer --machine --jwks deployer.jwks.json # no secret is made
or through the management API, POST /api/v1/clients with "token_endpoint_auth_method": "private_key_jwt" and "jwks": {"keys": [...]}. To get a token, the service signs a short JWT with its key (ES256, EdDSA or RS256, kid naming the key) and sends it instead of a secret:
iss, sub the client's id
aud the realm's issuer, as the only value: https://auth.example.com/acme (not the token endpoint)
exp at most five minutes ahead
jti unique: each assertion works once
typ (header, optional) client-authentication+jwt
curl https://auth.example.com/acme/token -d grant_type=client_credentials -d resource=https://api.example.com \
-d client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer -d client_assertion=$JWT
Coming from Auth0? Auth0 wants the token endpoint (https://<tenant>/oauth/token) in aud; Nod refuses it and wants the issuer instead (the error says which). Libraries that default to the token endpoint usually take an audience option. Most OAuth libraries do this for you given the private key (openid-client: PrivateKeyJwt(key)). The audience rule is RFC 10070's fix for audience injection: Nod refuses an assertion naming its token endpoint, or naming anything besides its issuer. A client authenticates one way only: a key client can't use a secret, and a secret client can't switch to a key.
With a TLS client certificate (mTLS, RFC 8705)
A service can also authenticate with a TLS client certificate, for FAPI-style deployments. Its access tokens are then bound to that certificate (cnf.x5t#S256), so a stolen token is useless without the private key. Register the client with its certificates as a JWKS whose keys carry them in x5c (base64 DER):
nod client register acme payments --machine --jwks payments-certs.jwks.json --tls
or "token_endpoint_auth_method": "self_signed_tls_client_auth" with jwks over the management API.
Nod doesn't terminate the client's TLS itself: the proxy in front of it does. The proxy asks for a client certificate, accepts the client's own (self-signed is fine; the thumbprint is what's registered), and passes it on in the Client-Cert header (RFC 9440: Client-Cert: :<base64 DER>:). Nod believes that header only from the addresses in --trusted-proxies. From anyone else it's removed before any handler sees it. An API that accepts these tokens checks that the certificate on its own TLS connection has the thumbprint in cnf.x5t#S256. Userinfo does that check itself.
A client authenticates one way only: with its certificate, no secret or assertion, and its tokens aren't also DPoP-bound.
APIs calling APIs for a person (token exchange)
The orders API, handling Alice's request, needs to call the billing API as Alice. It exchanges her token for one to billing (RFC 8693), in her name:
nod client register acme orders-svc --machine
nod client grant acme orders-svc https://billing.example --scope billing:read --scope billing:write
nod client exchange acme orders-svc --from https://orders.example
curl -u orders-svc:$SECRET $ISSUER/token -d grant_type=urn:ietf:params:oauth:grant-type:token-exchange \
-d subject_token=$ALICES_TOKEN -d subject_token_type=urn:ietf:params:oauth:token-type:access_token \
-d resource=https://billing.example
- Only for people's tokens to the APIs the service serves (
--from), presented by that service (a machine client, authenticated). A token for any other API, or a service's own token, is refused. - The new token is Alice's, for billing:
subis Alice,act.subisorders-svc, so billing can tell who acts for whom. Its scopes are whatorders-svcwas granted there and what Alice's roles allow there. - It lives no longer than her token, and shares her grant: when that's revoked (sign-out everywhere, blocking), the exchanged token stops working at introspection too.
- Actor tokens, and exchanging for ID or refresh tokens, aren't supported.
Scopes only on a person's behalf
A grant is also what the service can get for itself with client credentials. When it should write only for a person, never alone, mark those scopes on-behalf-only:
nod client grant acme orders-svc https://billing.example --scope billing:read --on-behalf-only billing:write
(--on-behalf-only S grants S too; through the management API, PUT /clients/{id}/grants with {"resource", "scopes", "on_behalf_only"}, each of on_behalf_only one of scopes.)
- Never in its own token. Client credentials asking for
billing:writegetsinvalid_scope; asking for nothing getsbilling:readalone. With every scope marked, there's nothing it can have for itself (invalid_scope). - In an exchanged token as any other scope: granted, and bounded by what the person holds.
- The management API checks it at every call, as it checks the rest of the grant: a scope marked on-behalf-only is a 403 in the client's own token, even one issued before the scope was marked. Other APIs see only the token, which client credentials no longer issues with it; one issued before lives out its lifetime (the API's
token_ttl_s). - A scope kept for itself mustn't cover a marked one (
billing:*for itself andbilling:writemarked is refused), and granting again replaces the marks with the grant: a grant without--on-behalf-onlyworks both ways, as every grant did before.
Scope patterns
An API whose scopes name things created all the time can define patterns: * stands for one whole segment between colons, and a dotted prefix ending a segment (nod.*, a.b.*) stands for any dotted name under that prefix, at any depth (nod.acme, nod.acme.deploys; never nod itself or nodx). Sendack's topic scopes use it: sendack:topic:nod.*:publish.
nod api register acme https://twilightdb.example --scope 'twilightdb:db:*:read' --scope 'twilightdb:db:*:write'
nod client grant acme app https://twilightdb.example --scope twilightdb:db:orders:read # one database
nod client grant acme ops https://twilightdb.example --scope 'twilightdb:db:*:read' # any (operator)
- Tokens carry concrete scopes, such as
twilightdb:db:orders:read. A wildcard scope appears in a token only if that very wildcard was granted. - **Your API treats
*in a token scope as "any".** - **
*matches exactly one non-empty segment,** sotwilightdb:db:a:b:readmatches nothing.