|
| 1 | +# OAuth Discovery |
| 2 | + |
| 3 | +RFC 9728 Protected Resource Metadata and JWT authentication for vgi-rpc HTTP services. |
| 4 | + |
| 5 | +## Quick Overview |
| 6 | + |
| 7 | +vgi-rpc HTTP servers can advertise their OAuth configuration so clients |
| 8 | +discover auth requirements automatically — no out-of-band configuration needed. |
| 9 | + |
| 10 | +### Server setup |
| 11 | + |
| 12 | +```python |
| 13 | +from vgi_rpc.http import OAuthResourceMetadata, jwt_authenticate, make_wsgi_app |
| 14 | +from vgi_rpc import RpcServer |
| 15 | + |
| 16 | +metadata = OAuthResourceMetadata( |
| 17 | + resource="https://api.example.com/vgi", |
| 18 | + authorization_servers=("https://auth.example.com",), |
| 19 | + scopes_supported=("read", "write"), |
| 20 | +) |
| 21 | + |
| 22 | +auth = jwt_authenticate( |
| 23 | + issuer="https://auth.example.com", |
| 24 | + audience="https://api.example.com/vgi", |
| 25 | +) |
| 26 | + |
| 27 | +server = RpcServer(MyService, MyServiceImpl()) |
| 28 | +app = make_wsgi_app( |
| 29 | + server, |
| 30 | + authenticate=auth, |
| 31 | + oauth_resource_metadata=metadata, |
| 32 | +) |
| 33 | +``` |
| 34 | + |
| 35 | +### Client discovery |
| 36 | + |
| 37 | +```python |
| 38 | +from vgi_rpc.http import http_oauth_metadata, http_connect |
| 39 | + |
| 40 | +meta = http_oauth_metadata("https://api.example.com") |
| 41 | +print(meta.authorization_servers) # ("https://auth.example.com",) |
| 42 | + |
| 43 | +with http_connect(MyService, "https://api.example.com") as svc: |
| 44 | + result = svc.protected_method() |
| 45 | +``` |
| 46 | + |
| 47 | +## How It Works |
| 48 | + |
| 49 | +1. Server serves `/.well-known/oauth-protected-resource` (RFC 9728) |
| 50 | +2. 401 responses include `WWW-Authenticate: Bearer resource_metadata="..."` |
| 51 | +3. Client fetches metadata to discover authorization server(s) |
| 52 | +4. Client authenticates with the AS and sends Bearer token |
| 53 | + |
| 54 | +### Discovery from a 401 |
| 55 | + |
| 56 | +If a client doesn't know the server's auth requirements upfront, it can |
| 57 | +discover them from a 401 response: |
| 58 | + |
| 59 | +```python |
| 60 | +from vgi_rpc.http import parse_resource_metadata_url, fetch_oauth_metadata |
| 61 | + |
| 62 | +# 1. Make a request that returns 401 |
| 63 | +resp = client.post("/vgi/my_method", ...) |
| 64 | + |
| 65 | +# 2. Parse the metadata URL from WWW-Authenticate header |
| 66 | +www_auth = resp.headers["www-authenticate"] |
| 67 | +metadata_url = parse_resource_metadata_url(www_auth) |
| 68 | +# "https://api.example.com/.well-known/oauth-protected-resource/vgi" |
| 69 | + |
| 70 | +# 3. Fetch the metadata |
| 71 | +meta = fetch_oauth_metadata(metadata_url) |
| 72 | +print(meta.authorization_servers) # use these to authenticate |
| 73 | +``` |
| 74 | + |
| 75 | +## OAuthResourceMetadata |
| 76 | + |
| 77 | +Frozen dataclass configuring the server's RFC 9728 metadata document. |
| 78 | +Pass to `make_wsgi_app(oauth_resource_metadata=...)` to enable OAuth discovery. |
| 79 | + |
| 80 | +| Field | Type | Required | Description | |
| 81 | +|---|---|---|---| |
| 82 | +| `resource` | `str` | Yes | Canonical URL of the protected resource | |
| 83 | +| `authorization_servers` | `tuple[str, ...]` | Yes | Authorization server issuer URLs (must be non-empty) | |
| 84 | +| `scopes_supported` | `tuple[str, ...]` | No | OAuth scopes the resource understands | |
| 85 | +| `bearer_methods_supported` | `tuple[str, ...]` | No | Token delivery methods (default `("header",)`) | |
| 86 | +| `resource_signing_alg_values_supported` | `tuple[str, ...]` | No | JWS algorithms for signed responses | |
| 87 | +| `resource_name` | `str \| None` | No | Human-readable name | |
| 88 | +| `resource_documentation` | `str \| None` | No | URL to developer docs | |
| 89 | +| `resource_policy_uri` | `str \| None` | No | URL to privacy policy | |
| 90 | +| `resource_tos_uri` | `str \| None` | No | URL to terms of service | |
| 91 | + |
| 92 | +Raises `ValueError` if `resource` is empty or `authorization_servers` is empty. |
| 93 | + |
| 94 | +## jwt_authenticate() |
| 95 | + |
| 96 | +Factory that creates a JWT-validating `authenticate` callback using Authlib. |
| 97 | + |
| 98 | +```python |
| 99 | +from vgi_rpc.http import jwt_authenticate |
| 100 | + |
| 101 | +auth = jwt_authenticate( |
| 102 | + issuer="https://auth.example.com", |
| 103 | + audience="https://api.example.com/vgi", |
| 104 | + jwks_uri="https://auth.example.com/.well-known/jwks.json", # optional |
| 105 | + principal_claim="sub", # default |
| 106 | + domain="jwt", # default |
| 107 | +) |
| 108 | +``` |
| 109 | + |
| 110 | +| Parameter | Type | Default | Description | |
| 111 | +|---|---|---|---| |
| 112 | +| `issuer` | `str` | required | Expected `iss` claim | |
| 113 | +| `audience` | `str` | required | Expected `aud` claim | |
| 114 | +| `jwks_uri` | `str \| None` | `None` | JWKS URL (discovered from OIDC if `None`) | |
| 115 | +| `claims_options` | `Mapping \| None` | `None` | Additional Authlib claim options | |
| 116 | +| `principal_claim` | `str` | `"sub"` | JWT claim for `AuthContext.principal` | |
| 117 | +| `domain` | `str` | `"jwt"` | Domain for `AuthContext` | |
| 118 | + |
| 119 | +**JWKS caching**: Keys are fetched lazily on first request and cached in-process. |
| 120 | +On unknown `kid` (decode error), keys are automatically refreshed once. Other |
| 121 | +validation failures (expired token, wrong issuer/audience) fail immediately |
| 122 | +without a network round-trip. |
| 123 | + |
| 124 | +Requires `pip install vgi-rpc[oauth]` — raises a clear `ImportError` with |
| 125 | +install instructions if Authlib is not available. |
| 126 | + |
| 127 | +## http_oauth_metadata() |
| 128 | + |
| 129 | +Client-side function to discover a server's OAuth configuration. |
| 130 | + |
| 131 | +```python |
| 132 | +from vgi_rpc.http import http_oauth_metadata |
| 133 | + |
| 134 | +meta = http_oauth_metadata("https://api.example.com") |
| 135 | +if meta is not None: |
| 136 | + print(meta.authorization_servers) |
| 137 | +``` |
| 138 | + |
| 139 | +Returns `OAuthResourceMetadataResponse` or `None` (if server returns 404). |
| 140 | + |
| 141 | +**Note:** The `prefix` parameter (default `"/vgi"`) must match the server's |
| 142 | +`make_wsgi_app(prefix=...)`. A mismatch results in a 404 (`None` return). |
| 143 | + |
| 144 | +## fetch_oauth_metadata() |
| 145 | + |
| 146 | +Fetch metadata from an explicit URL (typically from a 401 `WWW-Authenticate` header). |
| 147 | + |
| 148 | +```python |
| 149 | +from vgi_rpc.http import fetch_oauth_metadata |
| 150 | + |
| 151 | +meta = fetch_oauth_metadata("https://api.example.com/.well-known/oauth-protected-resource/vgi") |
| 152 | +``` |
| 153 | + |
| 154 | +## parse_resource_metadata_url() |
| 155 | + |
| 156 | +Extract the `resource_metadata` URL from a `WWW-Authenticate` header. |
| 157 | + |
| 158 | +```python |
| 159 | +from vgi_rpc.http import parse_resource_metadata_url |
| 160 | + |
| 161 | +url = parse_resource_metadata_url('Bearer resource_metadata="https://..."') |
| 162 | +# "https://..." |
| 163 | +``` |
| 164 | + |
| 165 | +Returns `None` if the header doesn't contain `resource_metadata`. |
| 166 | + |
| 167 | +## OAuthResourceMetadataResponse |
| 168 | + |
| 169 | +Frozen dataclass returned by `http_oauth_metadata()` and `fetch_oauth_metadata()`. |
| 170 | +Same fields as `OAuthResourceMetadata` (the server-side config class). |
| 171 | + |
| 172 | +## Standards Compliance |
| 173 | + |
| 174 | +- [RFC 9728](https://www.rfc-editor.org/rfc/rfc9728) — OAuth 2.0 Protected Resource Metadata |
| 175 | +- [RFC 8414](https://www.rfc-editor.org/rfc/rfc8414) — OAuth 2.0 Authorization Server Metadata |
| 176 | +- [RFC 6750](https://www.rfc-editor.org/rfc/rfc6750) — Bearer Token Usage |
| 177 | +- Compatible with MCP's OAuth implementation |
| 178 | + |
| 179 | +## Installation |
| 180 | + |
| 181 | +```bash |
| 182 | +# OAuth discovery (no extra deps beyond [http]) |
| 183 | +pip install vgi-rpc[http] |
| 184 | + |
| 185 | +# JWT authentication (adds Authlib) |
| 186 | +pip install vgi-rpc[http,oauth] |
| 187 | +``` |
0 commit comments