> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Authorization

> Explains OAuth authorization best practices including discovery, audience validation, token delegation rules, and forbidding token passthrough so resource servers accept only tokens intended for them.

This section uses an example from Alan's warehouse database server to explain common OAuth pitfalls and correct authorization behavior.

Consider the token presented with a request. Each token contains several claims you can inspect; one important claim is the audience (`aud`) — the intended recipient of the token. Example token:

```json theme={null}
{
  "sub": "alan",
  "aud": "billing",
  "exp": 1790000000
}
```

In this scenario the token with `"aud": "billing"` was presented to the warehouse server. The warehouse server accepted and executed the request without complaint — but it was never authorized to do so. This is a classic case of token misuse.

Authorization behavior is specified per transport: the spec describes recommended practices but does not force every server to perform authorization in all transports. For remote transports (HTTP) the recommendation is to use OAuth 2.1. For local transports (stdio), credentials often come from environment variables and do not cross a network boundary.

![A slide titled "Per Transport" comparing two setups: on the left an HTTP — Remote diagram shows a laptop client connecting over the network to a remote server, and on the right a stdio — Local diagram shows a client and server inside a local boundary exchanging messages (one labeled "password").](https://mintcdn.com/kodekloud-c4ac6d9a/0UyWJzvxepsOrybu/images/Model-Context-Protocol-Associate-MCPA-Certification/Trust-Boundaries-and-Attacks/Authorization/per-transport-http-remote-stdio-local.jpg?fit=max\&auto=format\&n=0UyWJzvxepsOrybu\&q=85\&s=5637486c22d49f26780004701be15dd1)

## OAuth actors and their roles

When a client uses HTTP and OAuth, three parties typically participate:

| Role | Purpose | Example |
| - | - | - |
| Resource server | Hosts the protected resource and verifies tokens | WarehouseDB |
| OAuth client | Requests access and obtains tokens on behalf of a user | MCP client |
| Authorization server | Authenticates the user and issues tokens | [Okta](https://www.okta.com), [Google](https://accounts.google.com) |

Summary: clients obtain tokens, the authorization server issues them, and the resource server validates them.

## How a client discovers the right authorization server

Discovery generally happens in a few steps. The resource server helps the client find the authorization server by returning metadata hints. Below is a concise step-by-step flow.

### Step 1 — Initial request triggers discovery hint

* Client requests a protected resource without a token.
* Resource server responds with `401 Unauthorized` and a `WWW-Authenticate` header that points to resource metadata.

Example response header:

```http theme={null}
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer resource_metadata="https://warehouse.example/.well-known/oauth-protected-resource"
```

This header tells the client where to find guidance on how to obtain a valid token.

### Step 2 — Resource metadata

* The URL from the header hosts a published resource-server metadata document.
* That document lists authorization servers the resource trusts and other metadata in a standardized format.

### Step 3 — Authorization server discovery

* The client selects an authorization server from the resource metadata list and fetches that server's description.
* The authorization server metadata includes the endpoints the client needs, for example:

```json theme={null}
{
  "authorization_endpoint": "https://login.okta.com/authorize",
  "token_endpoint": "https://login.okta.com/token"
}
```

### Step 4 — Standard OAuth login (authorization code + PKCE)

* The client directs the user to the authorization endpoint.
* The user authenticates; the client receives a short-lived authorization code.
* The client exchanges the code at the token endpoint for an access token. The code exchange should use PKCE (Proof Key for Code Exchange) to prevent authorization code replay attacks. See RFC 7636 for details.

![A diagram titled "Step 4 – Standard OAuth Login" showing an OAuth Client (laptop) on the left, an Authorization Server (server rack) in the center, and a user icon labeled "Alan" on the right. An envelope marked "token" points from the client to the server, and an arrow shows the server communicating back to the user.](https://mintcdn.com/kodekloud-c4ac6d9a/0UyWJzvxepsOrybu/images/Model-Context-Protocol-Associate-MCPA-Certification/Trust-Boundaries-and-Attacks/Authorization/oauth-standard-login-client-server-token.jpg?fit=max\&auto=format\&n=0UyWJzvxepsOrybu\&q=85\&s=3ef219bb63a61e392bf53ed376872a70)

## Audience (aud) claim and the resource parameter

When requesting a token, the client indicates the intended target with parameters such as `resource` (RFC 8707) or other audience mechanisms. The authorization server encodes this intent into the token's `aud` claim.

A resource server must perform a simple check on every incoming token: verify the `aud` matches the server identifier (i.e., “is this token meant for me?”). If the audience does not match, the resource must reject the request.

In Alan’s case, the warehouse server skipped the audience check and accepted a token issued for `"billing"`. Accepting such a token is called token passthrough and is explicitly forbidden by the specification.

![A dark presentation slide titled "Forbidden Outright" with a boxed specification excerpt about "Token Passthrough." The text states token passthrough is explicitly forbidden and MCP servers "MUST NOT" accept tokens not issued for them.](https://mintcdn.com/kodekloud-c4ac6d9a/0UyWJzvxepsOrybu/images/Model-Context-Protocol-Associate-MCPA-Certification/Trust-Boundaries-and-Attacks/Authorization/forbidden-outright-token-passthrough.jpg?fit=max\&auto=format\&n=0UyWJzvxepsOrybu\&q=85\&s=d14a3c39a69625d04078e2da863fc061)

A resource server MUST validate the token signature, expiration, and the audience (`aud`) claim and reject any token not issued for that server. Token passthrough is explicitly forbidden.

## Token delegation and calling upstream services

The same audience principle applies when a server calls an upstream service on behalf of a request:

* Do not forward an inbound token to an upstream service unless that token's audience explicitly includes the upstream service and your security model allows delegation.
* Obtain a token whose `aud` identifies the upstream service via an appropriate OAuth flow (client credentials, on-behalf-of, or other delegation mechanism).

Best-practice checklist:

* Validate inbound tokens fully: signature, `exp`, `aud`, `iss`, and scopes/permissions.
* When making outbound calls, present a token intended for the upstream service.
* Use standard OAuth grants for machine-to-machine tokens and delegation (client credentials, on-behalf-of).
* Avoid reusing or forwarding tokens issued for other audiences.

## Key takeaways

* Authorization behavior is transport-aware; follow the recommended practices for your transport (e.g., OAuth 2.1 for HTTP).
* Use discovery metadata so clients can find authorization servers securely.
* Always validate tokens, and always enforce the `aud` claim.
* Never accept or forward tokens that were not issued for the service handling the request.

## Links and references

* [OAuth 2.1 Overview](https://oauth.net/2.1/)
* [RFC 7636 — PKCE (Proof Key for Code Exchange)](https://datatracker.ietf.org/doc/html/rfc7636)
* [RFC 8707 — Resource Indicators for OAuth 2.0](https://www.rfc-editor.org/rfc/rfc8707)
* [Okta](https://www.okta.com)

> [card] [Watch Video](https://learn.kodekloud.com/user/courses/model-context-protocol-associate-mcpa-certification/module/31dedbc5-6173-4700-b50e-7a581095c897/lesson/5b261e3b-8e8f-4563-b866-2d516681374e)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.