Skip to content

The v1 docs follow nauthera-server's main branch and its 0.x releases on the way to 1.0, so fields and APIs can still change.

v1

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.

StepWhoResource
1. Give the team a range of identifiersPlatform teamOrganization spec.protectedResources
2. Declare the attributesPlatform teamOrganization spec.userAttributes
3. Register the APIAPI teamProtectedResource
4. Carry attributes in its tokensAPI teamProtectedResource spec.attributes
5. Let applications call itApplication teamsOIDCClient spec.resources
6. Check tokens in the APIAPI teamThe API's own code
7. Let an application update attributesPlatform team, backend teamOrganization spec.attributeWriteGrants

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 wantWhat to set
An internal API only certain services may callA 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 requestAttributes declared with accessToken: true and selected in the resource's spec.attributes (step 4)
Several teams sharing one cluster and one issuerOne 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 expirespec.introspectionClient, and the API introspects each token (step 6)
One token for two APIsThe 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 centerapplicationWrite: true on the attribute, a grant for the backend's client, and the service attribute API (step 7)
A remote MCP serverProtect 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://hr.acme.example/api and not https://hr.acme.example.evil/. In the main organization, the same entry goes in the server's configuration, as orgs.main.protected_resources 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:

  • scopes decides who receives the value: a client must be granted hr, which the employee consents to. The same scope releases both attributes through UserInfo.
  • accessToken: true lets 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: read shows the value on the employee's account page without letting them change it. write would let them change it; none, the default, hides it.
  • maxLength keeps 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 its maxLength (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-introspector
kubectl apply -f hr-api.yaml
kubectl -n hr get protectedresource hr-api   # PHASE Ready, reason Registered
  • identifier is the resource a client sends and the aud its 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 as Refused and names the form to write.
  • scopes are 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.
  • introspectionClient names a confidential OIDCClient in 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
A protected resource: its identifier, token lifetime, introspection client and scopes
A protected resource on the dashboard: its identifier, token lifetime, introspection client and scopes.

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_center

Each 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 hr and requests it, and the user consents. A client registered for hr that 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 a client_credentials token 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 (causes attribute_value_invalid and token_attributes_too_large) rather than leave an attribute out. A token for an API that selects neither is still issued. See the invalid_target runbook.
  • 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.

The token claims preview naming each declared attribute a token leaves out, and why
The token claims preview naming the declared attributes a token leaves out, and the reason for each.

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: true

It 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 resource the client does not list, or one the organization does not register, is refused invalid_target at /authorize, /par, /device_authorization and /token, and the token endpoint checks again at every refresh. A client listing none may name none, so add spec.resources to 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_credentials with the same resource, 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:

  1. The signature, against https://auth.example.com/orgs/acme/.well-known/jwks.json.
  2. The header's typ is at+jwt, so an ID token is refused.
  3. iss is the organization's issuer.
  4. aud names https://hr.acme.example/api.
  5. exp, nbf and iat.
  6. scope holds the scope the route needs, hr.employees:read here.
  7. 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.attributeWriteGrants. 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 OIDCClient by namespace and name, 1 to 16 attributes, and exactly one of allUsers: true or groups: up to 16 Groups, 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 Organization grants. 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_id holds nothing, and the admin API reports client_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 Organization is Pending with AttributeGrantReferenceNotFound; the rest of it is written all the same. A grant that widens what a client may write is recorded at warning on organization.updated.
  • The main organization's grants are orgs.main.attribute_write_grants in the server's configuration, naming the client by clientId and namespace and groups by display name.

Register the client

The client is confidential, registered for client_credentials and the scope nauthera.user-attributes:write:

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: true

The 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>/v1/user-attributes. 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 sub of their tokens.
  • A merge patch (RFC 7396): a member sets its attribute, null removes 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 400 validation_error whose field errors name attributes.<name>: not_allowed for a department that is no member, pattern for a malformed cost center, read_only for 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_credentials or 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 in changed.

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

WhoWhereWhat they may change
The userThe account page, /account beneath the organization's issuer, which an application can link toAttributes declared userAccess: write
A new userSign-upEvery required attribute declared write, which the form asks for
An administratorThe dashboard's user page, the admin API, nauthera-cliAny declared attribute, held to its declaration
A granted applicationThe service attribute API, as aboveThe 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 seeWhyFix
The ProtectedResource is Refused on spec.identifier, naming the namespaceNo spec.protectedResources entry gives the namespace a prefix of itAllocate one (step 1)
Refused, naming the form to writeThe identifier is not in the form tokens carryWrite it as the message says: https://hr.acme.example is https://hr.acme.example/
Refused on spec.attributes[i].nameThe organization does not declare the attribute with accessToken: trueSet it on the declaration (step 2)
Refused on spec.attributesThe selection's worst case is over 4096 bytesGive each string a maxLength and each list a maxItems, or select fewer
IntrospectionClientNotFoundThe introspection client is missing, not registered yet or of another organizationApply it in the same namespace; the API is registered without it meanwhile
invalid_target, cause resource_not_registeredThe organization registers protected resources, and not this identifierRegister it, byte for byte as clients send it
invalid_target, cause resource_not_entitledThe client does not list the API in spec.resourcesAdd it
invalid_target, cause attribute_value_invalidThe user's value of a selected attribute breaks its declarationCorrect the value, or the declaration; the admin API lists it under attribute_violations
A token without an attributeThe grant lacks its scope, the user has no value, or another API in aud does not select itPreview it on the dashboard's Token claims page, which names the reason
The Organization is Pending with AttributeGrantReferenceNotFoundA grant's client or group does not resolveApply the OIDCClient or Group it names
401 from the attribute APINot a client's own client_credentials token, or the client is no longer confidential and registered for the grant type and the scopeRequest the token as in Write
403 from the attribute APINo grant covering this user, or the token lacks nauthera.user-attributes:writeCheck the grant's attributes and allUsers or groups

Every operator reason and OAuth error code has a runbook.