Protected resource metadata
Authorization serverIn OAuth, the service that handles a person's sign-in and approval and decides which tokens to issue to a client. It may belong to the same company as the API, but its job is separate. metadata left the printer with one value a person typed: which authorizationDeciding whether a requested action is permitted, such as whether a signed-in account may delete a particular photo. Authentication says which account is asking; authorization decides what it can do. server goes with the photo APIAn application programming interface: a defined way for software to interact with a service. A web API offers endpoints where other programs send HTTP requests.. This lesson follows the printer as an API names its own authorization servers, and shows the two decisions that keep that safe: whether the API's document really describes the address the printer called, and whether the server it names deserves Ava's visit.
An API that describes itself
The printer knows the photo API because its developers told it about the API. They configured the address https://api.photos.example, the issuerThe party that creates and signs a token, assertion, or certificate, named in a token's iss claim. A recipient must check that the issuer is exactly the one it expected and trusts. https://auth.photos.example whose tokens it accepts, the scope photos.read, and the fact that tokens go in the Authorization header. Authorization server metadata reduced the authorization server's settings to one trusted issuer, but the link between the API and that issuer is still something a person typed.
Now the printer's developers want to go further. They want people to print from photo libraries the printer has never been set up for: any service that offers the same kind of photo API. Ava would type the address of her library's API, and the printer would work out the rest. That only works if an API can describe itself, and it is only safe if the printer checks what the API says before acting on it.
Protected resource metadata, published as RFC 9728, does for APIs what authorization server metadata does for authorization servers. OAuthA framework that lets a person give an application limited access to their resources at another service, using tokens instead of sharing their password. OAuth is about access; OpenID Connect adds sign-in. calls an API that accepts access tokens a protected resource, and the specification lets each one publish a JSONA text format for structured information, written as named values inside curly braces. APIs, tokens, and discovery documents all use it. document saying which authorization servers it accepts tokens from, which scopes it uses, and how it wants tokens presented.
An API is named by its resource identifier: an HTTPSHTTP protected by TLS, so traffic is encrypted and checked for changes while it travels. It protects the connection, but does not prove that the site at the other end is honest. URLA web address, such as https://photos.example/api/albums/42?limit=10. It names how to connect (https), the domain, the path to a particular page or resource, and any extra parameters after the question mark. with no fragment, and normally no query either. The photo API's identifier is https://api.photos.example, the same value the photo service puts in the aud claim of tokens issued for it. All domains, tokens and values in these examples are fictional. Like the authorization server's document, the API's lives at a well-known address built from its identifier, here with the name oauth-protected-resource:
GET /.well-known/oauth-protected-resource HTTP/1.1
Host: api.photos.exampleHTTP/1.1 200 OK
Content-Type: application/json
{
"resource": "https://api.photos.example",
"authorization_servers": ["https://auth.photos.example"],
...
}The insertion rule from authorization server metadata applies when the identifier has a path. If the photo service published a second version of its API with the identifier https://api.photos.example/v2, that API's document would be at https://api.photos.example/.well-known/oauth-protected-resource/v2.
Each protected resource publishes its own document at an address derived from its own identifier, even when one server hosts several APIs. The specification relies on that: because a document's address is fixed by the identifier it describes, a document can speak only for the resource whose address it sits under.
Asking the API directly
A client does not always know an API's identifier. Sometimes all it has is an address to call, such as the one Ava typed. RFC 9728 lets the API point the way when a client calls that address without a tokenA piece of data issued by a trusted service that a client presents as a credential, for example to call an API or to report a sign-in. Some tokens carry readable claims; others are random strings that only the issuer can interpret.. The printer requests https://api.photos.example exactly as Ava typed it:
GET / HTTP/1.1
Host: api.photos.example
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer resource_metadata="https://api.photos.example/.well-known/oauth-protected-resource"The request line shows / because that is how HTTPThe Hypertext Transfer Protocol, the rules browsers, apps, and servers use to send requests and responses on the web. sends an address with no path. As Using access tokens described, a request that carries no token earns a 401 with a WWW-Authenticate challenge and no error code. The resource_metadata parameter adds the address of the API's metadata document.
The parameter is not limited to plain bearer tokens. An API that requires DPoPAn OAuth extension, RFC 9449, that binds tokens to a key pair the client holds. The client signs a fresh proof for each request, so a stolen token is useless without the private key. can include it in a DPoP challenge like those in Validating proofs at the server, and it can sit in the same challenge as other parameters, such as the max_age value a step-up challenge uses. An API can also send it later, to a client that already uses it, as a signal that its metadata may have changed. The client then fetches the document again and validates it before using any new values, which lets an API move to a different authorization server without coordinating with every client in advance.
Reading the resource document
The photo API's complete document is short. Most of it answers questions the printer used to settle with a developer's help:
{
"resource": "https://api.photos.example",
"authorization_servers": ["https://auth.photos.example"],
"scopes_supported": ["photos.read", "photos.write", "photos.delete"],
"bearer_methods_supported": ["header"],
"dpop_signing_alg_values_supported": ["ES256"],
"dpop_bound_access_tokens_required": false,
"resource_name": "Photo API",
"resource_documentation": "https://photos.example/developers/api"
}resource is the only required member. It repeats the identifier the document describes, and the client checks it before trusting anything else in the document.
authorization_servers lists the issuer identifiers of authorization servers whose tokens this API accepts. An API may leave some out, and an API whose authorization servers cannot be listed in advance omits the member entirely.
scopes_supported lists the scopes used to request access to this API. As with the authorization server's list, it shows what exists, not what to ask for. The printer still requests only photos.read, because a photo book needs nothing more.
bearer_methods_supported says how the API accepts bearer tokens. The defined values are header, body and query. This API accepts only the Authorization header, which is also the method Using access tokens recommended. If the member were missing, a client could not conclude anything either way.
The two DPoP members say that the API accepts DPoP proofs signed with ES256 but does not insist on them, because dpop_bound_access_tokens_required is false. The DPoP-bound tokens of the printer's phone app work here, and so do the plain bearer tokens of its website. An API that set the value to true would refuse any token not bound to a key.
resource_name and resource_documentation are for people. Like a client's name in its registration, described in Redirect URIs and client metadata, the resource name is whatever the API's operator chose to type.
Checking the resource value
Every document along the way was written by someone else. For the photo service, those documents say what the printer's developers would have configured anyway. For an API someone typed into a box, they say whatever its operator wants them to say.
The first check mirrors the issuer check from Authorization server metadata. A metadata address is built from a resource identifier, as https://api.photos.example/.well-known/oauth-protected-resource is built from https://api.photos.example, and the document's resource member must be identical to the identifier its address was built from. If it is not, the client must not use any of the document.
A second check applies when the metadata address came from a challenge, and it stops a quieter trick. Suppose Ava was persuaded to add a library at https://api.attacker.example. The printer calls that address, and the attacker's API answers with a challenge that points at the photo API's genuine metadata:
GET / HTTP/1.1
Host: api.attacker.example
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer resource_metadata="https://api.photos.example/.well-known/oauth-protected-resource"Everything the printer fetches next is authentic, and it passes the first check. The document is the photo API's own, it names the real photo service, and Ava signs in at the real photo service. The printer receives a genuine token for the photo API, so audience restriction does nothing to stop what follows. The printer then sends its requests for Ava's photos to the attacker's API, the address she gave it, with that token attached, and the attacker replays the token at the real photo API.
The defense is to compare the document's resource with the address the client actually called. When metadata was found through a challenge, RFC 9728 requires its resource value to be identical to the URL the client used for the request that produced the challenge, and if it is not, none of the document may be used. The comparison is exact, like the issuer comparison, so it matches only when the client called the resource identifier itself. In code, that means comparing with the address as the client stored it, not a copy an HTTP library has rewritten, for example by adding a trailing slash. The printer's request to the photo API went to https://api.photos.example, exactly the identifier the photo API's document names, so the genuine API passes. Here the printer called https://api.attacker.example while the document names https://api.photos.example, so the printer stops before any token exists.
Calling the photo API
- Address called
- https://api.photos.example
- Challenge points at
- the photo API's metadata
- resource
- https://api.photos.example
Identical: use the document
Calling the attacker's API
- Address called
- https://api.attacker.example
- Challenge points at
- the photo API's metadata
- resource
- https://api.photos.example
Different: stop before any token exists
Without the check, the attacker's API would receive a genuine photo API token and replay it at the real photo API.
Fetching and refreshing safely
Discovery makes the printer's backend request addresses that other parties chose: the API address Ava typed, the metadata address in a challenge, the issuer in a document. That invites server-side request forgery, tricking a server into sending requests to addresses an attacker picks, often internal services the attacker cannot reach directly but the server can. Someone who entered an internal address as their photo library could make the printer's backend probe the printing company's own network.
RFC 9728 says clients should take precautions against this, such as blocking requests to internal address ranges. In practice, a backend that fetches discovered addresses accepts only HTTPS, resolves each host and rejects addresses that lead inside its own network: private addresses such as 10.0.0.1, loopback addresses such as 127.0.0.1, and link-local addresses such as 169.254.169.254, where many cloud servers answer questions about themselves. It applies the same rule to any redirect, and limits how long it waits and how much it reads. It checks the server certificate on every fetch, exactly as it does for authorization server metadata.
Resource metadata is fetched with an ordinary HTTP GET, so ordinary caching applies, and an API can say how long its document may be reused with a Cache-Control header. When an API sends a new challenge containing resource_metadata, the client fetches the document again and validates the fresh copy as it did the first. Like authorization server metadata, a resource's document can also carry signed_metadata; a client that supports it checks the signature and decides whether it trusts the signer.
Check your understanding
Try these questions before moving on. If an answer isn't right, use the feedback and try again.
Try it in the Lab
- Go from an API's address to a token for it, refusing documents that do not fitFollow the photo API's resource_metadata challenge, refuse a borrowed document, an untrusted server and unapproved destinations, get a token that names the API, and watch discovery fail closed when the document goes.