> ## 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.

# Proving It and Keeping It Small

> Guidance for securing OAuth authorization code flows in mobile apps, covering PKCE, client identification, token audiences and scopes, refresh token rotation, redirect URI exact matching, and least privilege

Say Alan just shipped a calendar app for phones. The basic user flow is simple: sign in, pick a date, and add events. Behind the scenes, the app must prove to a remote calendar server that it has the right to act on the user’s behalf. The common solution is OAuth — specifically the current recommended guidance: OAuth 2.1.

An OAuth interaction looks like this: the user signs in with an authorization server, which returns an authorization code (a one-time ticket). The app takes that code back to the authorization server and exchanges it for an access token. The app then attaches the access token to requests so the calendar server knows who is asking.

Example request using a Bearer access token:

```http theme={null}
GET /events HTTP/1.1
Host: calendar.example.com
Authorization: Bearer <access_token>
```

![A diagram titled "How the App Proves Access" showing an Authorization Server and an OAuth token mediating between Alan's mobile app (left) and a Calendar Server (right).](https://mintcdn.com/kodekloud-c4ac6d9a/0UyWJzvxepsOrybu/images/Model-Context-Protocol-Associate-MCPA-Certification/Trust-Boundaries-and-Attacks/Proving-It-and-Keeping-It-Small/app-proves-access-oauth-diagram.jpg?fit=max\&auto=format\&n=0UyWJzvxepsOrybu\&q=85\&s=4710ccd51cb188c928f33408f3e0c90a)

## Binding the response to the request

Before the client uses the authorization code, the app must ensure the code actually came from the expected authorization server and from the same flow it started. Common checks include:

* Verifying the `state` parameter returned with the authorization response matches the original state the app sent.
* Ensuring the `redirect_uri` used in the response is an exact character-for-character match with the registered redirect URI.
* Validating the issuer (`iss`) claim inside tokens (for example, the ID token) against the authorization server’s metadata, instead of trusting an `iss` query parameter handed in the browser redirect.

Do not trust arbitrary query parameters in redirects — use token claims or authorization-server metadata as the source of truth.

## Client identification and registration

When the authorization server asks “who is this app?”, the app must identify itself. There are two common approaches:

* Dynamic client registration: the client POSTs metadata (name, callback URL, etc.) and receives a `client_id` and `client_secret` in response.
* Metadata URL: the client provides a URL where the authorization server fetches a client metadata document (for example, `https://calendar.example.com/app.json`) describing the client.

When the authorization server issues credentials, treat each issuer separately. If Alan’s app talks to two authorization servers, it should keep separate credential sets — one per issuer.

![A diagram titled "One Set per Issuer" showing two authentication servers (Auth Server A and Auth Server B) on either side of a mobile app labeled "Alan's App" with a calendar UI. Each server is connected to the app with distinct key/token labels, illustrating separate credential sets per issuer.](https://mintcdn.com/kodekloud-c4ac6d9a/0UyWJzvxepsOrybu/images/Model-Context-Protocol-Associate-MCPA-Certification/Trust-Boundaries-and-Attacks/Proving-It-and-Keeping-It-Small/one-set-per-issuer-diagram.jpg?fit=max\&auto=format\&n=0UyWJzvxepsOrybu\&q=85\&s=49a0a11644d91d90e3703069edfbf272)

Your `client_secret` is effectively a password for the app. If the same secret is shared across issuers, either issuer could impersonate the client at the other issuer — never reuse secrets across independent authorization servers.

## Token audience and minimal privileges

Access tokens include an `aud` (audience) claim that identifies the intended resource server:

```json theme={null}
{
  "aud": "calendar.example.com"
}
```

Only the audience in the token should accept and honor it. Tokens also encode what the client is allowed to do (scopes) and how long the token is valid.

Example token fields:

```json theme={null}
{
  "aud": "calendar.example.com",
  "scope": "read:calendar",
  "expires_in": 900
}
```

Follow the principle of least privilege: give tokens as little power and lifetime as necessary. The most useful protections are summarized below.

| Best practice | Why it matters | Example |
| - | - | - |
| Request only required scopes | Limits what a leaked token can do | `scope: "read:calendar"` |
| Use read-only tokens for read-only apps | Prevents unwanted deletes/changes | Grant `read` instead of `write` scopes |
| Short-lived access tokens | Limits window of exposure if leaked | `expires_in: 900` (15 minutes) |
| Refresh token rotation | Limits reuse of stolen refresh tokens | Issue a new refresh token every time one is used |
| Never put tokens in URLs | URLs are logged and can leak tokens | Use `Authorization: Bearer <token>` header |

Never include access tokens in URLs or query strings (for example `https://calendar.example.com/events?token=...`). Put them in Authorization headers instead.

## Refresh tokens and rotation

When an access token expires, the app uses a refresh token to get a new access token. Because refresh tokens are longer-lived and powerful, they should be stored securely on the device and rotated: issue a new refresh token on each refresh and invalidate the previous one. Rotation reduces the usefulness of a stolen refresh token.

## Preventing code interception with PKCE

An attacker on the device could try to intercept the authorization code before the legitimate app redeems it. PKCE (pronounced “pixie”) defends against that attack:

* The client generates a random `code_verifier` before starting the authorization request.
* The client derives a `code_challenge` from the verifier and includes it in the initial request.
* When the code is exchanged at the token endpoint, the client must present the original `code_verifier`. The authorization server matches it to the earlier `code_challenge`.

PKCE binds the authorization code to the client that started the flow and is required for every client.

PKCE prevents code interception by binding the authorization code to the app that started the flow via a `code_challenge` / `code_verifier` pair.

![An illustration titled "Protecting the Code" shows Alan's Phone with a legitimate app and a malicious app. A token tag labeled "Sp1x10BeZQ" links the phone to an Authorization Server on the right.](https://mintcdn.com/kodekloud-c4ac6d9a/0UyWJzvxepsOrybu/images/Model-Context-Protocol-Associate-MCPA-Certification/Trust-Boundaries-and-Attacks/Proving-It-and-Keeping-It-Small/protecting-code-alans-phone-auth-server.jpg?fit=max\&auto=format\&n=0UyWJzvxepsOrybu\&q=85\&s=72a9a181fb410bbc6d86ea57678d57b0)

## Redirect URI exact matching

The redirect URI returned by the authorization server must match the value registered for the client exactly, character-for-character:

```YAML theme={null}
redirect_uri: https://calendar.example.com/callback
registered:   https://calendar.example.com/callback
```

Subtle differences (trailing slashes, different schemes, or different hostnames) can open security holes.

## Deprecated and removed flows

Because of credential exposure risks, certain older flows are no longer recommended or are removed from best practice:

| Flow | Reason removed |
| - | - |
| Implicit flow (token in browser fragment) | Tokens returned directly to the browser expose long-lived credentials to attackers and logs |
| Resource-owner password credentials flow (password through client) | Passing user credentials through third-party clients increases theft risk |

Use the authorization code flow with PKCE (OAuth 2.1 guidance) instead.

## Roots vs. the real enforcement boundary

When clients are autonomous (for example, an agent driven by an LLM), they cannot predict every future request. The protocol allows the client to present a Roots list that indicates the paths and hosts it intends to access (for example `/alan/calendars`, `/alan/photos`, `api.example.com`). Roots are useful as intent and for UX, but they are not the hard enforcement mechanism.

![A stylized infographic titled "Roots and the Real Boundary" showing a phone labeled "Alan's Agent" on the left, a central "Roots List" with entries like /alan/calendars, /alan/photos and api.example.com, and a "Calendar Server" on the right. A key-like token badge appears above, linking the components.](https://mintcdn.com/kodekloud-c4ac6d9a/0UyWJzvxepsOrybu/images/Model-Context-Protocol-Associate-MCPA-Certification/Trust-Boundaries-and-Attacks/Proving-It-and-Keeping-It-Small/alans-agent-roots-list-calendar-token.jpg?fit=max\&auto=format\&n=0UyWJzvxepsOrybu\&q=85\&s=87c46d3cc84ad8dd0525c1687b9dadeb)

The true enforcement boundary is the token itself: the `aud` and `scope` claims determine what the agent may actually access. If the token says `aud: calendar.example.com` and `scope: read:calendar`, then attempts to access a photo server or perform writes outside the token’s scopes will fail.

```json theme={null}
{
  "aud": "calendar.example.com",
  "scope": "read:calendar"
}
```

Later sections will detail the specification’s warnings about name-based attacks and other adversarial techniques.

## Links and references

* OAuth 2.1 (recommended guidance): [https://www.rfc-editor.org/rfc/rfc6749](https://www.rfc-editor.org/rfc/rfc6749) (start with OAuth 2.0 core and follow OAuth 2.1 updates)
* PKCE (RFC 7636): [https://www.rfc-editor.org/rfc/rfc7636](https://www.rfc-editor.org/rfc/rfc7636)
* OAuth 2.0 Security Best Current Practices: [https://www.rfc-editor.org/rfc/rfc8252](https://www.rfc-editor.org/rfc/rfc8252)

For implementation details and platform-specific guidance, consult the authorization server’s documentation and the OAuth working group publications.

> [card] [Watch Video](https://learn.kodekloud.com/user/courses/model-context-protocol-associate-mcpa-certification/module/31dedbc5-6173-4700-b50e-7a581095c897/lesson/0232ddbe-a6ac-4df1-b290-e688128cb75a)


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