Protect an API with a ProtectedResource
Docs version v1 (main)Nauthera is pre-1.0, in a design-partner programUpdated
This guide registers an HR API with Nauthera from git and follows one token from the
sign-in to the API. At the end, only the applications that list the API get tokens for
it, each token lives at most ten minutes, it carries the signed-in employee's
department and cost center so the API authorizes without calling back, and the HR
system's sync job updates those two attributes for the next token to carry. You need a
Nauthera release with its operator running and kubectl. The main
limit: an access token keeps the attribute values it was minted with until it expires,
so the API's token lifetime is how long a change can take to reach it.
| Step | Who | Resource |
|---|---|---|
| 1. Give the team a range of identifiers | Platform team | Organization spec.protectedResources |
| 2. Declare the attributes | Platform team | Organization spec.userAttributes |
| 3. Register the API | API team | ProtectedResource |
| 4. Carry attributes in its tokens | API team | ProtectedResource spec.attributes |
| 5. Let applications call it | Application teams | OIDCClient spec.resources |
| 6. Check tokens in the API | API team | The API's own code |
| 7. Let an application update attributes | Platform team, backend team | Organization spec. |
How the pieces fit
A protected resource is an API an organization's access tokens may name as their audience (RFC 8707). Each piece is owned by the team that runs it, and none of them can reach past what the level above allows:
- The platform team decides which namespaces may register which identifiers, which attributes exist, and which applications may write them. One team cannot take another team's audience, raise its tokens' lifetime or invent an attribute.
- The API team registers its API: the identifier, the scopes it defines, how long its tokens live, which declared attributes they carry, and the client it introspects as.
- Each application team lists, on its
OIDCClient, the APIs its application calls. Once an organization registers one protected resource, a client gets tokens only for registered APIs it lists.
Use cases
| You want | What to set |
|---|---|
| An internal API only certain services may call | A ProtectedResource with a short accessTokenTtlSeconds, and spec.resources on exactly those services' OIDCClients, each using client_credentials |
| An API that authorizes on who the user is (their department, region, plan or tier) without calling UserInfo on every request | Attributes declared with accessToken: true and selected in the resource's spec.attributes (step 4) |
| Several teams sharing one cluster and one issuer | One spec.protectedResources entry per team namespace with that team's prefix (step 1), so no team can register an audience another team's API trusts |
| An API that must notice a revoked grant before its tokens expire | spec.introspectionClient, and the API introspects each token (step 6) |
| One token for two APIs | The client lists both and names both in resource: the token's aud names each, it lives as long as the shorter-lived allows, and it carries only the attributes both select |
| A backend that owns an attribute, such as a subscription plan, loyalty tier or cost center | applicationWrite: true on the attribute, a grant for the backend's client, and the service attribute API (step 7) |
| A remote MCP server | Protect an MCP server: the same resource, plus the metadata MCP clients discover it by |
1. Give the team a range of identifiers
An organization accepts a ProtectedResource only under an identifier that starts with a
prefix it gives the resource's namespace. This lets the hr namespace register any API
under https://hr.acme.example/:
apiVersion: nauthera.io/v1alpha1
kind: Organization
metadata:
name: acme
spec:
slug: acme
displayName: Acme
namespaces: [hr]
protectedResources:
- namespaces: [hr]
identifierPrefixes: ["https://hr.acme.example/"]A prefix is https:// with a host, ending in /, or urn: ending in :.
https://hr.acme.example/ covers https: and not
https:. In the main organization, the same entry goes in the
server's configuration, as orgs. with identifier_prefixes.
An organization that allocates nothing accepts no ProtectedResource; see
Allocating identifiers.
2. Declare the attributes
The API wants each employee's department and cost center. Declare both on the
organization, add them to the Organization from step 1, and mark each one
accessToken: true so a protected resource may select it:
apiVersion: nauthera.io/v1alpha1
kind: Organization
metadata:
name: acme
spec:
slug: acme
displayName: Acme
namespaces: [hr]
protectedResources:
- namespaces: [hr]
identifierPrefixes: ["https://hr.acme.example/"]
userAttributes:
- name: department
type: enum
userAccess: read # the employee sees it on the account page
applicationWrite: true # a grant may let an application write it (step 7)
scopes: [hr] # released only to a grant holding hr
accessToken: true # a ProtectedResource may select it
labels: {en: Department, de: Abteilung}
values:
- {value: sales, labels: {en: Sales, de: Vertrieb}}
- {value: engineering, labels: {en: Engineering, de: Entwicklung}}
- {value: people, labels: {en: People, de: Personal}}
- name: cost_center
type: string
maxLength: 16
pattern: "CC-[0-9]{4}"
userAccess: read
applicationWrite: true
scopes: [hr]
accessToken: true
labels: {en: Cost center, de: Kostenstelle}Each setting decides one thing:
scopesdecides who receives the value: a client must be grantedhr, which the employee consents to. The same scope releases both attributes through UserInfo.accessToken: truelets a protected resource select the attribute. It releases nothing by itself, and turning it off stops every resource's tokens carrying it from the next token on.userAccess: readshows the value on the employee's account page without letting them change it.writewould let them change it;none, the default, hides it.maxLengthkeeps the attribute's worst case small. A resource's selection may take at most 4096 bytes of a token at its declared bounds, and a string counts up to six bytes a character up to itsmaxLength(1024 without one): select a string only with one.
The types, constraints, labels and every rule of a declaration are on User Attributes.
3. Register the API
The HR team registers its API in its own namespace:
apiVersion: nauthera.io/v1alpha1
kind: ProtectedResource
metadata:
name: hr-api
namespace: hr
spec:
identifier: https://hr.acme.example/api
displayName:
en: HR API
de: Personal-API
scopes:
- name: hr.employees:read
descriptions:
en: Read employee records
de: Mitarbeiterdaten lesen
- name: hr.employees:write
descriptions:
en: Change employee records
accessTokenTtlSeconds: 600
introspectionClient: hr-api-introspectorkubectl apply -f hr-api.yaml
kubectl -n hr get protectedresource hr-api # PHASE Ready, reason Registeredidentifieris theresourcea client sends and theaudits token names, written in the form the server compares: scheme and host in lower case, no default port, an empty path as/. The server refuses any other spelling asRefusedand names the form to write.scopesare the scopes the API defines, up to 64. The dashboard's page for the API shows each with its description in the viewer's language.accessTokenTtlSeconds(30 to 86400) bounds every token for the API, whatever the calling client's own lifetime. It is also how long a changed attribute can lag in a token already issued, so keep it short for an API that authorizes on attributes.introspectionClientnames a confidentialOIDCClientin the same namespace, by its resource name, that the API introspects tokens as (step 6). Leave it out if the API checks JWTs locally only.
The introspection client is an ordinary confidential client:
apiVersion: nauthera.io/v1alpha1
kind: OIDCClient
metadata:
name: hr-api-introspector
namespace: hr
spec:
clientId: hr-api-introspector
displayName: HR API introspection
grantTypes:
- client_credentials
tokenEndpointAuthMethod: client_secret_basic
secret:
generate: true
4. Carry attributes in its tokens
Add the attributes the API reads to the resource:
apiVersion: nauthera.io/v1alpha1
kind: ProtectedResource
metadata:
name: hr-api
namespace: hr
spec:
identifier: https://hr.acme.example/api
displayName:
en: HR API
scopes:
- name: hr.employees:read
descriptions:
en: Read employee records
accessTokenTtlSeconds: 600
introspectionClient: hr-api-introspector
attributes:
- name: department
- name: cost_centerEach entry names a declared attribute; in: token, the default, is the one destination.
The server refuses, on spec.attributes[i].name, an attribute the organization does not
declare with accessToken: true, and, on spec.attributes, a selection whose worst case
is over 4096 bytes.
A user's access token for the API then carries an attribute when all of these hold:
- The grant's scopes, not the client's. The client registers
hrand requests it, and the user consents. A client registered forhrthat does not ask for it gets a token without the attributes. - Every audience. A token naming two APIs carries only what both select. A token
naming no
resource, or an identifier the organization does not register, carries none, and aclient_credentialstoken has no user and so no attributes. - No partial tokens. A selected value that breaks its declaration, or values that
encode past 4096 bytes, refuse that user's token with
invalid_target(causesattribute_value_invalidandtoken_) rather than leave an attribute out. A token for an API that selects neither is still issued. See theattributes_ too_ large invalid_targetrunbook. - Fresh at every mint. Each token, a refreshed one's included, carries the values and the declaration as they are when it is minted.
The token's payload, with the API's claims at the end:
{
"iss": "https://auth.example.com/orgs/acme",
"sub": "0193f4e2-7c1a-7b3e-9d2f-5a8c1e4b6d70",
"aud": "https://hr.acme.example/api",
"client_id": "hr-portal",
"scope": "openid hr hr.employees:read",
"org": "0193f4e0-1b2c-7d4e-8f90-a1b2c3d4e5f6",
"sid": "8kQ3vZ0hTn2dGm5c",
"auth_time": 1795305600,
"iat": 1795305612,
"nbf": 1795305612,
"exp": 1795306212,
"jti": "0193f4e2-8d10-7f2a-b3c4-d5e6f7a8b9c0",
"department": "engineering",
"cost_center": "CC-4711"
}The dashboard's Token claims page previews exactly this for a user, a client and a resource, and names each declared attribute it leaves out, and why.

5. Let applications call it
The portal signs employees in and calls the API for them. Its OIDCClient lists the API in
spec.resources, written exactly as the resource's spec.identifier is, and registers
the scopes it asks for, hr among them:
apiVersion: nauthera.io/v1alpha1
kind: OIDCClient
metadata:
name: hr-portal
namespace: hr
spec:
clientId: hr-portal
displayName: HR portal
redirectUris:
- https://portal.acme.example/callback
grantTypes:
- authorization_code
- refresh_token
responseTypes:
- code
scopes:
- openid
- hr
- hr.employees:read
- offline_access
resources:
- https://hr.acme.example/api
tokenEndpointAuthMethod: client_secret_basic
pkcePolicy: required_s256
allowRefresh: true
secret:
generate: trueIt asks for a token for the API with the resource parameter, at the authorization
endpoint and again at the token endpoint:
# The browser goes to:
https://auth.example.com/orgs/acme/authorize?client_id=hr-portal&response_type=code\
&redirect_uri=https%3A%2F%2Fportal.acme.example%2Fcallback\
&scope=openid%20hr%20hr.employees%3Aread%20offline_access\
&resource=https%3A%2F%2Fhr.acme.example%2Fapi\
&code_challenge=$CHALLENGE&code_challenge_method=S256&state=$STATE
# The portal redeems the code:
curl -s https://auth.example.com/orgs/acme/token \
-u "hr-portal:$CLIENT_SECRET" \
-d grant_type=authorization_code -d code="$CODE" \
-d redirect_uri=https://portal.acme.example/callback \
-d code_verifier="$VERIFIER" \
-d resource=https://hr.acme.example/api- Only listed APIs. In an organization that registers a protected resource, a
resourcethe client does not list, or one the organization does not register, is refusedinvalid_targetat/authorize,/par,/device_authorizationand/token, and the token endpoint checks again at every refresh. A client listing none may name none, so addspec.resourcesto every client that calls a registered API before or as the first one is registered. - Any order. A listed API need not be registered yet; a manifest may apply the client and the API in either order, and the server refuses a token for the API until it is.
- A service calling the API as itself uses
client_credentialswith the sameresource, and gets a token bounded by the API's lifetime, without attributes.
6. Check tokens in the API
The API never talks to the token endpoint. It validates each access token locally against the issuer's JWKS, and checks:
- The signature, against
https:./ / auth. example. com/ orgs/ acme/ . well-known/ jwks. json - The header's
typisat+jwt, so an ID token is refused. issis the organization's issuer.audnameshttps:./ / hr. acme. example/ api exp,nbfandiat.scopeholds the scope the route needs,hr.employees:readhere.- For DPoP-bound tokens, the proof against
cnf.jkt.
Then it authorizes on the attributes: an employee reads only their own department's records, a cost center decides which budget a request books against. The Go, .NET and Spring Boot guides set this validation up.
An API that must see a revoked grant before its tokens expire introspects them as its
introspection client. Introspection describes every token whose aud names the API, and
answers any other caller active: false:
curl -s https://auth.example.com/orgs/acme/introspect \
-u "hr-api-introspector:$INTROSPECTION_SECRET" \
-d token="$ACCESS_TOKEN"The answer says whether the token is active, with its scope, client_id, sub, aud
and exp; read the attributes from the token itself.
7. Let an application update attributes
The HR system is where a move to another department is recorded. Its sync job, hr-sync,
writes department and cost_center on the employee's account, and the employee's next
token for the API carries the new values. It needs no user-admin role: the organization
grants this one client these two attributes, and nothing else.
Grant the client
The attribute must be declared with applicationWrite: true (step 2 does), and the
organization grants the client in spec.. Add the grant to the
Organization from step 2, keeping everything else it declares:
apiVersion: nauthera.io/v1alpha1
kind: Organization
metadata:
name: acme
spec:
slug: acme
displayName: Acme
namespaces: [hr]
# protectedResources and userAttributes as in step 2
attributeWriteGrants:
- client: {namespace: hr, name: hr-sync}
attributes: [department, cost_center]
allUsers: true- A grant names one confidential
OIDCClientby namespace and name, 1 to 16 attributes, and exactly one ofallUsers: trueorgroups: up to 16Groups, by namespace and name, whose members it covers. A regional HR system can be granted only the members of its region's group. - Only the
Organizationgrants. No client can grant itself through its registration or dynamic registration, being released an attribute grants nothing, and a grant confers no read access. - A grant holds the client registered when it is written. A client deleted and created
again under the same
client_idholds nothing, and the admin API reportsclient_replaced; remove the grant and add it again to grant the new client. - A grant whose client or group does not resolve is not held, and the
OrganizationisPendingwithAttributeGrantReferenceNotFound; the rest of it is written all the same. A grant that widens what a client may write is recorded at warning onorganization.updated. - The main organization's grants are
orgs.in the server's configuration, naming the client bymain. attribute_ write_ grants clientIdandnamespaceand groups by display name.
Register the client
The client is confidential, registered for client_credentials and the scope
nauthera.:
apiVersion: nauthera.io/v1alpha1
kind: OIDCClient
metadata:
name: hr-sync
namespace: hr
spec:
clientId: hr-sync
displayName: HR system sync
grantTypes:
- client_credentials
scopes:
- nauthera.user-attributes:write
tokenEndpointAuthMethod: client_secret_basic
secret:
generate: trueThe scope opens no admin operation, and dynamic registration refuses it with the rest of
the nauthera. namespace, so a self-registered client never holds it.
Write
The job gets a token for the attribute API, <issuer>/. That exact
audience and no other is accepted, so a token for the attribute API is good for nothing
else, and no other token is good for it:
TOKEN=$(curl -s https://auth.example.com/orgs/acme/token \
-u "hr-sync:$CLIENT_SECRET" \
-d grant_type=client_credentials \
-d scope=nauthera.user-attributes:write \
-d resource=https://auth.example.com/orgs/acme/v1/user-attributes | jq -r .access_token)
curl -s -X PATCH \
https://auth.example.com/orgs/acme/v1/user-attributes/users/0193f4e2-7c1a-7b3e-9d2f-5a8c1e4b6d70 \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{"attributes": {"department": "people", "cost_center": "CC-2001"}}'{"changed": ["cost_center", "department"]}- The user is named by their id, the
subof their tokens. - A merge patch (RFC 7396): a member sets its attribute,
nullremoves it, and an absent one is left alone. A member equal to the stored value writes nothing. - All or nothing. Each value is held to its declaration, and one refused member
refuses the whole patch with a
400validation_errorwhose field errors nameattributes.<name>:not_allowedfor a department that is no member,patternfor a malformed cost center,read_onlyfor an attribute the grant does not name. - No oracle. A client with no grant, a user the grant does not cover and a user who
does not exist are answered alike,
403. The answer names what changed and never a value: writing grants no read. - Read on every request. The API reads the grant and the client's registration on
every write, so narrowing or removing the grant, making the client public, or taking
client_credentialsor the scope off its registration holds against a token already issued. - Audited as
user.updated, with the client as the actor and the changed names inchanged.
When the API sees the change
A token already issued is not changed. The API's accessTokenTtlSeconds bounds how long
an old value lives, and the next refresh carries the new one. UserInfo answers with the
new value at once.
Other ways an attribute changes
| Who | Where | What they may change |
|---|---|---|
| The user | The account page, /account beneath the organization's issuer, which an application can link to | Attributes declared userAccess: write |
| A new user | Sign-up | Every required attribute declared write, which the form asks for |
| An administrator | The dashboard's user page, the admin API, nauthera-cli | Any declared attribute, held to its declaration |
| A granted application | The service attribute API, as above | The attributes its grant names, on the users it covers |
An application acting for the signed-in user, rather than as itself, sends them to the account page for an attribute they may change.
When something is refused
| What you see | Why | Fix |
|---|---|---|
The ProtectedResource is Refused on spec.identifier, naming the namespace | No spec.protectedResources entry gives the namespace a prefix of it | Allocate one (step 1) |
Refused, naming the form to write | The identifier is not in the form tokens carry | Write it as the message says: https://hr.acme.example is https://hr.acme.example/ |
Refused on spec.attributes[i].name | The organization does not declare the attribute with accessToken: true | Set it on the declaration (step 2) |
Refused on spec.attributes | The selection's worst case is over 4096 bytes | Give each string a maxLength and each list a maxItems, or select fewer |
IntrospectionClientNotFound | The introspection client is missing, not registered yet or of another organization | Apply it in the same namespace; the API is registered without it meanwhile |
invalid_target, cause resource_not_registered | The organization registers protected resources, and not this identifier | Register it, byte for byte as clients send it |
invalid_target, cause resource_not_entitled | The client does not list the API in spec.resources | Add it |
invalid_target, cause attribute_value_invalid | The user's value of a selected attribute breaks its declaration | Correct the value, or the declaration; the admin API lists it under attribute_violations |
| A token without an attribute | The grant lacks its scope, the user has no value, or another API in aud does not select it | Preview it on the dashboard's Token claims page, which names the reason |
The Organization is Pending with AttributeGrantReferenceNotFound | A grant's client or group does not resolve | Apply the OIDCClient or Group it names |
401 from the attribute API | Not a client's own client_credentials token, or the client is no longer confidential and registered for the grant type and the scope | Request the token as in Write |
403 from the attribute API | No grant covering this user, or the token lacks nauthera. | Check the grant's attributes and allUsers or groups |
Every operator reason and OAuth error code has a runbook.
Related
- ProtectedResource: every field and status reason
- User Attributes: declaring, releasing and writing attributes
- Organization:
spec.protectedResources,spec.userAttributesandspec.attributeWriteGrants - OIDCClient:
spec.resources - Tokens & Sender Constraint: token formats, DPoP and introspection
- Protect an MCP server: a protected resource for MCP clients