Protect your API

Verify who is calling your own API using onefold — with userinfo, or with audience-bound JWT access tokens issued for a protected resource you register (RFC 8707).

When your frontend or another client calls your own API, the API has to establish who the user is. It must not trust a user ID sent by the client. onefold gives you two ways to do that.

Look the token upAudience-bound tokens
Your API receivesAn opaque access tokenA signed JWT made for your API
Your API verifies it byCalling onefold's userinfo endpointChecking the signature and claims itself
Network call per requestYesNo
SetupNoneRegister a protected resource
Sees revocationImmediatelyWhen the token expires, or by introspection

Start with the first. Use the second when you want offline verification, or when the token must be usable at your API and nowhere else.

Look the token up

Have the client send the onefold access token to your API, and ask onefold who it belongs to:

curl https://onefold.eitozx.com/api/v1/userinfo/ \
  -H "Authorization: Bearer ACCESS_TOKEN"
  • 200 returns the user's claims. Use sub as the user's ID.
  • 401 means the token is invalid, expired or revoked. Refuse the request.

Each call counts toward your application's per-minute request budget. Cache the result for a short time, keyed on the token, instead of calling on every request.

An ordinary access token is opaque. Your API cannot verify one locally.

Audience-bound tokens

Register your API as a protected resource. A client then asks for an access token for that resource, and receives a signed JWT whose aud is your API. Your API verifies it without calling onefold.

Register the resource

On the application's OAuth2 tab, under Protected resources, choose Register a resource and enter a name and the API's identifier, for example https://api.example.com.

The identifier:

  • is an absolute https URL,
  • has no query string, no fragment and no credentials,
  • can be registered once, by one application,
  • is compared exactly. https://api.example.com/v1 and https://api.example.com/v1/ are two different identifiers. Only a bare trailing slash on the host makes no difference: https://api.example.com/ is the same as https://api.example.com.

An application can register up to 10 protected resources.

Request a token for it

Add resource to the authorization request, exactly as registered:

GET https://onefold.eitozx.com/o/authorize/
  ?response_type=code
  &client_id=YOUR_CLIENT_ID
  &resource=https://api.example.com
  &…
  • Send exactly one resource.
  • At the token endpoint, leave resource out or repeat the same value. A different value is refused with invalid_target.
  • A refresh keeps the audience: tokens from a refresh are for the same resource.

The client and the API can belong to different applications. Any application can request a token for a registered resource.

Verify the token in your API

The access token is a JWT signed with RS256, with the header typ set to at+jwt. Verify the signature with the key from https://onefold.eitozx.com/.well-known/jwks.json whose kid matches, then check the claims.

ClaimCheck
issEquals https://onefold.eitozx.com
audEquals your API's identifier, as a single string
expIs in the future
subThe user's ID, as a string
scopeThe granted scopes, separated by spaces
client_idThe application the token was issued to
jtiA unique ID for this token
import { createRemoteJWKSet, jwtVerify } from "jose";

const jwks = createRemoteJWKSet(
  new URL("https://onefold.eitozx.com/.well-known/jwks.json"),
);

export async function authenticate(request: Request) {
  const token = request.headers.get("authorization")?.replace(/^Bearer /, "");
  if (!token) throw new Error("missing token");

  const { payload } = await jwtVerify(token, jwks, {
    issuer: "https://onefold.eitozx.com",
    audience: "https://api.example.com",
    algorithms: ["RS256"],
  });
  return { userId: payload.sub, scopes: String(payload.scope).split(" ") };
}

The token carries no email and no profile. Identify the user by sub.

An audience-bound token works only at its audience

onefold's own userinfo endpoints refuse an audience-bound token. A token issued for your API cannot be used to read the user's claims from onefold, and an ordinary token is not accepted as proof for your API. Request each for its purpose.

Check revocation with introspection

A signature check cannot tell that a token was revoked before it expired. When that matters, introspect the token:

curl -X POST https://onefold.eitozx.com/o/introspect/ \
  -u "YOUR_CLIENT_ID:YOUR_CLIENT_SECRET" \
  -d token=ACCESS_TOKEN
  • Authenticate as the application that registered the resource. It must be a confidential client.
  • An active token answers {"active": true, …} with scope, client_id, sub, aud, iss, token_type and exp.
  • Anything else answers {"active": false}: a token that is expired, revoked or unknown, an ordinary token with no audience, or a token for a resource you do not own. The answer does not say which.
  • Each call counts toward your request budget.

Remove a resource

Removing a protected resource stops new tokens being issued for it. Tokens already issued keep working until they expire.

Check scopes in your API

Whichever method you use, the token says who the user is and which onefold scopes they granted. What that user may do in your product is your API's decision. onefold has no roles or permissions for your end users.

For an MCP server

An MCP server is an API with a discovery convention on top. See Secure your own MCP server.

On this page