oauth-flow-test-author
Overview
OAuth 2.0 (RFC 6749) defines four grant types per datatracker.ietf.org/doc/html/rfc6749:
"1. Authorization Code (§1.3.1): The authorization code is
obtained by using an authorization server as an intermediary
between the client and resource owner.
2. Implicit (§1.3.2): The implicit grant is a simplified
authorization code flow optimized for clients implemented in a
browser using a scripting language such as JavaScript.
3. Resource Owner Password Credentials (§1.3.3): The resource
owner password credentials (i.e., username and password) can
be used directly as an authorization grant to obtain an access
token.
4. Client Credentials (§1.3.4): The client credentials can be
used as an authorization grant when the authorization scope is
limited to protected resources under the control of the
client."
Per RFC 9700 (OAuth 2.0 Security Best Current Practice, March 2025):
- Implicit grant is deprecated.
- Resource Owner Password Credentials grant is deprecated.
- Authorization Code with PKCE (per datatracker.ietf.org/doc/html/rfc7636)
is the canonical flow for all clients, public or confidential.
This skill is the per-flow test recipe. The canonical authorization-code +
PKCE flow is authored end to end in the Worked example below; the
less-common grants and negative cases live in
references/per-flow-test-recipes.md.
When to use
- Authoring tests for an OAuth 2.0 / OIDC client (web, mobile,
SPA, M2M).
- Reviewing PRs that touch auth-flow code.
- The team needs flow-coverage independent of the IdP choice
(Keycloak / Auth0 / Okta / mock).
How to use
- Identify the grant(s) the client uses from the flow-selection table below.
- Author the authorization-code + PKCE happy path (the Worked example) - the canonical flow for browser / native / mobile clients.
- Add the negative and secondary-grant tests each client needs (state CSRF, client-credentials, refresh-token rotation, OIDC nonce, scope-downgrade, redirect-URI) from references/per-flow-test-recipes.md.
- Run the end-to-end coverage checklist for every OAuth/OIDC client in scope.
Flow-selection decision surface
| Client type |
Grant to test |
Required tests |
Recipe |
| Browser / SPA / native / mobile |
Authorization Code + PKCE (S256) |
Happy path + state CSRF + redirect-URI + (OIDC nonce if openid) |
Worked example + references |
| Machine-to-machine (M2M) |
Client Credentials |
Happy path; assert no refresh_token |
references |
| Any client holding a refresh token |
Refresh Token |
Rotation + reuse-detection |
references |
| Any OIDC client |
Authorization Code + ID Token |
Nonce match + scope-downgrade |
Worked example + references |
| Legacy Implicit / RO-Password |
(none - deprecated per RFC 9700) |
Migrate to Auth Code + PKCE |
- |
Worked example - authorization-code + PKCE (canonical)
Per RFC 6749 §4.1 (per rfc6749) the flow:
- Client directs resource owner to authorization endpoint with
client_id, redirect_uri, response_type=code, scope,
state (RECOMMENDED for CSRF defense per §10.12), nonce
(OIDC), and PKCE params (code_challenge + code_challenge_method).
- Authorization server authenticates the resource owner.
- Server redirects back to client with the authorization code +
the original
state.
- Client requests token endpoint with
grant_type=authorization_code,
code, redirect_uri, client_id, and PKCE code_verifier.
- Server returns access token (+ refresh token + ID token if OIDC).
PKCE per RFC 7636 §4.2 (rfc7636):
"Two methods are defined:
- plain:
code_challenge = code_verifier
- S256:
code_challenge = BASE64URL-ENCODE(SHA256(ASCII(code_verifier)))"
Always use S256; plain defeats the purpose of PKCE.
Test pattern (Python with requests + Playwright for browser flow):
import secrets, hashlib, base64, requests
from urllib.parse import urlparse, parse_qs
def make_pkce_pair():
verifier = secrets.token_urlsafe(64)
challenge = base64.urlsafe_b64encode(
hashlib.sha256(verifier.encode("ascii")).digest()
).rstrip(b"=").decode("ascii")
return verifier, challenge
def test_authz_code_pkce_flow(idp_url, client_id, redirect_uri, browser):
verifier, challenge = make_pkce_pair()
state = secrets.token_urlsafe(32)
nonce = secrets.token_urlsafe(32)
# Step 1: redirect to authorize endpoint
authz_url = (
f"{idp_url}/authorize?"
f"client_id={client_id}&response_type=code&"
f"redirect_uri={redirect_uri}&scope=openid+profile&"
f"state={state}&nonce={nonce}&"
f"code_challenge={challenge}&code_challenge_method=S256"
)
# Browser interaction (simulated via Playwright):
page = browser.new_page()
page.goto(authz_url)
page.fill("#username", "alice")
page.fill("#password", "alicepass")
page.click("#submit")
# Step 3: redirect lands on redirect_uri with code + state
redirected_to = page.url
parsed = urlparse(redirected_to)
query = parse_qs(parsed.query)
assert query["state"][0] == state # RFC §10.12 CSRF defense
code = query["code"][0]
# Step 4: token endpoint
token_response = requests.post(
f"{idp_url}/token",
data={
"grant_type": "authorization_code",
"code": code,
"redirect_uri": redirect_uri,
"client_id": client_id,
"code_verifier": verifier, # PKCE proof
},
)
assert token_response.status_code == 200
body = token_response.json()
assert "access_token" in body
assert body["token_type"] == "Bearer"
The happy path asserts the returned state matches; the negative
state-mismatch test, plus the client-credentials, refresh-rotation,
OIDC-nonce, scope-downgrade, and redirect-URI recipes, are in
references/per-flow-test-recipes.md.
End-to-end coverage checklist
For each OAuth/OIDC client in scope:
- Auth-code + PKCE happy path (Worked example)
- State parameter validation (references)
- Client-credentials happy path (if M2M; references)
- Refresh-token rotation + reuse detection (references)
- OIDC nonce validation (if OIDC; references)
- Scope-downgrade handling (references)
- Redirect-URI strict matching (references)
- Token expiration handling (use-after-expiry returns 401)
- Token revocation (RFC 7009) if endpoint supports it
Anti-patterns
| Anti-pattern |
Why it fails |
Fix |
Use PKCE plain method |
Defeats PKCE; RFC 7636 §4.2 specifies S256 as recommended |
Always S256 (Worked example) |
| Skip state validation in the callback handler |
CSRF vulnerable |
State CSRF negative test (references) |
| Hardcode redirect_uri prefix matching |
Substring match accepts evil URIs |
Strict equality (references) |
| Test only the happy path |
Negative cases (mismatched state, invalid PKCE, expired token) untested |
Negative tests (references) |
| Use Implicit or RO-Password grants in new code |
Deprecated per RFC 9700 |
Auth Code + PKCE |
Limitations
- This is a build-an-X workflow. Tests use the application's HTTP
client + browser automation library; this skill is the per-flow
checklist.
- IdP-specific quirks (audience, custom auth servers, sign-in
policies) layer on top - see
keycloak-tests,
auth0-tests,
okta-tests for IdP-specific patterns.
- RFC 9700 supersedes earlier security BCPs; pin to the latest
version when authoring tests.
- OAuth 2.1 (in-progress) consolidates the deprecations from RFC
9700; prefer 2.1 patterns when the IdP supports them.
References
- rfc6749 - RFC 6749 OAuth 2.0 Authorization Framework
- rfc7636 - RFC 7636 PKCE
- IETF RFC 9700 - OAuth 2.0 Security Best Current Practice (March 2025)
- IETF RFC 7009 - OAuth 2.0 Token Revocation
- openid.net/specs/openid-connect-core-1_0.html - OIDC Core spec
- references/per-flow-test-recipes.md - state CSRF, client-credentials, refresh-token rotation, OIDC nonce, scope-downgrade, and redirect-URI recipes.
keycloak-tests,
auth0-tests,
okta-tests - IdP-specific patterns
session-management-test-author - companion: post-auth session lifecycle
1---2name: oauth-flow-test-author3description: "Use whenever an OAuth 2.0 or OpenID Connect client or resource server needs tests, whatever the identity provider: auth code with PKCE, client credentials, refresh-token rotation and reuse detection, state and nonce checks, scopes and strict redirect-URI matching, per the RFCs."4metadata:5 source: https://skillmd.com/skills/testland/oauth-flow-test-author6---78# oauth-flow-test-author910## Overview1112OAuth 2.0 (RFC 6749) defines four grant types per [datatracker.ietf.org/doc/html/rfc6749][rfc6749]:1314[rfc6749]: https://datatracker.ietf.org/doc/html/rfc67491516> "1. **Authorization Code** (§1.3.1): The authorization code is17> obtained by using an authorization server as an intermediary18> between the client and resource owner.19> 2. **Implicit** (§1.3.2): The implicit grant is a simplified20> authorization code flow optimized for clients implemented in a21> browser using a scripting language such as JavaScript.22> 3. **Resource Owner Password Credentials** (§1.3.3): The resource23> owner password credentials (i.e., username and password) can24> be used directly as an authorization grant to obtain an access25> token.26> 4. **Client Credentials** (§1.3.4): The client credentials can be27> used as an authorization grant when the authorization scope is28> limited to protected resources under the control of the29> client."3031**Per RFC 9700 (OAuth 2.0 Security Best Current Practice, March 2025):**32- Implicit grant is **deprecated**.33- Resource Owner Password Credentials grant is **deprecated**.34- Authorization Code with PKCE (per [datatracker.ietf.org/doc/html/rfc7636][rfc7636])35 is the canonical flow for **all** clients, public or confidential.3637[rfc7636]: https://datatracker.ietf.org/doc/html/rfc76363839This skill is the per-flow test recipe. The canonical authorization-code +40PKCE flow is authored end to end in the Worked example below; the41less-common grants and negative cases live in42[references/per-flow-test-recipes.md](references/per-flow-test-recipes.md).4344## When to use4546- Authoring tests for an OAuth 2.0 / OIDC client (web, mobile,47 SPA, M2M).48- Reviewing PRs that touch auth-flow code.49- The team needs flow-coverage independent of the IdP choice50 (Keycloak / Auth0 / Okta / mock).5152## How to use53541. Identify the grant(s) the client uses from the flow-selection table below.552. Author the authorization-code + PKCE happy path (the Worked example) - the canonical flow for browser / native / mobile clients.563. Add the negative and secondary-grant tests each client needs (state CSRF, client-credentials, refresh-token rotation, OIDC nonce, scope-downgrade, redirect-URI) from [references/per-flow-test-recipes.md](references/per-flow-test-recipes.md).574. Run the end-to-end coverage checklist for every OAuth/OIDC client in scope.5859## Flow-selection decision surface6061| Client type | Grant to test | Required tests | Recipe |62|---|---|---|---|63| Browser / SPA / native / mobile | Authorization Code + PKCE (S256) | Happy path + state CSRF + redirect-URI + (OIDC nonce if `openid`) | Worked example + references |64| Machine-to-machine (M2M) | Client Credentials | Happy path; assert no `refresh_token` | references |65| Any client holding a refresh token | Refresh Token | Rotation + reuse-detection | references |66| Any OIDC client | Authorization Code + ID Token | Nonce match + scope-downgrade | Worked example + references |67| Legacy Implicit / RO-Password | (none - deprecated per RFC 9700) | Migrate to Auth Code + PKCE | - |6869## Worked example - authorization-code + PKCE (canonical)7071Per RFC 6749 §4.1 (per [rfc6749][rfc6749]) the flow:72731. Client directs resource owner to **authorization endpoint** with74 `client_id`, `redirect_uri`, `response_type=code`, `scope`,75 `state` (RECOMMENDED for CSRF defense per §10.12), `nonce`76 (OIDC), and PKCE params (`code_challenge` + `code_challenge_method`).772. Authorization server authenticates the resource owner.783. Server redirects back to client with the authorization code +79 the original `state`.804. Client requests **token endpoint** with `grant_type=authorization_code`,81 `code`, `redirect_uri`, `client_id`, and PKCE `code_verifier`.825. Server returns access token (+ refresh token + ID token if OIDC).8384**PKCE per RFC 7636 §4.2** ([rfc7636][rfc7636]):8586> "Two methods are defined:87> - **plain:** `code_challenge = code_verifier`88> - **S256:** `code_challenge = BASE64URL-ENCODE(SHA256(ASCII(code_verifier)))`"8990Always use **S256**; `plain` defeats the purpose of PKCE.9192**Test pattern (Python with requests + Playwright for browser flow):**9394```python95import secrets, hashlib, base64, requests96from urllib.parse import urlparse, parse_qs9798def make_pkce_pair():99 verifier = secrets.token_urlsafe(64)100 challenge = base64.urlsafe_b64encode(101 hashlib.sha256(verifier.encode("ascii")).digest()102 ).rstrip(b"=").decode("ascii")103 return verifier, challenge104105def test_authz_code_pkce_flow(idp_url, client_id, redirect_uri, browser):106 verifier, challenge = make_pkce_pair()107 state = secrets.token_urlsafe(32)108 nonce = secrets.token_urlsafe(32)109110 # Step 1: redirect to authorize endpoint111 authz_url = (112 f"{idp_url}/authorize?"113 f"client_id={client_id}&response_type=code&"114 f"redirect_uri={redirect_uri}&scope=openid+profile&"115 f"state={state}&nonce={nonce}&"116 f"code_challenge={challenge}&code_challenge_method=S256"117 )118 # Browser interaction (simulated via Playwright):119 page = browser.new_page()120 page.goto(authz_url)121 page.fill("#username", "alice")122 page.fill("#password", "alicepass")123 page.click("#submit")124125 # Step 3: redirect lands on redirect_uri with code + state126 redirected_to = page.url127 parsed = urlparse(redirected_to)128 query = parse_qs(parsed.query)129 assert query["state"][0] == state # RFC §10.12 CSRF defense130 code = query["code"][0]131132 # Step 4: token endpoint133 token_response = requests.post(134 f"{idp_url}/token",135 data={136 "grant_type": "authorization_code",137 "code": code,138 "redirect_uri": redirect_uri,139 "client_id": client_id,140 "code_verifier": verifier, # PKCE proof141 },142 )143 assert token_response.status_code == 200144 body = token_response.json()145 assert "access_token" in body146 assert body["token_type"] == "Bearer"147```148149The happy path asserts the returned `state` matches; the negative150state-mismatch test, plus the client-credentials, refresh-rotation,151OIDC-nonce, scope-downgrade, and redirect-URI recipes, are in152[references/per-flow-test-recipes.md](references/per-flow-test-recipes.md).153154## End-to-end coverage checklist155156For each OAuth/OIDC client in scope:1571581. Auth-code + PKCE happy path (Worked example)1592. State parameter validation (references)1603. Client-credentials happy path (if M2M; references)1614. Refresh-token rotation + reuse detection (references)1625. OIDC nonce validation (if OIDC; references)1636. Scope-downgrade handling (references)1647. Redirect-URI strict matching (references)1658. Token expiration handling (use-after-expiry returns 401)1669. Token revocation (RFC 7009) if endpoint supports it167168## Anti-patterns169170| Anti-pattern | Why it fails | Fix |171|---|---|---|172| Use PKCE `plain` method | Defeats PKCE; RFC 7636 §4.2 specifies S256 as recommended | Always S256 (Worked example) |173| Skip state validation in the callback handler | CSRF vulnerable | State CSRF negative test (references) |174| Hardcode redirect_uri prefix matching | Substring match accepts evil URIs | Strict equality (references) |175| Test only the happy path | Negative cases (mismatched state, invalid PKCE, expired token) untested | Negative tests (references) |176| Use Implicit or RO-Password grants in new code | Deprecated per RFC 9700 | Auth Code + PKCE |177178## Limitations179180- This is a build-an-X workflow. Tests use the application's HTTP181 client + browser automation library; this skill is the per-flow182 checklist.183- IdP-specific quirks (audience, custom auth servers, sign-in184 policies) layer on top - see `keycloak-tests`,185 `auth0-tests`,186 `okta-tests` for IdP-specific patterns.187- RFC 9700 supersedes earlier security BCPs; pin to the latest188 version when authoring tests.189- OAuth 2.1 (in-progress) consolidates the deprecations from RFC190 9700; prefer 2.1 patterns when the IdP supports them.191192## References193194- [rfc6749][rfc6749] - RFC 6749 OAuth 2.0 Authorization Framework195- [rfc7636][rfc7636] - RFC 7636 PKCE196- IETF RFC 9700 - OAuth 2.0 Security Best Current Practice (March 2025)197- IETF RFC 7009 - OAuth 2.0 Token Revocation198- openid.net/specs/openid-connect-core-1_0.html - OIDC Core spec199- [references/per-flow-test-recipes.md](references/per-flow-test-recipes.md) - state CSRF, client-credentials, refresh-token rotation, OIDC nonce, scope-downgrade, and redirect-URI recipes.200- `keycloak-tests`,201 `auth0-tests`,202 `okta-tests` - IdP-specific patterns203- `session-management-test-author` - companion: post-auth session lifecycle