# Lab L13 verification output
# Date: 2026-11-26T12:53:54Z  Host: macOS 27.2.1 arm64  uv 0.10.2
# Command: ./run.sh verify   (uv sync --locked, then .venv/bin/python verify_client.py)
# uv.lock sha256: 390da8a290f3e3d07cdbd251c01e91d54660002b1e9c7b0fe1fad416dd522037
# Exit code: 0

Lab L13 verification (authorization)
  mcp 2.3.0 | PyJWT 2.15.1 | cryptography 50.0.2 | httpx2 2.16.2 | starlette 1.10.0 | uvicorn 0.54.0 | python 3.12.12

[A] Step-by-step flow (raw HTTP), then the official SDK Client
  A1 unauthenticated POST /mcp -> HTTP 401; WWW-Authenticate: Bearer error="invalid_token", error_description="Authentication required", resource_metadata="http://127.0.0.1:8000/.well-known/oauth-protected-resource/mcp"
  PASS  A1 unauthenticated call gets 401
  PASS  A1 WWW-Authenticate carries resource_metadata
  A2 protected resource metadata: {'resource': 'http://127.0.0.1:8000/mcp', 'authorization_servers': ['http://127.0.0.1:9000'], 'scopes_supported': ['notes:read'], 'bearer_methods_supported': ['header']}
  PASS  A2 RFC 9728 metadata names this server as the resource
  PASS  A2 metadata lists the authorization server
  A3 AS metadata: issuer=http://127.0.0.1:9000 pkce=['S256'] iss_param=True
  PASS  A3 AS metadata issuer equals the advertised issuer (exact string)
  PASS  A3 AS advertises PKCE S256 (a client MUST refuse to proceed otherwise)
  PASS  A4 PKCE S256 authorization with resource: code issued, state and iss validated
  PASS  A5 token request with code_verifier and resource succeeds
  A5 token claims (unverified view): iss=http://127.0.0.1:9000 aud=http://127.0.0.1:8000/mcp sub=learner-001 scope='notes:read notes:write' lifetime=300s
  PASS  A5 token audience is bound to this MCP server (RFC 8707)
  A6 negotiated protocol version: 2026-07-28
  PASS  A6 authenticated client negotiates 2026-07-28
  PASS  A6 tools listed
  whoami -> {'subject': 'learner-001', 'client_id': 'lab-public-client', 'scopes': ['notes:read', 'notes:write'], 'audience': 'http://127.0.0.1:8000/mcp'}
  PASS  A6 whoami sees subject and audience
  PASS  A6 list_notes succeeds
  PASS  A6 add_note succeeds with notes:write
  quote_of_the_day -> {'day': 1, 'quote': 'Synthetic quote 2: never pass a token through.'}
  PASS  A6 upstream tool succeeds
  PASS  A7 upstream NEVER received the client's token (no passthrough)
  PASS  A7 upstream received the server's own service credential

[B] Same flow driven by the official SDK OAuthClientProvider (pre-registered public client)
  authorization request: code_challenge_method=S256 resource=http://127.0.0.1:8000/mcp scope='notes:read'
  PASS  B1 SDK client used PKCE S256
  PASS  B1 SDK client sent the RFC 8707 resource parameter
  PASS  B1 authorization response carried iss (RFC 9207)
  PASS  B2 SDK-authorized client negotiates 2026-07-28
  PASS  B2 list_notes succeeds with the SDK-obtained token
  add_note with scope 'notes:read' -> is_error=True: 'Error executing tool add_note: Insufficient scope: this tool requires notes:write.'
  PASS  B3 least privilege: SDK requested only the advertised notes:read, so add_note is refused

[C] Negative tests
  token for http://127.0.0.1:8100/mcp -> HTTP 401
  PASS  C1 token issued for ANOTHER audience is rejected (401)
  token without notes:read -> HTTP 403; Bearer error="insufficient_scope", error_description="Required scope: notes:read", resource_metadata="http://127.0.0.1:8000/.well-known/oauth-protected-resource/mcp"
  PASS  C2 missing required scope -> 403 insufficient_scope
  PASS  C3 tool requiring notes:write refused for a read-only token
  authorize without code_challenge -> 302 invalid_request
  PASS  C4 AS rejects authorization without PKCE
  authorize with code_challenge_method=plain -> 302 invalid_request
  PASS  C4 AS rejects PKCE method 'plain' (S256 only)
  token without code_verifier -> 400 invalid_request
  PASS  C4 AS rejects token request without code_verifier
  token with wrong code_verifier -> 400 invalid_grant
  PASS  C4 AS rejects a wrong code_verifier
  PASS  C4 control: the correct code_verifier succeeds
  PASS  C4 authorization code is single-use
  PASS  C5 AS rejects authorization without resource
  PASS  C5 AS rejects an unknown resource
  token with a different resource than /authorize -> 400 invalid_target
  PASS  C5 token resource must match the authorization request
  PASS  C5 token request without resource rejected
  PASS  C6 control: short-lived token accepted while valid
  same token after expiry -> HTTP 401
  PASS  C6 expired token rejected (401)
  PASS  C7 correct claims but forged signature rejected
  PASS  C7 valid signature but wrong issuer rejected
  PASS  C7 control: same claims, right issuer and key accepted
  PASS  C7 alg=none token rejected
  PASS  C8 token in the query string is not accepted (header only)
  PASS  C10 valid token but foreign Origin -> 403
  PASS  C10 control: valid token with allowed Origin -> 200
  AuthSettings default validate_token_resource=None; warning: MCPDeprecationWarning
  PASS  C9 SDK default leaves the audience gate off and only warns (lab sets it to True)
  PASS  C11 port 8000 not reachable on LAN address (loopback only)
  PASS  C11 port 9000 not reachable on LAN address (loopback only)
  PASS  C11 port 8002 not reachable on LAN address (loopback only)

ALL CHECKS PASSED (49)
